无法使用IF

时间:2018-03-06 05:27:10

标签: python pandas

我刚拿起编码,我的部分工作要求我找到股票买卖信号的盈利能力。 到目前为止我已经做了整体,但我无法删除最后一次购买,给我买买卖,而不是jus买卖

nss = len(Stock_signals)
for i in range(nss):
   if Stock_signals.Signal[i] == 'Buy':
  break
   else:
  Stock_signals.loc[Stock_signals.index[i] ,'Signal'] = Stock_signals.Signal[i][0]

bi = i
for i in range(bi,nss):
   if Stock_signals.Signal[i] == 'Buy':
      for j in range(i+1,nss):
     if Stock_signals.Signal[j] == 'Buy':
        Stock_signals.loc[Stock_signals.index[j] ,'Signal'] = 'B'
     else:
        break

for i in range(nss):
   if Stock_signals.Signal[i] == 'Sell':
  for j in range(i+1,nss):
     if Stock_signals.Signal[j] == 'Sell':
        Stock_signals.loc[Stock_signals.index[j] ,'Signal'] = 'S'
     else:
        break



if Stock_signals.Signal[-1] == ('B'):
  Stock_signals.loc[Stock_signals.index[-1] ,'Signal'] = Stock_signals.Signal[-1][0]`


Date         Price     Signal                        
2011-02-03  36.060001    Buy

2011-10-31  25.850000   Sell

2011-11-10  22.700001    Buy

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你会发现这段代码:

i

从数据框中删除此行:

if Stock_signals.Signal[-1] == ('B'):
  Stock_signals.loc[Stock_signals.index[-1] ,'Signal'] = Stock_signals.Signal[-1][0]`

但是您要检查2011-11-10 22.700001 Buy 而不是signal == 'B'

尝试:

signal == 'Buy'