Exemple #1
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
Exemple #2
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)
Exemple #3
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)
Exemple #4
0
    async def test_cancel_stop_order(self):
        _, exchange_manager, trader_inst = await self.init_default()
        orders_manager = exchange_manager.exchange_personal_data.orders_manager

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

        assert stop_order not in orders_manager.get_open_orders()

        assert await trader_inst.create_order(stop_order)

        assert stop_order in orders_manager.get_open_orders()

        assert await trader_inst.cancel_order(stop_order) is True

        assert stop_order not in orders_manager.get_open_orders()

        await self.stop(exchange_manager)
Exemple #5
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
Exemple #6
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
Exemple #7
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
Exemple #8
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