Complete "Lesson 5: Risk Management — Position Sizing, Stop Loss, and Risk-Reward" first to unlock this lesson.Go to previous lesson

Introduction

A trading plan without backtesting is just a guess. In this lesson you will combine the technical analysis tools (candlestick patterns, support/resistance, RSI, MACD, moving averages) and the risk management rules (position sizing, stop-loss placement, risk-to-reward ratio) covered in earlier lessons into one personal, written plan. Then you will run that plan against historical data to check whether the strategy actually holds up — before risking any real money.

flowchart showing the 5 stages of building and validating a trading plan: define rules, code rules, run backtest, analyze results, refine rules

Step 1 — Write Your Trading Rules in Plain Language

Before writing any code, write your entry, exit, and risk rules as a numbered list. Example ruleset for a BTC/USDT swing trade:

Rule 1 — Entry: Open a long position when RSI(14) is below 35, price closes above the 50-period EMA, and MACD histogram turns positive.

Rule 2 — Stop-loss: Place stop 2% below the entry candle's low.

Rule 3 — Take-profit: Close half at 1:2 risk-to-reward, move stop to breakeven, close the rest at 1:4.

Rule 4 — Position size: Risk no more than 1% of total account equity per trade.

Keep the list short. If a rule cannot be written in one sentence, it is too vague to code.

Step 2 — Convert Rules into Testable Code

Encode each rule as a function that returns a boolean (entry signal) or a numeric value (stop level, position size). Using Python and the backtesting library:

python
from backtesting import Strategy, Backtest
from backtesting.lib import crossover
import pandas as pd
def ema(series, period):
return series.ewm(span=period, adjust=False).mean()
def rsi(series, period=14):
delta = series.diff()
gain = delta.where(delta > 0, 0).rolling(period).mean()
loss = -delta.where(delta < 0, 0).rolling(period).mean()
rs = gain / loss
return 100 - (100 / (1 + rs))
class SwingPlan(Strategy):
rsi_period = 14
risk_pct = 0.01
rr_ratio = 2.0
def init(self):
close = pd.Series(self.data.Close)
self.ema50 = self.I(ema, close, 50)
self.rsi14 = self.I(rsi, close, self.rsi_period)
def next(self):
price = self.data.Close[-1]
if not self.position and self.rsi14[-1] < 35 and price > self.ema50[-1]:
sl = self.data.Low[-1] * 0.98
tp = price + (price - sl) * self.rr_ratio
size = self.equity * self.risk_pct / (price - sl)
self.buy(size=size, sl=sl, tp=tp)

The function `next()` is called on every candle and represents one full decision cycle. If you can map every rule from Step 1 to a line in this function, your plan is testable.

Step 3 — Run the Backtest and Read the Output

Load historical OHLCV data and run:

python
bt = Backtest(data, SwingPlan, cash=10000, commission=0.001)
stats = bt.run()
print(stats)
bt.plot()

The output includes key metrics: total return, max drawdown, Sharpe ratio, win rate, and number of trades. Do not chase a high return number alone. A 40% return with a 12% max drawdown is more realistic than a 200% return with a 60% drawdown.

Step 4 — Common Mistakes to Avoid

Look-ahead bias: using tomorrow's close to decide today's trade. Always shift indicators by one bar before comparing to current price.

Overfitting: tweaking parameters until the backtest is profitable. If your strategy only works with EMA length 47 on BTC 4h data from 2022, it is curve-fitted.

Ignoring fees and slippage: crypto exchanges charge 0.05–0.10% per side. A strategy with a 0.3% average win cannot survive.

Too few trades: a backtest with 8 trades over one year is statistically meaningless. Aim for at least 50–100 trades before trusting the result.

Summary

A complete trading plan is a numbered ruleset, not a feeling. Encoding those rules in code forces clarity. Backtesting reveals whether the plan is profitable, robust, and worth trading live — or whether it needs to go back to Step 1.

Next lesson will cover paper trading: executing the validated plan in real-time on a testnet account before committing capital.

Lesson practice

Lesson Checkpoint

Review what you learned and get feedback on your work.

0/1
Answered · 0/1