Docs
Open Studio

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. 1 Get data
    data.get('price:adj_close')

    Dates down, stocks across

  2. 2 Write a condition
    close > close.average(60)

    True = hold that day

  3. 3 Backtest
    sim(position, resample='M')

    Returns, drawdown, trades

  4. 4 Go live
    PortfolioSyncManager

    Updates 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 finlab

Then 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.

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()
dateAAPLMSFTNVDAAMZN
2026-10-26382.38612.12221.88249.09
2026-10-27385.93610.66223.87253.1
2026-10-28382.29610.3221.24254.6
2026-10-29387.94609.3219.47243.79
2026-10-30383.18599.84210.21240.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
AAPLMSFTNVDA
10/279851120
10/2810150118
10/2910449121
10/3010352117
close.average(60) 60-day average
AAPLMSFTNVDA
10/2710052115
10/2810051116
10/2910151117
10/3010151118
position What to hold
AAPLMSFTNVDA
10/27FalseFalseTrue
10/28TrueFalseTrue
10/29TrueFalseTrue
10/30TrueTrueFalse
Each cell is one stock on one day. A comparison works on whole tables at once, matching dates and stocks for you; each True is a stock to hold that day. Numbers are illustrative.

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:

Backtest report · interactive Open full size

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:

Interactive level

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')
Condition is True Held
20252026
Your strategy −4%
Hold them all +110%
Buys 23

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:

MetricWhat it means
Annual returnThe whole backtest's return, as an average per year
Max drawdownThe deepest fall from a peak: the hardest stretch to sit through
Sharpe ratioReturn per unit of volatility; higher is steadier
Win rateThe 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?".

↑↓ Move Enter Open Esc Close