def on_entrust(self, context: HftContext, localid: int, stdCode: str, bSucc: bool, msg: str, userTag: str): if bSucc: context.stra_log_text("%s下单成功,本地单号:%d" % (stdCode, localid)) else: context.stra_log_text("%s下单失败,本地单号:%d,错误信息:%s" % (stdCode, localid, msg))
def on_init(self, context:HftContext): ''' 策略初始化,启动的时候调用\n 用于加载自定义数据\n @context 策略运行上下文 ''' #先订阅实时数据 context.stra_sub_ticks(self.__code__) self.__ctx__ = context
def on_channel_ready(self, context:HftContext): undone = context.stra_get_undone(self.__code__) if undone != 0 and len(self.__orders__.keys()) == 0: context.stra_log_text("%s存在不在管理中的未完成单%f手,全部撤销" % (self.__code__, undone)) isBuy = (undone > 0) ids = context.stra_cancel_all(self.__code__, isBuy) for localid in ids: self.__orders__[localid] = localid self.__cancel_cnt__ += len(ids) context.stra_log_text("cancelcnt -> %d" % (self.__cancel_cnt__)) self.__channel_ready__ = True
def on_tick(self, context: HftContext, stdCode: str, newTick: dict): if self.__code__ != stdCode: return #如果有未完成订单,则进入订单管理逻辑 if len(self.__orders__.keys()) != 0: self.check_orders() return if not self.__channel_ready__: return self.__last_tick__ = newTick #如果已经入场,则做频率检查 if self.__last_entry_time__ is not None and self.__freq__ != 0: #当前时间,一定要从api获取,不然回测会有问题 now = makeTime(self.__ctx__.stra_get_date(), self.__ctx__.stra_get_time(), self.__ctx__.stra_get_secs()) span = now - self.__last_entry_time__ if span.total_seconds() <= self.__freq__: return #信号标志 signal = 0 #最新价作为基准价格 price = newTick["price"] #计算理论价格 pxInThry = (newTick["bidprice"][0] * newTick["askqty"][0] + newTick["askprice"][0] * newTick["bidqty"][0]) / ( newTick["askqty"][0] + newTick["bidqty"][0]) context.stra_log_text("理论价格%f,最新价:%f" % (pxInThry, price)) if pxInThry > price: #理论价格大于最新价,正向信号 signal = 1 context.stra_log_text("出现正向信号") elif pxInThry < price: #理论价格小于最新价,反向信号 signal = -1 context.stra_log_text("出现反向信号") if signal != 0: self.ticker += 1 #读取当前持仓 curPos = context.stra_get_position(self.__code__) #读取品种属性,主要用于价格修正 commInfo = context.stra_get_comminfo(self.__code__) #当前时间,一定要从api获取,不然回测会有问题 now = makeTime(self.__ctx__.stra_get_date(), self.__ctx__.stra_get_time(), self.__ctx__.stra_get_secs()) #如果出现正向信号且当前仓位小于等于0,则买入 if signal > 0: #买入目标价格=基准价格+偏移跳数*报价单位 targetPx = price + commInfo.pricetick * self.__offset__ #执行买入指令,返回所有订单的本地单号 ids = context.stra_buy(self.__code__, targetPx, self.lots, "enterlong") #将订单号加入到管理中 for localid in ids: self.__orders__[localid] = localid #更新入场时间 self.__last_entry_time__ = now #如果出现反向信号且当前持仓大于等于0,则卖出 elif signal < 0: return #买入目标价格=基准价格-偏移跳数*报价单位 targetPx = price - commInfo.pricetick * self.__offset__ #执行卖出指令,返回所有订单的本地单号 ids = context.stra_sell(self.__code__, targetPx, self.lots, "entershort") #将订单号加入到管理中 for localid in ids: self.__orders__[localid] = localid #更新入场时间 self.__last_entry_time__ = now
def on_channel_lost(self, context: HftContext): context.stra_log_text("交易通道连接丢失") self.__channel_ready__ = False