-
yfinance data adjustment for split and dividend
-
sometimes you may need an original price,but unfortunately yfinance provide the OHLC prices adjusted for split, users may want to adjust backward for some reason to get the original prices. at this time, it is the piece of code that may help with you.
-
the single function returns a dataframe structure that contains adj. OHLC(i.e. adj. Close adj. High ...) and at the same time it can reverse the already split-adjusted OHLC to the unadjusted, original ones if the 'adjustback_for_split' boolean value is set to True
- 1,import your yfinance data,with
actions = True
import yfinance as yf
msft = yf.download("MSFT",actions = True)
# warning: DON'T use history(),the data is somehow incorrect for adjustment - 2,call the function with
adjustback_for_split = Trueif you want to get the original prices.
msft = adjust_yfinance_stock_price(msft,adjustback_for_split = True)| Open | High | Low | Close | Adj Close | Volume | Dividends | Stock Splits | Split Ratio | Adj. Close | Adj. Volume | Adj. Open | Adj. High | Adj. Low | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Date | ||||||||||||||
| 1986-03-13 | 25.500096 | 29.250144 | 25.500096 | 27.999936 | 0.059946 | 3582600.0 | 0.0 | 0.0 | 1.0 | 0.059946 | 1.031789e+09 | 0.054594 | 0.062623 | 0.054594 |
| 1986-03-14 | 27.999936 | 29.500128 | 27.999936 | 28.999872 | 0.062087 | 1070000.0 | 0.0 | 0.0 | 1.0 | 0.062087 | 3.081600e+08 | 0.059946 | 0.063158 | 0.059946 |
| 1986-03-17 | 28.999872 | 29.750112 | 28.999872 | 29.500128 | 0.063158 | 462400.0 | 0.0 | 0.0 | 1.0 | 0.063158 | 1.331712e+08 | 0.062087 | 0.063694 | 0.062087 |
| 1986-03-18 | 29.500128 | 29.750112 | 28.499904 | 28.749888 | 0.061552 | 235300.0 | 0.0 | 0.0 | 1.0 | 0.061552 | 6.776640e+07 | 0.063158 | 0.063694 | 0.061017 |
| 1986-03-19 | 28.749888 | 28.999872 | 27.999936 | 28.249920 | 0.060482 | 166300.0 | 0.0 | 0.0 | 1.0 | 0.060482 | 4.789440e+07 | 0.061552 | 0.062087 | 0.059946 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2024-09-09 | 407.239990 | 408.649994 | 402.149994 | 405.720001 | 405.720001 | 15295100.0 | 0.0 | 0.0 | 1.0 | 405.720001 | 1.529510e+07 | 407.239990 | 408.649994 | 402.149994 |
| 2024-09-10 | 408.200012 | 416.329987 | 407.700012 | 414.200012 | 414.200012 | 19594300.0 | 0.0 | 0.0 | 1.0 | 414.200012 | 1.959430e+07 | 408.200012 | 416.329987 | 407.700012 |
| 2024-09-11 | 415.500000 | 423.989990 | 409.579987 | 423.040009 | 423.040009 | 19266900.0 | 0.0 | 0.0 | 1.0 | 423.040009 | 1.926690e+07 | 415.500000 | 423.989990 | 409.579987 |
| 2024-09-12 | 423.309998 | 427.369995 | 419.750000 | 427.000000 | 427.000000 | 17418800.0 | 0.0 | 0.0 | 1.0 | 427.000000 | 1.741880e+07 | 423.309998 | 427.369995 | 419.750000 |
| 2024-09-13 | 425.829987 | 431.829987 | 425.459991 | 430.589996 | 430.589996 | 15861900.0 | 0.0 | 0.0 | 1.0 | 430.589996 | 1.586190e+07 | 425.829987 | 431.829987 | 425.459991 |
9704 rows × 14 columns