On 31 August 2020 the price of AAPL fell by 74% in one day and nobody lost a cent: Apple had split its shares four for one. A pipeline that does not know about the split reports a crash. This topic builds the factors that repair it, for splits and for cash dividends.
Why a price can fall without a loss
Where we are: the last two topics gave every source one shape, one name per instrument and one calendar. Every table now has a row for every open day. But a present row can still be wrong for comparison. Prices are only comparable across days when a share means the same thing on both. A corporate action breaks that. By the end of this topic you will be able to build the factor of a split, undo and redo an adjustment of prices and volume, find the cliffs that a split leaves in printed prices, and adjust closes for cash dividends.
Splits and dividends are the two actions this topic handles, but they are not the only ones. A spin-off gives owners shares of a new company and lowers the price of the old one; a merger replaces one stock with another; a rights issue lets owners buy new shares at a discount. Each changes prices in its own way and needs its own rule. The method in this topic, a list of dated actions and a factor built from it, is the same for all of them, which is why the list is kept as data.
A stock split exchanges each share for several. In a 2-for-1 split an owner of one share, worth 100 dollars, owns two shares worth 50 dollars each. The owner is exactly as rich, but the price printed on the next day is half of the day before. Volume doubles for the same reason. If a pipeline stores the prices as they were printed, every split is a fake crash, and every statistic built on returns (volatility, drawdown, a backtest) is wrong. The fix is to multiply the old prices by a number so that the series is continuous. That number is the adjustment factor.
The number by which a price (or a volume) on a given date is multiplied, or divided, so that it means the same thing as a price today. It is 1 for dates after the last corporate action and a product of ratios for earlier dates.
Here is the problem in four numbers. The prices below are made up: a 2-for-1 split happens between the second and the third day. The first line shows the returns of the printed prices, the second the returns after the old prices are halved.
raw = pd.Series([100.0, 102.0, 51.0, 52.0], index=pd.bdate_range("2021-03-01", periods=4))print(raw.pct_change().round(4).tolist())adjusted = raw * [0.5, 0.5, 1.0, 1.0]1print(adjusted.pct_change().round(4).tolist())
[nan, 0.02, -0.5, 0.0196] [nan, 0.02, 0.0, 0.0196]
- 1Only the days before the split are scaled; the days after it already show the new price.
The printed series has a return of -50% on the day of the split, which never happened. After halving the first two prices the third return is 0.0, which is the true move (the stock did not change in value at all). The first return is nan because the first day has no day before it. The rest of the topic is about finding the right factor for every date, and about applying it in both directions.
Split factors
A split has a date and a ratio. The ratio is the number of new shares per old share: 2 for a 2-for-1 split, 0.1 for a 1-for-10 reverse split in which ten shares become one. The date used in this lab is the last day that was quoted at the old price, so every price on that day or earlier is multiplied by the ratio. With several splits the factors multiply: a price before both splits is multiplied by both ratios. The method has three steps.
- 1Start every date at 1Make one factor per date, all equal to 1, as a numpy array.
- 2Apply each split to the dates it coversFor every split, multiply the factors of the dates on or before the split date by its ratio, and leave the others as they are.
np.wherechooses between two arrays date by date. - 3Read the factorsThe factor of a date now includes all the splits that came after it. Dates after the last split keep the factor 1.
Worked example 1. *Problem:* six days, 1 to 8 March 2021, with a 2-for-1 split dated 2 March and a 3-for-1 split dated 4 March. Find the factor of each day. *Plan:* start at 1, then apply the first split to the dates up to 2 March and the second to the dates up to 4 March. *Steps:* loop over the splits with np.where.
days = pd.to_datetime(["2021-03-01", "2021-03-02", "2021-03-03", "2021-03-04", "2021-03-05", "2021-03-08"])splits = [("2021-03-02", 2.0), ("2021-03-04", 3.0)]factors = np.ones(len(days))for when, ratio in splits: factors = np.where(days <= pd.Timestamp(when), factors * ratio, factors)1print(factors.tolist())
[6.0, 6.0, 3.0, 3.0, 1.0, 1.0]
- 1<= keeps the split day itself at the old price.
*Check:* the first two days are before both splits and carry 2 x 3 = 6, the next two are before only the second split and carry 3, and the last two carry 1. *What to notice:* the simple method of using < instead of <= moves the boundary by one day. Here is the same loop with <, then a reverse split with the ratio 0.1.
strict = np.ones(len(pd.bdate_range("2021-03-01", "2021-03-08")))days = pd.bdate_range("2021-03-01", "2021-03-08")for when, ratio in [("2021-03-02", 2.0), ("2021-03-04", 3.0)]: strict = np.where(days < pd.Timestamp(when), strict * ratio, strict)print(strict.tolist())reverse = np.where(days <= pd.Timestamp("2021-03-02"), 0.1, 1.0)print(reverse.tolist())
[6.0, 3.0, 3.0, 1.0, 1.0, 1.0] [0.1, 0.1, 1.0, 1.0, 1.0, 1.0]
*Check:* with < the second day carries 3 instead of 6, so the price of the split day would be adjusted as if the first split had already happened. The reverse split gives 0.1 on the early days: the old price is divided by ten, because ten old shares became one new one. *What to notice:* a factor below 1 is as valid as a factor above 1, and the boundary day is the first thing to test.
A "4-for-1 split" has the ratio 4 in this lab, and the old prices are multiplied by 4 to show what the market printed. A vendor that gives the ratio the other way round (0.25) turns every adjustment upside down. Read the vendor's description, and check with one known split: the cliff must disappear.
Undoing and redoing an adjustment
Vendors usually send prices that are already adjusted, and a pipeline sometimes needs the prices that were printed on the day, for example to match an old trade. Going from adjusted prices to printed prices is called undoing the adjustment, and the opposite is applying it. Both use the same factors. Prices change in one direction and volume in the other, because the same dollars changed hands in more shares when each share was cheaper.
Worked example 2. *Problem:* adjusted prices 10.0, 10.2, 10.1, 10.3 with volumes 3000, 2400, 2100, 1500 and the factors 2, 2, 1, 1. Find the printed prices and volumes, and check that the traded value is unchanged. *Plan:* multiply the prices by the factors, divide the volumes by them and round to whole shares. *Steps:* multiply, divide, round, compare price times volume.
prices = np.array([10.0, 10.2, 10.1, 10.3])volume = np.array([3000, 2400, 2100, 1500])factors = np.array([2.0, 2.0, 1.0, 1.0])printed = prices * factorsprinted_volume = np.round(volume / factors).astype("int64")1print(printed.tolist(), printed_volume.tolist())print((prices * volume).round(2).tolist())print((printed * printed_volume).round(2).tolist())print(np.round(np.array([500.5, 501.5, 666.67])).tolist())2
[20.0, 20.4, 10.1, 10.3] [1500, 1200, 2100, 1500] [30000.0, 24480.0, 21210.0, 15450.0] [30000.0, 24480.0, 21210.0, 15450.0] [500.0, 502.0, 667.0]
- 1Volume goes the other way, and np.round gives whole shares.
- 2A half goes to the even number: 500.5 becomes 500 and 501.5 becomes 502.
*Check:* the first two prices doubled to 20.0 and 20.4 while the volumes halved to 1500 and 1200, and the traded value (price times volume) is the same in both lines of numbers, which is the best test that an adjustment is right: a split must not change the money that changed hands. *What to notice:* rounding a volume is an approximation. Dividing 2001 by 2 gives 1000.5, and the last line shows how np.round treats halves. If you round half up by hand, you will differ from the tests on every odd volume.
Serious stores keep the printed prices as the truth and the list of corporate actions next to them, and apply the factors when the data is read. A new split then needs one new row in the list, not a rewrite of years of prices.
Finding the cliffs
How do you know a series has an unadjusted split? Look for a cliff: a one-day move far larger than any normal day. A threshold of 30% is a reasonable alarm for stocks, because ordinary days rarely move that far. The test also tells you the ratio. If the close fell to half, the previous close divided by the new close is about 2, and a ratio that is a whole number or a simple fraction suggests a split, while a ratio such as 2.04 suggests a real fall in price.
Worked example 3. *Problem:* the closes are 40, 41, 20.5, 20.8, 10.2. Which days moved by more than 30%, and what ratio does each look like? *Plan:* compute the returns with pct_change, keep the days where the absolute return is above 0.3, then divide the previous close by the new close for those days. *Steps:* returns, filter, ratio.
closes = pd.Series([40.0, 41.0, 20.5, 20.8, 10.2])returns = closes.pct_change()print(returns.round(4).tolist())big = returns[returns.abs() > 0.3]1print(big.round(4).to_dict())print((closes.shift(1) / closes)[big.index].round(2).tolist())2
[nan, 0.025, -0.5, 0.0146, -0.5096]
{2: -0.5, 4: -0.5096}
[2.0, 2.04]- 1abs() so that a rise counts as well as a fall.
- 2shift(1) is the previous day's close.
*Check:* the third and the fifth days (positions 2 and 4) have moves of -50% and -51%, and the ratios are 2.0 and 2.04. *What to notice:* the simple method, looking only at falls, would miss a reverse split, which shows as a rise. The ratio 2.0 is a split candidate and 2.04 is not: the filter finds suspects, and a person or a list of corporate actions decides.
Cash dividends
A dividend is a payment per share. On the ex-date the price drops by about the dividend, which is also not a loss for the owner who received it in cash. To compare prices across the ex-date, earlier prices are scaled down by a ratio: one minus the dividend divided by the close of the day before the ex-date. Every earlier price is multiplied by it, and several dividends multiply. The price that results is called a total-return price, because it lets a return include the dividend.
Worked example 4. *Problem:* closes 50.0, 50.4, 50.1, 49.6; the fourth day is the ex-date of a dividend of 0.5. Adjust the first three closes. *Plan:* the ratio uses the close of the day before the ex-date (the third day, 50.1), and only the days before the ex-date are scaled. *Steps:* ratio, scale the earlier days, leave the rest.
closes = [50.0, 50.4, 50.1, 49.6]ex_row = 3ratio = 1 - 0.5 / closes[ex_row - 1]1print(round(ratio, 6))print([round(c * ratio, 4) if i < ex_row else c for i, c in enumerate(closes)])2
0.99002 [49.501, 49.897, 49.6, 49.6]
- 1The close before the ex-date, not the close on it.
- 2Only the days before the ex-date are scaled.
*Check:* the ratio is 0.99002, a fall of 0.998% for a dividend of 1% of the price, and the third and fourth adjusted closes are both 49.6: the dividend no longer shows as a fall. *What to notice:* using the close on the ex-date itself (49.6) would give a ratio of 0.98992 and a small error on every earlier day, which is exactly the kind of error that nobody notices for years.
Many vendors already send adjusted prices. Adjusting data that is already adjusted counts every split twice, and the price series is wrong in a way that looks smooth. Find out what the vendor did before you apply a factor, and record in the table which adjustments it carries.
Putting it together
The real pipeline does these steps in order. Take the table as the vendor sent it, find out whether it is adjusted, use the list of corporate actions to rebuild or apply the factors, then check the result: no cliffs where a split is listed, and a traded value that did not change. The lab runs this on AAPL, whose file contains two splits and twelve dividends, with the printed prices rebuilt from the adjusted ones so that you can see the cliffs appear and disappear.
Worked example 5. *Problem:* printed closes 100, 102, 51, 52, 51, 50.5. A 2-for-1 split is dated on the second day, and a dividend of 0.5 has its ex-date on the sixth day. Find the fully adjusted closes and compare the returns before and after. *Plan:* the split factor of a day up to the split date is 0.5 when we adjust (we divide by the ratio); the dividend ratio uses the close of the day before the ex-date, 51, and scales the first five days; the two factors multiply. *Steps:* split factors, dividend ratio, product, returns.
printed = np.array([100.0, 102.0, 51.0, 52.0, 51.0, 50.5])split = np.array([0.5, 0.5, 1.0, 1.0, 1.0, 1.0])ratio = 1 - 0.5 / printed[4]1dividend = np.where(np.arange(6) < 5, ratio, 1.0)adjusted = printed * split * dividend2print(adjusted.round(4).tolist())print((printed[1:] / printed[:-1] - 1).round(4).tolist())print((adjusted[1:] / adjusted[:-1] - 1).round(4).tolist())
[49.5098, 50.5, 50.5, 51.4902, 50.5, 50.5] [0.02, -0.5, 0.0196, -0.0192, -0.0098] [0.02, 0.0, 0.0196, -0.0192, 0.0]
- 1The close before the ex-date is the fifth value, index 4.
- 2The two factors multiply: they are independent.
*Check:* the printed returns contain the false -50% on the split day and a small dip on the ex-date, while the adjusted returns have neither: the split day is 0.0, and the ex-date return is 0.0 too, because the dip was exactly the dividend. *What to notice:* the order of the two factors does not matter, because both are plain multiplications. What matters is that each uses the right reference: the split by its date, the dividend by the close before the ex-date.
One more habit makes adjustments safe: always test continuity after you apply them. For a split, the return on the split day must be normal. For all adjustments together, no return may be larger than the cliff threshold unless you can name the news that caused it. This test is cheap, it needs no second source, and it catches the two classic mistakes, a ratio the wrong way round and an adjustment applied twice.
- FunctionQuestion it answers
- split_factors(dates, splits)By how much was each date's price changed by splits?ratios multiply
- unadjust(frame, splits)What did the market print?volume goes the other way
- adjust(raw, splits)What is comparable today?the inverse of unadjust
- jumps(frame, threshold)Where are the cliffs?size, up or down
- dividend_factor(frame, dividends)How much did dividends scale each close?uses the close before the ex-date
- study(bars)Do the cliffs vanish on real AAPL?uses all of the above
What you will build
The lab follows the lecture. Task 1 builds the split factors and task 2 undoes the adjustment of a table to get the printed prices. Task 3 applies the adjustment, the inverse. Task 4 finds the cliffs, task 5 builds the dividend factors, and task 6 runs it all on real AAPL prices and corporate actions.
Next topic: you can now make prices comparable across days. The next topic, Tiers of market data: L1 to L3, goes the other way. It looks inside a day: the quotes and orders that the bars summarise, and how the levels of data differ in what they contain.
Lab · what's graded
6 tasks · 100 pts- The split factors15 pts
- Undo the splits15 pts
- Apply the splits15 pts
- Find the cliffs10 pts
- The dividend factors20 pts
- The study on the SPY table25 pts