试试用变量锁定开仓的次数,然后每次k更新时初始化变量
from tqsdk import TqApi number_of_open = 0 #记录开仓次数 allow_open = 3 #允许的开仓次数 # api = TqApi(auth="信易账户,账户密码") SYMBOL = 'SHFE.rb2101' klines = api.get_kline_serial(SYMBOL, 60*60) try: while True: api.wait_update() if api.is_changing(klines.iloc[-1], "datetime"): number_of_open = 0 # 假设开仓条件为True if True and number_of_open < allow_open: order = api.insert_order(SYMBOL, "BUY", "OPEN", 2, 5000) number_of_open += 1 except KeyboardInterrupt: api.close()
tygk98 发表新评论 2020年9月29日
谢谢分享!