怎样快速校验你的选股逻辑是否有效——基于QAStrategy
通常,我们会使用QA提供到方法存下本地历史数据,并基于历史数据使用add_func函数选股。并得到出信号的记录。
最后 选股结果长成这个样子:
因为是日线基本的选股,所有我选择交易时间为第二天的开盘价,所有使用了QA.QA_util_get_next_trade_date(x)
有了信号,怎样快速校验在市场的表现呢?其实使用天神的QAStrategy几行代码就能快速展现出来。
’’'
import QUANTAXIS as QA
from QAStrategy.qastockbase import QAStrategyStockBase
class Strategy(QAStrategyStockBase): def on_bar(self,data):
pos=self.get_positions(data.name[1])
if pos.volume_long!=0:
pos.on_price_change(float(data['close']))
if (pos.last_price-pos.open_price_long)/pos.open_price_long>0.2:
print(f'{str(data.name[0])[:10]} :止盈 卖出 股票{data.name[1]} 买入价格为:{pos.open_price_long}, 卖出价格为:{data.close}')
self.send_order('SELL', 'CLOSE', data.name[1],price=data.close, volume=pos.volume_long)
elif (pos.last_price-pos.open_price_long)/pos.open_price_long< -0.2:
print(f'{str(data.name[0])[:10]} :止损 卖出 股票{data.name[1]},买入价格为:{pos.open_price_long} ,卖出价格为:{data.close}')
self.send_order('SELL', 'CLOSE', data.name[1],price=data.close, volume=pos.volume_long)
elif len(stock.loc[(stock.code==data.name[1])& (stock.trade_date==str(data.name[0])[:10])])==1:
print(f'{str(data.name[0])[:10]} 买入 股票{data.name[1]} 价格为:{data.open}')
self.send_order('BUY', 'OPEN', data.name[1],price=data.open, volume=2000)
else:
pass
s=Strategy(code=stock.code.to_list(), frequence=‘day’, start=‘2020-01-01’, end=‘2020-07-31’, strategy_id=‘on_red_corss_3_line_20_NO_bank1’)
s.run_backtest()
’’'
执行完毕后,在可视化界面就可以看到你的选股表现了。
2 回复
哎哟,不错哦!!!!
谢谢分享