Get started
Quickstart
On this page4
This page takes you from nothing to your first backtest: install → load data → write a rule → backtest, in about five minutes. Every snippet runs as is.
- 1 Get data
data.get('price:adj_close')Dates down, stocks across
- 2 Write a condition
close > close.average(60)True = hold that day
- 3 Backtest
sim(position, resample='M')Returns, drawdown, trades
- 4 Go live
PortfolioSyncManagerUpdates and orders every day
Pick where to run it
For a first try, use Google Colab: it runs in your browser, with nothing to install.
Open a new notebook in Google Colab and run this in the first cell:
!pip install finlabYou need Python 3.10 or later. In a terminal:
pip install finlabIf package versions conflict, use Docker or install into a fresh virtual environment (python -m venv).
The image comes with FinLab and JupyterLab:
docker pull finlab/jupyter-finlab
docker run -p 8888:8888 finlab/jupyter-finlabOpen JupyterLab at the address the terminal prints.
Describe your idea in one sentence in FinLab Studio, such as "each month, buy stocks at a one-year high". AI writes the code and backtests it for you. You can also paste the code on this page into Studio and run it there.
Run your first backtest
Load closing prices
data.get() loads a dataset by its "table:field" name. The first run opens your browser to sign in to FinLab; a free account is enough.
from finlab import data
data.set_market('us')
close = data.get('price:adj_close')
close.tail()| date | AAPL | MSFT | NVDA | AMZN |
|---|---|---|---|---|
| 2026-10-26 | 382.38 | 612.12 | 221.88 | 249.09 |
| 2026-10-27 | 385.93 | 610.66 | 223.87 | 253.1 |
| 2026-10-28 | 382.29 | 610.3 | 221.24 | 254.6 |
| 2026-10-29 | 387.94 | 609.3 | 219.47 | 243.79 |
| 2026-10-30 | 383.18 | 599.84 | 210.21 | 240.35 |
You get back a table: one row per trading day, one column per stock (only a few are shown). Every FinLab dataset has this shape, including fundamentals and filings.
Write a stock-selection rule
Compare whole tables with Pandas. This line means "the close is at a 300-trading-day high":
position = close >= close.rolling(300).max()The result is a table of the same shape, each cell True or False; True means hold that stock on that day. A comparison works on the whole table at once, as this example of "close above its 60-day average" shows:
close Closing price | AAPL | MSFT | NVDA | |
|---|---|---|---|
| 10/27 | 98 | 51 | 120 |
| 10/28 | 101 | 50 | 118 |
| 10/29 | 104 | 49 | 121 |
| 10/30 | 103 | 52 | 117 |
close.average(60) 60-day average | AAPL | MSFT | NVDA | |
|---|---|---|---|
| 10/27 | 100 | 52 | 115 |
| 10/28 | 100 | 51 | 116 |
| 10/29 | 101 | 51 | 117 |
| 10/30 | 101 | 51 | 118 |
position What to hold | AAPL | MSFT | NVDA | |
|---|---|---|---|
| 10/27 | False | False | True |
| 10/28 | True | False | True |
| 10/29 | True | False | True |
| 10/30 | True | True | False |
Backtest
Hand the rule to backtest.sim(). resample='M' checks the rule at each month end and trades on the next trading day:
from finlab import backtest
report = backtest.sim(position, resample='M')
report.display()report.display() draws the full report. This is what the code above produces; you can explore it right here:
Read the report
The top of the report shows the equity curve and the figures people check first:
| Metric | What it means |
|---|---|
| Annual return | The whole backtest's return, as an average per year |
| Max drawdown | The deepest fall from a peak: the hardest stretch to sit through |
| Sharpe ratio | Return per unit of volatility; higher is steadier |
| Win rate | The share of trades that made money |
To get these figures in code, use report.get_stats(); to see every trade, use report.get_trades().
Next steps
- Write better rules Take the top names by rank, combine conditions, set entries and exits.
- Load more data Fundamentals, filings and how to find a dataset's name.
- Tune the backtest Rebalance frequency, stops, fees and the trade log.
- Run it daily Rerun the strategy every day and get rebalance alerts or place orders.
Not sure what a dataset is called? Search fields in the data catalog, or install the FinLab Skill in an AI coding assistant (Claude Code, Cursor and others) and ask "which datasets cover revenue?".
Was this page helpful?