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:
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 < 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
If price falls 5-10% below its moving average, the algo might trigger a buy.
Let’s say:
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)
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!
Disclaimer: This article is for informational purposes only and does not constitute financial advice. It is not produced by the desk of the Kotak Securities Research Team, nor is it a report published by the Kotak Securities Research Team. The information presented is compiled from several secondary sources available on the internet and may change over time. Investors should conduct their own research and consult with financial professionals before making any investment decisions. Read the full disclaimer here.
Investments in securities market are subject to market risks, read all the related documents carefully before investing. Brokerage will not exceed SEBI prescribed limit. The securities are quoted as an example and not as a recommendation. SEBI Registration No-INZ000200137 Member Id NSE-08081; BSE-673; MSE-1024, MCX-56285, NCDEX-1262.
Disclaimer: This article is for informational purposes only and does not constitute financial advice. It is not produced by the desk of the Kotak Securities Research Team, nor is it a report published by the Kotak Securities Research Team. The information presented is compiled from several secondary sources available on the internet and may change over time. Investors should conduct their own research and consult with financial professionals before making any investment decisions. Read the full disclaimer here.
Investments in securities market are subject to market risks, read all the related documents carefully before investing. Brokerage will not exceed SEBI prescribed limit. The securities are quoted as an example and not as a recommendation. SEBI Registration No-INZ000200137 Member Id NSE-08081; BSE-673; MSE-1024, MCX-56285, NCDEX-1262.
Explore our comprehensive video library that blends expert market insights with Kotak's innovative financial solutions to support your goals.