Beispiel #1
0
async def test_refresh_simulated_trader_portfolio_from_order(backtesting_trader):
    config, exchange_manager, trader = backtesting_trader
    portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager

    if os.getenv('CYTHON_IGNORE'):
        return
    order = BuyLimitOrder(trader)
    await order.initialize()
    with patch.object(portfolio_manager.portfolio, 'update_portfolio_available',
                      new=Mock()) as update_portfolio_available_mock:
        update_portfolio_available_mock.assert_not_called()
        portfolio_manager._refresh_simulated_trader_portfolio_from_order(order)
        update_portfolio_available_mock.assert_called_once()

    price = random_price()
    order.update(
        price=random_price(),
        quantity=random_quantity(max_value=DEFAULT_MARKET_QUANTITY / price),
        symbol="BTC/USDT"
    )
    await order.on_fill(force_fill=True)
    assert order.is_filled()

    with patch.object(portfolio_manager.portfolio, 'update_portfolio_from_filled_order',
                      new=Mock()) as update_portfolio_from_filled_order_mock:
        update_portfolio_from_filled_order_mock.assert_not_called()
        portfolio_manager._refresh_simulated_trader_portfolio_from_order(order)
        update_portfolio_from_filled_order_mock.assert_called_once()
Beispiel #2
0
    async def test_cancel_open_orders_multi_symbol(self):
        config, exchange_manager, trader_inst = await self.init_default()
        orders_manager = exchange_manager.exchange_personal_data.orders_manager
        trades_manager = exchange_manager.exchange_personal_data.trades_manager

        # Test buy order
        market_buy = BuyMarketOrder(trader_inst)
        market_buy.update(order_type=TraderOrderType.BUY_MARKET,
                          symbol="BTC/USDC",
                          current_price=decimal.Decimal("70"),
                          quantity=decimal.Decimal("10"),
                          price=decimal.Decimal("70"))

        # Test buy order
        limit_sell = SellLimitOrder(trader_inst)
        limit_sell.update(order_type=TraderOrderType.SELL_LIMIT,
                          symbol="NANO/USDT",
                          current_price=decimal.Decimal("70"),
                          quantity=decimal.Decimal("10"),
                          price=decimal.Decimal("70"))

        # Test sell order
        market_sell = SellMarketOrder(trader_inst)
        market_sell.update(order_type=TraderOrderType.SELL_MARKET,
                           symbol=self.DEFAULT_SYMBOL,
                           current_price=decimal.Decimal("70"),
                           quantity=decimal.Decimal("10"),
                           price=decimal.Decimal("70"))

        # Test buy order
        limit_buy = BuyLimitOrder(trader_inst)
        limit_buy.update(order_type=TraderOrderType.BUY_LIMIT,
                         symbol=self.DEFAULT_SYMBOL,
                         current_price=decimal.Decimal("70"),
                         quantity=decimal.Decimal("10"),
                         price=decimal.Decimal("70"))

        await trader_inst.create_order(market_buy)
        await trader_inst.create_order(market_sell)
        await trader_inst.create_order(limit_buy)
        await trader_inst.create_order(limit_sell)

        # market orders not in open orders as they are instantly filled
        assert market_buy not in orders_manager.get_open_orders()
        assert market_sell not in orders_manager.get_open_orders()

        assert limit_buy in orders_manager.get_open_orders()
        assert limit_sell in orders_manager.get_open_orders()

        assert len(trades_manager.trades) == 2

        assert await trader_inst.cancel_open_orders(self.DEFAULT_SYMBOL) is True

        assert limit_buy not in orders_manager.get_open_orders()
        assert limit_sell in orders_manager.get_open_orders()

        # added cancelled orders as cancelled trades
        assert len(trades_manager.trades) == 3

        await self.stop(exchange_manager)
