简体   繁体   English

遍历股票价格的行和列python

[英]iterating through rows and columns of stock price python

I have a code below 我下面有一个代码

result, diff = [], []

for index, row in final.iterrows():
    for column in final.columns:
        if ((final['close'] - final['open']) > 20):
            diff = final['close'] - final['open']
            result = 1
        elif ((final['close'] - final['open']) < -20):
            diff = final['close'] - final['open']
            result = -1
        elif (-20 < (final['close'] - final['open']) < 20 ):
            diff = final['close'] - final['open']
            result = 0
        else:
            continue

The intention is to for every time stamp, check if close - open is greater than 20 pips, then assign a buy value to it. 目的是针对每个时间戳,检查收盘价-开盘价是否大于20点,然后为其分配买入价。 If it's less than -20 assign a sell value, if in between assign a 0. 如果小于-20,则分配一个卖出价格;如果介于两者之间,则分配一个0。

I am getting this error 我收到此错误

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
[Finished in 35.418s

Someone experienced with pandas would give a better answer, but since no one is answering here's mine. 曾经对pandas有经验的人会给出更好的答案,但由于没人在这里回答我的问题。 You generally don't want to iterate directly with pandas.Dataframes as that defeats the purpose. 您通常不希望直接使用pandas.Dataframes进行迭代,因为这样做会达到目的。 A pandas solution would look more like: pandas解决方案看起来更像是:

import pandas as pd

data = {
    'symbol': ['WZO', 'FDL', 'KXW', 'GYU', 'MIR', 'YAC', 'CDE', 'DSD', 'PAM', 'BQE'],
    'open': [356, 467, 462, 289, 507, 654, 568, 646, 440, 625],
    'close': [399, 497, 434, 345, 503, 665, 559, 702, 488, 608]
}
df = pd.DataFrame.from_dict(data)
df['diff'] = df['close'] - df['open']
df.loc[(df['diff'] < 20) & (df['diff'] > -20), 'result'] = 0
df.loc[df['diff'] >= 20, 'result'] = 1
df.loc[df['diff'] <= -20, 'result'] = -1

df now contains: df现在包含:

  symbol  open  close  diff  result
0    WZO   356    399    43     1.0
1    FDL   467    497    30     1.0
2    KXW   462    434   -28    -1.0
3    GYU   289    345    56     1.0
4    MIR   507    503    -4     0.0
5    YAC   654    665    11     0.0
6    CDE   568    559    -9     0.0
7    DSD   646    702    56     1.0
8    PAM   440    488    48     1.0
9    BQE   625    608   -17     0.0

Regarding your code, I'll repeat my comment from above: You are iterating by row , but then using the whole DataFrame final in your conditions. 关于您的代码,我将从上面重复我的评论:您正在按row进行迭代,然后在您的条件下使用整个DataFrame final I think you meant to do row there. 我想你打算在那里row You don't need to iterate over columns grabbing your values by index. 您无需遍历按索引获取值的列。 Your conditions miss for when final['close'] - final['open'] is exactly 20. result, diff = [], [] are lists at the top, but then assigned as integers in the loop. final['close'] - final['open']正好为20时,您的条件会丢失。 result, diff = [], []是顶部列表,但随后在循环中分配为整数。 Perhaps you want result.append() ? 也许您想要result.append()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM