Lesson 16 · Real-World Data Analytics
Backtesting a Trading Strategy in Python (Without Fooling Yourself) | Data Analytics #16
Most back-tests lie. We back-test a moving-average crossover on real Apple prices in pandas the honest way: lagged positions to avoid look-ahead bias,…
- CourseReal-World Data Analytics
- Lesson16 of 100
- FormatJupyter notebook · 30 code cells
- Data1 dataset
What you'll learn
Datasets used in this lesson
Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.
- aapl_clean.csv preview58.9 KB
📓 Full notebook
Download .ipynbData Analytics 100, Video 16: Backtesting a Simple Trading Strategy#
- Video sixteen of the hundred-video real-world data analytics series.
- Turning the real moving-average crossover into an honest, cost-aware backtest against real Apple prices.
- Let's get into it.
This lesson is for learners who have built a moving-average signal and now want to know whether it would actually have made money. The business question is simple: if you had traded Apple shares using a 10-day versus 30-day moving-average crossover, would you have beaten just buying and holding, once you account for trading costs and avoid peeking at the future?
The data is the cleaned Apple price file from earlier in the series, aapl_clean.csv, with a Date column and the daily closing price in AAPL.Close (506 trading days). You will turn the crossover signal into a lagged position, charge a cost every time the position changes, rebuild individual trades, and judge the result with win rate, profit factor, Sharpe ratio and maximum drawdown. You will also test a second strategy, RSI mean-reversion, and stress-test the cost assumption.
In this lesson you will:
- build an honest, cost-aware backtest with
shift,pct_changeandcumprod - reconstruct a trade log and measure trade-level statistics
- compare strategies on risk as well as return
Prerequisites: you should be comfortable with pandas Series, rolling windows and basic matplotlib; the previous lesson on moving averages is a good warm-up.
Part 1: A Real Backtest Needs Real Honesty#
A backtest replays a trading rule on historical prices to see how it would have performed. It is very easy to fool yourself, so honesty matters from the start. This step only loads the tools and data: pd.read_csv with parse_dates=['Date'] reads the cleaned Apple file and turns the date column into real datetimes, and close keeps just the AAPL.Close price Series that every later calculation uses. There is no output here. A useful habit is to check aapl.shape and aapl.head() yourself before building anything on top of the data.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
aapl = pd.read_csv('aapl_clean.csv', parse_dates=['Date'])
close = aapl['AAPL.Close']
Part 2: Real Strategy Rules#
Before you can test a strategy you need precise, mechanical rules. Here the rule is: be invested when the 10-day simple moving average (SMA) is above the 30-day SMA, otherwise stay in cash. close.rolling(10).mean() and close.rolling(30).mean() build the two averages, the comparison gives True or False per day, and .astype(int) turns that into 1 or 0. The value_counts() output shows 255 days with signal 1 and 251 days with signal 0, a near even split. Note that the first 29 days count as 0 because the 30-day average does not exist yet, so the comparison is False.
sma_fast = close.rolling(10).mean()
sma_slow = close.rolling(30).mean()
signal = (sma_fast > sma_slow).astype(int)
signal.value_counts()
Part 3: Real Position Lag, Avoiding Real Lookahead Bias#
Lookahead bias means using information you could not have known at the time. The signal is calculated from today's closing price, so you can only act on it tomorrow. signal.shift(1) moves every value down one row so that today's position is yesterday's signal, and .fillna(0) treats the empty first day as cash. close.pct_change() gives each day's percentage price change. The output of position.head(12) is all zeros, which is expected: the slow average is still warming up, so no position is taken in the first days. Forgetting this one-day shift is the most common way backtests look far better than reality.
position = signal.shift(1).fillna(0)
daily_return = close.pct_change()
position.head(12)
Part 4: Real Gross Strategy Returns#
Gross returns show what the strategy earns before any trading costs, which is the upper limit of what you could hope for. Multiplying position * daily_return keeps the day's return when you are invested (position 1) and zero when you are in cash. Then (1 + returns).cumprod() compounds the daily returns into the growth of $1, with `fillna(0)` handling the first day's missing return. The output, 1.17, means $1 would have grown to about $1.17, a 17% gross gain over the period. Treat this as optimistic: real trading is never free.
gross_strategy_return = position * daily_return
cumulative_gross = (1 + gross_strategy_return.fillna(0)).cumprod()
round(cumulative_gross.iloc[-1], 3)
Part 5: Real Transaction Costs#
Every time you buy or sell you pay commission, spread or slippage, so a strategy that trades often can lose its edge to costs. position.diff().abs() is 1 on any day the position switches, in or out, and 0 otherwise. Multiplying by cost_per_trade = 0.001 charges 0.1% per switch. transaction_costs.sum() gives 0.021, so the total drag is about 2.1 percentage points spread over 21 position changes. The 0.1% figure is an assumption, so it is worth testing a higher value later, which this notebook does.
position_change = position.diff().abs().fillna(0)
cost_per_trade = 0.001
transaction_costs = position_change * cost_per_trade
transaction_costs.sum()
Part 6: Real Net Strategy Returns#
Net returns are what you would actually keep, so they are the numbers that matter for any decision. The code subtracts the daily cost from the gross daily return, then compounds with cumprod() exactly as before. The output shows $1 growing to 1.146, about a 14.6% gain, compared with 1.17 gross. Costs knocked roughly 2.4 percentage points off the ending value. That gap looks small here because the strategy only switched position about 21 times; a faster rule with many more switches would lose much more.
net_strategy_return = gross_strategy_return - transaction_costs
cumulative_net = (1 + net_strategy_return.fillna(0)).cumprod()
round(cumulative_net.iloc[-1], 3)
Part 7: Real Strategy vs Real Buy-and-Hold#
A strategy is only worth the effort if it beats the simple alternative of buying once and holding. Buy-and-hold is built by compounding every daily return with cumprod(), with no position filter and no costs. The output tuple (1.146, 1.059) shows the net SMA strategy ending at $1.146 per $1 versus $1.059 for buy-and-hold, so the crossover finished ahead by about 8.7 percentage points over this window. Ending value alone does not tell the full story, which is why risk measures follow later.
cumulative_buyhold = (1 + daily_return.fillna(0)).cumprod()
round(cumulative_net.iloc[-1], 3), round(cumulative_buyhold.iloc[-1], 3)
Part 8: Visualizing Real Net Strategy vs Real Buy-and-Hold#
A chart shows how the returns were earned, not just where they ended. The code plots three equity curves against aapl['Date']: net strategy, gross strategy (dashed with linestyle='--' and faded with alpha=0.6) and buy-and-hold. It saves the figure with plt.savefig and closes it with plt.close(), so nothing is displayed inline; open backtest_equity_curve.png to view it. Look for where the strategy sits in cash and stays flat while buy-and-hold falls, and how close the gross and net lines are, which shows the cost drag.
plt.figure(figsize=(11, 6))
plt.plot(aapl['Date'], cumulative_net, label='Real Net Strategy (After Costs)')
plt.plot(aapl['Date'], cumulative_gross, label='Real Gross Strategy (No Costs)', linestyle='--', alpha=0.6)
plt.plot(aapl['Date'], cumulative_buyhold, label='Real Buy and Hold')
plt.xlabel('Real Date')
plt.ylabel('Real Growth of $1 Invested')
plt.title('Real SMA Crossover Backtest, Gross vs Net vs Buy-and-Hold')
plt.legend()
plt.tight_layout()
plt.savefig('backtest_equity_curve.png', dpi=120)
plt.close()
Part 9: Real Trade-by-Trade Log#
A single total return can hide very uneven behaviour, so the next step is to identify individual trades. position.diff() is +1 on the day you move into the market and -1 on the day you move out. Filtering position on those values and taking .index.tolist() gives the row positions of entries and exits. The output (11, 10) shows 11 entries but only 10 exits, which means the last trade was still open when the data ended.
position_diff = position.diff().fillna(0)
entry_indices = position[position_diff == 1].index.tolist()
exit_indices = position[position_diff == -1].index.tolist()
len(entry_indices), len(exit_indices)
Part 10: Real Pairing of Entries and Exits#
Entries and exits must be matched into round trips before you can measure each trade. The loop takes each entry, finds all exits that come after it with a list comprehension, and pairs the entry with the first one, later_exits[0]. If no exit follows, as with the final open position, that entry is skipped. The output, 10, confirms ten completed trades. Leaving out the open trade is a reasonable choice, but remember that its unrealised gain or loss is not in the trade statistics.
trades = []
for entry_idx in entry_indices:
later_exits = [x for x in exit_indices if x > entry_idx]
if later_exits:
trades.append((entry_idx, later_exits[0]))
len(trades)
Part 11: Real Trade Returns#
Trade-level returns tell you how the strategy behaves trade by trade. For each pair, close.iloc[exit_i] / close.iloc[entry_i] - 1 measures the price change from the entry day's close to the exit day's close. These are simple price returns and do not subtract the trading costs. The output lists ten returns: only three are positive (3.12%, 3.32% and 5.70%) and seven are negative, ranging from about -0.6% to -5.1%. That is a very different picture from the healthy overall gain.
trade_returns = [close.iloc[exit_i] / close.iloc[entry_i] - 1 for entry_i, exit_i in trades]
trade_returns_series = pd.Series(trade_returns)
trade_returns_series.round(4)
Part 12: Real Win Rate#
Win rate is the share of trades that made money. It is easy to explain to stakeholders, but on its own it can mislead. The code splits returns into real_wins (greater than zero) and real_losses (zero or below), then divides the number of wins by the total number of trades. The output, 30.0, means only 3 of the 10 trades were winners. Many trend-following strategies have low win rates and rely on a few large winners, so a low win rate is not automatically a bad sign.
real_wins = trade_returns_series[trade_returns_series > 0]
real_losses = trade_returns_series[trade_returns_series <= 0]
win_rate = len(real_wins) / len(trade_returns_series)
round(win_rate * 100, 1)
Part 13: Real Profit Factor#
Profit factor is total gains from winning trades divided by total losses from losing trades. A value above 1 means winners outweigh losers. The code sums the winning returns, takes the absolute value of the summed losses with abs(), and guards against dividing by zero with a conditional that returns np.nan. The output, 0.569, means the measured trades lost almost twice as much as they won. This seems to contradict the positive net return; the gap comes from compounding, the open final trade, and measuring trades close to close, so always check trade-level and daily-level results together.
total_wins_amount = real_wins.sum()
total_losses_amount = abs(real_losses.sum())
profit_factor = total_wins_amount / total_losses_amount if total_losses_amount > 0 else np.nan
round(profit_factor, 3)
Part 14: Real Average Win vs Real Average Loss#
Comparing the size of a typical win with a typical loss explains how a low win rate can still work, or fail. The code takes .mean() of the winning and losing returns, multiplies by 100 to express them as percentages and rounds to two decimals. The output (4.05, -3.05) shows the average win was about 4.05% and the average loss about -3.05%. Wins are bigger than losses, but with only 30% of trades winning, that edge is not large enough to make the trade list profitable on its own.
avg_win = round(real_wins.mean() * 100, 2)
avg_loss = round(real_losses.mean() * 100, 2)
avg_win, avg_loss
Part 15: Real Strategy Sharpe Ratio#
The Sharpe ratio measures return per unit of risk: annual return divided by annual volatility. Higher is better. The code annualises the mean daily net return by multiplying by 252 trading days and annualises the standard deviation by multiplying by np.sqrt(252), because volatility grows with the square root of time. The output is 0.559 for the strategy. This simple version ignores the risk-free rate, which is fine for comparing two strategies on the same period but not for quoting an official Sharpe figure.
strategy_annual_return = net_strategy_return.mean() * 252
strategy_annual_vol = net_strategy_return.std() * np.sqrt(252)
strategy_sharpe = strategy_annual_return / strategy_annual_vol
round(strategy_sharpe, 3)
Part 16: Real Buy-and-Hold Sharpe Ratio#
The strategy's Sharpe ratio only means something next to a benchmark. The code repeats the same calculation on the raw daily returns, which is the buy-and-hold investor's experience. The output (0.559, 0.239) shows the SMA strategy earned more than twice as much return per unit of risk as buy-and-hold. Part of the reason is that the strategy sits in cash about half the time, which lowers its volatility. Always use the same annualisation method for both sides of a comparison.
buyhold_annual_return = daily_return.mean() * 252
buyhold_annual_vol = daily_return.std() * np.sqrt(252)
buyhold_sharpe = buyhold_annual_return / buyhold_annual_vol
round(strategy_sharpe, 3), round(buyhold_sharpe, 3)
Part 17: Real Maximum Drawdown Comparison#
Maximum drawdown is the largest fall from a previous peak to a later low. It answers the practical question: how much pain would you have had to sit through? The max_drawdown function tracks the highest value so far with cummax(), measures how far each day sits below that peak as a percentage, and takes the .min(), the deepest dip. The output (-19.03, -32.08) shows the net strategy's worst fall was about 19% versus about 32% for buy-and-hold. Many investors care about drawdown more than ending value, because big drops make people abandon a strategy.
def max_drawdown(cumulative):
running_peak = cumulative.cummax()
return ((cumulative - running_peak) / running_peak).min()
round(max_drawdown(cumulative_net) * 100, 2), round(max_drawdown(cumulative_buyhold) * 100, 2)
Part 18: Real Time in the Market#
Time in the market tells you how much of the period your money was actually exposed to price moves. Because position holds only 0s and 1s, its .mean() is the share of days with a position of 1. The output, 50.2, means the strategy was invested on about half of all trading days. Earning a higher net return than buy-and-hold while invested only half the time is a key reason the risk measures look better. Cash also frees money for other uses, which a single return figure ignores.
pct_time_invested = position.mean()
round(pct_time_invested * 100, 1)
Part 19: Real Walk-Forward Split#
A walk-forward or out-of-sample check asks whether results hold up on a period the strategy was not tuned on. The code sets split_point at 60% of the rows with int(len(aapl) * 0.6), slices the net returns into an earlier and a later segment with .iloc, and compounds each separately. The output (0.947, 1.209) shows the strategy lost about 5% in the first 60% of the data and gained about 21% in the final 40%. Results this uneven suggest the outcome depends heavily on which stretch of history you look at.
split_point = int(len(aapl) * 0.6)
in_sample_net_return = net_strategy_return.iloc[:split_point]
out_sample_net_return = net_strategy_return.iloc[split_point:]
in_sample_growth = (1 + in_sample_net_return.fillna(0)).cumprod().iloc[-1]
out_sample_growth = (1 + out_sample_net_return.fillna(0)).cumprod().iloc[-1]
round(in_sample_growth, 3), round(out_sample_growth, 3)
Part 20: A Real Alternative Strategy, RSI Mean-Reversion#
Testing a different style of rule helps you avoid falling in love with one lucky result. RSI (Relative Strength Index) is a 0 to 100 momentum gauge; below 30 is usually read as oversold. The code takes daily price changes with diff(), splits them into gains with clip(lower=0) and losses with -delta.clip(upper=0), averages each over 14 days, and computes 100 - 100 / (1 + rs). The signal is 1 when RSI is below 30, meaning buy after sharp falls in the hope of a bounce. There is no output; it only builds the signal.
delta = close.diff()
gain = delta.clip(lower=0)
loss = -delta.clip(upper=0)
rs = gain.rolling(14).mean() / loss.rolling(14).mean()
rsi = 100 - (100 / (1 + rs))
rsi_signal = (rsi < 30).astype(int)
Part 21: Real RSI Strategy Backtest#
The RSI rule gets exactly the same honest treatment as the crossover so the comparison is fair. shift(1) lags the signal by one day, multiplying by daily_return gives gross returns, diff().abs() flags position changes, and the same cost_per_trade of 0.1% is charged on each. Compounding with cumprod() gives the growth of $1. The output, 0.883, means this mean-reversion strategy turned $1 into about $0.88, a loss of roughly 12% after costs. Buying dips did not work on this stretch of Apple prices.
rsi_position = rsi_signal.shift(1).fillna(0)
rsi_gross_return = rsi_position * daily_return
rsi_trade_flags = rsi_position.diff().abs().fillna(0)
rsi_net_return = rsi_gross_return - (rsi_trade_flags * cost_per_trade)
cumulative_rsi_net = (1 + rsi_net_return.fillna(0)).cumprod()
round(cumulative_rsi_net.iloc[-1], 3)
Part 22: Real Head-to-Head Strategy Comparison#
Putting all results in one small table makes the ranking obvious. The code builds a pd.Series from a dictionary of the three final values, rounds them, and sorts with sort_values(ascending=False) so the best is first. The output ranks the SMA crossover first at 1.146, buy-and-hold second at 1.059 and RSI mean-reversion last at 0.883. On this data a trend-following rule beat a dip-buying rule, but that can easily reverse on another asset or period.
comparison = pd.Series({'SMA Crossover (Net)': cumulative_net.iloc[-1], 'RSI Mean-Reversion (Net)': cumulative_rsi_net.iloc[-1], 'Buy and Hold': cumulative_buyhold.iloc[-1]})
comparison.round(3).sort_values(ascending=False)
Part 23: Visualizing All Three Real Equity Curves#
Seeing all three equity curves on one chart shows when each strategy made or lost money. The code plots the net SMA, net RSI and buy-and-hold curves against date, adds labels and a legend, and saves the image to strategy_comparison.png before closing the figure, so there is no inline output. When you open it, look for the long flat stretches in the RSI line where it sits in cash, and check whether the SMA strategy's lead over buy-and-hold builds steadily or comes from one short period.
plt.figure(figsize=(11, 6))
plt.plot(aapl['Date'], cumulative_net, label='Real SMA Crossover (Net)')
plt.plot(aapl['Date'], cumulative_rsi_net, label='Real RSI Mean-Reversion (Net)')
plt.plot(aapl['Date'], cumulative_buyhold, label='Real Buy and Hold')
plt.xlabel('Real Date')
plt.ylabel('Real Growth of $1 Invested')
plt.title('Real Strategy Comparison, All Net of Costs')
plt.legend()
plt.tight_layout()
plt.savefig('strategy_comparison.png', dpi=120)
plt.close()
Part 24: Real Cost Sensitivity Check#
Your cost assumption might be too kind, so it is good practice to stress-test it. The code reuses the gross returns and position changes but charges 0.5% per switch (higher_cost = 0.005) instead of 0.1%. The output, 1.053, shows the SMA strategy's ending value falling from 1.146 to 1.053, which is now slightly below buy-and-hold at 1.059. In other words, the strategy's advantage disappears if real trading costs are around 0.5%. Always report how sensitive a result is to the cost you assumed.
higher_cost = 0.005
stressed_net_return = gross_strategy_return - (position_change * higher_cost)
stressed_cumulative = (1 + stressed_net_return.fillna(0)).cumprod()
round(stressed_cumulative.iloc[-1], 3)
Part 25: Real Number of Trades vs Real Cost Assumption#
Linking the number of trades to the cost drag helps you judge how fragile the result is. len(trades) counts completed round trips, and transaction_costs.sum() * 100 expresses the total cost charged as a percentage. The output (10, 2.1) shows ten completed trades and a cumulative cost drag of 2.1 percentage points at 0.1% per switch. More trades mean more cost, so a strategy with a thin edge and frequent switching is especially exposed to higher real-world costs.
n_completed_trades = len(trades)
total_cost_drag_pct = round(transaction_costs.sum() * 100, 2)
n_completed_trades, total_cost_drag_pct
Part 26: Saving the Real Trade Log#
Saving the trade log lets you or a colleague audit every trade later, in Excel or another tool. The code builds a DataFrame with EntryDate and ExitDate, looked up with aapl['Date'].iloc[...], plus each trade's Return, then writes it with to_csv(index=False) so no extra index column is saved. It reads the file back and checks the row count matches the number of trades. The output True confirms all ten trades were saved. Reloading what you write is a cheap way to catch file errors.
trade_log = pd.DataFrame({'EntryDate': [aapl['Date'].iloc[e] for e, x in trades], 'ExitDate': [aapl['Date'].iloc[x] for e, x in trades], 'Return': trade_returns})
trade_log.to_csv('trade_log.csv', index=False)
reloaded_log = pd.read_csv('trade_log.csv')
reloaded_log.shape[0] == len(trades)
Part 27: Saving the Real Backtest Summary#
A compact summary file is what you would share with a manager or reuse in a report. The code builds a three-row DataFrame with each strategy's ending value and maximum drawdown, reusing the max_drawdown function, saves it with to_csv(index=False) and reads it back. The output True confirms the file has three rows, one per strategy. Note that the summary includes the RSI strategy's drawdown, which was not printed earlier, so open backtest_summary.csv to see it.
backtest_summary = pd.DataFrame({'Strategy': ['SMA Crossover', 'RSI Mean-Reversion', 'Buy and Hold'], 'EndingValue': [round(cumulative_net.iloc[-1], 3), round(cumulative_rsi_net.iloc[-1], 3), round(cumulative_buyhold.iloc[-1], 3)], 'MaxDrawdownPct': [round(max_drawdown(cumulative_net) * 100, 2), round(max_drawdown(cumulative_rsi_net) * 100, 2), round(max_drawdown(cumulative_buyhold) * 100, 2)]})
backtest_summary.to_csv('backtest_summary.csv', index=False)
reloaded_summary = pd.read_csv('backtest_summary.csv')
reloaded_summary.shape[0] == 3
Part 28: Real Sanity Check on Trade Pairing#
Trade pairing logic is easy to get wrong, so a quick sanity check protects every trade statistic built on it. The code uses Python's all() with a generator expression to confirm that, for every trade, the exit row comes after the entry row. The output True means no trade has an exit before its entry. Small checks like this cost one line and are worth adding whenever you write your own loop logic, because a silent pairing bug would distort win rate and profit factor without any error.
all(exit_i > entry_i for entry_i, exit_i in trades)
Part 29: Real Recap Print#
A one-sentence recap turns numbers into a message a non-technical reader can understand. The code converts the final growth values into percentage returns with (value - 1) * 100, then uses an f-string to combine them with the trade count and win rate. The printed output reads: after costs across 10 completed trades, the SMA crossover returned 14.6% versus 5.9% for buy-and-hold, with a 30.0% win rate. When you write such summaries, consider adding drawdown too, since return alone hides risk.
net_pct = round((cumulative_net.iloc[-1] - 1) * 100, 1)
bh_pct = round((cumulative_buyhold.iloc[-1] - 1) * 100, 1)
print(f'After real trading costs across {n_completed_trades} completed trades, the SMA crossover returned {net_pct}% versus {bh_pct}% for buy-and-hold, with a {round(win_rate*100,1)}% win rate.')
Part 30: Real Longest Winning Streak#
Streaks matter psychologically: long runs of losses are what make people abandon a strategy. The code marks each trade as 1 for a win or -1 for a loss, then itertools.groupby groups consecutive equal values. The list comprehension counts the length of each group of wins, and max() picks the longest, with a fallback of 0 if there are none. The output, 2, means the strategy never strung together more than two winning trades in a row. Try the same idea for losing streaks to see the worst run.
import itertools
streak_signs = [1 if r > 0 else -1 for r in trade_returns]
win_streaks = [sum(1 for _ in group) for key, group in itertools.groupby(streak_signs) if key == 1]
longest_win_streak = max(win_streaks) if win_streaks else 0
longest_win_streak
Wrap-Up: What You Learned#
- An honest backtest has to lag the real signal by one day and charge a real trading cost every time the position changes, otherwise it is quietly cheating.
- Reconstructing individual real trades reveals a win rate and profit factor that a single aggregate return number would hide completely.
- Comparing real Sharpe ratio and real maximum drawdown side by side, not just ending value, is what tells you whether a strategy is actually worth holding through.
- A real walk-forward split is a basic, honest check for whether a fixed rule is genuinely robust or just fit to one lucky stretch of history.
- Testing a real second, different style of strategy, mean-reversion instead of momentum, keeps you from mistaking one lucky rule for a genuinely reliable edge.
- Next video: applying these real same tools to a real cryptocurrency market, testing whether these lessons carry over to a very differently behaved real asset.
Practice on your own
- Change the moving-average windows to 20 and 50 days, rerun the backtest, and compare the net ending value, Sharpe ratio and maximum drawdown with the 10/30 version.
- Adapt the streak code to find the longest losing streak, by keeping groups where
key == -1, and compare it with the longest winning streak of 2. - Rerun the cost sensitivity step with costs of 0.002, 0.003 and 0.004, store each ending value in a small Series, and find roughly where the SMA strategy stops beating buy-and-hold.
The next video applies these same backtesting tools to a cryptocurrency market, so you can see whether the lessons carry over to a very differently behaved asset.
🧠 Test yourself
Pick an answer to check it. Your best score is saved in this browser.
Why does the lesson use
signal.shift(1)to make the position?A signal based on today's close cannot be traded at today's close. Lagging by one day keeps the backtest honest.
What does this code print?
import pandas as pd pos = pd.Series([0, 0, 1, 1, 0]) cost = pos.diff().abs().fillna(0) * 0.001 print(round(cost.sum(), 4))
The position changes twice (0 to 1, then 1 to 0), so two trades are charged at 0.001 each, totalling 0.002.
In the lesson, how is the profit factor calculated?
A profit factor above 1 means total gains exceeded total losses. It uses amounts, not counts, so it differs from win rate.
What is the purpose of the walk-forward split at 60% in the lesson?
If a rule only works in the first part of the data but not in the later part, it may be overfit to one stretch of history.
Why does the lesson test a higher cost (0.5% instead of 0.1%) in the cost sensitivity check?
A strategy that trades often can look good with tiny costs and poor with realistic ones. Stress testing costs shows how fragile the result is.
💻 Practice exercises
Try each one in the notebook or in your own editor before opening the solution.
Given closes [100, 102, 101, 105, 107] and a 0/1 signal [0, 1, 1, 0, 1] (decided at each day's close), build the lagged position, compute the daily strategy return, and print the cumulative growth of $1.
💡 Hint
Use signal.shift(1).fillna(0) times close.pct_change().
✅ Show solution
import pandas as pd close = pd.Series([100, 102, 101, 105, 107]) signal = pd.Series([0, 1, 1, 0, 1]) position = signal.shift(1).fillna(0) # act the next day daily = close.pct_change() strategy = position * daily growth = (1 + strategy.fillna(0)).cumprod() print(growth.round(4).tolist())
On a 200-day random walk (seed 3, start 100, normal steps std 1) build a 5/20 SMA crossover strategy with a lagged position. Charge 0.1% per position change, then print net growth of $1 and buy-and-hold growth of $1.
💡 Hint
Transaction cost = position.diff().abs() * 0.001, subtracted from gross returns.
✅ Show solution
import numpy as np import pandas as pd rng = np.random.default_rng(3) close = pd.Series(100 + rng.normal(0, 1, 200).cumsum()) daily = close.pct_change() signal = (close.rolling(5).mean() > close.rolling(20).mean()).astype(int) position = signal.shift(1).fillna(0) gross = position * daily cost = position.diff().abs().fillna(0) * 0.001 net = gross - cost print('Net strategy:', round((1 + net.fillna(0)).cumprod().iloc[-1], 3)) print('Buy and hold:', round((1 + daily.fillna(0)).cumprod().iloc[-1], 3))
Using a 300-day random walk (seed 9, start 100, normal steps std 1), run a 10/30 SMA crossover. Find the entry and exit days from position changes, pair them into trades, compute each trade's return, and print number of trades, win rate (%) and profit factor.
💡 Hint
Entries are where position.diff() == 1 and exits where it == -1.
✅ Show solution
import numpy as np import pandas as pd rng = np.random.default_rng(9) close = pd.Series(100 + rng.normal(0, 1, 300).cumsum()) signal = (close.rolling(10).mean() > close.rolling(30).mean()).astype(int) position = signal.shift(1).fillna(0) change = position.diff().fillna(0) entries = change[change == 1].index.tolist() exits = change[change == -1].index.tolist() trades = [] for e in entries: later = [x for x in exits if x > e] if later: trades.append((e, later[0])) rets = pd.Series([close.loc[x] / close.loc[e] - 1 for e, x in trades]) wins = rets[rets > 0] losses = rets[rets <= 0] win_rate = len(wins) / len(rets) * 100 if len(rets) else float('nan') pf = wins.sum() / abs(losses.sum()) if abs(losses.sum()) > 0 else float('nan') print('Trades:', len(trades)) print('Win rate %:', round(win_rate, 1)) print('Profit factor:', round(pf, 3))
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



