Skip to content

FinLab Getting Started: Build Your First Backtest in 3 Minutes

The fastest path is to choose an execution environment, download closing price data, write the first stock-selection condition, then run backtest.sim(). If you want AI-assisted strategy writing, start directly from FinLab Studio.

Use case Recommended entry Next step
No installation, test a strategy immediately FinLab Studio Describe the strategy in natural language
First time using FinLab Google Colab Run the examples on this page
Existing Python environment Local pip install finlab Download data and backtest
Complex dependency environment Docker Open JupyterLab

Related guides: Data Download, Backtesting, and FAQ.

Installation

Supports Windows, macOS, Linux, and Colab; Docker also available.

Visit FinLab Studio to write strategies through AI conversation — no installation required.

# Run in a new Colab notebook
!pip install finlab
pip install finlab

Compatibility note

If you encounter dependency issues, try the Docker version instead.

docker pull finlab/jupyter-finlab
docker run -p 8888:8888 finlab/jupyter-finlab
# Open JupyterLab using the URL shown in the terminal

Download Data

Use the following code to download data. You can browse available datasets.

from finlab import data

data.set_market('us')
close = data.get('price:adj_close')
close.tail()
from finlab import data

data.set_market('tw')
close = data.get('price:close')
close.tail()

Write a Strategy

Use simple Pandas syntax to write strategy logic. For example, buy the S&P 500 stocks that close at a 250-day high:

from finlab import data
from finlab import backtest

data.set_market('us')
with data.us_universe(index='SP500'):
    close = data.get('price:adj_close')

# S&P 500 stocks closing at a 250-day high
position = close == close.rolling(250).max()

Backtest

report = backtest.sim(position, resample='M', name='S&P 500 new highs')
report.display()

data.us_universe(index='SP500') uses today's S&P 500 members for the whole history, so past results are overstated (survivorship bias); see Historical index backtests for a point-in-time version.