diff --git a/bt/algos.py b/bt/algos.py index 8b6e787..47343de 100644 --- a/bt/algos.py +++ b/bt/algos.py @@ -913,11 +913,14 @@ class SetStat(Algo): * stat (str|DataFrame): A dataframe of the same dimension as target.universe If a string is passed, frame is accessed using target.get_data This is the preferred way of using the algo. + * lag (DateOffset): Lag interval. The stat used today is the one calculated + at today - lag Sets: * stat """ - def __init__(self, stat): + def __init__(self, stat, lag=pd.DateOffset(days=0)): + self.lag = lag if isinstance(stat, pd.DataFrame): self.stat_name = None self.stat = stat @@ -930,7 +933,12 @@ def __call__(self, target): stat = self.stat else: stat = target.get_data(self.stat_name) - target.temp["stat"] = stat.loc[target.now] + + t0 = target.now - self.lag + if t0 not in stat.index: + return False + + target.temp["stat"] = stat.loc[t0] return True diff --git a/setup.py b/setup.py index acb8813..d27b85f 100644 --- a/setup.py +++ b/setup.py @@ -32,11 +32,7 @@ def local_file(filename): keywords="python finance quant backtesting strategies algotrading algorithmic trading", url="https://github.com/pmorissette/bt", license="MIT", - install_requires=[ - "ffn>=1.0.0", - "pyprind>=2.11", - "tqdm>=4" - ], + install_requires=["ffn>=1.0.0", "pyprind>=2.11", "tqdm>=4"], extras_require={ "dev": [ "cython>=0.29.25",