6.2.2Backtesting Frameworks

Learn about clean historical data sourcing

3,099 words14 min readdifficulty · medium

What makes historical data "dirty"?

Historical stock data becomes corrupted through four main channels:

1. Survivorship Bias

Why this happens:

  • Free data providers often maintain only currently-listed tickers
  • Each year ~5-8% of stocks delist (bankruptcy, acquisitions, compliance failures)
  • A backtest from 2000-2020 missing dotcom busts or 2008 failures will never see those losses

Mathematical impact:

Let's derive the bias magnitude. Suppose:

  • True population: N0N_0 stocks at time t=0t=0
  • Survival rate: ss per year
  • Your dataset has only survivors: Ns=N0sTN_s = N_0 \cdot s^T after TT years

If average return is μ\mu for survivors and μdead=0.8\mu_{dead} = -0.8 (80% loss) for delistings:

True return=sμ+(1s)μdead\text{True return} = s \cdot \mu + (1-s) \cdot \mu_{dead}

Biased return=μ\text{Biased return} = \mu

Bias=μ[sμ+(1s)μdead]=(1s)(μμdead)\text{Bias} = \mu - [s \cdot \mu + (1-s) \cdot \mu_{dead}] = (1-s)(\mu - \mu_{dead})

With s=0.94s=0.94 (6% annual delisting), μ=0.12\mu=0.12, μdead=0.8\mu_{dead}=-0.8:

Bias=0.06×(0.12(0.8))=0.06×0.92=5.5% per year\text{Bias} = 0.06 \times (0.12 - (-0.8)) = 0.06 \times 0.92 = 5.5\% \text{ per year}

Your backtest is inflated by 5.5 percentage points annually.

2. Look-Ahead Bias

How it sneaks in:

Price data is usually provided adjusted for splits/dividends. On2020-01-15, you see:

  • Adjusted price: $100
  • This reflects a 2-for-1 split on 2020-06-01

The trap: If your strategy on 2020-01-15 uses "100"incalculationsexpectingittomean"shareprice,"youreimplicitlyusingpostsplitinformation.Theunadjustedpricewas100" in calculations expecting it to mean "share price," you're implicitly using post-split information. The **unadjusted** price was 200.

Correct usage: Always use adjusted prices for returns, unadjusted for absolute price levels (like "buy stocks under $10").

3. Corporate Action Errors

Types and adjustments:

| Action | Effect | Adjustment | |-----|------------| | Split n:1n:1 | Shares multiply by nn | Divide all prior prices by nn | | Dividend DD | Cash paid out | Subtract DD from prior prices | | Merger | Ticker changes | Link histories with correct ratio | | Spinoff | New entity created | Split parent value proportionally |

Derivation of dividend adjustment:

Day before dividend: stock worth PP, includes DD cash value. P=Pbusiness+DP = P_{\text{business}} + D

Day after (ex-dividend): cash is gone, business remains: Pex=PbusinessPDP_{\text{ex}} = P_{\text{business}} P - D

The price "drops" by DD, but shareholder value is preserved (stock + cash = constant). Adjusted series must show no drop:

Padj, before=Praw, before×PexP=Praw, before×PDPP_{\text{adj, before}} = P_{\text{raw, before}} \times \frac{P_{\text{ex}}}{P}= P_{\text{raw, before}} \times \frac{P-D}{P}

4. Point-in-Time Data

Why financial data changes retroactively:

  • Restatements: Company discovers accounting error, revises 3 years of earnings
  • Reclassifications: Sector assignments change (is Tesla automotive or tech?)
  • Filing delays: 10-K due 90 days after fiscal year-end, but many file late

What you need:

For a backtest on 2015-03-15:

  • Use financials as filed by2015-03-15, not the "final restated" version from 2018
  • Use sector classifications as of 2015-03-15
  • If 10-K wasn't filed yet, you don't have that data

Without this: You're trading with future knowledge (the restatement), inflating performance.


Clean data requirements checklist


Data source tiers

Tier 1: Professional (Expensive but Clean)

  • Bloomberg Terminal: $24,000/year, point-in-time fundamentals, delisting returns
  • Refinitiv (Reuters): Similar pricing, global coverage
  • FactSet: Institutional-grade, strong corporate actions

Pros: Survivorship-free, accurate splits/dividends, point-in-time toggle Cons: Cost prohibitive for individuals

Tier 2: Specialized Backtest Vendors

  • Norgate Data: $300-800/year, US equities with delisted stocks, accurate adjustments
  • CSI Data: Similar, includes futures
  • Sharadar (Quandl/Nasdaq Data Link): $50-200/month, good fundamentals, delisting coverage

Pros: Built for backtesting, documented methodology, affordable Cons: Limited asset classes, US-focused mostly

Tier 3: Free/API (Requires Heavy Cleaning)

  • Yahoo Finance: Free, survivorship bias, spoty delisting data, occasional errors
  • Alpha Vantage: Free tier limited, better than Yahoo
  • IEX Cloud: Cheap API, current data focus

Pros: Accessible, good for learning Cons: Manual survivorship correction needed, verify every corporate action, no point-in-time fundamentals


Data validation techniques


Recall Explain to a 12-year-old

Imagine you're testing whether your new skateboard trick works by watching videos of only the kids who succeeded and made it to the X Games. All the videos of kids who tried and failed aren't in your collection because they gave up skating. If you learn the trick based only on success videos, you'll think it's way easier than it really is—you never saw the broken arms and quit attempts.

Clean historical data is like getting videos of everyone who tried the trick, including the failures. In stock backtesting:

  • Dirty data = only companies that survived (the X Games winners)
  • Clean data = all companies including the bankruptcies (everyone who tried)

