我的实现目标:
采用多线程对多个股票进行同时的交易,后续可能会多策略多股票同时采用多线程交易,
但是目前测试下来发现以下问题:
问题一(已解决):
在多线程模块中history函数好像无法使用,可以采用get_price来获取交易数据
问题二(未解决):
在主线程(handle_bar执行线程)定义的全局变量g.test,然后在线程中修改g.test后,回到主线程中g.test的数据是修改前的,也就是线程中对g.test的修改是无效的。涉及所有的g中的变量
问题三(未解决):
新建线程中的下单交易order等无法交易,想通过问题二的方式传递数据出去到主线程进行交易么,碰到全局变量g无法更改的问题。
handle_bar中的线程启动方式参考以下:
for row in g.stockdf.itertuples(index=False) :
#前置购买条件未满足、并且当日无持仓的股票不操作
if row.buy == False and row.sell == False: continue
#当日完成买入/卖出交易操作的股票不操作
if row.ordered == True : continue
#线程名记录(策略名+股票名)
funindex = row.policy + row.stock.replace('.','')
if funindex in g.threadfun :
t = g.threadfun[funindex]
#该线程已经停止
if t.is_alive() == False :
del g.threadfun[funindex]
else:
#等待完成
log.warn('[handle_bar]%s线程(ID:%s,name:%s)运行中,本次handle_bar不运行'%(funindex,t.ident,t.name))
t.join()
continue
#启动交易线程
t = threading.Thread(target=row.runfun,args=(row.stock,row.buy,row.sell,))
t.setName(funindex)
g.threadfun[funindex] = t
t.start()