edge case: Surge API incorrect stock split adjustments for AAPL
At Surge, we pride ourselves on delivering accurate, real-time, and historical financial data to help you track your portfolio with confidence. Our unified platform for stock and crypto assets relies heavily on the integrity of this data. However, as any engineer working with complex data knows, edge cases are inevitable. This article delves into a specific challenge we recently faced and resolved: incorrect stock split adjustments for Apple (AAPL) historical data served via the Surge API.
Understanding Stock Splits and Their Impact
Before we dive into the specific issue, let's quickly recap stock splits and why they're crucial for historical data accuracy. A stock split is a corporate action where a company increases the number of its outstanding shares by dividing existing shares. For example, a 2-for-1 split means that for every share you own, you now have two, and the price per share is halved.
The critical implication for data providers like Surge is that historical prices must be adjusted to reflect these splits. If you look at AAPL's price chart from before its 2014 7-for-1 split, and then compare it to the price after, without adjustment, the pre-split price would appear astronomically high relative to current prices. This makes performance analysis, charting, and comparison utterly meaningless.
For accurate historical data, we need to adjust: * Share Price: Divide pre-split prices by the split ratio. * Volume: Multiply pre-split volumes by the split ratio. * Shares Outstanding: Multiply pre-split shares outstanding by the split ratio.
These adjustments ensure that the total value of your holding remains consistent across the split date, and that historical performance calculations are correct.
The AAPL Anomaly: A Deep Dive
Apple (AAPL) is not just a tech giant; it's also a poster child for stock splits, having undergone several significant ones throughout its history. This makes it a particularly interesting and challenging case for historical data providers.
AAPL's notable splits include: * 2-for-1 on June 21, 2000 * 2-for-1 on February 28, 2005 * 7-for-1 on June 9, 2014 * 4-for-1 on August 31, 2020
The problem we identified was that the Surge API was, for a period, returning incorrect adjusted historical prices for AAPL. Specifically, prices from before the 2014 split were not being adjusted by the cumulative factor of both the 2014 and 2020 splits. This led to historical prices appearing much higher than they should have been when viewed in the context of today's share count.
The root cause was traced back to an oversight in our internal data processing pipeline. While our system correctly identified and applied individual split events, the cumulative adjustment factor for symbols with multiple, successive splits was not always being propagated correctly across the entire historical dataset. In AAPL's case, the 2014 7-for-1 split was sometimes being missed or incompletely applied when calculating the fully adjusted historical prices, especially for data points preceding that split, when also accounting for the later 2020 split.
Demonstrating the Problem
To illustrate the issue, let's look at a hypothetical (but representative) curl command to our API before the fix was deployed. We'll query AAPL's closing price on June 6, 2014 – the last trading day before its 7-for-1 split.
curl "https://api.surge.com/v1/historical/AAPL?start_date=2014-06-06&end_date=2014-06-06&adjusted=true&api_key=YOUR_API_KEY"
Prior to our fix, the (simplified) JSON response might have looked something like this:
{
"symbol": "AAPL",
"data": [
{
"date": "2014-06-06",
"open": 161.27,
"high": 161.70,
"low": 160.00,
"close": 161.39, <-- INCORRECT ADJUSTED CLOSE
"volume": 94705600
}
],
"message": "Historical data retrieved successfully."
}
The closing price on June 6, 2014, was actually $645.57 (unadjusted). For a fully adjusted price, considering both the 2014 (7:1) and 2020 (4:1) splits, the calculation should be: $645.57 / 7 / 4 = $23.056.
The 161.39 in the erroneous response above is precisely $645.57 / 4, indicating that only the 2020 4-for-1 split was applied, and the earlier 2014 7-