【发布时间】:2021-03-17 06:48:41
【问题描述】:
这是一个纸质交易算法代码。出于某种原因,如果没有。 2 如果没有。 4 没有被执行。我使用相同的代码进行回测,并且效果很好。但是,当实时部署时,我在程序流程中遇到的问题很少。非常感谢任何帮助。谢谢!
position = 'Close'
while True:
df =pd.DataFrame(kite.historical_data(instrument_id, from_date='2020-12-01', to_date='2020-12-04', interval='minute'))
i = (len(df.index)-1)
LTP = kite.ltp(symbol)[symbol]['last_price']
if (EMACrossover_bull(df, i) or consolidation_bull(df, i)) and (position == 'Close'): #if no. 1
position = 'Open1'
entry = LTP
entry_time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
target = entry*(1+tar)
stop_loss = entry*(1-stl)
print('{} Long position initiated at {}. Target: {} Stop Loss:{}'.format(entry_time, entry, target, stop_loss))
if (position == 'Open1') and (LTP > target or LTP < stop_loss): #if no. 2
position = 'Close'
exitt = LTP
exit_time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
change = exitt-entry
print('{} Long position closed at {}. G/L: {} '.format(exit_time, exitt, change))
if (EMACrossover_bear(df, i) or consolidation_bear(df, i)) and (position == 'Close'): #if no. 3
position = 'Open2'
entry = LTP
entry_time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
target = entry*(1-tar)
stop_loss = entry*(1+stl)
print('{} Short position initiated at {}. Target: {} Stop Loss:{}'.format(entry_time, entry, target, stop_loss))
if (position == 'Open2') and (LTP < target or LTP > stop_loss): #if no. 4
position = 'Close'
exitt = LTP
exit_time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
change = entry-exitt
print('{} Short position closed at {}. G/L: {} '.format(exit_time, exitt, change))
这是我得到的输出: enter image description here
【问题讨论】:
-
除了我们无法运行您的代码之外,因为我们无法访问数据帧,您是否尝试在 if 语句之前打印出值?因为看起来 LTP 位于
target和stop_loss的中间,所以它应该跳过该语句。 -
嘿,感谢您花时间查看代码。问题是它与数据帧、LTP、目标或 stop_loss 无关。它关于变量“位置”。如果您查看“位置”的值是如何更新的,您会发现没有两个“ifs”应该连续运行,因为“位置”的值在每个 if 块中都会更新。如果块 3 已连续执行多次,您会看到输出。这让我很困惑
标签: python-3.x algorithm loops if-statement