Get started
Quickstart
On this page5
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
Start with an AI workflow: describe a strategy in FinLab Studio in your browser, or install the FinLab Skill in a local AI coding tool for help with code, backtesting and debugging. Choose Colab, local Python or Docker when you want to write Python or notebooks yourself.
Set up an AI coding tool that supports Agent Skills and can run local commands, such as Claude Code or Codex, and install Node.js 22.20 or later. Run this in a terminal and select your tool when prompted:
npx skills add https://github.com/koreal6803/finlab-ai --skill finlabThen ask your AI: "Backtest a multi-factor stock selection strategy." It can help query Taiwan and US data, write strategies, run backtests and interpret performance. See the FinLab AI installation guide for installation options.
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, with no Python or AI tool to install. You can also paste the code on this page into Studio and run it there.
Open a new notebook in Google Colab and run this in the first cell:
!pip install finlabColab login uses an SDK device slot and reuses an existing login record with the same name. Limits depend on your plan; see login and device slots for how to check yours.
You 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.
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:
Try other conditions
Same three steps, a different condition or rebalancing schedule, a different result. Drag below to rerun it on real prices of eight large caps:
Drag and switch: let a condition pick what to hold
Mission 1 of 3 Drag Days to 20 or fewer and watch the blue holdings fill in
position = close >= close.rolling(300).max() report = backtest.sim(position, resample='M')
Real closes of 8 large caps since January 2024. A simplified backtest (equal weights, no fees) to show how sim() trades.
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 your AI coding tool with the command above and ask "which datasets cover revenue?".
Was this page helpful?