Пример #1
0
 def _update_positions(self, order, trans):
     """ 更新持仓 """
     ## @todo 区分多空
     pos = self.current_positions.setdefault(trans.contract,
                                             Position(trans))
     if trans.side == TradeSide.KAI:
         # 开仓
         pos.cost = (pos.cost * pos.quantity + trans.price *
                     trans.quantity) / (pos.quantity + trans.quantity)
         pos.quantity += trans.quantity
     elif trans.side == TradeSide.PING:
         # 平仓
         pos.quantity -= trans.quantity
Пример #2
0
 def _update_positions(self, trans):
     """ 更新持仓 """
     poskey = PositionKey(trans.contract, trans.direction)
     pos = self.positions.setdefault(poskey, Position(trans))
     #print len(self.positions)
     #print pos.contract, pos.quantity
     if trans.side == TradeSide.KAI:
         # 开仓
         pos.cost = (pos.cost * pos.quantity + trans.price *
                     trans.quantity) / (pos.quantity + trans.quantity)
         pos.quantity += trans.quantity
     elif trans.side == TradeSide.PING:
         # 平仓
         pos.quantity -= trans.quantity
         if pos.quantity == 0:
             del self.positions[poskey]
Пример #3
0
 def _update_positions(self, trans):
     """ 更新持仓 """
     poskey = PositionKey(trans.contract, trans.direction)
     if trans.side == TradeSide.CANCEL:
         pos = self.positions.get(poskey, None)
         if pos:
             pos.closable += trans.quantity
         return
     pos = self.positions.setdefault(poskey, Position(trans))
     if trans.side == TradeSide.KAI:
         pos.cost = (pos.cost*pos.quantity + trans.price*trans.quantity) / \
                     (pos.quantity+trans.quantity)
         pos.quantity += trans.quantity
         if trans.contract.is_stock:
             pos.today += trans.quantity
         else:
             pos.closable += trans.quantity
         assert(pos.quantity == pos.today + pos.closable)
     elif trans.side == TradeSide.PING:
         pos.quantity -= trans.quantity
         if pos.quantity == 0:
             del self.positions[poskey]