-
Notifications
You must be signed in to change notification settings - Fork 431
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
CorporateActions: new algo to model dividends and splits #382
Open
danilogalisteu
wants to merge
5
commits into
pmorissette:master
Choose a base branch
from
danilogalisteu:patch-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Commits on Jul 1, 2024
-
CorporateActions: new algo to model dividends and splits
This is an initial proposal for an algo to model dividends and splits. The intention is to be able to use unadjusted price data and to get historically correct transactions (position sizes and commissions). It should be tested more extensively before adoption. The only way I could find to change positions due to splits on the fly was by changing the `_position` member on each security (otherwise security and portfolio values wouldn't be updated correctly). This requires the algo to be run on every row and so, it needs to be positioned before any algos that skip bars, such as RunMonthly. This and other constraints are described in the last paragraph of the docstring. This algorithm could be improved by someone more familiar with the library internals. The input data is similar to what can be obtained from free sources such as Yahoo Finance. An example is provided below, showing backtests with adjusted prices and unadjusted prices with splits and dividends. The results will not be identical, since adjusted data considers that the dividend inflows are reinvested on the same security at the `ex` date. The simulation using CorporateActions will get the dividend inflows as cash at the `ex` date, but the cash will be reinvested on the whole portfolio only at the next rebalancing event. Finally, this simulation doesn't completely correspond to reality since dividends are paid at a later time and not on the `ex` date. But since data about payment dates is not easy to obtain, this improvement was left for the future. Sample code: ``` import pandas as pd import yahooquery as yq import bt data = yq.Ticker(['GLD', 'TLT', 'VTI']).history(period='max', interval='1d').unstack('symbol') data.index = pd.to_datetime(data.index) divs = data['dividends'] splits = data['splits'].replace(0.0, 1.0) close = data['adjclose'] splits_multiplier = splits.sort_index(ascending=False).cumprod().shift(1).ffill().fillna(1.0).sort_index() unadjclose = data['close'] * splits_multiplier s_adj = bt.Strategy('adj', [ bt.algos.RunMonthly(run_on_end_of_period=True), bt.algos.SelectAll(), bt.algos.WeighEqually(), bt.algos.Rebalance()]) s_div = bt.Strategy('div', [ bt.algos.CorporateActions(divs, splits), bt.algos.RunMonthly(run_on_end_of_period=True), bt.algos.SelectAll(), bt.algos.WeighEqually(), bt.algos.Rebalance()]) b_adj = bt.Backtest(s_adj, close, initial_capital=100000, commissions=lambda quantity, price: 0, integer_positions=False) b_div = bt.Backtest(s_div, unadjclose, initial_capital=100000, commissions=lambda quantity, price: 0, integer_positions=True) r = bt.run(b_adj, b_div) r.display() r.plot() ``` Update algos.py Removed white space on empty lines.
Configuration menu - View commit details
-
Copy full SHA for 60868c6 - Browse repository at this point
Copy the full SHA 60868c6View commit details
Commits on Sep 22, 2024
-
Merge pull request #1 from pmorissette/master
Merge from upstream
Configuration menu - View commit details
-
Copy full SHA for 2af27b5 - Browse repository at this point
Copy the full SHA 2af27b5View commit details -
Configuration menu - View commit details
-
Copy full SHA for 8206424 - Browse repository at this point
Copy the full SHA 8206424View commit details -
Configuration menu - View commit details
-
Copy full SHA for f6e90b0 - Browse repository at this point
Copy the full SHA f6e90b0View commit details
Commits on Sep 23, 2024
-
relaxed divs and splits dataframe format (no need to have all price c…
…olumns); added extra price column on test data to check
Configuration menu - View commit details
-
Copy full SHA for 19741fa - Browse repository at this point
Copy the full SHA 19741faView commit details
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.