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 studio.finlab.tw.

Use case Recommended entry Next step
No installation, test a strategy immediately studio.finlab.tw 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 studio.finlab.tw 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
close = data.get('price:收盤價')
close.tail()
from finlab import data
data.set_market('us')
close = data.get('price:adj_close')
close.tail()

Write a Strategy

Use simple Pandas syntax to write strategy logic. For example, a 300-day high breakout strategy:

from finlab import data

close = data.get('price:收盤價')

# Buy stocks making 300-day highs
position = close >= close.rolling(300).max()
position
from finlab import data

data.set_market('us')
close = data.get('price:adj_close')
close = close[close.index.dayofweek < 5]  # remove weekends

# Buy stocks making 300-day highs
position = close >= close.rolling(300).max()
position

Backtest

from finlab import backtest

report = backtest.sim(position, resample='M')
report.display()