Skip to content

Commit

Permalink
Introduce an optional lag parameter in SetStat
Browse files Browse the repository at this point in the history
  • Loading branch information
lnzeta authored and timkpaine committed Jun 24, 2024
1 parent 4638d21 commit 3ad49fd
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions bt/algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,11 +889,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
Expand All @@ -906,7 +909,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


Expand Down

0 comments on commit 3ad49fd

Please sign in to comment.