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