Backtesting and EMA Strategy
What is Backtesting?
Backtesting is a process used in trading and investing to test how a trading strategy would have
performed in the past using historical market data. Instead of risking real money, the strategy is
simulated on past prices to see whether it would have been profitable or not.
The key idea is:
• If a strategy performs well on past data, it may perform well in the future.
• Backtesting helps traders evaluate profitability, risk, and consistency of a strategy before
applying it in live markets.
Commonly evaluated metrics in backtesting:
• Total Return – how much profit/loss the strategy made.
• Sharpe Ratio – risk-adjusted return.
• Win Rate – percentage of profitable trades.
• Drawdown – the largest drop from a peak to a low (risk measure).
Backtesting with Exponential Moving Averages (EMA)
An Exponential Moving Average (EMA) is a type of moving average that gives more weight to
recent prices, making it more responsive to market changes compared to a Simple Moving Average
(SMA).
In this strategy:
• We use two EMAs – a short-term EMA (20 periods) and a long-term EMA (50 periods).
• Buy Signal: When the short EMA crosses above the long EMA → market trend turning
bullish.
• Sell Signal: When the short EMA crosses below the long EMA → market trend turning
bearish.
This is called a crossover strategy, widely used to capture medium-term market trends.
PYTHON CODE AND OUTPUT FOR BACKTESTING