def test_add_select_trade_in_history(self): _, exchange_inst, trader_inst, trades_manager_inst = self.init_default() self.stop(trader_inst) assert len(trades_manager_inst.get_trade_history()) == 0 symbol = "BTC/USD" new_order = SellLimitOrder(trader_inst) new_order.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT], symbol, 90, 4, 90) new_trade = Trade(exchange_inst, new_order) # add new trade trades_manager_inst.add_new_trade_in_history(new_trade) assert len(trades_manager_inst.get_trade_history()) == 1 assert trades_manager_inst.get_trade_history()[0] == new_trade # doesn't add an existing trade trades_manager_inst.add_new_trade_in_history(new_trade) assert len(trades_manager_inst.get_trade_history()) == 1 assert trades_manager_inst.get_trade_history()[0] == new_trade # select trade assert trades_manager_inst.select_trade_history() == trades_manager_inst.get_trade_history() new_order2 = SellLimitOrder(trader_inst) new_order2.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT], "BTC/EUR", 90, 4, 90) new_trade2 = Trade(exchange_inst, new_order2) trades_manager_inst.add_new_trade_in_history(new_trade2) assert len(trades_manager_inst.get_trade_history()) == 2 assert trades_manager_inst.select_trade_history(symbol) == [new_trade]
def notify_order_close(self, order, cancel=False, cancel_linked_only=False): # Cancel linked orders for linked_order in order.get_linked_orders(): self.cancel_order(linked_order) # If need to cancel the order call the method and no need to update the portfolio (only availability) if cancel: order_closed = None orders_canceled = order.get_linked_orders() + [order] self.cancel_order(order) _, profitability_percent, profitability_diff = self.get_trades_manager( ).get_profitability_without_update() elif cancel_linked_only: order_closed = None orders_canceled = order.get_linked_orders() _, profitability_percent, profitability_diff = self.get_trades_manager( ).get_profitability_without_update() else: order_closed = order orders_canceled = order.get_linked_orders() # update portfolio with ended order with self.get_order_portfolio(order) as pf: pf.update_portfolio(order) # debug purpose profitability, profitability_percent, profitability_diff = self.get_trades_manager( ).get_profitability() self.logger.info("Current portfolio profitability : {0}".format( PrettyPrinter.portfolio_profitability_pretty_print( profitability, profitability_percent, self.get_trades_manager().get_reference()))) # add to trade history self.trades_manager.add_new_trade_in_history( Trade(self.exchange, order)) # remove order to open_orders self.order_manager.remove_order_from_list(order) profitability_activated = order_closed is not None # update current order list with exchange if not self.simulate: self.update_open_orders(order.get_order_symbol()) # notification order.get_order_notifier().end(order_closed, orders_canceled, order.get_profitability(), profitability_percent, profitability_diff, profitability_activated)
def create_order(self, order, portfolio, loaded=False): linked_to = None new_order = order is_to_keep = True is_already_in_history = False # if this order is linked to another (ex : a sell limit order with a stop loss order) if new_order.linked_to is not None: new_order.linked_to.add_linked_order(new_order) linked_to = new_order.linked_to if not loaded: if not self.simulate and not self.check_if_self_managed(new_order.get_order_type()): created_order = self.exchange.create_order(new_order.get_order_type(), new_order.get_order_symbol(), new_order.get_origin_quantity(), new_order.get_origin_price(), new_order.origin_stop_price) # get real order from exchange new_order = self.parse_exchange_order_to_order_instance(created_order) # rebind order notifier and linked portfolio to new order instance new_order.order_notifier = order.get_order_notifier() new_order.get_order_notifier().set_order(new_order) new_order.linked_portfolio = portfolio # update the availability of the currency in the portfolio portfolio.update_portfolio_available(new_order, is_new_order=True) title = "Order creation" else: new_order.set_is_from_this_octobot(False) title = "Order loaded" is_already_in_history = self.trades_manager.is_in_history(new_order) if is_already_in_history or \ (new_order.get_status() != OrderStatus.OPEN and new_order.get_status() != OrderStatus.PARTIALLY_FILLED): is_to_keep = False self.logger.info(f"{title} : {new_order.get_order_symbol()} | " f"{new_order.get_order_type()} | " f"Price : {new_order.get_origin_price()} | " f"Quantity : {new_order.get_origin_quantity()} | " f"Status : {new_order.get_status()} " f"{'' if is_to_keep else ': will be archived in trades history if not already'}") if is_to_keep: # notify order manager of a new open order self.order_manager.add_order_to_list(new_order) elif not is_already_in_history: self.trades_manager.add_new_trade_in_history(Trade(self.exchange, new_order)) # if this order is linked to another if linked_to is not None: new_order.add_linked_order(linked_to) return new_order
async def test_get_total_paid_fees(self): _, exchange_inst, trader_inst, trades_manager_inst = await self.init_default( ) self.stop(trader_inst) symbol = "BTC/USD" new_order = SellLimitOrder(trader_inst) new_order.new(TraderOrderType.SELL_LIMIT, symbol, 90, 4, 90) new_order.fee = { FeePropertyColumns.COST.value: 100, FeePropertyColumns.CURRENCY.value: "BTC" } new_trade = Trade(exchange_inst, new_order) trades_manager_inst.add_new_trade_in_history(new_trade) assert trades_manager_inst.get_total_paid_fees() == {"BTC": 100} new_order2 = SellLimitOrder(trader_inst) new_order2.new(TraderOrderType.SELL_LIMIT, symbol, 90, 4, 90) new_order2.fee = { FeePropertyColumns.COST.value: 200, FeePropertyColumns.CURRENCY.value: "PLOP" } new_trade2 = Trade(exchange_inst, new_order2) trades_manager_inst.add_new_trade_in_history(new_trade2) assert trades_manager_inst.get_total_paid_fees() == { "BTC": 100, "PLOP": 200 } new_order3 = SellLimitOrder(trader_inst) new_order3.new(TraderOrderType.SELL_LIMIT, symbol, 90, 4, 90) new_order3.fee = { FeePropertyColumns.COST.value: 0.01111, FeePropertyColumns.CURRENCY.value: "PLOP" } new_trade3 = Trade(exchange_inst, new_order3) trades_manager_inst.add_new_trade_in_history(new_trade3) assert trades_manager_inst.get_total_paid_fees() == { "BTC": 100, "PLOP": 200.01111 }
def create_order(self, order, portfolio, loaded=False): linked_to = None new_order = order is_to_keep = True is_already_in_history = False # if this order is linked to another (ex : a sell limit order with a stop loss order) if new_order.linked_to is not None: new_order.linked_to.add_linked_order(new_order) linked_to = new_order.linked_to if not loaded: new_order = self._create_not_loaded_order(order, new_order, portfolio) title = "Order creation" else: new_order.set_is_from_this_octobot(False) title = "Order loaded" is_already_in_history = self.trades_manager.is_in_history( new_order) if is_already_in_history or \ (new_order.get_status() != OrderStatus.OPEN and new_order.get_status() != OrderStatus.PARTIALLY_FILLED): is_to_keep = False self.logger.info( f"{title} : {new_order.get_order_symbol()} | " f"{new_order.get_order_type()} | " f"Price : {new_order.get_origin_price()} | " f"Quantity : {new_order.get_origin_quantity()} | " f"Status : {new_order.get_status()} " f"{'' if is_to_keep else ': will be archived in trades history if not already'}" ) if is_to_keep: # notify order manager of a new open order self.order_manager.add_order_to_list(new_order) elif not is_already_in_history: self.trades_manager.add_new_trade_in_history( Trade(self.exchange, new_order)) # if this order is linked to another if linked_to is not None: new_order.add_linked_order(linked_to) return new_order
def notify_order_close(self, order): # Cancel linked orders for linked_order in order.get_linked_orders(): linked_order.cancel_order() self.order_manager.remove_order_from_list(linked_order) # update portfolio with ended order self.portfolio.update_portfolio_available(order, False) _, profitability_percent, profitability_diff = self.portfolio.update_portfolio( order) # add to trade history self.trades_manager.add_new_trade(Trade(self.exchange, order)) # remove order to open_orders self.order_manager.remove_order_from_list(order) # notification order.get_order_notifier().end(order, order.get_linked_orders(), profitability_diff, profitability_percent)
def parse_exchange_order_to_trade_instance(self, order): order_inst = self.parse_exchange_order_to_order_instance(order) trade = Trade(self.exchange, order_inst) self.trades_manager.add_new_trade_in_history(trade)