Products
Platform
Research
Market
Learn
Partner
Support
IPO
Logo_light
Module 5
Popular Algo Trading Strategies
Course Index

Chapter 2 | 2 min read

Algo Strategy: Mean Reversion – Buy the Dip, Sell the Spike

You’ve heard it before:

“Buy low, sell high.” “Stocks always return to their average.”

That’s the philosophy behind mean reversion.

When a stock moves too far from its usual price range, algo traders assume it’ll come back (or "revert") to the mean (average). So they buy the dip or sell the spike.

Imagine a stock usually trades between ₹90 and ₹110.

One day, due to panic, it drops to ₹80.

A mean reversion algo will buy at ₹80, expecting it to bounce back toward ₹100.

It assumes the price move was an overreaction — not a new trend.

Indicators to detect mean reversion opportunities:

  • Bollinger Bands
    These bands expand and contract based on volatility.
    If price touches the lower band, it may be oversold – a buy signal.
    If it touches the upper band, it may be overbought – a sell signal.
import pandas as pd

def calculate_bollinger_bands
(data, window=20, num_std=2):
    """
Calculate Bollinger Bands.
    
Parameters:
data    : DataFrame with
 'close' prices
window  : Moving average 
period
num_std : Number of standard
 deviations
    
Returns:
DataFrame with columns: 
MA, Upper Band, Lower Band
    """
data['MA'] = data['close']
.rolling(window=window).mean()
data['STD'] = data['close']
.rolling(window=window).std()
data['Upper'] = data['MA']
+ (data['STD'] * num_std)
data['Lower'] = data['MA']
- (data['STD'] * num_std)
return data[['MA', 'Upper'
, 'Lower']]

# Example usage:
# df = calculate_bollinger
_bands(price_data)

  • RSI (Relative Strength Index)

RSI < 30 = Oversold (Buy)
RSI > 70 = Overbought (Sell)

def calculate_rsi(data,
 period=14):
"""
Calculate Relative Strength 
Index (RSI).
    
Parameters:
data   : DataFrame with
 'close' prices
period : Lookback period
 (default = 14 days)

Returns:
Series containing RSI values
"""
delta = data['close'].diff()
  # Price changes

#Separate gains and losses
gain = delta.where(delta >
 0, 0)
loss = -delta.where(delta 
< 0, 0)

#Use Exponential Moving
 Average 
(smoother than simple average)
avg_gain = gain.ewm(span=
period, adjust=False).mean()
avg_loss = loss.ewm(span
=period
, adjust=False).mean()

    #Relative Strength (RS)
    rs = avg_gain / avg_loss

    #RSI Formula
    rsi = 100 - (100 / (1 
+ rs))
    return rsi

  • Moving Average Pullbacks

If price falls 5-10% below its moving average, the algo might trigger a buy.

Let’s say:

  • Stock's 20-day moving average = ₹100
  • Current price = ₹88
  • RSI = 25

Your algo says, “This is too cheap!”
It buys the stock and exits when price hits ₹98 or RSI crosses 50.

import pandas as pd

def calculate_
moving_average
(data, window=20):
    """
Calculate Simple 
Moving Average (SMA).
    
Parameters:
data   : DataFrame with 
a 'close' column
window : Lookback period 
(default = 20)
    
 Returns:
 Series with SMA values
    """
 return data['close'].rolling
(window=window).mean()

# Example usage:
# price_data['SMA_20'] 
= calculate
_moving_average(price_data, 
window=20)

  • Markets overreact to news, panic, or short-term hype
  • Prices tend to return to fair value
  • It works well in sideways or range-bound markets
  • Trending markets can kill this strategy – the stock may keep falling!
  • Never assume a dip = reversal. Always use:
  • Stoploss
  • Position sizing
  • Time-based exit (e.g., exit if no reversal in 3 days)
  • Combine multiple indicators (e.g., RSI + Bollinger)
  • Avoid trading during news events (like RBI policy or earnings)
  • Limit exposure to a few trades at a time

So, momentum = ride the wave
Mean reversion = catch the bounce

Both have their place. Algos help you remove emotion and follow logic.

Next up: A very interesting concept – Pairs Trading, where you don’t care about market direction at all!

Is this chapter helpful?
Previous
Algo Strategy: Momentum Strategy - Ride the wave
Next
Algo Strategy: Pairs Trading – Neutral & Smart

Discover our extensive knowledge center

Explore our comprehensive video library that blends expert market insights with Kotak's innovative financial solutions to support your goals.