async def test_notify_order_close_with_linked_orders(self): config, _, trader_inst = await self.init_default() # Test buy order market_buy = BuyMarketOrder(trader_inst) market_buy.new(TraderOrderType.BUY_MARKET, "BTC/EUR", 70, 10, 70) # Test buy order limit_sell = SellLimitOrder(trader_inst) limit_sell.new(TraderOrderType.SELL_LIMIT, "NANO/USDT", 70, 10, 70) # Test stop loss order stop_loss = StopLossOrder(trader_inst) stop_loss.new(TraderOrderType.STOP_LOSS, "BTC/USD", 60, 10, 60) stop_loss.add_linked_order(limit_sell) limit_sell.add_linked_order(stop_loss) # create order notifier to prevent None call market_buy.order_notifier = OrderNotifier(config, market_buy) stop_loss.order_notifier = OrderNotifier(config, stop_loss) limit_sell.order_notifier = OrderNotifier(config, limit_sell) trader_inst.get_order_manager().add_order_to_list(market_buy) trader_inst.get_order_manager().add_order_to_list(stop_loss) trader_inst.get_order_manager().add_order_to_list(limit_sell) await trader_inst.notify_order_close(limit_sell) assert market_buy in trader_inst.get_open_orders() assert stop_loss not in trader_inst.get_open_orders() assert limit_sell not in trader_inst.get_open_orders() self.stop(trader_inst)
def test_cancel_open_orders_multi_symbol(self): config, _, trader_inst = self.init_default() # Test buy order market_buy = BuyMarketOrder(trader_inst) market_buy.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_MARKET], "BTC/EUR", 70, 10, 70) # Test buy order limit_sell = SellLimitOrder(trader_inst) limit_sell.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT], "NANO/USDT", 70, 10, 70) # Test sell order market_sell = SellMarketOrder(trader_inst) market_sell.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_MARKET], self.DEFAULT_SYMBOL, 70, 10, 70) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_LIMIT], self.DEFAULT_SYMBOL, 70, 10, 70) # create order notifier to prevent None call market_buy.order_notifier = OrderNotifier(config, market_buy) market_sell.order_notifier = OrderNotifier(config, market_sell) limit_buy.order_notifier = OrderNotifier(config, limit_buy) limit_sell.order_notifier = OrderNotifier(config, limit_sell) trader_inst.get_order_manager().add_order_to_list(market_buy) trader_inst.get_order_manager().add_order_to_list(market_sell) trader_inst.get_order_manager().add_order_to_list(limit_buy) trader_inst.get_order_manager().add_order_to_list(limit_sell) assert market_buy in trader_inst.get_open_orders() assert market_sell in trader_inst.get_open_orders() assert limit_buy in trader_inst.get_open_orders() assert limit_sell in trader_inst.get_open_orders() trader_inst.cancel_open_orders(self.DEFAULT_SYMBOL) assert market_buy in trader_inst.get_open_orders() assert market_sell not in trader_inst.get_open_orders() assert limit_buy not in trader_inst.get_open_orders() assert limit_sell in trader_inst.get_open_orders() self.stop(trader_inst)
async def test_order_fill_callback_not_in_db(): decider, creator, _, trader = await _get_tools() filled_order = BuyLimitOrder(trader) filled_order.new(TraderOrderType.BUY_LIMIT, "BTC/USDT", 100, 1, price=100) filled_order.order_notifier = OrderNotifier(trader.config, filled_order) await _fill_order(filled_order, trader, ignore_open_orders=True) open_orders = trader.get_order_manager().get_open_orders() assert len(open_orders) == 3 assert all(o.get_status() == OrderStatus.OPEN for o in open_orders) assert all(o.get_side() == TradeOrderSide.SELL for o in open_orders) total_sell_quantity = sum(o.get_origin_quantity() for o in open_orders) assert filled_order.get_origin_quantity( ) * 0.9999 <= total_sell_quantity <= filled_order.get_origin_quantity() price = filled_order.get_filled_price() max_price = price * creator.PRICE_WEIGH_TO_PRICE_PERCENT[ decider.DEFAULT_SELL_TARGET] increment = (max_price - price) / decider.sell_orders_per_buy assert round(open_orders[0].get_origin_price(), 7) == round(price + increment, 7) assert round(open_orders[1].get_origin_price(), 7) == round(price + 2 * increment, 7) assert round(open_orders[2].get_origin_price(), 7) == round(price + 3 * increment, 7)
def create_order_instance(self, order_type, symbol, current_price, quantity, price=None, stop_price=None, linked_to=None, status=None, order_id=None, quantity_filled=None, timestamp=None, linked_portfolio=None): # create new order instance order_class = OrderConstants.TraderOrderTypeClasses[order_type] order = order_class(self) # manage order notifier if linked_to is None: order_notifier = OrderNotifier(self.config, order) else: order_notifier = linked_to.get_order_notifier() order.new(order_type=order_type, symbol=symbol, current_price=current_price, quantity=quantity, price=price, stop_price=stop_price, order_notifier=order_notifier, order_id=order_id, status=status, quantity_filled=quantity_filled, timestamp=timestamp, linked_to=linked_to, linked_portfolio=linked_portfolio) return order
def create_order(self, order_type, symbol, quantity, price=None, stop_price=None, linked_to=None): self.logger.info("Order creation : {0} | {1} | Price : {2}".format( symbol, order_type, price)) # create new order instance order_class = OrderConstants.TraderOrderTypeClasses[order_type] order = order_class(self) # manage order notifier if linked_to is None: order_notifier = OrderNotifier(self.config, order) else: order_notifier = linked_to.get_order_notifier() order.new(order_type, symbol, quantity, price, stop_price, order_notifier) # notify order manager of a new open order self.order_manager.add_order_to_list(order) # update the availability of the currency in the portfolio self.portfolio.update_portfolio_available(order, True) # if this order is linked to another (ex : a sell limit order with a stop loss order) if linked_to is not None: linked_to.add_linked_order(order) order.add_linked_order(linked_to) return order
def test_notify_order_close(self): config, _, trader_inst = self.init_default() # Test buy order market_buy = BuyMarketOrder(trader_inst) market_buy.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_MARKET], "BTC/EUR", 70, 10, 70) # Test buy order limit_sell = SellLimitOrder(trader_inst) limit_sell.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT], "NANO/USDT", 70, 10, 70) # Test stop loss order stop_loss = StopLossOrder(trader_inst) stop_loss.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.STOP_LOSS], "BTC/USD", 60, 10, 60) # create order notifier to prevent None call market_buy.order_notifier = OrderNotifier(config, market_buy) limit_sell.order_notifier = OrderNotifier(config, limit_sell) stop_loss.order_notifier = OrderNotifier(config, stop_loss) trader_inst.get_order_manager().add_order_to_list(market_buy) trader_inst.get_order_manager().add_order_to_list(stop_loss) trader_inst.get_order_manager().add_order_to_list(limit_sell) trader_inst.notify_order_close(limit_sell, True) trader_inst.notify_order_close(market_buy, True) assert market_buy not in trader_inst.get_open_orders() assert limit_sell not in trader_inst.get_open_orders() assert stop_loss in trader_inst.get_open_orders() self.stop(trader_inst)
async def test_cancel_open_orders_default_symbol(self): config, _, trader_inst = await self.init_default() # Test buy order market_buy = BuyMarketOrder(trader_inst) market_buy.new(TraderOrderType.BUY_MARKET, self.DEFAULT_SYMBOL, 70, 10, 70) # Test sell order market_sell = SellMarketOrder(trader_inst) market_sell.new(TraderOrderType.SELL_MARKET, self.DEFAULT_SYMBOL, 70, 10, 70) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new(TraderOrderType.BUY_LIMIT, self.DEFAULT_SYMBOL, 70, 10, 70) # create order notifier to prevent None call market_buy.order_notifier = OrderNotifier(config, market_buy) market_sell.order_notifier = OrderNotifier(config, market_sell) limit_buy.order_notifier = OrderNotifier(config, limit_buy) trader_inst.get_order_manager().add_order_to_list(market_buy) trader_inst.get_order_manager().add_order_to_list(market_sell) trader_inst.get_order_manager().add_order_to_list(limit_buy) assert market_buy in trader_inst.get_open_orders() assert market_sell in trader_inst.get_open_orders() assert limit_buy in trader_inst.get_open_orders() await trader_inst.cancel_open_orders(self.DEFAULT_SYMBOL) assert market_buy not in trader_inst.get_open_orders() assert market_sell not in trader_inst.get_open_orders() assert limit_buy not in trader_inst.get_open_orders() self.stop(trader_inst)