Also, some videos are edited after the fact—maybe they cut out the part where the kid fell first. That's look-ahead bias: the video shows info that wasn't obvious when it was filmed. Clean data means seeing everything exactly as it happened, with no edited-in future knowledge.



Connections

  • 6.2.01-Define-backtesting-vs-paper-trading – Why clean data matters more in backtesting
  • 6.2.03-Understand-transaction-cost-modeling – Clean data must include realistic fills
  • 6.1.02-Understand-compounding-vs-averagereturns – Survivorship bias inflates compound returns
  • 5.3.01-Learn-about-the-efficient-market-hypothesis – Point-in-time data tests true EMH
  • 6.4.01-Identify-overfitting-in-strategy-parameters – Dirty data creates false parameter optima

#flashcards/stock-market

What is survivorship bias in historical data? :: When your dataset contains only securities that survived to the present, excluding delisted/bankrupt companies, creating an upward-biased performance record because failures are invisible.

Why does survivorship bias inflate backtest returns?
Because it removes all the losing trades from bankruptcies/delistings (typically -80% to -100% losses), leaving only the winners. With ~6% annual delistings and losers at -80%, this adds ~5.5% fake annual return.
What is look-ahead bias?
Using information a backtest that wasn't available at the historical decision time—like restated financials, future index memberships, or adjusted prices used incorrectly for price-level rules.
How should you use adjusted vs. unadjusted prices?
Use adjusted prices for return calculations (they account for splits/dividends), but unadjusted prices for absolute price rules (like "buy stocks under $10") to avoid look-ahead bias from future corporate actions.
What is point-in-time data?
Historical data that reflects information exactly as it was known on a specific date, not as restated later—critical for fundamentals which are often revised years after initial filing.
Why does missing data forward-fill create bias?
A missing price might mean the stock was halted or illiquid—you couldn't have traded there. Forward-filling assumes you exited at a price that didn't exist, especially dangerous during crises when quotes vanish for days.
What four checks validate OHLC data integrity?
1) Low≤ Open and Close ≤ High, 2) High ≥ Low, 3) Price changes must have volume > 0, 4) No duplicate timestamps. Violations indicate corrupted data.
How do you adjust prices backward for a dividend?
Multiply all prior prices by (P - D)/P where P is the price before dividend and D is the dividend amount, preserving the proportional return while removing the cash drop.
What's the difference between Tier 1 and Tier 2 data sources?
Tier 1 (Bloomberg, Refinitiv) are 20k+/yearprofessionalterminalswithfullpointintimeanddelistingcoverage.Tier2(Norgate,Sharadar)are20k+/year professional terminals with full point-in-time and delisting coverage. Tier 2 (Norgate, Sharadar) are 300-800/year backtest-focused vendors with survivorship-free data but less asset class coverage.
Why must corporate action adjustments be applied in reverse chronological order?
Because each adjustment factor depends on the post-action price. Starting from today and working backward, you multiply by each factor (splits, dividends) to reconstruct what historical prices "should be" in today's share structure.

Concept Map

caused by

caused by

caused by

excludes

produces

uses future info

miscalculates splits/dividends

misleads

fixes

fixes

enables

enables

prevents

Clean Historical Data

Dirty Data

Survivorship Bias

Look-Ahead Bias

Corporate Action Errors

Point-in-Time Membership

Delisting Returns

Inflated Backtest Returns

Real-World Losses

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Clean historical data sourcing matlab hai ke apke backtest ke results reliable aur realistic ho. Jab aap purana stock data download karte ho free sources se jaise Yahoo Finance, toh usme bohot sari hidden problems hotiain. Sabse badi problem hai survivorship bias—iska matlab ke aapke dataset mein sirf woh companies hain jo aj bhi zinda hain, joh bankrupt hui ya delist hui woh gayab ho gayi. Toh aapka backtest dikhata hai sirf winners ka data, aur har saal ke 6-8% losers ko ignore kar deta hai. Result? Aapko lagta hai strategy 40% return de rahi hai, lekin reality mein woh 10% bhi nahi degi kyunki aapne sare failures ko count hi nahi kiya.

Dusri badi problem hai corporate actions jaise stock splits aur dividends. Jab Apple ne 7-for-1 split kiya 2014 mein, toh agar aapka data properly adjusted nahi hai, toh chart pe dikhega ki pricek din mein 85% crash ho gayi—lekin woh crash thi hi nahi, bas shares multiply ho gaye. Adjusted prices use karne ka sahi tarika hai returns calculate karne ke liye, lekin agar aap "buy stocks under $10" jaisa rule laga rahe ho toh unadjusted price use karna padega, warna future ka information already apke backtest mein aa jayega (look-ahead bias).

Professional traders Bloomberg ya Refinitiv jaise expensive sources use karte hain (24,000/year)kyunkiwohpointintimedatadetehainmatlabexactlywohinformationjousdinavailablethi,badmeinrevisekigayinahi.IndividualtraderskeliyeNorgateDatayaSharadarjaiseaffordableoptionshain(24,000/year) kyunki woh point-in-time data dete hain—matlab exactly woh information jo us din available thi, bad mein revise ki gayi nahi. Individual traders ke liye Norgate Data ya Sharadar jaise affordable options hain (300-800/year) jo survivorship-free data aur accurate corporate actions provide karte hain.Agar aap free data use kar rahe ho, toh manually validate karna padega—delisted stocks ki list alag se download karo, OHLC integrity check karo (Low <= Open, Close <= High), aur har bade corporate action ko verify karo company ke investor relations website se.

Bottom line: Dirty data pe backtest karna matlab apne ap ko fool karna. Thoda paisa lagao quality data pe, ya fir free data ko thoroughly clean karo, warna apki strategy real trading mein fail hogi aur aapko samajh nahi ayega kyun.

Test yourself — Backtesting Frameworks

Connections