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 43983f6 commit 7040ba6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 10 additions & 2 deletions bt/algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down
6 changes: 1 addition & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 7040ba6

Please sign in to comment.