def _close_holding(self, trade):
    left_quantity = trade.last_quantity
    delta = 0
    if (trade.side == SIDE.BUY):
        if ((trade.position_effect == POSITION_EFFECT.CLOSE) and (len(self._sell_old_holding_list) != 0)):
            (old_price, old_quantity) = self._sell_old_holding_list.pop()
            if (old_quantity > left_quantity):
                consumed_quantity = left_quantity
                self._sell_old_holding_list = [(old_price, (old_quantity - left_quantity))]
            else:
                consumed_quantity = old_quantity
            left_quantity -= consumed_quantity
            delta += self._cal_realized_pnl(old_price, trade.last_price, trade.side, consumed_quantity)
        while True:
            if (left_quantity <= 0):
                break
            (oldest_price, oldest_quantity) = self._sell_today_holding_list.pop()
            if (oldest_quantity > left_quantity):
                consumed_quantity = left_quantity
                self._sell_today_holding_list.append((oldest_price, (oldest_quantity - left_quantity)))
            else:
                consumed_quantity = oldest_quantity
            left_quantity -= consumed_quantity
            delta += self._cal_realized_pnl(oldest_price, trade.last_price, trade.side, consumed_quantity)
    else:
        if ((trade.position_effect == POSITION_EFFECT.CLOSE) and (len(self._buy_old_holding_list) != 0)):
            (old_price, old_quantity) = self._buy_old_holding_list.pop()
            if (old_quantity > left_quantity):
                consumed_quantity = left_quantity
                self._buy_old_holding_list = [(old_price, (old_quantity - left_quantity))]
            else:
                consumed_quantity = old_quantity
            left_quantity -= consumed_quantity
            delta += self._cal_realized_pnl(old_price, trade.last_price, trade.side, consumed_quantity)
        while True:
            if (left_quantity <= 0):
                break
            (oldest_price, oldest_quantity) = self._buy_today_holding_list.pop()
            if (oldest_quantity > left_quantity):
                consumed_quantity = left_quantity
                self._buy_today_holding_list.append((oldest_price, (oldest_quantity - left_quantity)))
                left_quantity = 0
            else:
                consumed_quantity = oldest_quantity
            left_quantity -= consumed_quantity
            delta += self._cal_realized_pnl(oldest_price, trade.last_price, trade.side, consumed_quantity)
    return delta
def _close_holding(self, trade):
    left_quantity = trade.last_quantity
    delta = 0
    if (trade.side == SIDE.BUY):
        if ((trade.position_effect == POSITION_EFFECT.CLOSE) and (len(self._sell_old_holding_list) != 0)):
            (old_price, old_quantity) = self._sell_old_holding_list.pop()
            if (old_quantity > left_quantity):
                consumed_quantity = left_quantity
                self._sell_old_holding_list = [(old_price, (old_quantity - left_quantity))]
            else:
                consumed_quantity = old_quantity
            left_quantity -= consumed_quantity
            delta += self._cal_realized_pnl(old_price, trade.last_price, trade.side, consumed_quantity)
        while True:
            if (left_quantity <= 0):
                break
            (oldest_price, oldest_quantity) = self._sell_today_holding_list.pop()
            if (oldest_quantity > left_quantity):
                consumed_quantity = left_quantity
                self._sell_today_holding_list.append((oldest_price, (oldest_quantity - left_quantity)))
            else:
                consumed_quantity = oldest_quantity
            left_quantity -= consumed_quantity
            delta += self._cal_realized_pnl(oldest_price, trade.last_price, trade.side, consumed_quantity)
    else:
        if ((trade.position_effect == POSITION_EFFECT.CLOSE) and (len(self._buy_old_holding_list) != 0)):
            (old_price, old_quantity) = self._buy_old_holding_list.pop()
            if (old_quantity > left_quantity):
                consumed_quantity = left_quantity
                self._buy_old_holding_list = [(old_price, (old_quantity - left_quantity))]
            else:
                consumed_quantity = old_quantity
            left_quantity -= consumed_quantity
            delta += self._cal_realized_pnl(old_price, trade.last_price, trade.side, consumed_quantity)
        while True:
            if (left_quantity <= 0):
                break
            (oldest_price, oldest_quantity) = self._buy_today_holding_list.pop()
            if (oldest_quantity > left_quantity):
                consumed_quantity = left_quantity
                self._buy_today_holding_list.append((oldest_price, (oldest_quantity - left_quantity)))
                left_quantity = 0
            else:
                consumed_quantity = oldest_quantity
            left_quantity -= consumed_quantity
            delta += self._cal_realized_pnl(oldest_price, trade.last_price, trade.side, consumed_quantity)
    return delta