Beispiel #3
0
async def test_update_portfolio_with_stop_loss_buy_orders(backtesting_trader):
    config, exchange_manager, trader = backtesting_trader
    portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager

    # Test buy order
    limit_sell = SellLimitOrder(trader)
    limit_sell.update(order_type=TraderOrderType.SELL_LIMIT,
                      symbol="BTC/USDT",
                      current_price=90,
                      quantity=4,
                      price=90)

    # Test buy order
    limit_buy = BuyLimitOrder(trader)
    limit_buy.update(order_type=TraderOrderType.BUY_LIMIT,
                     symbol="BTC/USDT",
                     current_price=50,
                     quantity=4,
                     price=50)

    # Test stop loss order
    stop_loss = StopLossOrder(trader, side=TradeOrderSide.BUY)
    stop_loss.update(order_type=TraderOrderType.STOP_LOSS,
                     symbol="BTC/USDT",
                     current_price=60,
                     quantity=4,
                     price=60)

    portfolio_manager.portfolio.update_portfolio_available(stop_loss, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy, True)

    assert round(
        portfolio_manager.portfolio.get_currency_portfolio(
            "BTC", commons_constants.PORTFOLIO_AVAILABLE), 1) == 6
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 800
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 10
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1000

    # cancel limits
    portfolio_manager.portfolio.update_portfolio_available(limit_buy, False)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell, False)

    await fill_limit_or_stop_order(stop_loss)

    # filling stop loss
    # typical stop loss behavior --> update available before update portfolio
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 14
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 760
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 14
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 760
Beispiel #4
0
    async def test_cancel_open_orders_default_symbol(self):
        config, exchange_manager, trader_inst = await self.init_default()
        orders_manager = exchange_manager.exchange_personal_data.orders_manager
        trades_manager = exchange_manager.exchange_personal_data.trades_manager
        portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager
        initial_portfolio = copy.deepcopy(portfolio_manager.portfolio.portfolio)

        # Test buy order
        limit_buy_1 = BuyLimitOrder(trader_inst)
        limit_buy_1.update(order_type=TraderOrderType.BUY_LIMIT,
                           symbol=self.DEFAULT_SYMBOL,
                           current_price=decimal.Decimal("70"),
                           quantity=decimal.Decimal("10"),
                           price=decimal.Decimal("70"))

        # Test sell order
        limit_sell = SellLimitOrder(trader_inst)
        limit_sell.update(order_type=TraderOrderType.SELL_LIMIT,
                          symbol=self.DEFAULT_SYMBOL,
                          current_price=decimal.Decimal("70"),
                          quantity=decimal.Decimal("10"),
                          price=decimal.Decimal("70"))

        # Test buy order
        limit_buy_2 = BuyLimitOrder(trader_inst)
        limit_buy_2.update(order_type=TraderOrderType.BUY_LIMIT,
                           symbol=self.DEFAULT_SYMBOL,
                           current_price=decimal.Decimal("30"),
                           quantity=decimal.Decimal("10"),
                           price=decimal.Decimal("30"))

        await trader_inst.create_order(limit_buy_1)
        await trader_inst.create_order(limit_sell)
        await trader_inst.create_order(limit_buy_2)

        assert limit_buy_1 in orders_manager.get_open_orders()
        assert limit_sell in orders_manager.get_open_orders()
        assert limit_buy_2 in orders_manager.get_open_orders()

        assert not trades_manager.trades

        assert await trader_inst.cancel_open_orders(self.DEFAULT_SYMBOL) is True

        assert limit_buy_1 not in orders_manager.get_open_orders()
        assert limit_sell not in orders_manager.get_open_orders()
        assert limit_buy_2 not in orders_manager.get_open_orders()

        # added cancelled orders as cancelled trades
        assert len(trades_manager.trades) == 3
        assert all(trade.status is OrderStatus.CANCELED
                   for trade in trades_manager.trades.values())
        assert all(
            portfolio_manager.portfolio.portfolio[currency] == initial_portfolio[currency]
            for currency in portfolio_manager.portfolio.portfolio.keys()
        )

        await self.stop(exchange_manager)
