A baseline is a reference result used to judge whether an investment strategy adds value. In backtesting, it commonly compares a strategy’s gain with buying a fixed number of shares at the start and selling them at the end, while accounting for transaction costs. section 11.3.1
A useful baseline establishes:
- A fixed investment amount, which determines how many shares can be purchased at the starting price. section 11.3.1
- A comparable buy-and-hold return, calculated from the starting and ending share prices. section 11.3.1
- Trading costs deducted from strategy gains before deciding whether the strategy succeeds. section 11.3.1
Comparing an SMA strategy with holding shares
With $1,000, calculate the shares affordable on the first trading day. Compare an SMA crossover strategy’s final gain against the gain from buying those shares initially and selling them on the final day. Subtract transaction costs from the strategy result; only an excess over the baseline supports calling the strategy successful. NVIDIA provides a convenient starting case, but its strong historical performance can make the comparison unusually favorable. section 11.3.1
Listing 11.2 Executing a single backtest
def run_single_backtest(ticker: str, cash, start_date: str, end_date: str,
transaction_costs = 1) -> pd.DataFrame:
df = load_data(ticker, start_date, end_date)
shares = round(cash / df["Close"][0])
print(f"Shares: {shares}. start price: {df["Close"][0]}. end price: "
f"{df["Close"].iloc[-1]}. to beat: {df["Close"].iloc[-1]*shares –
df["Close"][0]*shares –
transaction_costs}")
df = add_sma(df, 10, 20)
res = backtest(df, 10, 20, shares, True)
gain = res['cash_movement'].sum()
gain = gain - transaction_costs * len(res['cash_movement'])
print(f"Gains: {gain})")
return df
results_nvda
= run_single_backtest("NVDA", 1000, "2015-01-01", "2025-01-01")
Run a single backtest that calculates shares, reports the return needed to beat the baseline, and subtracts transaction costs from strategy gains. section 11.3.1















