async def test_trailing_stop_with_new_price(trailing_stop_order):
    trailing_stop_order, order_price, price_events_manager = await initialize_trailing_stop(
        trailing_stop_order)
    await trailing_stop_order.set_trailing_percent(2)
    new_trailing_price = random_price(min_value=order_price + 1)

    # set mark price
    set_mark_price(trailing_stop_order, new_trailing_price)

    # move trailing price
    price_events_manager.handle_recent_trades([
        random_recent_trade(price=new_trailing_price,
                            timestamp=trailing_stop_order.timestamp)
    ])
    await wait_asyncio_next_cycle()
    assert not trailing_stop_order.is_filled()

    # test fill stop loss with new order price reference
    price_events_manager.handle_recent_trades([
        random_recent_trade(price=get_price_percent(
            new_trailing_price, trailing_stop_order.trailing_percent),
                            timestamp=trailing_stop_order.timestamp)
    ])
    await wait_asyncio_next_cycle()
    assert trailing_stop_order.is_filled()
Beispiel #2
0
async def test_trailing_stop_with_new_price_inversed(trailing_stop_order):
    trailing_stop_order, order_price, price_events_manager = await initialize_trailing_stop(
        trailing_stop_order, side=TradeOrderSide.BUY)
    await trailing_stop_order.set_trailing_percent(5)
    new_trailing_price = random_price(min_value=order_price * 0.99,
                                      max_value=order_price * 1.01)

    # set mark price
    set_mark_price(trailing_stop_order, new_trailing_price)

    # move trailing price
    price_events_manager.handle_recent_trades([
        random_recent_trade(price=new_trailing_price,
                            timestamp=trailing_stop_order.timestamp)
    ])
    await wait_asyncio_next_cycle()
    assert not trailing_stop_order.is_filled()

    # test fill stop loss with new order price reference
    price_events_manager.handle_recent_trades([
        random_recent_trade(price=get_price_percent(
            new_trailing_price,
            trailing_stop_order.trailing_percent,
            selling_side=False),
                            timestamp=trailing_stop_order.timestamp)
    ])
    await wait_asyncio_next_cycle()
    assert trailing_stop_order.is_filled()
async def test_trailing_stop_trigger(trailing_stop_order):
    trailing_stop_order, order_price, price_events_manager = await initialize_trailing_stop(
        trailing_stop_order)
    await trailing_stop_order.set_trailing_percent(10)
    max_trailing_hit_price = get_price_percent(
        order_price, trailing_stop_order.trailing_percent)

    # set mark price
    set_mark_price(trailing_stop_order, order_price)

    price_events_manager.handle_recent_trades([
        random_recent_trade(price=random_price(
            min_value=max_trailing_hit_price, max_value=order_price - 1),
                            timestamp=trailing_stop_order.timestamp)
    ])
    await wait_asyncio_next_cycle()
    assert not trailing_stop_order.is_filled()
    price_events_manager.handle_recent_trades([
        random_recent_trade(price=order_price,
                            timestamp=trailing_stop_order.timestamp - 1)
    ])
    await wait_asyncio_next_cycle()
    assert not trailing_stop_order.is_filled()
    price_events_manager.handle_recent_trades([
        random_recent_trade(price=order_price,
                            timestamp=trailing_stop_order.timestamp)
    ])

    await wait_asyncio_next_cycle()
    assert not trailing_stop_order.is_filled()
    price_events_manager.handle_recent_trades([
        random_recent_trade(price=max_trailing_hit_price,
                            timestamp=trailing_stop_order.timestamp)
    ])
    await wait_asyncio_next_cycle()
    assert trailing_stop_order.is_filled()