Beispiel #1
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 #2
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 #3
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 #4
0
    async def test_close_filled_order(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="NANO/USDT",
                          current_price=70,
                          quantity=10,
                          price=70)

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

        await trader_inst.create_order(market_buy)
        await trader_inst.create_order(stop_loss)
        await trader_inst.create_order(limit_sell)

        assert len(trades_manager.trades) == 1

        await limit_sell.on_fill(force_fill=True)

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

        # added filled orders as filled trades
        assert len(trades_manager.trades) == 2
        assert trades_manager.get_trade(
            market_buy.order_id).status is OrderStatus.FILLED
        assert trades_manager.get_trade(
            limit_sell.order_id).status is OrderStatus.FILLED

        await self.stop(exchange_manager)
Beispiel #5
0
    async def test_close_filled_order_with_linked_orders(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="NANO/USDT",
                          current_price=70,
                          quantity=10,
                          price=70)

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

        stop_loss.linked_orders = [limit_sell]
        limit_sell.linked_orders = [stop_loss]

        await trader_inst.create_order(market_buy)
        await trader_inst.create_order(stop_loss)
        await trader_inst.create_order(limit_sell)

        assert len(trades_manager.trades) == 1

        limit_sell.filled_price = limit_sell.origin_price
        limit_sell.status = OrderStatus.FILLED
        await limit_sell.on_fill(force_fill=True)

        assert market_buy not in orders_manager.get_open_orders()
        assert stop_loss not in orders_manager.get_open_orders()
        assert limit_sell not in orders_manager.get_open_orders()

        # added filled orders as filled trades
        assert len(trades_manager.trades) == 3
        assert trades_manager.get_trade(
            market_buy.order_id).status is OrderStatus.FILLED
        assert trades_manager.get_trade(
            limit_sell.order_id).status is OrderStatus.FILLED
        assert trades_manager.get_trade(
            stop_loss.order_id).status is OrderStatus.CANCELED
        with pytest.raises(KeyError):
            trades_manager.get_trade(f"{market_buy.order_id}1")

        await self.stop(exchange_manager)
Beispiel #6
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 #7
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 #8
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 #9
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 #10
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 #11
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