Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing RunPeriod doesn't do anything #387

Open
aelkholy opened this issue Sep 30, 2022 · 3 comments
Open

Changing RunPeriod doesn't do anything #387

aelkholy opened this issue Sep 30, 2022 · 3 comments

Comments

@aelkholy
Copy link

aelkholy commented Sep 30, 2022

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.

@aelkholy
Copy link
Author

aelkholy commented Oct 5, 2022

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.

@aelkholy aelkholy closed this as completed Oct 5, 2022
@aelkholy
Copy link
Author

aelkholy commented Oct 5, 2022

[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)

@MDDietz1
Copy link
Contributor

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants