# Backtesting Data Python: Clean Historical Data for Strategies

Source: https://finzdata.com/guides/backtesting-data-python

Get survivorship-free backtesting data python workflows with point-in-time fundamentals, macro vintages and adjustments via FinzData API and client.

Backtesting Data Python

# Backtesting Data Python

Clean backtesting data python pipelines start with survivorship-free prices, point-in-time fundamentals and macro vintages, and correct adjustments. FinzData provides a yfinance-compatible Python client and REST API that returns pandas DataFrames with 16,000+ US stocks back to January 2000 including delisted names, plus COT, SEC fundamentals, 13F, short interest, and 18,000+ macro series with revisions.

[Get a free API key](https://api.finzdata.com/) [See pricing](https://finzdata.com/shop) [Download free samples](https://finzdata.com/samples)

## Why data quality breaks backtests

Backtests fail when data contains survivorship bias, look-ahead bias, or incorrect adjustments. Survivorship bias drops delisted names, inflating returns. Look-ahead bias uses information not available at the time. Adjustments applied inconsistently distort signals. A reliable backtesting data python setup requires point-in-time snapshots for prices, fundamentals and macro, and explicit control over splits and dividends.

## Survivorship-free prices with yfinance style client

FinzData mirrors yfinance so existing code changes little. The client returns pandas DataFrames and supports Ticker.history and download patterns.

Coverage includes 16,000+ US stocks going back more than 26 years with history starting January 2000 including delisted names, 231 ETFs, daily, hourly, 5-minute and 1-minute bars. Prices are stored as traded with splits and dividends published and applied on read via adjust='none'|'splits'|'all'.

Explore live charts at [/explore](https://finzdata.com/explore) and see free 10-row samples at [/samples](https://finzdata.com/samples).

## CODE: Prices, adjustments and delisted universe

```
import finzdata as yf
import pandas as pd

# yfinance compatible history
ticker = yf.Ticker("AAPL")
hist = ticker.history(start="2000-01-01", interval="1d")
print(hist.head())

# Client with explicit adjustment control
c = yf.Client()
prices = c.prices(sym="AAPL", adjust="splits")
# adjust='none' for traded prices, 'splits' for split-adjusted, 'all' for splits+dividends

# Survivorship-free universe example
universe = c.prices(sym="AAPL", adjust="none")
print(universe.shape)
```

## Point-in-time fundamentals and macro vintages

SEC XBRL company facts are provided point-in-time by filing date. Daily point-in-time valuation metrics P/E, P/B, P/S, EV/EBITDA, market cap, dividend yield are available from 2009. Earnings release dates from 8-K Item 2.02 are available since 2004.

18,000+ US macro series are provided with every revision, FRED/ALFRED style vintages. Query a series as of a specific publication date with as_of to prevent look-ahead bias. CFTC Commitments of Traders data includes legacy, disaggregated, financial futures and supplemental reports for 950+ futures markets since 1986 weekly, with COT index 0-100 computed.

Backtesting data python workflows benefit from explicit as_of queries for both fundamentals and macro.

## CODE: Fundamentals, valuation, macro and COT

```
import finzdata as yf
c = yf.Client()

# Point-in-time valuation
val = c.valuation(ticker="AAPL", start="2020-01-01")
print(val.head())

# Point-in-time concept by filing date
pe = c.fundamentals(ticker="AAPL", concept="PriceEarningsRatio", as_of="2023-06-30")
print(pe)

# Financial statements point-in-time
income = c.statements(ticker="AAPL", statement="income", freq="quarterly")
print(income.head())

# Earnings release dates
er = c.earnings_dates(ticker="AAPL")
print(er.head())

# Macro series with vintage as of date
cpi = c.macro(id="CPIAUCSL", as_of="2024-01-15")
print(cpi)

# COT report
cot = c.cot(report="legacy", market="E-mini S&P 500")
print(cot.head())
```

## Institutional flows and short interest

Backtests benefit from 13F institutional holdings, FINRA short interest since 2017, and SEC Form 4 insider trades. These datasets are available via the client and REST API and return pandas DataFrames for easy joining to price series.

See datasets at [/datasets](https://finzdata.com/datasets) and pricing at [/pricing](https://finzdata.com/shop).

Consistent backtesting data python pipelines join price, fundamental, macro and flow data on a common point-in-time index.

## Frequently asked questions

### How do I avoid survivorship bias in backtesting data python?

Use a survivorship-free universe that includes delisted names. FinzData provides 16,000+ US stocks back to January 2000 including delisted names. Query the full universe and filter by point-in-time criteria rather than using a current list of tickers.

### What adjustment should I use for backtests?

Store prices as traded and apply adjustments explicitly on read. FinzData stores prices as traded with splits and dividends published and applied on read. Use adjust='none' for traded, adjust='splits' for split-adjusted, adjust='all' for splits and dividends, and keep the choice consistent across the backtest.

### How do I prevent look-ahead bias with fundamentals?

Use point-in-time fundamentals by filing date and as_of queries. FinzData SEC XBRL facts are point-in-time by filing date and daily point-in-time valuation is available from 2009. Query with as_of to reflect only information available on that date.

### Can I get macro data with revisions for backtesting?

Yes. 18,000+ US macro series are provided with every revision in FRED/ALFRED style vintages. Use c.macro(id, as_of=...) to retrieve the vintage known on a specific date.

### Is the API yfinance compatible?

Yes. FinzData client mirrors yfinance with Ticker, history, download and returns pandas DataFrames. REST API at https://api.finzdata.com returns JSON or CSV, with Parquet and bulk files on paid plans.

### What is the free tier?

Free tier includes last 12 months of every dataset, 1,000 API calls per day, no card. Paid look-back bundles start at $195 for the first 5 years, $95 for each further 5 years, plus $19.95 per month for daily updates. Institutional is $975 once plus $59.95 per month.

## Related

- [Survivorship bias free stock data](https://finzdata.com/guides/survivorship-bias-free-stock-data)

- [Point-in-time fundamentals backtesting](https://finzdata.com/guides/point-in-time-fundamentals-backtesting)

- [Macro data vintages python](https://finzdata.com/guides/macro-data-vintages-python)

- [COT data python](https://finzdata.com/guides/cot-data-python)

- [Yfinance alternative](https://finzdata.com/guides/yfinance-alternative)

- [Historical stock data API](https://finzdata.com/guides/historical-stock-data-api)

[Get a free API key](https://api.finzdata.com/) [See pricing](https://finzdata.com/shop) [Download free samples](https://finzdata.com/samples)
🌐EnglishDeutschEspañolFrançaisItalianoPortuguês日本語简体中文한국어Polskiעברית
