SQLite plus pandas gives repeatable queries across research notebooks

Published: 2026-01-24

URL: https://pyquantnews.substack.com/p/build-an-automated-quant-database


Each week, I send out one Python tutorial to help you get started with algorithmic trading, market data analysis, and quant finance. Upgrade to a paid plan to access the code notebooks. Subscribe now In today’s post, you’ll build a small Python script that backfills history and appends new daily bars automatically.

What a price database is A price database is a single place where you store cleaned, consistent market data so you can query it anytime. Quants have relied on internal price stores for decades because systematic research needs repeatable inputs. Early quant teams at banks and multi-strategy funds invested heavily in data pipelines once they learned that strategy performance often changes when data changes.

How pros use it Today this looks like a simple pipeline that downloads data, normalizes the schema, and appends new rows on schedule. On a desk, the database layer is usually separated from research code so analysts can trust the same tables across notebooks and backtests. Even if the production stack uses Postgres or cloud warehouses, many researchers prototype the exact same workflow locally with SQLite because it is fast, file-based, and uses standard SQL.

The professional habit you’re borrowing is to make ingestion idempotent and traceable, so you can rerun jobs without guessing what changed. Why it’s worth learning If you’re learning Python for trading, a local database saves you from the most common time sink, which is repeatedly cleaning the same data.

It also makes your backtests easier to reproduce because your feature code pulls from a stable table instead of a folder of one-off downloads. Let’s see how it works with Python. Subscribe to get the code notebooks. Library installation Install the third-party libraries we need to download market data and work with tabular results in a notebook or script. We do not install sqlite3 or sys because they are part of the Python standard library, so they come with Python by default. Imports and setup We use sys (argv) to read command-line arguments, sqlite3 to create a local SQLite database file, pandas for DataFrame cleaning and SQL I/O, and yfinance to fetch daily OHLCV bars from Yahoo Finance.

Keeping imports explicit helps us separate the “data plumbing” layer (SQLite) from the “research” layer (pandas), which is the same separation we want in a reproducible workflow. Normalize downloaded bars into schema Download raw bars with yfinance, then normalize column names and add a symbol so every row is self-identifying in our database.

This “schema normalization” is where beginners usually lose reproducibility, because the same dataset can end up with different column names across files and sessions. Adding a “symbol” column upfront also prevents the classic mistake of mixing tickers when you later append more data. Append market data into SQLite Write normalized DataFrames into a single SQLite table named “stockdata” so we can backfill history and then keep appending new rows. Using DataFrame.tosql keeps ingestion “boring” and repeatable, which is exactly the professional habit we want for research.

In a real pipeline we would also enforce uniqueness (for example on date+symbol) to make reruns idempotent, but even this simple append-based store removes a lot of CSV chaos. Treating the database connection as an argument (”con”) makes it easy to reuse the same functions in scripts, notebooks, or scheduled jobs without rewriting anything. Run ingestion from the command line Provide a small CLI entry point so we can backfill (”bulk”) or do a daily update (”last”) without touching notebook state.

The key idea here is operational: once ingestion is a one-line command, we stop re-downloading and re-cleaning ad hoc files for every new strategy test. SQLite writes to a single “market_data.sqlite” file, which makes it easy to version, back up, and share across projects. Query stored data for research Load stored rows back into pandas with SQL so our analysis notebooks can start from a stable, auditable table instead of fresh downloads. Pulling data with SQL is what makes the “price database” useful: we can filter by symbol and conditions (like volume) before we even compute indicators.

This is the backbone of reproducible research, because the exact query that produced your backtest input is easy to save, rerun, and audit later. Your next steps You now have a small Python pipeline that backfills history, normalizes the schema, and appends new daily bars into a local SQLite price database.

That gives you stable, queryable inputs for charts and backtests, so your results stay reproducible and you stop wasting time rebuilding datasets.