async def test_sell_limit_order_trigger(sell_limit_order):
    order_price = decimal_random_price(min_value=2)
    sell_limit_order.update(
        price=order_price,
        quantity=decimal_random_quantity(max_value=DEFAULT_SYMBOL_QUANTITY),
        symbol=DEFAULT_ORDER_SYMBOL,
        order_type=TraderOrderType.SELL_LIMIT,
    )
    sell_limit_order.exchange_manager.is_backtesting = True  # force update_order_status
    await sell_limit_order.initialize()
    sell_limit_order.exchange_manager.exchange_personal_data.orders_manager.upsert_order_instance(
        sell_limit_order)
    price_events_manager = sell_limit_order.exchange_manager.exchange_symbols_data.get_exchange_symbol_data(
        DEFAULT_ORDER_SYMBOL).price_events_manager
    price_events_manager.handle_recent_trades([
        decimal_random_recent_trade(price=decimal_random_price(
            max_value=order_price - trading_constants.ONE),
                                    timestamp=sell_limit_order.timestamp)
    ])
    await wait_asyncio_next_cycle()
    assert not sell_limit_order.is_filled()
    price_events_manager.handle_recent_trades([
        decimal_random_recent_trade(price=order_price,
                                    timestamp=sell_limit_order.timestamp - 1)
    ])
    await wait_asyncio_next_cycle()
    assert not sell_limit_order.is_filled()
    price_events_manager.handle_recent_trades([
        decimal_random_recent_trade(price=order_price,
                                    timestamp=sell_limit_order.timestamp)
    ])

    await wait_asyncio_next_cycle()
    assert sell_limit_order.is_filled()
示例#2
0
async def test_take_profit_limit_order_trigger(take_profit_limit_order):
    order_price = decimal_random_price(min_value=2)
    take_profit_limit_order.limit_price = order_price + decimal.Decimal(10)
    take_profit_limit_order.update(
        price=order_price,
        quantity=decimal_random_quantity(
            max_value=decimal.Decimal(DEFAULT_SYMBOL_QUANTITY / 10)),
        symbol=DEFAULT_ORDER_SYMBOL,
        order_type=TraderOrderType.TAKE_PROFIT_LIMIT,
    )
    take_profit_limit_order.exchange_manager.is_backtesting = True  # force update_order_status
    await take_profit_limit_order.initialize()
    take_profit_limit_order.exchange_manager.exchange_personal_data.orders_manager.upsert_order_instance(
        take_profit_limit_order)
    price_events_manager = take_profit_limit_order.exchange_manager.exchange_symbols_data.get_exchange_symbol_data(
        DEFAULT_ORDER_SYMBOL).price_events_manager
    price_events_manager.handle_recent_trades([
        decimal_random_recent_trade(
            price=decimal_random_price(max_value=order_price -
                                       trading_constants.ONE),
            timestamp=take_profit_limit_order.timestamp)
    ])
    await wait_asyncio_next_cycle()
    assert not take_profit_limit_order.is_filled()
    price_events_manager.handle_recent_trades([
        decimal_random_recent_trade(
            price=order_price, timestamp=take_profit_limit_order.timestamp - 1)
    ])
    await wait_asyncio_next_cycle()
    assert not take_profit_limit_order.is_filled()
    price_events_manager.handle_recent_trades([
        decimal_random_recent_trade(
            price=order_price, timestamp=take_profit_limit_order.timestamp)
    ])

    # wait for 2 cycles as secondary orders are created
    await wait_asyncio_next_cycle()
    await wait_asyncio_next_cycle()
    assert take_profit_limit_order.is_filled()
async def test_handle_recent_trades(price_events_manager):
    random_price_1 = decimal_random_price(min_value=decimal.Decimal(2))
    random_timestamp_1 = random_timestamp(min_value=2, max_value=1000)
    price_event_1 = price_events_manager.add_event(random_price_1,
                                                   random_timestamp_1, True)
    with patch.object(price_event_1, 'set', new=Mock()) as price_event_1_set:
        price_events_manager.handle_recent_trades([])
        with pytest.raises(AssertionError):
            price_event_1_set.assert_called_once()
        price_events_manager.handle_recent_trades([
            decimal_random_recent_trade(
                price=decimal_random_price(max_value=random_price_1 -
                                           trading_constants.ONE),
                timestamp=random_timestamp(max_value=random_timestamp_1 - 1)),
            decimal_random_recent_trade(
                price=decimal_random_price(max_value=random_price_1 -
                                           trading_constants.ONE),
                timestamp=random_timestamp(max_value=random_timestamp_1 - 1)),
            decimal_random_recent_trade(
                price=decimal_random_price(max_value=random_price_1 -
                                           trading_constants.ONE),
                timestamp=random_timestamp(max_value=random_timestamp_1 - 1))
        ])
        with pytest.raises(AssertionError):
            price_event_1_set.assert_called_once()
        price_events_manager.handle_recent_trades([
            decimal_random_recent_trade(price=decimal_random_price(
                max_value=random_price_1 - trading_constants.ONE)),
            decimal_random_recent_trade(price=random_price_1,
                                        timestamp=random_timestamp_1),
            decimal_random_recent_trade(price=decimal_random_price(
                max_value=random_price_1 - trading_constants.ONE)),
            decimal_random_recent_trade(price=decimal_random_price(
                max_value=random_price_1 - trading_constants.ONE))
        ])
        price_event_1_set.assert_called_once()