As soon as anyone learns a little about algo trading someone will say “you’ve gotta backtest”. And I don’t disagree with this. BUT also, don’t ever start thinking backtesting is any substitute for actual analysis, modelling and, most importantly - actual live trading.

You’ve got to do some thinking, come up with a novel idea - and get it to the market.

We’re going to show this in our Workshop in a few days. Make sure you sign up for it.

Test - Definitely

Algos are trades that are executed by programs. Any program of any importance or complexity must be tested. It should be tested in many ways - testing is a big subject in computer science. One type of testing is to simulate the real world with recorded data. This is a good idea.

For one thing, with enough data you can become reasonably confident that the program won’t actually fail (or get to debug it if it does). You can fix problems - e.g. arithmetic - that might’ve caused it to lose you much money.

Definitely worth doing. But is that backtesting?

What backtesting for an algo is supposed to be is to determine its return if it had been utilised during a historical period. But doing that is much more than just replaying the data.

Backtest yourself - and everyone else in the market

An important part of backtesting is to model correctly how the algo trader would behave. Traders will never just let an algo go - they must watch it continuously. There may be bugs. There may be extreme market events. And of course - there may be drawdowns. Traders may switch off their algos in these cases - this must be modelled. What’s the probability? All must be accounted for in order to make a backtest accurate.

Also, when your algo trades it’s not alone. It must compete with other traders (and algos). Who’ll be first to react? High-frequency traders will beat everyone else. But for the rest of us predicting our position in the order book is a statistical game. It requires analysis and modelling. We must estimate our slippage.

History repeats: backtesting is consistently dubious

Just as “everyone” will stress the importance of backtesting “everyone” (often the same people) will make clear that backtesting results can’t be relied upon. So why do them?

Our feeling is that they are an important sanity check. Is your idea wildly off mark? Does it work at all? After that - better to get into the real market with initially small quantities: get trading.

Still, for your that sanity check it’s worth doing it well. Here’s an example:

import pandas as pd
import numpy as np
import ccxt
import matplotlib.pyplot as plt

# Connect to the BitMEX exchange
exchange = ccxt.bitmex({
    'rateLimit': 500,
    'enableRateLimit': True,

# Download historical data for XBTUSD
symbol = 'XBTUSD'
timeframe = '1d'
ohlcv = exchange.fetch_ohlcv(symbol, timeframe)

# Convert the data to a Pandas dataframe
ohlcv = pd.DataFrame(ohlcv, columns=['timestamp', 'Open', 'High', 'Low', 'Close', 'Volume'])
ohlcv['timestamp'] = pd.to_datetime(ohlcv['timestamp'], unit='ms')
ohlcv.set_index('timestamp', inplace=True)

# Define the short and long windows for moving averages
short_window = 50
long_window = 200

# Compute the short and long moving averages
ohlcv["SMA50"] = ohlcv["Close"].rolling(window=short_window).mean()
ohlcv["SMA200"] = ohlcv["Close"].rolling(window=long_window).mean()

# Create a signal when the short moving average crosses above the long moving average
ohlcv["Signal"] = np.where(ohlcv["SMA50"] > ohlcv["SMA200"], 1, 0)

# Calculate the daily returns of the strategy
ohlcv["Returns"] = ohlcv["Close"].pct_change() * ohlcv["Signal"].shift(1)

# Calculate the cumulative returns of the strategy
ohlcv["Cumulative Returns"] = (1 + ohlcv["Returns"]).cumprod()

# Plot the cumulative returns of the strategy
ohlcv["Cumulative Returns"].plot()

# Calculate the Sharpe ratio of the strategy
sharpe_ratio = ohlcv["Returns"].mean() / ohlcv["Returns"].std() * np.sqrt(365)
print("Sharpe Ratio:", sharpe_ratio)

In our Workshop - actually the second in a series - we’ll continue developing a novel strategy. Our analysis - including a bit of backtesting - suggests a positive return. Will that occur? Only the market can tell. Sign up to find out. We’ll be trading live!