Skip to content

Commit

Permalink
Add price_data_delay_tolerance hint
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Jul 20, 2024
1 parent 032514f commit 044b57a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
12 changes: 11 additions & 1 deletion tests/backtest/test_backtest_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import logging
from decimal import Decimal

import pandas as pd
import pytest

from tradeexecutor.backtest.backtest_runner import guess_data_delay_tolerance
from tradeexecutor.state.identifier import AssetIdentifier
from tradeexecutor.state.trade import TradeStatus
from tradeexecutor.strategy.reserve_currency import ReserveCurrency
Expand Down Expand Up @@ -260,7 +262,7 @@ def test_buy_with_fee(
logger: logging.Logger,
state: State,
wallet: SimulatedWallet,
strategy_universe,
strategy_universe,
routing_model: BacktestRoutingModel,
pricing_model: BacktestPricing,
wbnb: AssetIdentifier,
Expand Down Expand Up @@ -341,3 +343,11 @@ def test_buy_sell_backtest_with_fee(
# Check our wallet was credited
assert wallet.get_balance(busd.address) == pytest.approx(Decimal('9990.040840796019796453251579'))
assert wallet.get_balance(wbnb.address) == 0


def test_price_data_tolerance(
strategy_universe,
):
"""Workaround for Uni v3 issues"""
strategy_universe.price_data_delay_tolerance = datetime.timedelta(days=365)
assert guess_data_delay_tolerance(strategy_universe) == pd.Timedelta(days=365)
5 changes: 4 additions & 1 deletion tradeexecutor/backtest/backtest_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,10 @@ def guess_data_delay_tolerance(universe: TradingStrategyUniverse) -> pd.Timedelt
This could work around some data quality issues or early historical data.
"""
if universe.data_universe.time_bucket == TimeBucket.d7:

if universe.price_data_delay_tolerance:
return pd.Timedelta(universe.price_data_delay_tolerance)
elif universe.data_universe.time_bucket == TimeBucket.d7:
data_delay_tolerance = pd.Timedelta("9d")
else:
data_delay_tolerance = pd.Timedelta("2d")
Expand Down
10 changes: 10 additions & 0 deletions tradeexecutor/strategy/trading_strategy_universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ class TradingStrategyUniverse(StrategyExecutionUniverse):
#:
options: UniverseOptions | None = None

#: How much lag we allow in the price feed.
#:
#: This can be set for a very high value when working with open ended universes where the liquidity pools are
#: disappear when liquidity providers leave.
#:
#: TODO: Use carefully - this is mostly a workaround variable and
#: will have a more robust TVL/liquidity solution in the future.
#:
price_data_delay_tolerance: datetime.timedelta | None = None

def __repr__(self):
pair_count = self.data_universe.pairs.get_count()
if pair_count <= 3:
Expand Down

0 comments on commit 044b57a

Please sign in to comment.