async def test_set_mark_price(prices_manager): prices_manager.set_mark_price(10) assert prices_manager.mark_price == 10 if not os.getenv('CYTHON_IGNORE'): assert prices_manager.mark_price_set_time == prices_manager.exchange_manager.exchange.get_exchange_current_time( ) assert prices_manager.valid_price_received_event.is_set()
async def test_set_mark_price_for_rt_source_only(prices_manager): assert prices_manager.set_mark_price( 10, MarkPriceSources.RECENT_TRADE_AVERAGE.value) is False assert prices_manager.mark_price == 0 # drop first RT mark price assert prices_manager.set_mark_price( 25, MarkPriceSources.RECENT_TRADE_AVERAGE.value) is True assert prices_manager.mark_price == 25 check_event_is_set(prices_manager)
async def test_set_mark_price_for_exchange_source(prices_manager): prices_manager.set_mark_price(10, MarkPriceSources.EXCHANGE_MARK_PRICE.value) assert prices_manager.mark_price == 10 check_event_is_set(prices_manager) prices_manager.set_mark_price(25, MarkPriceSources.RECENT_TRADE_AVERAGE.value) assert prices_manager.mark_price == 10 # Drop first RT update prices_manager.set_mark_price(30, MarkPriceSources.RECENT_TRADE_AVERAGE.value) assert prices_manager.mark_price == 30 prices_manager.set_mark_price(20, MarkPriceSources.TICKER_CLOSE_PRICE.value) assert prices_manager.mark_price == 30 prices_manager.set_mark_price(15, MarkPriceSources.EXCHANGE_MARK_PRICE.value) assert prices_manager.mark_price == 15
async def test_get_mark_price(prices_manager): # without a set price with pytest.raises(asyncio.TimeoutError): await prices_manager.get_mark_price(0.01) assert not prices_manager.valid_price_received_event.is_set() # set price prices_manager.set_mark_price(10) assert await prices_manager.get_mark_price(0.01) == 10 assert prices_manager.valid_price_received_event.is_set() # expired price if not os.getenv('CYTHON_IGNORE'): prices_manager.exchange_manager.backtesting.time_manager.current_timestamp = 66666666 with pytest.raises(asyncio.TimeoutError): await prices_manager.get_mark_price(0.01) assert not prices_manager.valid_price_received_event.is_set() # reset price with this time prices_manager.set_mark_price(10) assert await prices_manager.get_mark_price(0.01) == 10 assert prices_manager.valid_price_received_event.is_set() # current time move within allowed range if not os.getenv('CYTHON_IGNORE'): prices_manager.exchange_manager.backtesting.time_manager.current_timestamp = 1 assert await prices_manager.get_mark_price(0.01) == 10 assert prices_manager.valid_price_received_event.is_set() # new value prices_manager.set_mark_price(42.0000172) assert await prices_manager.get_mark_price(0.01) == 42.0000172 assert prices_manager.valid_price_received_event.is_set()
async def test_set_mark_price_for_ticker_with_rt_source_outdated( prices_manager): prices_manager.set_mark_price(5, MarkPriceSources.RECENT_TRADE_AVERAGE.value) assert prices_manager.mark_price == 0 # Drop first RT update assert prices_manager.set_mark_price( 10, MarkPriceSources.RECENT_TRADE_AVERAGE.value) is True assert prices_manager.mark_price == 10 check_event_is_set(prices_manager) assert prices_manager.set_mark_price( 40, MarkPriceSources.TICKER_CLOSE_PRICE.value) is False assert prices_manager.mark_price == 10 # should not be updated because RECENT_TRADE_AVERAGE has not expired check_event_is_set(prices_manager) assert prices_manager.set_mark_price( 20, MarkPriceSources.TICKER_CLOSE_PRICE.value) is False assert prices_manager.mark_price == 10 # should not be updated because RECENT_TRADE_AVERAGE has not expired check_event_is_set(prices_manager) # force rt source expiration if not os.getenv('CYTHON_IGNORE'): prices_manager.mark_price_from_sources[ MarkPriceSources.RECENT_TRADE_AVERAGE.value] = None assert prices_manager.set_mark_price( 40, MarkPriceSources.TICKER_CLOSE_PRICE.value) is True assert prices_manager.mark_price == 40 # should be updated because RECENT_TRADE_AVERAGE has expired check_event_is_set(prices_manager) assert prices_manager.set_mark_price( 8, MarkPriceSources.RECENT_TRADE_AVERAGE.value) is False assert prices_manager.mark_price == 40 # Drop first RT update after reset assert prices_manager.mark_price_from_sources[ MarkPriceSources.RECENT_TRADE_AVERAGE.value][1] == 0 assert prices_manager.set_mark_price( 15, MarkPriceSources.RECENT_TRADE_AVERAGE.value) is True assert prices_manager.mark_price == 15 assert prices_manager.mark_price_from_sources[ MarkPriceSources.RECENT_TRADE_AVERAGE.value][1] != 0 check_event_is_set(prices_manager) assert prices_manager.set_mark_price( 20, MarkPriceSources.TICKER_CLOSE_PRICE.value) is False assert prices_manager.mark_price == 15 # should not be updated because RECENT_TRADE_AVERAGE has not expired check_event_is_set(prices_manager) # force rt source expiration prices_manager.mark_price_from_sources[ MarkPriceSources.RECENT_TRADE_AVERAGE.value] = ( prices_manager.mark_price_from_sources[ MarkPriceSources.RECENT_TRADE_AVERAGE.value][0], prices_manager.mark_price_from_sources[ MarkPriceSources.RECENT_TRADE_AVERAGE.value][1] - PricesManager.MARK_PRICE_VALIDITY) prices_manager.set_mark_price( 40, MarkPriceSources.TICKER_CLOSE_PRICE.value) assert prices_manager.mark_price == 40 # should be updated because RECENT_TRADE_AVERAGE has expired check_event_is_set(prices_manager)
async def test_set_mark_price_for_ticker_source_only(prices_manager): assert prices_manager.set_mark_price( 10, MarkPriceSources.TICKER_CLOSE_PRICE.value) is True assert prices_manager.mark_price == 10 check_event_is_set(prices_manager)
async def test_set_mark_price(prices_manager): prices_manager.set_mark_price(10, MarkPriceSources.EXCHANGE_MARK_PRICE.value) assert prices_manager.mark_price == 10 check_event_is_set(prices_manager)