Ejemplo n.º 1
0
    def test_add_select_trade_in_history(self):
        _, exchange_inst, trader_inst, trades_manager_inst = self.init_default()
        self.stop(trader_inst)
        assert len(trades_manager_inst.get_trade_history()) == 0
        symbol = "BTC/USD"
        new_order = SellLimitOrder(trader_inst)
        new_order.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT], symbol, 90, 4, 90)
        new_trade = Trade(exchange_inst, new_order)

        # add new trade
        trades_manager_inst.add_new_trade_in_history(new_trade)
        assert len(trades_manager_inst.get_trade_history()) == 1
        assert trades_manager_inst.get_trade_history()[0] == new_trade

        # doesn't add an existing trade
        trades_manager_inst.add_new_trade_in_history(new_trade)
        assert len(trades_manager_inst.get_trade_history()) == 1
        assert trades_manager_inst.get_trade_history()[0] == new_trade

        # select trade
        assert trades_manager_inst.select_trade_history() == trades_manager_inst.get_trade_history()

        new_order2 = SellLimitOrder(trader_inst)
        new_order2.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT], "BTC/EUR", 90, 4, 90)
        new_trade2 = Trade(exchange_inst, new_order2)

        trades_manager_inst.add_new_trade_in_history(new_trade2)
        assert len(trades_manager_inst.get_trade_history()) == 2
        assert trades_manager_inst.select_trade_history(symbol) == [new_trade]
Ejemplo n.º 2
0
    async def test_reset_portfolio_available(self):
        _, portfolio_inst, _, trader_inst, sub_portfolio_inst = await self.init_default(
        )

        # Test buy order
        limit_sell = SellLimitOrder(trader_inst)
        limit_sell.new(TraderOrderType.SELL_LIMIT, "BTC/USD", 90, 4, 90)

        sub_portfolio_inst.update_portfolio_available(limit_sell, True)
        sub_portfolio_inst.reset_portfolio_available()
        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
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
    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
Ejemplo n.º 8
0
    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)

        fill_limit_or_stop_order(stop_loss, 59, 61)

        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
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
    def test_reset_portfolio_available(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)

        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

        self.stop(trader_inst)
Ejemplo n.º 11
0
    def test_reset_portfolio_available(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)

        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

        self.stop(trader_inst)
Ejemplo n.º 12
0
    async def test_get_total_paid_fees(self):
        _, exchange_inst, trader_inst, trades_manager_inst = await self.init_default(
        )
        self.stop(trader_inst)
        symbol = "BTC/USD"
        new_order = SellLimitOrder(trader_inst)
        new_order.new(TraderOrderType.SELL_LIMIT, symbol, 90, 4, 90)
        new_order.fee = {
            FeePropertyColumns.COST.value: 100,
            FeePropertyColumns.CURRENCY.value: "BTC"
        }
        new_trade = Trade(exchange_inst, new_order)
        trades_manager_inst.add_new_trade_in_history(new_trade)

        assert trades_manager_inst.get_total_paid_fees() == {"BTC": 100}

        new_order2 = SellLimitOrder(trader_inst)
        new_order2.new(TraderOrderType.SELL_LIMIT, symbol, 90, 4, 90)
        new_order2.fee = {
            FeePropertyColumns.COST.value: 200,
            FeePropertyColumns.CURRENCY.value: "PLOP"
        }
        new_trade2 = Trade(exchange_inst, new_order2)
        trades_manager_inst.add_new_trade_in_history(new_trade2)

        assert trades_manager_inst.get_total_paid_fees() == {
            "BTC": 100,
            "PLOP": 200
        }

        new_order3 = SellLimitOrder(trader_inst)
        new_order3.new(TraderOrderType.SELL_LIMIT, symbol, 90, 4, 90)
        new_order3.fee = {
            FeePropertyColumns.COST.value: 0.01111,
            FeePropertyColumns.CURRENCY.value: "PLOP"
        }
        new_trade3 = Trade(exchange_inst, new_order3)
        trades_manager_inst.add_new_trade_in_history(new_trade3)

        assert trades_manager_inst.get_total_paid_fees() == {
            "BTC": 100,
            "PLOP": 200.01111
        }
Ejemplo n.º 13
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)
        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)
Ejemplo n.º 14
0
    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
Ejemplo n.º 15
0
    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
Ejemplo n.º 16
0
    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)
Ejemplo n.º 17
0
    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
Ejemplo n.º 18
0
    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
Ejemplo n.º 19
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
Ejemplo n.º 20
0
    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
Ejemplo n.º 21
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)
        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)