Blog
6 min read

How to Backtest a Trading Strategy From Your Terminal

A practical, step-by-step guide to backtesting an algorithmic trading strategy from the command line: from a plain-English idea to real historical metrics you can actually trust.

By Finny-AI team

Backtesting is the difference between a trading idea you hope works and one you have actually tested against history. This guide walks through backtesting a strategy end to end from your terminal: describing the idea, generating the code, running it against real market data, and reading the results without fooling yourself.

Why backtest from the terminal at all

No-code backtesters hide the logic behind a dashboard, so you can never be sure what was actually tested. Full quant frameworks give you control but demand a lot of boilerplate before you see a single result. Working from the terminal with generated, readable Python keeps you close to the logic while skipping the plumbing. You can inspect every line, change it, and re-run in seconds.

Step 1: Install and start Finny

If you have not already, install the CLI. Full setup, including the Python and yfinance prerequisites, is covered in the install guide.

bash
npm install -g finny
finny

Step 2: Describe the strategy in plain English

You do not start by writing code. You start by describing the edge you want to test. Finny turns that description into a complete Python strategy that conforms to a canonical strategy contract, so the backtester knows exactly how to run it.

text
> Buy SPY when the 50-day moving average crosses above the
  200-day, and exit when it crosses back below.

This is a classic golden-cross trend strategy. It is a good first backtest because the rules are unambiguous, which makes the results easy to sanity-check.

Step 3: Run the backtest

Once the strategy is generated, run it against historical data. Finny's engine_v2 backtester pulls real price history and simulates the strategy bar by bar.

text
> backtest SPY from 2015 to 2024

Behind the scenes this fetches historical data, applies your entry and exit rules on each bar, tracks positions and equity, and produces a full performance report rather than a single number.

Step 4: Read the results honestly

The most common way to lie to yourself is to look only at total return. A strategy that returned 200% but sat through a 70% drawdown is not one most people could actually hold. engine_v2 reports metrics across four dimensions so you see the whole picture:

  • Return. Total and annualized return, plus how it compares to simply holding the asset.
  • Risk. Volatility, Sharpe, and Sortino, so you know what you paid in risk for those returns.
  • Drawdown. Maximum drawdown and recovery, the number that tells you whether you could survive the strategy emotionally.
  • Stability. Win rate, exposure, and consistency across the test window, so you can tell a real edge from one lucky year.

Watch for overfitting

If you tweak a strategy until it looks perfect on one slice of history, you have probably fit it to noise. Two habits help: test across a long window that includes different market regimes, and keep the rules simple enough that you can explain why they should work. If a strategy only performs when a parameter is set to one exact value, treat that as a warning, not a discovery.

Step 5: Iterate

Backtesting is a loop, not a single command. Change one thing at a time, re-run, and compare. Because the generated code is plain Python you own, you can edit it directly or ask Finny to adjust it in conversation.

text
> add a 5% trailing stop and re-run the backtest

From backtest to paper trading

A backtest that holds up is a hypothesis, not a guarantee. The next step is forward testing on live data without risking capital. When you are ready, connect a broker in paper-trading mode. Finny documents Alpaca, Binance, and Interactive Brokers workflows, and credentials stay on your machine.

Where to go next

The fastest way to run your first backtest is to install the CLI and describe an idea. Start with the install guide, then read how the backtester computes its metrics so you know exactly what you are reading.

— Finny-AI team