Research a trading idea in a Python notebook before you write the strategy

Use a Jupyter-style research notebook with QuantBook to pull history, test a signal and look at the data, then turn what holds up into a backtest.

  • Notebooks
  • Python
  • Research
Research a trading idea in a Python notebook: cover with a pixel light bulb

A backtest answers “would these exact rules have made money?”. Before that, there's a looser question: “is there anything here at all?”. A research notebook is the place for it. You run Python a cell at a time, look at the data, try a signal, plot it, and only write a strategy once something survives a few honest looks.

From notebook to strategy: run cells on a research kernel with QuantBook and the platform's datasets, test a signal, then write the algorithm and backtest it
Notebooks are for finding out whether an idea is worth a backtest. The backtest is still the test.

What a MarketsCoder notebook gives you

  • Notebooks live in your project, next to the algorithm, as ordinary .ipynb files.
  • QuantBook, LEAN's research API, with the same data and indicators your algorithms use.
  • Every dataset: the built-in ones, and your own uploads.
  • A private kernel: it runs in its own container with no internet access, like a backtest.
  • A machine size: Standard, Large or XL, picked in the notebook's toolbar.

A first notebook

Pull a few years of daily prices and look at them:

qb = QuantBook()
spy = qb.add_equity("SPY").symbol

history = qb.history(spy, 1500, Resolution.DAILY)
closes = history["close"].unstack(level=0)[spy]
closes.plot(title="SPY daily close")

Then test an idea, here whether being above the 200-day average said anything about the next month:

sma200 = closes.rolling(200).mean()
above = closes > sma200
next_month = closes.shift(-21) / closes - 1

print("Mean 21-day return when above:", next_month[above].mean())
print("Mean 21-day return when below:", next_month[~above].mean())
print("Days above / below:", above.sum(), (~above).sum())

Note the shift(-21): it looks forward, which is fine for measuring an outcome in research, and exactly what an algorithm must never do. Keeping that boundary clear is half of honest research; our guide to look-ahead bias covers the rest.

From notebook to strategy

When an idea holds up, write it as an algorithm and backtest it. That's where fills, fees, position sizing and the passage of time come in, and where many promising notebook results shrink. Ask the AI assistant to turn the notebook's logic into a strategy, and see our backtesting walkthrough for the structure.

Kernels, sizes and cost

A notebook's kernel starts when you run a cell and stops on its own after 30 minutes without one, so a forgotten tab doesn't keep billing. Writing and saving notebooks is free; a running kernel costs $0.10 an hour on a Standard machine. For big universes or long minute histories, switch to a Large or XL machine.

Frequently asked questions

What is a research notebook for trading?

A Jupyter notebook where you run Python cell by cell to load market data, compute indicators and test ideas interactively, before writing a full algorithm.

What is QuantBook?

LEAN's research API. In a notebook, QuantBook gives you the same data and indicators as an algorithm, so a signal you study there is the one your backtest will see.

What does a notebook cost on MarketsCoder?

Writing and saving notebooks is free. A running kernel is billed by the hour: $0.10 an hour on a Standard machine, more on Large and XL. It stops on its own after 30 minutes without running a cell.

Share on XLinkedIn

Test your own idea on point-in-time data.

Write a strategy in Python or describe it to the AI assistant, and see a full backtest report in minutes.

Get started