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_with_filled_orders(self): config, portfolio_inst, exchange_inst, trader_inst = 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( 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) == 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 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
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)