def execute_trade(self, trade: Trade) -> Trade: if trade.trade_type == TradeType.LIMIT_BUY: order = self._exchange.create_limit_buy_order( trade.symbol, trade.amount, trade.price) elif trade.trade_type == TradeType.MARKET_BUY: order = self._exchange.create_market_buy_order( trade.symbol, trade.amount) elif trade.trade_type == TradeType.LIMIT_SELL: order = self._exchange.create_limit_sell_order( trade.symbol, trade.amount, trade.price) elif trade.trade_type == TradeType.MARKET_SELL: order = self._exchange.create_market_sell_order( trade.symbol, trade.amount) else: return trade.copy() max_wait_time = time.time() + self._max_trade_wait_in_sec while order['status'] == 'open' and time.time() < max_wait_time: order = self._exchange.fetch_order(order.id) if order['status'] == 'open': self._exchange.cancel_order(order.id) self._performance = self._performance.append( { 'balance': self.balance, 'net_worth': self.net_worth, }, ignore_index=True) return Trade(symbol=trade.symbol, trade_type=trade.trade_type, amount=order['filled'], price=order['price'])
def execute_trade(self, trade: Trade) -> Trade: current_price = self.current_price(symbol=trade.symbol) commission = self._commission_percent / 100 filled_trade = trade.copy() if filled_trade.is_hold or not self._is_valid_trade(filled_trade): filled_trade.amount = 0 ''' elif filled_trade.is_buy: price_adjustment = price_adjustment = (1 + commission) filled_trade.price = max(round(current_price * price_adjustment, self._base_precision), self.base_precision) filled_trade.amount = round( (filled_trade.price * filled_trade.amount) / filled_trade.price, self._instrument_precision) elif filled_trade.is_sell: price_adjustment = (1 - commission) filled_trade.price = round(current_price * price_adjustment, self._base_precision) filled_trade.amount = round(filled_trade.amount, self._instrument_precision) ''' filled_trade = self._slippage_model.fill_order(filled_trade, current_price) self._update_account(filled_trade) return filled_trade
def execute_trade(self, trade: Trade) -> Trade: current_price = self.current_price(symbol=trade.symbol) # commission = self._commission_percent / 100 filled_trade = trade.copy() if filled_trade.is_hold or not self._is_valid_trade(filled_trade): filled_trade.amount = 0 # if filled_trade.is_long: # price_adjustment = 1 # filled_trade.price = round(current_price * price_adjustment, self._base_precision) # filled_trade.amount = round((filled_trade.price * filled_trade.amount) / filled_trade.price, # self._instrument_precision) # elif filled_trade.is_short: # price_adjustment = 1 # filled_trade.price = round(current_price * price_adjustment, self._base_precision) # filled_trade.amount = round(filled_trade.amount, self._instrument_precision) # # if not filled_trade.is_hold: # filled_trade = self._slippage_model.fill_order(filled_trade, current_price) self._update_account(filled_trade) return filled_trade