Esempio n. 1
0
 def _valid_order(self, order):
     """ 判断订单是否合法。 """
     if order.quantity <= 0:
         raise TradingError(err="交易数量不能小于0")
     # 撤单
     if order.side == TradeSide.CANCEL:
         if order not in self.open_orders:
             raise TradingError(err='撤销失败: 不存在该订单!')
     if order.side == TradeSide.PING:
         try:
             poskey = PositionKey(order.contract, order.direction)
             pos = self.positions[poskey]
             if pos.closable < order.quantity:
                 raise TradingError(err='可平仓位不足')
         except KeyError:
             # 没有持有该合约
             #logger.warn("不存在合约[%s]" % order.contract)
             raise TradingError(err="不存在合约[%s]" % order.contract)
     elif order.side == TradeSide.KAI:
         new_price = self._ticks[order.contract]
         if self.holding['cash'] < order.order_margin(new_price):
             raise TradingError(err='没有足够的资金开仓')
         else:
             self.holding['cash'] -= order.order_margin(new_price)
     return True
Esempio n. 2
0
 def _valid_order(self, order):
     """ 判断订单是否合法。 """
     if order.quantity <= 0:
         logger.warn("下单数量错误!")
         return False
     # 撤单
     if order.side == TradeSide.CANCEL:
         if order in self.open_orders:
             return True
         else:
             raise TradingError(err='撤销失败: 不存在该订单!')
     if order.side == TradeSide.PING:
         try:
             poskey = PositionKey(order.contract, order.direction)
             pos = self.positions[poskey]
             if pos.quantity >= order.quantity:
                 return True
         except KeyError:
             # 没有持有该合约
             logger.warn("不存在合约[%s]" % order.contract)
             #assert False
             return False
         logger.warn("下单仓位问题")
         return False
     elif order.side == TradeSide.KAI:
         if self.holding['cash'] < order.price * order.quantity:
             raise TradingError(err='没有足够的资金开仓')
         else:
             new_price = self._ticks[order.contract]
             self.holding['cash'] -= order.order_margin(new_price)
     return True
Esempio n. 3
0
 def update_fill(self, event):
     """ 处理委托单成交事件。 """
     # @TODO 订单编号和成交编号区分开
     assert event.type == Event.FILL
     trans = event.transaction
     try:
         self.open_orders.remove(trans.order)
     except KeyError:
         if trans.order.side == TradeSide.CANCEL:
             raise TradingError(err='重复撤单')
         else:
             assert(False and '重复成交')
     self._update_holding(trans)
     self._update_positions(trans)