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 test_update_portfolio_with_cancelled_orders(self): config, portfolio_inst, _, trader_inst = self.init_default() # Test buy order market_sell = SellMarketOrder(trader_inst) market_sell.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_MARKET], "BTC/USD", 80, 4.1, 80) # Test sell order limit_sell = SellLimitOrder(trader_inst) limit_sell.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT], "BTC/USD", 10, 4.2, 10) # Test stop loss order stop_loss = StopLossOrder(trader_inst) stop_loss.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.STOP_LOSS], "BTC/USD", 80, 3.6, 80) portfolio_inst.update_portfolio_available(stop_loss, True) portfolio_inst.update_portfolio_available(limit_sell, True) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_LIMIT], "BTC/USD", 50, 4, 50) portfolio_inst.update_portfolio_available(limit_buy, True) portfolio_inst.update_portfolio_available(market_sell, True) assert round( portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE), 1) == 1.7 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 800 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 portfolio_inst.update_portfolio_available(stop_loss, False) portfolio_inst.update_portfolio_available(limit_sell, False) portfolio_inst.update_portfolio_available(limit_buy, False) portfolio_inst.update_portfolio_available(market_sell, False) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 1000 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 self.stop(trader_inst)
async def test_update_portfolio_with_cancelled_orders(self): _, portfolio_inst, exchange_inst, trader_inst = await self.init_default( ) # force fees => shouldn't do anything exchange_inst.config[CONFIG_SIMULATOR][CONFIG_SIMULATOR_FEES] = { CONFIG_SIMULATOR_FEES_MAKER: 0.05, CONFIG_SIMULATOR_FEES_TAKER: 0.1 } # Test buy order market_sell = SellMarketOrder(trader_inst) market_sell.new(TraderOrderType.SELL_MARKET, "BTC/USD", 80, 4.1, 80) # Test sell order limit_sell = SellLimitOrder(trader_inst) limit_sell.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 10, 4.2, 10) # Test stop loss order stop_loss = StopLossOrder(trader_inst) stop_loss.new(TraderOrderType.STOP_LOSS, "BTC/USD", 80, 3.6, 80) portfolio_inst.update_portfolio_available(stop_loss, True) portfolio_inst.update_portfolio_available(limit_sell, True) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 50, 4, 50) portfolio_inst.update_portfolio_available(limit_buy, True) portfolio_inst.update_portfolio_available(market_sell, True) assert round( portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE), 1) == 1.7 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 800 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 # with no filled orders portfolio_inst.update_portfolio_available(stop_loss, False) portfolio_inst.update_portfolio_available(limit_sell, False) portfolio_inst.update_portfolio_available(limit_buy, False) portfolio_inst.update_portfolio_available(market_sell, False) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 1000 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000
def test_update_portfolio_with_stop_loss_orders(self): config, portfolio_inst, _, trader_inst = self.init_default() # Test buy order limit_sell = SellLimitOrder(trader_inst) limit_sell.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT], "BTC/USD", 90, 4, 90) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_LIMIT], "BTC/USD", 50, 4, 50) # Test stop loss order stop_loss = StopLossOrder(trader_inst) stop_loss.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.STOP_LOSS], "BTC/USD", 60, 4, 60) fill_limit_or_stop_order(stop_loss, 59, 61) portfolio_inst.update_portfolio_available(stop_loss, True) portfolio_inst.update_portfolio_available(limit_sell, True) portfolio_inst.update_portfolio_available(limit_buy, True) assert round( portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE), 1) == 6 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 800 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 # cancel limits portfolio_inst.update_portfolio_available(limit_buy, False) portfolio_inst.update_portfolio_available(limit_sell, False) # filling stop loss # typical stop loss behavior --> update available before update portfolio portfolio_inst.update_portfolio(stop_loss) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 6 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 1240 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 6 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1240
def test_update_portfolio(self): config, portfolio_inst, _, trader_inst = self.init_default() # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_LIMIT], "BTC/USD", 70, 10, 70) # update portfolio with creations portfolio_inst.update_portfolio_available(limit_buy, True) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 300 fill_limit_or_stop_order(limit_buy, 69, 71) portfolio_inst.update_portfolio(limit_buy) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 20 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 300 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 20 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 300 # Test buy order market_sell = SellMarketOrder(trader_inst) market_sell.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_MARKET], "BTC/USD", 80, 8, 80) # update portfolio with creations portfolio_inst.update_portfolio_available(market_sell, True) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 12 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 300 fill_market_order(market_sell, 80) # when filling market sell portfolio_inst.update_portfolio(market_sell) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 12 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 940 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 12 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 940
async def test_reset_portfolio_available(self): _, portfolio_inst, _, trader_inst = await self.init_default() # Test buy order limit_sell = SellLimitOrder(trader_inst) limit_sell.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 90, 4, 90) portfolio_inst.update_portfolio_available(limit_sell, True) portfolio_inst.reset_portfolio_available() assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 1000 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 # Test sell order limit_sell = SellLimitOrder(trader_inst) limit_sell.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 90, 4, 90) portfolio_inst.update_portfolio_available(limit_sell, True) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new(TraderOrderType.BUY_LIMIT, "VEN/BTC", 0.5, 4, 0.5) portfolio_inst.update_portfolio_available(limit_buy, True) # Test buy order btc_limit_buy = BuyLimitOrder(trader_inst) btc_limit_buy.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 10, 50, 10) portfolio_inst.update_portfolio_available(btc_limit_buy, True) # Test buy order btc_limit_buy2 = BuyLimitOrder(trader_inst) btc_limit_buy2.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 10, 50, 10) portfolio_inst.update_portfolio_available(btc_limit_buy2, True) # reset equivalent of the ven buy order portfolio_inst.reset_portfolio_available("BTC", 4 * 0.5) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 6 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 0 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 # reset equivalent of the btc buy orders 1 and 2 portfolio_inst.reset_portfolio_available("USD") assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 6 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 1000 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000
async def test_update_portfolio_with_multiple_symbols_orders(self): _, portfolio_inst, _, trader_inst = await self.init_default() # Test buy order market_buy = BuyMarketOrder(trader_inst) market_buy.new(TraderOrderType.BUY_MARKET, "ETH/USD", 7, 100, 7) # test buy order creation portfolio_inst.update_portfolio_available(market_buy, True) assert portfolio_inst.get_currency_portfolio("ETH", Portfolio.AVAILABLE) == 0 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 300 assert portfolio_inst.get_currency_portfolio("ETH", Portfolio.TOTAL) == 0 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 await fill_market_order(market_buy, 7) await portfolio_inst.update_portfolio(market_buy) assert portfolio_inst.get_currency_portfolio( "ETH", Portfolio.AVAILABLE) == 100 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 300 assert portfolio_inst.get_currency_portfolio("ETH", Portfolio.TOTAL) == 100 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 300 # Test buy order market_buy = BuyMarketOrder(trader_inst) market_buy.new(TraderOrderType.BUY_MARKET, "LTC/BTC", 0.0135222, 100, 0.0135222) # test buy order creation portfolio_inst.update_portfolio_available(market_buy, True) assert portfolio_inst.get_currency_portfolio("LTC", Portfolio.AVAILABLE) == 0 assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 8.647780000000001 assert portfolio_inst.get_currency_portfolio("LTC", Portfolio.TOTAL) == 0 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 await fill_market_order(market_buy, 0.0135222) await portfolio_inst.update_portfolio(market_buy) assert portfolio_inst.get_currency_portfolio( "LTC", Portfolio.AVAILABLE) == 100 assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 8.647780000000001 assert portfolio_inst.get_currency_portfolio("LTC", Portfolio.TOTAL) == 100 assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.TOTAL) == 8.647780000000001 # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new(TraderOrderType.BUY_LIMIT, "XRP/BTC", 0.00012232132312312, 3000.1214545, 0.00012232132312312) # test buy order creation portfolio_inst.update_portfolio_available(limit_buy, True) assert portfolio_inst.get_currency_portfolio("XRP", Portfolio.AVAILABLE) == 0 assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 8.280801174155501 assert portfolio_inst.get_currency_portfolio("XRP", Portfolio.TOTAL) == 0 assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.TOTAL) == 8.647780000000001 # cancel portfolio_inst.update_portfolio_available(limit_buy, False) assert portfolio_inst.get_currency_portfolio("XRP", Portfolio.AVAILABLE) == 0 assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 8.647780000000001 assert portfolio_inst.get_currency_portfolio("XRP", Portfolio.TOTAL) == 0 assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.TOTAL) == 8.647780000000001
async def test_update_portfolio_with_multiple_filled_orders(self): _, portfolio_inst, _, trader_inst = await self.init_default() # Test buy order limit_sell = SellLimitOrder(trader_inst) limit_sell.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 90, 4, 90) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 60, 2, 60) # Test buy order limit_buy_2 = BuyLimitOrder(trader_inst) limit_buy_2.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 50, 4, 50) # Test buy order limit_buy_3 = BuyLimitOrder(trader_inst) limit_buy_3.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 46, 2, 46) # Test buy order limit_buy_4 = BuyLimitOrder(trader_inst) limit_buy_4.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 41, 1.78, 41) # Test buy order limit_buy_5 = BuyLimitOrder(trader_inst) limit_buy_5.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 0.2122427, 3.72448, 0.2122427) # Test buy order limit_buy_6 = BuyLimitOrder(trader_inst) limit_buy_6.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 430, 1.05, 430) # Test sell order limit_sell_2 = SellLimitOrder(trader_inst) limit_sell_2.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 10, 2, 10) # Test stop loss order stop_loss_2 = StopLossOrder(trader_inst) stop_loss_2.new(TraderOrderType.STOP_LOSS, "BTC/USD", 10, 2, 10) # Test sell order limit_sell_3 = SellLimitOrder(trader_inst) limit_sell_3.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 20, 1, 20) # Test stop loss order stop_loss_3 = StopLossOrder(trader_inst) stop_loss_3.new(TraderOrderType.STOP_LOSS, "BTC/USD", 20, 1, 20) # Test sell order limit_sell_4 = SellLimitOrder(trader_inst) limit_sell_4.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 50, 0.2, 50) # Test stop loss order stop_loss_4 = StopLossOrder(trader_inst) stop_loss_4.new(TraderOrderType.STOP_LOSS, "BTC/USD", 45, 0.2, 45) # Test sell order limit_sell_5 = SellLimitOrder(trader_inst) limit_sell_5.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 11, 0.7, 11) # Test stop loss order stop_loss_5 = StopLossOrder(trader_inst) stop_loss_5.new(TraderOrderType.STOP_LOSS, "BTC/USD", 9, 0.7, 9) portfolio_inst.update_portfolio_available(stop_loss_2, True) portfolio_inst.update_portfolio_available(stop_loss_3, True) portfolio_inst.update_portfolio_available(stop_loss_4, True) portfolio_inst.update_portfolio_available(stop_loss_5, True) portfolio_inst.update_portfolio_available(limit_sell, True) portfolio_inst.update_portfolio_available(limit_sell_2, True) portfolio_inst.update_portfolio_available(limit_sell_3, True) portfolio_inst.update_portfolio_available(limit_sell_4, True) portfolio_inst.update_portfolio_available(limit_sell_5, True) portfolio_inst.update_portfolio_available(limit_buy, True) portfolio_inst.update_portfolio_available(limit_buy_2, True) portfolio_inst.update_portfolio_available(limit_buy_3, True) portfolio_inst.update_portfolio_available(limit_buy_4, True) portfolio_inst.update_portfolio_available(limit_buy_5, True) portfolio_inst.update_portfolio_available(limit_buy_6, True) assert round( portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE), 1) == 2.1 assert round( portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE), 7) == 62.7295063 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 # cancels portfolio_inst.update_portfolio_available(stop_loss_3, False) portfolio_inst.update_portfolio_available(stop_loss_5, False) portfolio_inst.update_portfolio_available(limit_sell_2, False) portfolio_inst.update_portfolio_available(limit_buy, False) portfolio_inst.update_portfolio_available(limit_buy_3, False) portfolio_inst.update_portfolio_available(limit_buy_5, False) portfolio_inst.update_portfolio_available(limit_sell_4, False) # filling await fill_limit_or_stop_order(stop_loss_2, 9, 11) await fill_limit_or_stop_order(limit_sell, 89, 91) await fill_limit_or_stop_order(limit_sell_3, 19, 21) await fill_limit_or_stop_order(limit_buy_2, 49, 51) await fill_limit_or_stop_order(limit_sell_5, 9, 12) await fill_limit_or_stop_order(stop_loss_4, 44, 46) await fill_limit_or_stop_order(limit_buy_4, 40, 42) await fill_limit_or_stop_order(limit_buy_5, 0.2122426, 0.2122428) await fill_limit_or_stop_order(limit_buy_6, 429, 431) await portfolio_inst.update_portfolio(stop_loss_2) await portfolio_inst.update_portfolio(limit_buy_4) await portfolio_inst.update_portfolio(limit_sell) await portfolio_inst.update_portfolio(limit_sell_3) await portfolio_inst.update_portfolio(limit_buy_2) await portfolio_inst.update_portfolio(limit_sell_5) await portfolio_inst.update_portfolio(stop_loss_4) await portfolio_inst.update_portfolio(limit_buy_5) await portfolio_inst.update_portfolio(limit_buy_6) assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 12.65448 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 692.22 assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.TOTAL) == 12.65448 assert round( portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL), 7) == 691.4295063
async def test_update_portfolio_with_some_filled_orders(self): _, portfolio_inst, _, trader_inst = await self.init_default() # Test buy order limit_sell = SellLimitOrder(trader_inst) limit_sell.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 90, 4, 90) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 60, 2, 60) # Test buy order limit_buy_2 = BuyLimitOrder(trader_inst) limit_buy_2.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 50, 4, 50) # Test sell order limit_sell_2 = SellLimitOrder(trader_inst) limit_sell_2.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 10, 2, 10) # Test stop loss order stop_loss_2 = StopLossOrder(trader_inst) stop_loss_2.new(TraderOrderType.STOP_LOSS, "BTC/USD", 10, 2, 10) # Test sell order limit_sell_3 = SellLimitOrder(trader_inst) limit_sell_3.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 20, 1, 20) # Test stop loss order stop_loss_3 = StopLossOrder(trader_inst) stop_loss_3.new(TraderOrderType.STOP_LOSS, "BTC/USD", 20, 1, 20) portfolio_inst.update_portfolio_available(stop_loss_2, True) portfolio_inst.update_portfolio_available(stop_loss_3, True) portfolio_inst.update_portfolio_available(limit_sell, True) portfolio_inst.update_portfolio_available(limit_sell_2, True) portfolio_inst.update_portfolio_available(limit_sell_3, True) portfolio_inst.update_portfolio_available(limit_buy, True) portfolio_inst.update_portfolio_available(limit_buy_2, True) assert round( portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE), 1) == 3 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 680 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 # cancels portfolio_inst.update_portfolio_available(stop_loss_3, False) portfolio_inst.update_portfolio_available(limit_sell_2, False) portfolio_inst.update_portfolio_available(limit_buy, False) # filling await fill_limit_or_stop_order(stop_loss_2, 9, 11) await fill_limit_or_stop_order(limit_sell, 89, 91) await fill_limit_or_stop_order(limit_sell_3, 19, 21) await fill_limit_or_stop_order(limit_buy_2, 49, 51) await portfolio_inst.update_portfolio(stop_loss_2) await portfolio_inst.update_portfolio(limit_sell) await portfolio_inst.update_portfolio(limit_sell_3) await portfolio_inst.update_portfolio(limit_buy_2) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 7 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 1200 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 7 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1200
async def test_update_portfolio_with_filled_orders(self): _, portfolio_inst, exchange_inst, trader_inst = await self.init_default( ) # force fees => should have consequences exchange_inst.config[CONFIG_SIMULATOR][CONFIG_SIMULATOR_FEES] = { CONFIG_SIMULATOR_FEES_MAKER: 0.05, CONFIG_SIMULATOR_FEES_TAKER: 0.1 } # Test buy order market_sell = SellMarketOrder(trader_inst) market_sell.new(TraderOrderType.SELL_MARKET, "BTC/USD", 70, 3, 70) await fill_market_order(market_sell, 70) # Test sell order limit_sell = SellLimitOrder(trader_inst) limit_sell.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 100, 4.2, 100) # Test stop loss order stop_loss = StopLossOrder(trader_inst) stop_loss.new(TraderOrderType.STOP_LOSS, "BTC/USD", 80, 4.2, 80) limit_sell.add_linked_order(stop_loss) stop_loss.add_linked_order(limit_sell) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 50, 2, 50) await fill_limit_or_stop_order(limit_buy, 49, 51) # update portfolio with creations portfolio_inst.update_portfolio_available(market_sell, True) portfolio_inst.update_portfolio_available(limit_sell, True) portfolio_inst.update_portfolio_available(stop_loss, True) portfolio_inst.update_portfolio_available(limit_buy, True) assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 2.8 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 900 # when cancelling limit sell, market sell and stop orders portfolio_inst.update_portfolio_available(stop_loss, False) portfolio_inst.update_portfolio_available(limit_sell, False) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 7 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 900 # when filling limit buy await portfolio_inst.update_portfolio(limit_buy) assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 8.999 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 900 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 11.999 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 900 # when filling market sell await portfolio_inst.update_portfolio(market_sell) assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 8.999 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 1109.79 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 8.999 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.TOTAL) == 1109.79
async def test_update_portfolio(self): _, portfolio_inst, _, trader_inst, sub_portfolio_inst = await self.init_default() # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new(TraderOrderType.BUY_LIMIT, "BTC/USD", 70, 10, 70) # update portfolio with creations sub_portfolio_inst.update_portfolio_available(limit_buy, True) assert sub_portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10 * self.DEFAULT_PERCENT assert sub_portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 1000*self.DEFAULT_PERCENT - 700 # test parent assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 300 await fill_limit_or_stop_order(limit_buy, 69, 71) await sub_portfolio_inst.update_portfolio(limit_buy) assert sub_portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == (10 * self.DEFAULT_PERCENT)+10 assert sub_portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 1000*self.DEFAULT_PERCENT-700 assert sub_portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == (10 * self.DEFAULT_PERCENT) + 10 assert sub_portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 * self.DEFAULT_PERCENT - 700 # test parent assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 20 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 300 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 20 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 300 # Test buy order market_sell = SellMarketOrder(trader_inst) market_sell.new(TraderOrderType.SELL_MARKET, "BTC/USD", 80, 8, 80) # update portfolio with creations sub_portfolio_inst.update_portfolio_available(market_sell, True) assert sub_portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10*self.DEFAULT_PERCENT + 2 assert sub_portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 1000*self.DEFAULT_PERCENT-700 # test parent assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 12 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 300 await fill_market_order(market_sell, 80) # when filling market sell await sub_portfolio_inst.update_portfolio(market_sell) assert sub_portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10*self.DEFAULT_PERCENT + 2 assert sub_portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 1000*self.DEFAULT_PERCENT-60 assert sub_portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10*self.DEFAULT_PERCENT + 2 assert sub_portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000*self.DEFAULT_PERCENT-60 # test parent assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 12 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 940 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 12 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 940
async def test_add_remove_order_to_list(self): _, _, trader_inst, order_manager_inst, trader_simulator_inst, \ order_manager_simulator_inst = await self.init_default() # real trader assert len(order_manager_inst.get_open_orders()) == 0 r_order1 = BuyLimitOrder(trader_inst) r_order1.order_id = 1 r_order2 = StopLossOrder(trader_inst) r_order1.add_linked_order(r_order2) r_order2.add_linked_order(r_order1) # add both order_manager_inst.add_order_to_list(r_order1) order_manager_inst.add_order_to_list(r_order2) assert len(order_manager_inst.get_open_orders()) == 2 r_order3 = BuyLimitOrder(trader_inst) r_order3.order_id = 3 r_order4 = StopLossLimitOrder(trader_inst) r_order3.add_linked_order(r_order4) r_order4.add_linked_order(r_order3) # add both order_manager_inst.add_order_to_list(r_order3) order_manager_inst.add_order_to_list(r_order4) assert len(order_manager_inst.get_open_orders()) == 4 # add none order_manager_inst.add_order_to_list(r_order3) order_manager_inst.add_order_to_list(r_order4) order_manager_inst.add_order_to_list(r_order1) order_manager_inst.add_order_to_list(r_order2) assert len(order_manager_inst.get_open_orders()) == 4 # now remove assert order_manager_inst._already_has_real_or_linked_order(r_order2) assert order_manager_inst._order_in_list(r_order2) order_manager_inst.remove_order_from_list(r_order2) assert not order_manager_inst._already_has_real_or_linked_order(r_order2) assert not order_manager_inst._order_in_list(r_order2) assert len(order_manager_inst.get_open_orders()) == 3 assert order_manager_inst._already_has_real_or_linked_order(r_order1) order_manager_inst.remove_order_from_list(r_order1) assert not order_manager_inst._already_has_real_or_linked_order(r_order1) assert len(order_manager_inst.get_open_orders()) == 2 assert order_manager_inst._already_has_real_or_linked_order(r_order3) order_manager_inst.remove_order_from_list(r_order3) assert not order_manager_inst._already_has_real_or_linked_order(r_order3) assert len(order_manager_inst.get_open_orders()) == 1 assert order_manager_inst._already_has_real_or_linked_order(r_order4) assert order_manager_inst._order_in_list(r_order4) order_manager_inst.remove_order_from_list(r_order4) assert not order_manager_inst._already_has_real_or_linked_order(r_order4) assert not order_manager_inst._order_in_list(r_order4) assert len(order_manager_inst.get_open_orders()) == 0 # simulated trader assert len(order_manager_simulator_inst.get_open_orders()) == 0 r_order11 = BuyLimitOrder(trader_simulator_inst) r_order12 = StopLossOrder(trader_simulator_inst) r_order11.add_linked_order(r_order12) r_order12.add_linked_order(r_order11) # add both order_manager_simulator_inst.add_order_to_list(r_order11) order_manager_simulator_inst.add_order_to_list(r_order12) assert len(order_manager_simulator_inst.get_open_orders()) == 2 r_order13 = BuyLimitOrder(trader_simulator_inst) r_order14 = StopLossLimitOrder(trader_simulator_inst) r_order13.add_linked_order(r_order14) r_order14.add_linked_order(r_order13) # add both order_manager_simulator_inst.add_order_to_list(r_order13) order_manager_simulator_inst.add_order_to_list(r_order14) assert len(order_manager_simulator_inst.get_open_orders()) == 4 # add none order_manager_simulator_inst.add_order_to_list(r_order13) order_manager_simulator_inst.add_order_to_list(r_order14) order_manager_simulator_inst.add_order_to_list(r_order11) order_manager_simulator_inst.add_order_to_list(r_order12) assert len(order_manager_simulator_inst.get_open_orders()) == 4 # now remove assert not order_manager_inst._order_in_list(r_order13) assert not order_manager_inst._order_in_list(r_order14) assert not order_manager_inst._order_in_list(r_order11) assert not order_manager_inst._order_in_list(r_order12) assert order_manager_simulator_inst._order_in_list(r_order11) order_manager_simulator_inst.remove_order_from_list(r_order11) assert len(order_manager_simulator_inst.get_open_orders()) == 3 assert not order_manager_simulator_inst._order_in_list(r_order11) order_manager_simulator_inst.remove_order_from_list(r_order12) assert len(order_manager_simulator_inst.get_open_orders()) == 2 order_manager_simulator_inst.remove_order_from_list(r_order14) assert len(order_manager_simulator_inst.get_open_orders()) == 1 assert order_manager_simulator_inst._order_in_list(r_order13) order_manager_simulator_inst.remove_order_from_list(r_order13) assert len(order_manager_simulator_inst.get_open_orders()) == 0 assert not order_manager_simulator_inst._order_in_list(r_order13)
def test_update_portfolio_with_filled_orders(self): config, portfolio_inst, _, trader_inst = self.init_default() # Test buy order market_sell = SellMarketOrder(trader_inst) market_sell.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_MARKET], "BTC/USD", 70, 3, 70) fill_market_order(market_sell, 70) # Test sell order limit_sell = SellLimitOrder(trader_inst) limit_sell.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT], "BTC/USD", 100, 4.2, 100) # Test stop loss order stop_loss = StopLossOrder(trader_inst) stop_loss.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.STOP_LOSS], "BTC/USD", 80, 4.2, 80) limit_sell.add_linked_order(stop_loss) stop_loss.add_linked_order(limit_sell) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_LIMIT], "BTC/USD", 50, 2, 50) fill_limit_or_stop_order(limit_buy, 49, 51) # update portfolio with creations portfolio_inst.update_portfolio_available(market_sell, True) portfolio_inst.update_portfolio_available(limit_sell, True) portfolio_inst.update_portfolio_available(stop_loss, True) portfolio_inst.update_portfolio_available(limit_buy, True) assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 2.8 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 900 # when cancelling limit sell, market sell and stop orders portfolio_inst.update_portfolio_available(stop_loss, False) portfolio_inst.update_portfolio_available(limit_sell, False) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 7 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 900 # when filling limit buy portfolio_inst.update_portfolio(limit_buy) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 9 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 900 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 12 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 900 # when filling market sell portfolio_inst.update_portfolio(market_sell) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 9 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 1110 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 9 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1110
def test_update_portfolio_with_stop_loss_orders(self): config, portfolio_inst, _, trader_inst = self.init_default() # Test buy order limit_sell = SellLimitOrder(trader_inst) limit_sell.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT], "BTC/USD", 90, 4, 90) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_LIMIT], "BTC/USD", 50, 4, 50) # Test stop loss order stop_loss = StopLossOrder(trader_inst) stop_loss.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.STOP_LOSS], "BTC/USD", 60, 4, 60) last_prices = [] for i in range(0, SIMULATOR_LAST_PRICES_TO_CHECK): last_prices.insert(i, {}) last_prices[i]["price"] = random.uniform(59, 61) stop_loss.last_prices = last_prices stop_loss.update_order_status() portfolio_inst.update_portfolio_available(stop_loss, True) portfolio_inst.update_portfolio_available(limit_sell, True) portfolio_inst.update_portfolio_available(limit_buy, True) assert round( portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE), 1) == 6 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 800 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 # cancel limits portfolio_inst.update_portfolio_available(limit_buy, False) portfolio_inst.update_portfolio_available(limit_sell, False) # filling stop loss # typical stop loss behavior --> update available before update portfolio portfolio_inst.update_portfolio(stop_loss) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 6 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 1240 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 6 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1240 self.stop(trader_inst)
def test_update_portfolio_with_filled_orders(self): config, portfolio_inst, _, trader_inst = self.init_default() # Test buy order market_sell = SellMarketOrder(trader_inst) market_sell.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_MARKET], "BTC/USD", 70, 3, 70) last_prices = [{"price": 70}] market_sell.last_prices = last_prices market_sell.update_order_status() # Test sell order limit_sell = SellLimitOrder(trader_inst) limit_sell.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT], "BTC/USD", 100, 4.2, 100) # Test stop loss order stop_loss = StopLossOrder(trader_inst) stop_loss.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.STOP_LOSS], "BTC/USD", 80, 4.2, 80) limit_sell.add_linked_order(stop_loss) stop_loss.add_linked_order(limit_sell) # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_LIMIT], "BTC/USD", 50, 2, 50) last_prices = [] for i in range(0, SIMULATOR_LAST_PRICES_TO_CHECK): last_prices.insert(i, {}) last_prices[i]["price"] = random.uniform(49, 51) limit_buy.last_prices = last_prices limit_buy.update_order_status() # update portfolio with creations portfolio_inst.update_portfolio_available(market_sell, True) portfolio_inst.update_portfolio_available(limit_sell, True) portfolio_inst.update_portfolio_available(stop_loss, True) portfolio_inst.update_portfolio_available(limit_buy, True) assert portfolio_inst.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 2.8 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 900 # when cancelling limit sell, market sell and stop orders portfolio_inst.update_portfolio_available(stop_loss, False) portfolio_inst.update_portfolio_available(limit_sell, False) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 7 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 900 # when filling limit buy portfolio_inst.update_portfolio(limit_buy) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 9 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 900 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 12 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 900 # when filling market sell portfolio_inst.update_portfolio(market_sell) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 9 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 1110 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 9 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1110 self.stop(trader_inst)
def test_update_portfolio(self): config, portfolio_inst, _, trader_inst = self.init_default() # Test buy order limit_buy = BuyLimitOrder(trader_inst) limit_buy.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_LIMIT], "BTC/USD", 70, 10, 70) # update portfolio with creations portfolio_inst.update_portfolio_available(limit_buy, True) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 300 last_prices = [] for i in range(0, SIMULATOR_LAST_PRICES_TO_CHECK): last_prices.insert(i, {}) last_prices[i]["price"] = random.uniform(69, 71) limit_buy.last_prices = last_prices limit_buy.update_order_status() portfolio_inst.update_portfolio(limit_buy) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 20 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 300 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 20 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 300 # Test buy order market_sell = SellMarketOrder(trader_inst) market_sell.new( OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_MARKET], "BTC/USD", 80, 8, 80) # update portfolio with creations portfolio_inst.update_portfolio_available(market_sell, True) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 12 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 300 last_prices = [{"price": 80}] market_sell.last_prices = last_prices market_sell.update_order_status() # when filling market sell portfolio_inst.update_portfolio(market_sell) assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 12 assert portfolio_inst.get_currency_portfolio( "USD", Portfolio.AVAILABLE) == 940 assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 12 assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 940 self.stop(trader_inst)