1:老师您好,如下面代码所示,在on_trade回调函数中使用print总是报错,望回复,万分感激
代码如下:
def on_trade(self, trade: TradeData):
"""
Callback of new trade data update.
"""
if trade.offset == Offset.CLOSE:
self.bar_counts = 0
print(bar.datetime, "bar_counts持仓周期计数器平仓归零")
报错如下:
File "C:\Users\78405\Desktop\vnpy_20200624\vnpy\app\cta_strategy\backtesting.py", line 945, in cross_stop_order
self.strategy.on_trade(trade)
File "C:\Users\78405\strategies\AtrRsiStrategy_Thorn荆轮止损.py", line 239, in on_trade
print(bar.datetime, "bar_counts持仓周期计数器平仓归零")
NameError: name 'bar' is not defined
2:在on_bar中使用这句代码print(bar.datetime, "bar_counts持仓周期计数器平仓归零")不会报错,是因为不能在on_trade中使用吗?