Beispiel #5
0
async def test_update_portfolio(backtesting_trader):
    config, exchange_manager, trader = backtesting_trader
    portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager

    # Test buy order
    limit_buy = BuyLimitOrder(trader)
    limit_buy.update(order_type=TraderOrderType.BUY_LIMIT,
                     symbol="BTC/USDT",
                     current_price=70,
                     quantity=10,
                     price=70)

    # update portfolio with creations
    portfolio_manager.portfolio.update_portfolio_available(limit_buy, True)
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 10
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 300

    await fill_limit_or_stop_order(limit_buy)

    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 20
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 300
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 20
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 300

    # Test buy order
    market_sell = SellMarketOrder(trader)
    market_sell.update(order_type=TraderOrderType.SELL_MARKET,
                       symbol="BTC/USDT",
                       current_price=80,
                       quantity=8,
                       price=80)

    # update portfolio with creations
    portfolio_manager.portfolio.update_portfolio_available(market_sell, True)
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 12
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 300

    await fill_market_order(market_sell)

    # when filling market sell
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 12
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 940
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 12
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 940
Beispiel #6
0
    async def test_cancel_closed_order(self):
        _, exchange_manager, trader_inst = await self.init_default()

        # Test buy order
        limit_buy = BuyLimitOrder(trader_inst)
        limit_buy.update(order_type=TraderOrderType.SELL_LIMIT,
                         symbol=self.DEFAULT_SYMBOL,
                         current_price=decimal.Decimal("70"),
                         quantity=decimal.Decimal("10"),
                         price=decimal.Decimal("70"))

        # 1. cancel order: success
        assert await trader_inst.cancel_order(limit_buy) is True
        # 2. re-cancel order: failure (already cancelled)
        assert await trader_inst.cancel_order(limit_buy) is False

        await self.stop(exchange_manager)
Beispiel #7
0
async def test_update_portfolio_data(backtesting_trader):
    config, exchange_manager, trader = backtesting_trader
    portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager

    if not os.getenv('CYTHON_IGNORE'):
        with pytest.raises(errors.PortfolioNegativeValueError):
            portfolio_manager.portfolio._update_portfolio_data("USDT", -2000)
        with pytest.raises(errors.PortfolioNegativeValueError):
            portfolio_manager.portfolio._update_portfolio_data("BTC", -20)

    with pytest.raises(errors.PortfolioNegativeValueError):
        # Test buy order
        btc_limit_buy2 = BuyLimitOrder(trader)
        btc_limit_buy2.update(order_type=TraderOrderType.BUY_LIMIT,
                              symbol="BTC/USDT",
                              current_price=10,
                              quantity=5000000000,
                              price=10)

        portfolio_manager.portfolio.update_portfolio_available(
            btc_limit_buy2, True)
Beispiel #8
0
    async def test_cancel_limit_order(self):
        _, exchange_manager, trader_inst = await self.init_default()
        orders_manager = exchange_manager.exchange_personal_data.orders_manager

        # Test buy order
        limit_buy = BuyLimitOrder(trader_inst)
        limit_buy.update(order_type=TraderOrderType.BUY_LIMIT,
                         symbol=self.DEFAULT_SYMBOL,
                         current_price=decimal.Decimal("70"),
                         quantity=decimal.Decimal("10"),
                         price=decimal.Decimal("70"))

        assert limit_buy not in orders_manager.get_open_orders()

        assert await trader_inst.create_order(limit_buy)

        assert limit_buy in orders_manager.get_open_orders()

        assert await trader_inst.cancel_order(limit_buy) is True

        assert limit_buy not in orders_manager.get_open_orders()

        await self.stop(exchange_manager)
