python,用sleep(n)等待限价单执行过程中,handle_bar被触发,程序报错,如何解决?
设基础周期为1分钟
def handle_bar(context):
print('开始handle_bar')
for i in range(5):
print('执行循环内语句,模拟发单')
sleep(20)
执行Python脚本时遇到错误。
策略: <test_handlebar_sleep>
运行: <策略test_handlebar_sleep>
类型: 运行时错误
描述: 无响应
如何解决?替换sleep为别的函数?或者settimer?
我的目的是:已经提交的限价单,等待n秒成交(我用sleep),n秒不成交再撤单重新下单。
不知道用没有别的更好的方式实现等待n秒
# 本Python代码主要用于策略交易
# 可以自己import我们平台支持的第三方python模块,比如pandas、numpy等。
from PythonApi import *
# 在这个方法中编写任何的初始化逻辑。context对象将会在你的算法策略的任何方法之间做传递。--(必须实现)
def init(context):
# 在context中保存全局变量
#标的合约
context.s1 = "IF00"
#控制只交易一次
context.num=0
#存放订单id
context.buy_list = []
# before_trading此函数会在每天基准合约的策略交易开始前被调用,当天只会被调用一次。--(选择实现)
def before_trading(context):
pass
# 你选择的品种的数据更新将会触发此段逻辑,例如日或分钟历史数据切片或者是实时数据切片更新。--(必须实现)
def handle_bar(context):
#跌停价开一手多单
if context.num==0:
buy_id = buy_open(context.s1, "Limit",get_dynainf(context.s1,55),1)
context.buy_list.append(buy_id)
buy_id = buy_open(context.s1, "Limit",get_dynainf(context.s1,55),3)
context.buy_list.append(buy_id)
context.num = 1
#获取所有未成交单
order_list = get_orders(context.s1, 0)
if order_list is not(None):
#循环所有订单
for i in order_list:
if i.order_id in context.buy_list:
cancel_order (i.order_id)
def order_status(context,order):
#判断柜台回报信息,如果是撤单成功,并且是当前程序的下单id。
if order.status=='cancelled' and order.order_id in context.buy_list :
print('单子已撤,继续追单')
buy_open(context.s1, "Market",0,order.unfilled_quantity)
# after_trading函数会在每天交易结束后被调用,当天只会被调用一次。 --(选择实现)
def after_trading(context):
pass
datetime | datetime.datetime | 订单创建时间 |
我自己想明白了。