コード例 #1
0
    def tickPrice(self, reqId: TickerId, tickType: TickType, price: float,
                  attrib: TickAttrib):
        EWrapper.tickPrice(self, reqId, tickType, price, attrib)
        # print("tickPrice:", price)
        # print("long_or_short_ready: ", self.long_or_short_ready)
        # 跳过前N个周期
        self.my_strategy_order.skip_first_N_cycles()
        # 判断delay是否完成
        now = datetime.datetime.now()
        # print( now - self.last_price_time)
        if now - self.last_price_time > datetime.timedelta(seconds=10):
            delay_done = True
            self.last_price_time = now
            # print(now)
            # 更新指标周期
            self.my_strategy_order.index_18.update_last_wiMA()
            self.my_strategy_order.index_25.update_last_wiMA()
        else:
            delay_done = False

        # 当账户信息更新完毕后,上一订单完成后,orderId准备好后,延时有效之后,开始执行策略
        if self.lastOrderDone and self.orderID_ready and self.long_or_short_ready and delay_done:
            # 测试信号
            # print("tickPrice:", price)
            # 关断上一订单已完成标志位
            self.lastOrderDone = False
            # 将持仓状态数据传入策略
            self.my_strategy_order.get_current_state(self.long_or_short)
            # 关断持仓状态可读标志位
            self.long_or_short_ready = False

            # 将价格数据和持仓状态数据传入策略
            self.my_strategy_order.get_price(price)
            # 执行策略分析,返回决策结果
            order_contract, order_order = self.my_strategy_order.strategy_exe()
            # 执行策略
            if order_contract != "hold":
                self.placeOrder(self.next_orderID, order_contract, order_order)
                # print(self.next_orderID)
                # 关断orderId可用标志位
                self.orderID_ready = False
                # 申请orderID
                self.reqIds(0)
            else:
                # print("do nothing")
                self.lastOrderDone = True
                self.long_or_short_ready = True
コード例 #2
0
 def tickPrice(self, reqId:TickerId , tickType:TickType, price:float, attrib:TickAttrib):
     EWrapper.tickPrice(self,reqId,tickType, price, attrib)
     print("tickGeneric", reqId, tickType, price,attrib)