Beispiel #9
0
    async def test_cancel_all_open_orders_with_currency(self):
        config, exchange_manager, trader_inst = await self.init_default()
        orders_manager = exchange_manager.exchange_personal_data.orders_manager
        trades_manager = exchange_manager.exchange_personal_data.trades_manager

        # Test buy order
        market_buy = BuyMarketOrder(trader_inst)
        market_buy.update(order_type=TraderOrderType.BUY_MARKET,
                          symbol="BTC/USDC",
                          current_price=70,
                          quantity=10,
                          price=70)

        # Test buy order
        limit_sell = SellLimitOrder(trader_inst)
        limit_sell.update(order_type=TraderOrderType.SELL_LIMIT,
                          symbol="XRP/BTC",
                          current_price=70,
                          quantity=10,
                          price=70)

        # Test sell order
        market_sell = SellMarketOrder(trader_inst)
        market_sell.update(order_type=TraderOrderType.SELL_MARKET,
                           symbol=self.DEFAULT_SYMBOL,
                           current_price=70,
                           quantity=10,
                           price=70)

        # Test buy order
        limit_buy = BuyLimitOrder(trader_inst)
        limit_buy.update(order_type=TraderOrderType.BUY_LIMIT,
                         symbol=self.DEFAULT_SYMBOL,
                         current_price=70,
                         quantity=10,
                         price=70)

        # create order notifier to prevent None call
        await trader_inst.create_order(market_buy)
        await trader_inst.create_order(market_sell)
        await trader_inst.create_order(limit_buy)
        await trader_inst.create_order(limit_sell)

        # market orders not in open orders as they are instantly filled
        assert market_buy not in orders_manager.get_open_orders()
        assert market_sell not in orders_manager.get_open_orders()
        assert limit_buy in orders_manager.get_open_orders()
        assert limit_sell in orders_manager.get_open_orders()

        assert len(trades_manager.trades) == 2

        assert await trader_inst.cancel_all_open_orders_with_currency("XYZ") is True

        assert limit_buy in orders_manager.get_open_orders()
        assert limit_sell in orders_manager.get_open_orders()

        assert len(trades_manager.trades) == 2

        assert await trader_inst.cancel_all_open_orders_with_currency("XRP") is True

        assert limit_buy in orders_manager.get_open_orders()
        assert limit_sell not in orders_manager.get_open_orders()

        # added cancelled orders as cancelled trades
        assert len(trades_manager.trades) == 3

        await trader_inst.cancel_all_open_orders_with_currency("USDT")

        assert limit_buy not in orders_manager.get_open_orders()
        assert limit_sell not in orders_manager.get_open_orders()

        # added cancelled orders as cancelled trades
        assert len(trades_manager.trades) == 4

        await trader_inst.cancel_all_open_orders_with_currency("BTC")

        assert market_buy not in orders_manager.get_open_orders()
        assert market_sell not in orders_manager.get_open_orders()
        assert limit_buy not in orders_manager.get_open_orders()
        assert limit_sell not in orders_manager.get_open_orders()

        # added cancelled orders as cancelled trades
        assert len(trades_manager.trades) == 4

        await self.stop(exchange_manager)
