bt
bt copied to clipboard
child Strategy runs twice in simple parent child setup
Hi there,
I have tried to use the simple parent child strategy approach with 3 symbols from yahoo using this code. It seems that the child strategy is run twice for every date. Ist this expected behavior?
import bt
import matplotlib.pyplot as plt
import yfinance as yf
dx=yf.download(
tickers = "AMZN AAPL TSLA",
start="2020-01-01",
end="2021-12-31",
interval = "1d",
group_by = 'column',
auto_adjust = True,
prepost = True,
threads = True,
proxy = None
)
df=dx["Close"]
runit=bt.algos.Or([bt.algos.RunQuarterly()])
class RebalanceWrapper(bt.Algo):
def __init__(self,msg):
self.msg=msg
self.delegate=bt.algos.Rebalance()
def __call__(self,target):
print(f"{target.now} running {self.msg}")
return self.delegate(target)
parent = bt.Strategy('parent', [runit,
bt.algos.SelectAll(),
bt.algos.WeighEqually(),
RebalanceWrapper("PARENT BALANCE")],
['AMZN'])
mom_s = bt.Strategy('mom_s', [runit,
bt.algos.SelectAll(),
bt.algos.SelectMomentum(1),
bt.algos.WeighEqually(),
RebalanceWrapper("CHILD BALANCE")],
['AAPL', 'TSLA'], parent = parent)
t = bt.Backtest(parent, df)
r = bt.run(t)
And I realize that for a few dates a strategy is called twice (and not only once as I would expect).
This is what I get:
2020-01-02 00:00:00 running CHILD BALANCE
2020-01-02 00:00:00 running PARENT BALANCE
2020-01-02 00:00:00 running CHILD BALANCE
2020-04-01 00:00:00 running CHILD BALANCE
2020-04-01 00:00:00 running PARENT BALANCE
2020-04-01 00:00:00 running CHILD BALANCE
2020-07-01 00:00:00 running CHILD BALANCE
2020-07-01 00:00:00 running PARENT BALANCE
2020-07-01 00:00:00 running CHILD BALANCE
How come that the child strategy is called again, after the parent strategy has run? Is this the expected behavior? thanks for any help.
I think that is explained here https://github.com/pmorissette/bt/issues/21#issuecomment-78126410 The calculation starts at the bottom of the tree, with the child where all info is available, then moves up to the parents where child is a required input, and then down again as parent data has implications for the child...