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(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)
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(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_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_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( 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 # 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_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)
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)