Beispiel #10
0
async def test_reset_portfolio_available(backtesting_trader):
    config, exchange_manager, trader = backtesting_trader
    portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager

    # Test buy order
    limit_sell = SellLimitOrder(trader)
    limit_sell.update(order_type=TraderOrderType.SELL_LIMIT,
                      symbol="BTC/USDT",
                      current_price=90,
                      quantity=4,
                      price=90)

    portfolio_manager.portfolio.update_portfolio_available(limit_sell, True)
    portfolio_manager.portfolio.reset_portfolio_available()

    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 10
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 1000
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 10
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1000

    # Test sell order
    limit_sell = SellLimitOrder(trader)
    limit_sell.update(order_type=TraderOrderType.SELL_LIMIT,
                      symbol="BTC/USDT",
                      current_price=90,
                      quantity=4,
                      price=90)

    portfolio_manager.portfolio.update_portfolio_available(limit_sell, True)
    # Test buy order
    limit_buy = BuyLimitOrder(trader)
    limit_buy.update(order_type=TraderOrderType.BUY_LIMIT,
                     symbol="VEN/BTC",
                     current_price=0.5,
                     quantity=4,
                     price=0.5)

    portfolio_manager.portfolio.update_portfolio_available(limit_buy, True)

    # Test buy order
    btc_limit_buy = BuyLimitOrder(trader)
    btc_limit_buy.update(order_type=TraderOrderType.BUY_LIMIT,
                         symbol="BTC/USDT",
                         current_price=10,
                         quantity=50,
                         price=10)

    portfolio_manager.portfolio.update_portfolio_available(btc_limit_buy, True)

    # Test buy order
    btc_limit_buy2 = BuyLimitOrder(trader)
    btc_limit_buy2.update(order_type=TraderOrderType.BUY_LIMIT,
                          symbol="BTC/USDT",
                          current_price=10,
                          quantity=50,
                          price=10)

    portfolio_manager.portfolio.update_portfolio_available(
        btc_limit_buy2, True)

    # reset equivalent of the ven buy order
    portfolio_manager.portfolio.reset_portfolio_available("BTC", 4 * 0.5)

    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 6
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 10
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 0
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1000

    # reset equivalent of the btc buy orders 1 and 2
    portfolio_manager.portfolio.reset_portfolio_available("USDT")

    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 6
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 10
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 1000
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1000
Beispiel #11
0
async def test_update_portfolio_with_multiple_symbols_orders(
        backtesting_trader):
    config, exchange_manager, trader = backtesting_trader
    portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager

    # Test buy order
    market_buy = BuyMarketOrder(trader)
    market_buy.update(order_type=TraderOrderType.BUY_MARKET,
                      symbol="ETH/USDT",
                      current_price=7,
                      quantity=100,
                      price=7)

    # test buy order creation
    portfolio_manager.portfolio.update_portfolio_available(market_buy, True)
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "ETH", commons_constants.PORTFOLIO_AVAILABLE) == 0
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 300
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "ETH", commons_constants.PORTFOLIO_TOTAL) == 0
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1000

    await fill_market_order(market_buy)

    assert portfolio_manager.portfolio.get_currency_portfolio(
        "ETH", commons_constants.PORTFOLIO_AVAILABLE) == 100
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 300
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "ETH", commons_constants.PORTFOLIO_TOTAL) == 100
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 300

    # Test buy order
    market_buy = BuyMarketOrder(trader)
    market_buy.update(order_type=TraderOrderType.BUY_MARKET,
                      symbol="LTC/BTC",
                      current_price=0.0135222,
                      quantity=100,
                      price=0.0135222)

    # test buy order creation
    portfolio_manager.portfolio.update_portfolio_available(market_buy, True)
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "LTC", commons_constants.PORTFOLIO_AVAILABLE) == 0
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 8.647780000000001
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "LTC", commons_constants.PORTFOLIO_TOTAL) == 0
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 10

    await fill_market_order(market_buy)

    assert portfolio_manager.portfolio.get_currency_portfolio(
        "LTC", commons_constants.PORTFOLIO_AVAILABLE) == 100
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 8.647780000000001
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "LTC", commons_constants.PORTFOLIO_TOTAL) == 100
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 8.647780000000001

    # Test buy order
    limit_buy = BuyLimitOrder(trader)
    limit_buy.update(order_type=TraderOrderType.BUY_LIMIT,
                     symbol="XRP/BTC",
                     current_price=0.00012232132312312,
                     quantity=3000.1214545,
                     price=0.00012232132312312)

    # test buy order creation
    portfolio_manager.portfolio.update_portfolio_available(limit_buy, True)
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "XRP", commons_constants.PORTFOLIO_AVAILABLE) == 0
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 8.280801174155501
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "XRP", commons_constants.PORTFOLIO_TOTAL) == 0
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 8.647780000000001

    # cancel
    portfolio_manager.portfolio.update_portfolio_available(limit_buy, False)
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "XRP", commons_constants.PORTFOLIO_AVAILABLE) == 0
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 8.647780000000001
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "XRP", commons_constants.PORTFOLIO_TOTAL) == 0
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 8.647780000000001
Beispiel #12
0
async def test_update_portfolio_with_multiple_filled_orders(
        backtesting_trader):
    config, exchange_manager, trader = backtesting_trader
    portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager

    # Test buy order
    limit_sell = SellLimitOrder(trader)
    limit_sell.update(order_type=TraderOrderType.SELL_LIMIT,
                      symbol="BTC/USDT",
                      current_price=90,
                      quantity=4,
                      price=90)

    # Test buy order
    limit_buy = BuyLimitOrder(trader)
    limit_buy.update(order_type=TraderOrderType.BUY_LIMIT,
                     symbol="BTC/USDT",
                     current_price=60,
                     quantity=2,
                     price=60)

    # Test buy order
    limit_buy_2 = BuyLimitOrder(trader)
    limit_buy_2.update(order_type=TraderOrderType.BUY_LIMIT,
                       symbol="BTC/USDT",
                       current_price=50,
                       quantity=4,
                       price=50)

    # Test buy order
    limit_buy_3 = BuyLimitOrder(trader)
    limit_buy_3.update(order_type=TraderOrderType.BUY_LIMIT,
                       symbol="BTC/USDT",
                       current_price=46,
                       quantity=2,
                       price=46)

    # Test buy order
    limit_buy_4 = BuyLimitOrder(trader)
    limit_buy_4.update(order_type=TraderOrderType.BUY_LIMIT,
                       symbol="BTC/USDT",
                       current_price=41,
                       quantity=1.78,
                       price=41)

    # Test buy order
    limit_buy_5 = BuyLimitOrder(trader)
    limit_buy_5.update(order_type=TraderOrderType.BUY_LIMIT,
                       symbol="BTC/USDT",
                       current_price=0.2122427,
                       quantity=3.72448,
                       price=0.2122427)

    # Test buy order
    limit_buy_6 = BuyLimitOrder(trader)
    limit_buy_6.update(order_type=TraderOrderType.BUY_LIMIT,
                       symbol="BTC/USDT",
                       current_price=430,
                       quantity=1.05,
                       price=430)

    # Test sell order
    limit_sell_2 = SellLimitOrder(trader)
    limit_sell_2.update(order_type=TraderOrderType.SELL_LIMIT,
                        symbol="BTC/USDT",
                        current_price=10,
                        quantity=2,
                        price=10)

    # Test stop loss order
    stop_loss_2 = StopLossOrder(trader)
    stop_loss_2.update(order_type=TraderOrderType.STOP_LOSS,
                       symbol="BTC/USDT",
                       current_price=10,
                       quantity=2,
                       price=10)

    # Test sell order
    limit_sell_3 = SellLimitOrder(trader)
    limit_sell_3.update(order_type=TraderOrderType.SELL_LIMIT,
                        symbol="BTC/USDT",
                        current_price=20,
                        quantity=1,
                        price=20)

    # Test stop loss order
    stop_loss_3 = StopLossOrder(trader)
    stop_loss_3.update(order_type=TraderOrderType.STOP_LOSS,
                       symbol="BTC/USDT",
                       current_price=20,
                       quantity=1,
                       price=20)

    # Test sell order
    limit_sell_4 = SellLimitOrder(trader)
    limit_sell_4.update(order_type=TraderOrderType.SELL_LIMIT,
                        symbol="BTC/USDT",
                        current_price=50,
                        quantity=0.2,
                        price=50)

    # Test stop loss order
    stop_loss_4 = StopLossOrder(trader, side=TradeOrderSide.BUY)
    stop_loss_4.update(order_type=TraderOrderType.STOP_LOSS,
                       symbol="BTC/USDT",
                       current_price=45,
                       quantity=0.2,
                       price=45)

    # Test sell order
    limit_sell_5 = SellLimitOrder(trader)
    limit_sell_5.update(order_type=TraderOrderType.SELL_LIMIT,
                        symbol="BTC/USDT",
                        current_price=11,
                        quantity=0.7,
                        price=11)

    # Test stop loss order
    stop_loss_5 = StopLossOrder(trader)
    stop_loss_5.update(order_type=TraderOrderType.STOP_LOSS,
                       symbol="BTC/USDT",
                       current_price=9,
                       quantity=0.7,
                       price=9)

    portfolio_manager.portfolio.update_portfolio_available(stop_loss_2, True)
    portfolio_manager.portfolio.update_portfolio_available(stop_loss_3, True)
    portfolio_manager.portfolio.update_portfolio_available(stop_loss_4, True)
    portfolio_manager.portfolio.update_portfolio_available(stop_loss_5, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell_2, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell_3, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell_4, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell_5, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy_2, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy_3, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy_4, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy_5, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy_6, True)

    assert round(
        portfolio_manager.portfolio.get_currency_portfolio(
            "BTC", commons_constants.PORTFOLIO_AVAILABLE), 1) == 2.1
    assert round(
        portfolio_manager.portfolio.get_currency_portfolio(
            "USDT", commons_constants.PORTFOLIO_AVAILABLE), 7) == 62.7295063
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 10
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1000

    # cancels
    portfolio_manager.portfolio.update_portfolio_available(stop_loss_3, False)
    portfolio_manager.portfolio.update_portfolio_available(stop_loss_5, False)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell_2, False)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy, False)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy_3, False)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy_5, False)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell_4, False)

    # filling
    await fill_limit_or_stop_order(stop_loss_2)
    await fill_limit_or_stop_order(limit_sell)
    await fill_limit_or_stop_order(limit_sell_3)
    await fill_limit_or_stop_order(limit_buy_2)
    await fill_limit_or_stop_order(limit_sell_5)
    await fill_limit_or_stop_order(stop_loss_4)
    await fill_limit_or_stop_order(limit_buy_4)
    await fill_limit_or_stop_order(limit_buy_5)
    await fill_limit_or_stop_order(limit_buy_6)

    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 13.05448
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 674.22
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 13.05448
    assert round(
        portfolio_manager.portfolio.get_currency_portfolio(
            "USDT", commons_constants.PORTFOLIO_TOTAL), 7) == 673.4295063
