def test_update_portfolio_available(self):
        config, portfolio_inst, _, trader_inst = self.init_default()

        # Test buy order
        market_buy = BuyMarketOrder(trader_inst)
        market_buy.new(
            OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_MARKET],
            "BTC/USD", 70, 10, 70)
        # test buy order creation
        portfolio_inst.update_portfolio_available(market_buy, True)
        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.AVAILABLE) == 10
        assert portfolio_inst.get_currency_portfolio(
            "USD", Portfolio.AVAILABLE) == 300
        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.TOTAL) == 10
        assert portfolio_inst.get_currency_portfolio("USD",
                                                     Portfolio.TOTAL) == 1000

        # test buy order canceled --> return to init state and the update_portfolio will sync TOTAL with AVAILABLE
        portfolio_inst.update_portfolio_available(market_buy, 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

        # Test sell order
        limit_sell = SellLimitOrder(trader_inst)
        limit_sell.new(
            OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT],
            "BTC/USD", 60, 8, 60)
        # test sell order creation
        portfolio_inst.update_portfolio_available(limit_sell, True)
        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.AVAILABLE) == 2
        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 canceled --> return to init state and the update_portfolio will sync TOTAL with AVAILABLE
        portfolio_inst.update_portfolio_available(limit_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)
Beispiel #2
0
    def test_update_portfolio_available(self):
        config, portfolio_inst, _, trader_inst = self.init_default()

        # Test buy order
        market_buy = BuyMarketOrder(trader_inst)
        market_buy.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_MARKET],
                       "BTC/USD",
                       70,
                       10,
                       70)
        # test buy order creation
        portfolio_inst.update_portfolio_available(market_buy, True)
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 300
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000

        # test buy order canceled --> return to init state and the update_portfolio will sync TOTAL with AVAILABLE
        portfolio_inst.update_portfolio_available(market_buy, 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

        # Test sell order
        limit_sell = SellLimitOrder(trader_inst)
        limit_sell.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT],
                       "BTC/USD",
                       60,
                       8,
                       60)
        # test sell order creation
        portfolio_inst.update_portfolio_available(limit_sell, True)
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 2
        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 canceled --> return to init state and the update_portfolio will sync TOTAL with AVAILABLE
        portfolio_inst.update_portfolio_available(limit_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_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_available(self):
        _, portfolio_inst, _, trader_inst, sub_portfolio_inst = await self.init_default()

        # Test buy order
        market_buy = BuyMarketOrder(trader_inst)
        market_buy.new(TraderOrderType.BUY_MARKET,
                       "BTC/USD",
                       70,
                       10,
                       70)

        # test sub
        # test buy order creation
        sub_portfolio_inst.update_portfolio_available(market_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
        assert sub_portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 * self.DEFAULT_PERCENT
        assert sub_portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 * self.DEFAULT_PERCENT
        # test parent
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 300
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000

        # test buy order canceled --> return to init state and the update_portfolio will sync TOTAL with AVAILABLE
        sub_portfolio_inst.update_portfolio_available(market_buy, False)
        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
        assert sub_portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 * self.DEFAULT_PERCENT
        assert sub_portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 * self.DEFAULT_PERCENT

        # test parent
        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",
                       60,
                       8,
                       60)

        # test sub
        # test sell order creation
        sub_portfolio_inst.update_portfolio_available(limit_sell, True)
        assert sub_portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10 * self.DEFAULT_PERCENT - 8
        assert sub_portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 1000 * self.DEFAULT_PERCENT
        assert sub_portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 * self.DEFAULT_PERCENT
        assert sub_portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 * self.DEFAULT_PERCENT
        # test parent
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 2
        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 canceled --> return to init state and the update_portfolio will sync TOTAL with AVAILABLE
        sub_portfolio_inst.update_portfolio_available(limit_sell, False)
        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
        assert sub_portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 * self.DEFAULT_PERCENT
        assert sub_portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1000 * self.DEFAULT_PERCENT

        # test parent
        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