pmorissette/bt

Changing RunPeriod doesn't do anything

aelkholy opened this issue · 3 comments

So I have the following code:

strategy = bt.Strategy("strategy", [
                            bt.algos.RunDaily(),
                            bt.algos.SelectWhere(indicator_df),
                            bt.algos.WeighTarget(reset_to_datetime(weights_df)),
                            bt.algos.Rebalance()
                        ])
default_t = bt.Backtest(strategy, reset_to_datetime(stocks_df), initial_capital=initial_capital)
default_res = bt.run(default_t)

Stocks_df, indicator_df and weights_df all have daily data in them. Changing the line from bt.algos.RunDaily() to bt.algos.RunYearly gives me the same results when I was expecting the rebalancing frequency to change.

I tried working around this by passing in a set of dates corresponding to the time period I want to rebalance on

strategy = bt.Strategy("strategy", [
                                    bt.algos.RunOnDate(dates),
                                    bt.algos.SelectWhere(indicator_df),
                                    bt.algos.WeighTarget(reset_to_datetime(weights_df)),
                                    bt.algos.Rebalance()
                                ])

This throws an error here because it's checking if the date is in a list containing one element, which is a pd.datetimeindex I believe.

[Did not mean to close this adding my comment, so reopening]

So I have the following code:

strategy = bt.Strategy("strategy", [
                            bt.algos.RunDaily(),
                            bt.algos.SelectWhere(indicator_df),
                            bt.algos.WeighTarget(reset_to_datetime(weights_df)),
                            bt.algos.Rebalance()
                        ])
default_t = bt.Backtest(strategy, reset_to_datetime(stocks_df), initial_capital=initial_capital)
default_res = bt.run(default_t)

Stocks_df, indicator_df and weights_df all have daily data in them. Changing the line from bt.algos.RunDaily() to bt.algos.RunYearly gives me the same results when I was expecting the rebalancing frequency to change.

I tried working around this by passing in a set of dates corresponding to the time period I want to rebalance on

strategy = bt.Strategy("strategy", [
                                    bt.algos.RunOnDate(dates),
                                    bt.algos.SelectWhere(indicator_df),
                                    bt.algos.WeighTarget(reset_to_datetime(weights_df)),
                                    bt.algos.Rebalance()
                                ])

This throws an error here because it's checking if the date is in a list containing one element, which is a pd.datetimeindex I believe.

Originally posted by @aelkholy in #387 (comment)

These algos work for me, so I think something else is tripping up your code. I can't see anything specific that is wrong - but not sure what reset_to_datetime is doing - so it's likely something related to your data. Strip your strategy down to the minimum and add complexity back in to see when it breaks. For example, start with fixed weights, just varying rebalance frequency such as:

strat_weights = {'S&P 500': 0.6, 'Treasury': 0.4}
strat = bt.Strategy('Balanced Monthly', [
                      bt.algos.RunMonthly(),
                      bt.algos.SelectThese(list(['S&P 500', 'Treasury'])),
                      bt.algos.WeighSpecified(**strat_weights),
                      bt.algos.Rebalance()])

Replace RunMonthly with RunDaily etc and check results.display()