Beispiel #13
0
async def test_update_portfolio_with_some_filled_orders(backtesting_trader):
    config, exchange_manager, trader = backtesting_trader
    portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager

    # Test buy order
    limit_sell = SellLimitOrder(trader)
    limit_sell.update(order_type=TraderOrderType.SELL_LIMIT,
                      symbol="BTC/USDT",
                      current_price=90,
                      quantity=4,
                      price=90)

    # Test buy order
    limit_buy = BuyLimitOrder(trader)
    limit_buy.update(order_type=TraderOrderType.BUY_LIMIT,
                     symbol="BTC/USDT",
                     current_price=60,
                     quantity=2,
                     price=60)

    # Test buy order
    limit_buy_2 = BuyLimitOrder(trader)
    limit_buy_2.update(order_type=TraderOrderType.BUY_LIMIT,
                       symbol="BTC/USDT",
                       current_price=50,
                       quantity=4,
                       price=50)

    # Test sell order
    limit_sell_2 = SellLimitOrder(trader)
    limit_sell_2.update(order_type=TraderOrderType.SELL_LIMIT,
                        symbol="BTC/USDT",
                        current_price=10,
                        quantity=2,
                        price=10)

    # Test stop loss order
    stop_loss_2 = StopLossOrder(trader)
    stop_loss_2.update(order_type=TraderOrderType.STOP_LOSS,
                       symbol="BTC/USDT",
                       current_price=10,
                       quantity=2,
                       price=10)

    # Test sell order
    limit_sell_3 = SellLimitOrder(trader)
    limit_sell_3.update(order_type=TraderOrderType.SELL_LIMIT,
                        symbol="BTC/USDT",
                        current_price=20,
                        quantity=1,
                        price=20)

    # Test stop loss order
    stop_loss_3 = StopLossOrder(trader)
    stop_loss_3.update(order_type=TraderOrderType.STOP_LOSS,
                       symbol="BTC/USDT",
                       current_price=20,
                       quantity=1,
                       price=20)

    portfolio_manager.portfolio.update_portfolio_available(stop_loss_2, True)
    portfolio_manager.portfolio.update_portfolio_available(stop_loss_3, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell_2, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell_3, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy_2, True)

    assert round(
        portfolio_manager.portfolio.get_currency_portfolio(
            "BTC", commons_constants.PORTFOLIO_AVAILABLE), 1) == 3
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 680
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 10
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1000

    # Test stop loss order
    stop_loss_4 = StopLossOrder(trader, side=TradeOrderSide.BUY)
    stop_loss_4.update(order_type=TraderOrderType.STOP_LOSS,
                       symbol="BTC/USDT",
                       current_price=20,
                       quantity=4,
                       price=20)

    # Test stop loss order
    stop_loss_5 = StopLossOrder(trader, side=TradeOrderSide.BUY)
    stop_loss_5.update(order_type=TraderOrderType.STOP_LOSS,
                       symbol="BTC/USDT",
                       current_price=200,
                       quantity=4,
                       price=20)
    portfolio_manager.portfolio.update_portfolio_available(stop_loss_5, True)

    # portfolio did not change as stop losses are not affecting available funds
    assert round(
        portfolio_manager.portfolio.get_currency_portfolio(
            "BTC", commons_constants.PORTFOLIO_AVAILABLE), 1) == 3
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 680
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 10
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1000

    # cancels
    portfolio_manager.portfolio.update_portfolio_available(stop_loss_3, False)
    portfolio_manager.portfolio.update_portfolio_available(stop_loss_5, False)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell_2, False)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy, False)

    # filling
    await fill_limit_or_stop_order(stop_loss_2)
    await fill_limit_or_stop_order(limit_sell)
    await fill_limit_or_stop_order(limit_sell_3)
    await fill_limit_or_stop_order(limit_buy_2)

    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 7
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 1200
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 7
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1200

    await fill_limit_or_stop_order(stop_loss_4)
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 11
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 1120
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 11
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1120
Beispiel #14
0
async def test_update_portfolio_with_cancelled_orders(backtesting_trader):
    config, exchange_manager, trader = backtesting_trader
    portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager

    # force fees => shouldn't do anything
    exchange_manager.exchange.config[commons_constants.CONFIG_SIMULATOR][
        commons_constants.CONFIG_SIMULATOR_FEES] = {
            commons_constants.CONFIG_SIMULATOR_FEES_MAKER: 0.05,
            commons_constants.CONFIG_SIMULATOR_FEES_TAKER: 0.1
        }

    # Test buy order
    market_sell = SellMarketOrder(trader)
    market_sell.update(order_type=TraderOrderType.SELL_MARKET,
                       symbol="BTC/USDT",
                       current_price=80,
                       quantity=4.1,
                       price=80)

    # Test sell order
    limit_sell = SellLimitOrder(trader)
    limit_sell.update(order_type=TraderOrderType.SELL_LIMIT,
                      symbol="BTC/USDT",
                      current_price=10,
                      quantity=4.2,
                      price=10)

    # Test stop loss order
    stop_loss = StopLossOrder(trader)
    stop_loss.update(order_type=TraderOrderType.STOP_LOSS,
                     symbol="BTC/USDT",
                     current_price=80,
                     quantity=3.6,
                     price=80)

    portfolio_manager.portfolio.update_portfolio_available(stop_loss, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell, True)

    # Test buy order
    limit_buy = BuyLimitOrder(trader)
    limit_buy.update(order_type=TraderOrderType.BUY_LIMIT,
                     symbol="BTC/USDT",
                     current_price=50,
                     quantity=4,
                     price=50)

    portfolio_manager.portfolio.update_portfolio_available(limit_buy, True)
    portfolio_manager.portfolio.update_portfolio_available(market_sell, True)

    assert round(
        portfolio_manager.portfolio.get_currency_portfolio(
            "BTC", commons_constants.PORTFOLIO_AVAILABLE), 1) == 1.7
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 800
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 10
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1000

    # with no filled orders
    portfolio_manager.portfolio.update_portfolio_available(stop_loss, False)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell, False)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy, False)
    portfolio_manager.portfolio.update_portfolio_available(market_sell, False)

    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 10
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 1000
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 10
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1000
Beispiel #15
0
async def test_update_portfolio_with_filled_orders(backtesting_trader):
    config, exchange_manager, trader = backtesting_trader
    portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager

    # force fees => should have consequences
    exchange_manager.exchange.config[commons_constants.CONFIG_SIMULATOR][
        commons_constants.CONFIG_SIMULATOR_FEES] = {
            commons_constants.CONFIG_SIMULATOR_FEES_MAKER: 0.05,
            commons_constants.CONFIG_SIMULATOR_FEES_TAKER: 0.1
        }

    # Test buy order
    market_sell = SellMarketOrder(trader)
    market_sell.update(order_type=TraderOrderType.SELL_MARKET,
                       symbol="BTC/USDT",
                       current_price=70,
                       quantity=3,
                       price=70)

    # Test sell order
    limit_sell = SellLimitOrder(trader)
    limit_sell.update(order_type=TraderOrderType.SELL_LIMIT,
                      symbol="BTC/USDT",
                      current_price=100,
                      quantity=4.2,
                      price=100)

    # Test stop loss order
    stop_loss = StopLossOrder(trader)
    stop_loss.update(order_type=TraderOrderType.STOP_LOSS,
                     symbol="BTC/USDT",
                     current_price=80,
                     quantity=4.2,
                     price=80)

    limit_sell.add_linked_order(stop_loss)
    stop_loss.add_linked_order(limit_sell)

    # Test buy order
    limit_buy = BuyLimitOrder(trader)
    limit_buy.update(order_type=TraderOrderType.BUY_LIMIT,
                     symbol="BTC/USDT",
                     current_price=50,
                     quantity=2,
                     price=50)

    # update portfolio with creations
    portfolio_manager.portfolio.update_portfolio_available(market_sell, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell, True)
    portfolio_manager.portfolio.update_portfolio_available(stop_loss, True)
    portfolio_manager.portfolio.update_portfolio_available(limit_buy, True)

    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 2.8
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 900

    # when cancelling limit sell, market sell and stop orders
    portfolio_manager.portfolio.update_portfolio_available(stop_loss, False)
    portfolio_manager.portfolio.update_portfolio_available(limit_sell, False)

    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 7
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 900

    # when filling limit buy
    await fill_limit_or_stop_order(limit_buy)
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 8.999
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 900
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 11.999
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 900

    # when filling market sell
    await fill_market_order(market_sell)
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_AVAILABLE) == 8.999
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_AVAILABLE) == 1109.79
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "BTC", commons_constants.PORTFOLIO_TOTAL) == 8.999
    assert portfolio_manager.portfolio.get_currency_portfolio(
        "USDT", commons_constants.PORTFOLIO_TOTAL) == 1109.79