async def test_cancel_open_orders_multi_symbol(self): config, exchange_manager, trader_inst = await self.init_default() orders_manager = exchange_manager.exchange_personal_data.orders_manager trades_manager = exchange_manager.exchange_personal_data.trades_manager # Test buy order market_buy = BuyMarketOrder(trader_inst) market_buy.update(order_type=TraderOrderType.BUY_MARKET, symbol="BTC/USDC", current_price=decimal.Decimal("70"), quantity=decimal.Decimal("10"), price=decimal.Decimal("70")) # Test buy order limit_sell = SellLimitOrder(trader_inst) limit_sell.update(order_type=TraderOrderType.SELL_LIMIT, symbol="NANO/USDT", current_price=decimal.Decimal("70"), quantity=decimal.Decimal("10"), price=decimal.Decimal("70")) # Test sell order market_sell = SellMarketOrder(trader_inst) market_sell.update(order_type=TraderOrderType.SELL_MARKET, symbol=self.DEFAULT_SYMBOL, current_price=decimal.Decimal("70"), quantity=decimal.Decimal("10"), price=decimal.Decimal("70")) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.update(order_type=TraderOrderType.BUY_LIMIT, symbol=self.DEFAULT_SYMBOL, current_price=decimal.Decimal("70"), quantity=decimal.Decimal("10"), price=decimal.Decimal("70")) await trader_inst.create_order(market_buy) await trader_inst.create_order(market_sell) await trader_inst.create_order(limit_buy) await trader_inst.create_order(limit_sell) # market orders not in open orders as they are instantly filled assert market_buy not in orders_manager.get_open_orders() assert market_sell not in orders_manager.get_open_orders() assert limit_buy in orders_manager.get_open_orders() assert limit_sell in orders_manager.get_open_orders() assert len(trades_manager.trades) == 2 assert await trader_inst.cancel_open_orders(self.DEFAULT_SYMBOL) is True assert limit_buy not in orders_manager.get_open_orders() assert limit_sell in orders_manager.get_open_orders() # added cancelled orders as cancelled trades assert len(trades_manager.trades) == 3 await self.stop(exchange_manager)
async def test_close_filled_order_with_linked_orders(self): config, exchange_manager, trader_inst = await self.init_default() orders_manager = exchange_manager.exchange_personal_data.orders_manager trades_manager = exchange_manager.exchange_personal_data.trades_manager # Test buy order market_buy = BuyMarketOrder(trader_inst) market_buy.update(order_type=TraderOrderType.BUY_MARKET, symbol="BTC/USDC", current_price=70, quantity=10, price=70) # Test buy order limit_sell = SellLimitOrder(trader_inst) limit_sell.update(order_type=TraderOrderType.SELL_LIMIT, symbol="NANO/USDT", current_price=70, quantity=10, price=70) # Test stop loss order stop_loss = StopLossOrder(trader_inst) stop_loss.update(order_type=TraderOrderType.STOP_LOSS, symbol="BTC/USDT", current_price=60, quantity=10, price=60) stop_loss.linked_orders = [limit_sell] limit_sell.linked_orders = [stop_loss] await trader_inst.create_order(market_buy) await trader_inst.create_order(stop_loss) await trader_inst.create_order(limit_sell) assert len(trades_manager.trades) == 1 limit_sell.filled_price = limit_sell.origin_price limit_sell.status = OrderStatus.FILLED await limit_sell.on_fill(force_fill=True) assert market_buy not in orders_manager.get_open_orders() assert stop_loss not in orders_manager.get_open_orders() assert limit_sell not in orders_manager.get_open_orders() # added filled orders as filled trades assert len(trades_manager.trades) == 3 assert trades_manager.get_trade( market_buy.order_id).status is OrderStatus.FILLED assert trades_manager.get_trade( limit_sell.order_id).status is OrderStatus.FILLED assert trades_manager.get_trade( stop_loss.order_id).status is OrderStatus.CANCELED with pytest.raises(KeyError): trades_manager.get_trade(f"{market_buy.order_id}1") await self.stop(exchange_manager)
async def test_close_filled_order(self): config, exchange_manager, trader_inst = await self.init_default() orders_manager = exchange_manager.exchange_personal_data.orders_manager trades_manager = exchange_manager.exchange_personal_data.trades_manager # Test buy order market_buy = BuyMarketOrder(trader_inst) market_buy.update(order_type=TraderOrderType.BUY_MARKET, symbol="BTC/USDC", current_price=70, quantity=10, price=70) # Test buy order limit_sell = SellLimitOrder(trader_inst) limit_sell.update(order_type=TraderOrderType.SELL_LIMIT, symbol="NANO/USDT", current_price=70, quantity=10, price=70) # Test stop loss order stop_loss = StopLossOrder(trader_inst) stop_loss.update(order_type=TraderOrderType.STOP_LOSS, symbol="BTC/USDT", current_price=60, quantity=10, price=60) await trader_inst.create_order(market_buy) await trader_inst.create_order(stop_loss) await trader_inst.create_order(limit_sell) assert len(trades_manager.trades) == 1 await limit_sell.on_fill(force_fill=True) # market orders not in open orders as they are instantly filled assert market_buy not in orders_manager.get_open_orders() assert limit_sell not in orders_manager.get_open_orders() assert stop_loss in orders_manager.get_open_orders() # added filled orders as filled trades assert len(trades_manager.trades) == 2 assert trades_manager.get_trade( market_buy.order_id).status is OrderStatus.FILLED assert trades_manager.get_trade( limit_sell.order_id).status is OrderStatus.FILLED await self.stop(exchange_manager)
async def test_cancel_all_open_orders_with_currency(self): config, exchange_manager, trader_inst = await self.init_default() orders_manager = exchange_manager.exchange_personal_data.orders_manager trades_manager = exchange_manager.exchange_personal_data.trades_manager # Test buy order market_buy = BuyMarketOrder(trader_inst) market_buy.update(order_type=TraderOrderType.BUY_MARKET, symbol="BTC/USDC", current_price=70, quantity=10, price=70) # Test buy order limit_sell = SellLimitOrder(trader_inst) limit_sell.update(order_type=TraderOrderType.SELL_LIMIT, symbol="XRP/BTC", current_price=70, quantity=10, price=70) # Test sell order market_sell = SellMarketOrder(trader_inst) market_sell.update(order_type=TraderOrderType.SELL_MARKET, symbol=self.DEFAULT_SYMBOL, current_price=70, quantity=10, price=70) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.update(order_type=TraderOrderType.BUY_LIMIT, symbol=self.DEFAULT_SYMBOL, current_price=70, quantity=10, price=70) # create order notifier to prevent None call await trader_inst.create_order(market_buy) await trader_inst.create_order(market_sell) await trader_inst.create_order(limit_buy) await trader_inst.create_order(limit_sell) # market orders not in open orders as they are instantly filled assert market_buy not in orders_manager.get_open_orders() assert market_sell not in orders_manager.get_open_orders() assert limit_buy in orders_manager.get_open_orders() assert limit_sell in orders_manager.get_open_orders() assert len(trades_manager.trades) == 2 assert await trader_inst.cancel_all_open_orders_with_currency("XYZ") is True assert limit_buy in orders_manager.get_open_orders() assert limit_sell in orders_manager.get_open_orders() assert len(trades_manager.trades) == 2 assert await trader_inst.cancel_all_open_orders_with_currency("XRP") is True assert limit_buy in orders_manager.get_open_orders() assert limit_sell not in orders_manager.get_open_orders() # added cancelled orders as cancelled trades assert len(trades_manager.trades) == 3 await trader_inst.cancel_all_open_orders_with_currency("USDT") assert limit_buy not in orders_manager.get_open_orders() assert limit_sell not in orders_manager.get_open_orders() # added cancelled orders as cancelled trades assert len(trades_manager.trades) == 4 await trader_inst.cancel_all_open_orders_with_currency("BTC") assert market_buy not in orders_manager.get_open_orders() assert market_sell not in orders_manager.get_open_orders() assert limit_buy not in orders_manager.get_open_orders() assert limit_sell not in orders_manager.get_open_orders() # added cancelled orders as cancelled trades assert len(trades_manager.trades) == 4 await self.stop(exchange_manager)
async def test_update_portfolio_with_multiple_symbols_orders( backtesting_trader): config, exchange_manager, trader = backtesting_trader portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager # Test buy order market_buy = BuyMarketOrder(trader) market_buy.update(order_type=TraderOrderType.BUY_MARKET, symbol="ETH/USDT", current_price=7, quantity=100, price=7) # test buy order creation portfolio_manager.portfolio.update_portfolio_available(market_buy, True) assert portfolio_manager.portfolio.get_currency_portfolio( "ETH", commons_constants.PORTFOLIO_AVAILABLE) == 0 assert portfolio_manager.portfolio.get_currency_portfolio( "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 300 assert portfolio_manager.portfolio.get_currency_portfolio( "ETH", commons_constants.PORTFOLIO_TOTAL) == 0 assert portfolio_manager.portfolio.get_currency_portfolio( "USDT", commons_constants.PORTFOLIO_TOTAL) == 1000 await fill_market_order(market_buy) assert portfolio_manager.portfolio.get_currency_portfolio( "ETH", commons_constants.PORTFOLIO_AVAILABLE) == 100 assert portfolio_manager.portfolio.get_currency_portfolio( "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 300 assert portfolio_manager.portfolio.get_currency_portfolio( "ETH", commons_constants.PORTFOLIO_TOTAL) == 100 assert portfolio_manager.portfolio.get_currency_portfolio( "USDT", commons_constants.PORTFOLIO_TOTAL) == 300 # Test buy order market_buy = BuyMarketOrder(trader) market_buy.update(order_type=TraderOrderType.BUY_MARKET, symbol="LTC/BTC", current_price=0.0135222, quantity=100, price=0.0135222) # test buy order creation portfolio_manager.portfolio.update_portfolio_available(market_buy, True) assert portfolio_manager.portfolio.get_currency_portfolio( "LTC", commons_constants.PORTFOLIO_AVAILABLE) == 0 assert portfolio_manager.portfolio.get_currency_portfolio( "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 8.647780000000001 assert portfolio_manager.portfolio.get_currency_portfolio( "LTC", commons_constants.PORTFOLIO_TOTAL) == 0 assert portfolio_manager.portfolio.get_currency_portfolio( "BTC", commons_constants.PORTFOLIO_TOTAL) == 10 await fill_market_order(market_buy) assert portfolio_manager.portfolio.get_currency_portfolio( "LTC", commons_constants.PORTFOLIO_AVAILABLE) == 100 assert portfolio_manager.portfolio.get_currency_portfolio( "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 8.647780000000001 assert portfolio_manager.portfolio.get_currency_portfolio( "LTC", commons_constants.PORTFOLIO_TOTAL) == 100 assert portfolio_manager.portfolio.get_currency_portfolio( "BTC", commons_constants.PORTFOLIO_TOTAL) == 8.647780000000001 # Test buy order limit_buy = BuyLimitOrder(trader) limit_buy.update(order_type=TraderOrderType.BUY_LIMIT, symbol="XRP/BTC", current_price=0.00012232132312312, quantity=3000.1214545, price=0.00012232132312312) # test buy order creation portfolio_manager.portfolio.update_portfolio_available(limit_buy, True) assert portfolio_manager.portfolio.get_currency_portfolio( "XRP", commons_constants.PORTFOLIO_AVAILABLE) == 0 assert portfolio_manager.portfolio.get_currency_portfolio( "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 8.280801174155501 assert portfolio_manager.portfolio.get_currency_portfolio( "XRP", commons_constants.PORTFOLIO_TOTAL) == 0 assert portfolio_manager.portfolio.get_currency_portfolio( "BTC", commons_constants.PORTFOLIO_TOTAL) == 8.647780000000001 # cancel portfolio_manager.portfolio.update_portfolio_available(limit_buy, False) assert portfolio_manager.portfolio.get_currency_portfolio( "XRP", commons_constants.PORTFOLIO_AVAILABLE) == 0 assert portfolio_manager.portfolio.get_currency_portfolio( "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 8.647780000000001 assert portfolio_manager.portfolio.get_currency_portfolio( "XRP", commons_constants.PORTFOLIO_TOTAL) == 0 assert portfolio_manager.portfolio.get_currency_portfolio( "BTC", commons_constants.PORTFOLIO_TOTAL) == 8.647780000000001