def apply_trade(self, trade):
    trade_quantity = trade.last_quantity
    if (trade.side == SIDE.BUY):
        if (trade.position_effect == POSITION_EFFECT.OPEN):
            self._buy_avg_open_price = (((self._buy_avg_open_price * self.buy_quantity) + (trade_quantity * trade.last_price)) / (self.buy_quantity + trade_quantity))
            self._buy_transaction_cost += trade.transaction_cost
            self._buy_today_holding_list.insert(0, (trade.last_price, trade_quantity))
            return ((- 1) * self._margin_of(trade_quantity, trade.last_price))
        else:
            old_margin = self.margin
            self._sell_transaction_cost += trade.transaction_cost
            delta_realized_pnl = self._close_holding(trade)
            self._sell_realized_pnl += delta_realized_pnl
            return ((old_margin - self.margin) + delta_realized_pnl)
    elif (trade.position_effect == POSITION_EFFECT.OPEN):
        self._sell_avg_open_price = (((self._sell_avg_open_price * self.sell_quantity) + (trade_quantity * trade.last_price)) / (self.sell_quantity + trade_quantity))
        self._sell_transaction_cost += trade.transaction_cost
        self._sell_today_holding_list.insert(0, (trade.last_price, trade_quantity))
        return ((- 1) * self._margin_of(trade_quantity, trade.last_price))
    else:
        old_margin = self.margin
        self._buy_transaction_cost += trade.transaction_cost
        delta_realized_pnl = self._close_holding(trade)
        self._buy_realized_pnl += delta_realized_pnl
        return ((old_margin - self.margin) + delta_realized_pnl)
def apply_trade(self, trade):
    trade_quantity = trade.last_quantity
    if (trade.side == SIDE.BUY):
        if (trade.position_effect == POSITION_EFFECT.OPEN):
            self._buy_avg_open_price = (((self._buy_avg_open_price * self.buy_quantity) + (trade_quantity * trade.last_price)) / (self.buy_quantity + trade_quantity))
            self._buy_transaction_cost += trade.transaction_cost
            self._buy_today_holding_list.insert(0, (trade.last_price, trade_quantity))
            return ((- 1) * self._margin_of(trade_quantity, trade.last_price))
        else:
            old_margin = self.margin
            self._sell_transaction_cost += trade.transaction_cost
            delta_realized_pnl = self._close_holding(trade)
            self._sell_realized_pnl += delta_realized_pnl
            return ((old_margin - self.margin) + delta_realized_pnl)
    elif (trade.position_effect == POSITION_EFFECT.OPEN):
        self._sell_avg_open_price = (((self._sell_avg_open_price * self.sell_quantity) + (trade_quantity * trade.last_price)) / (self.sell_quantity + trade_quantity))
        self._sell_transaction_cost += trade.transaction_cost
        self._sell_today_holding_list.insert(0, (trade.last_price, trade_quantity))
        return ((- 1) * self._margin_of(trade_quantity, trade.last_price))
    else:
        old_margin = self.margin
        self._buy_transaction_cost += trade.transaction_cost
        delta_realized_pnl = self._close_holding(trade)
        self._buy_realized_pnl += delta_realized_pnl
        return ((old_margin - self.margin) + delta_realized_pnl)