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
Exemple #2
0
    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)
        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)
Exemple #4
0
    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)