Algorithmic Trading & Indicators
Beginner
Calculate Central Pivot Range (CPR) & Narrow CPR Scanner in Python
Compute daily and weekly Central Pivot Range (Pivot, BC, TC) levels in Python Pandas and scan for Narrow CPR breakout days on Nifty and stocks.
#pandas#numpy
Problem Statement
Intraday index traders rely heavily on Narrow CPR setups to anticipate trending breakout days, but manual plotting across 50+ F&O stocks takes hours.
Formula / Core Logic
Pivot (P) = (High + Low + Close) / 3
Bottom Central (BC) = (High + Low) / 2
Top Central (TC) = (Pivot - BC) + Pivot
Narrow CPR = Width_Pct < 0.25%
Installation
pip install pandas numpy
central-pivot-range-cpr-indicator-python-pandas.py
import pandas as pd
import numpy as np
def calculate_cpr(df):
df = df.copy()
prev_h = df['High'].shift(1)
prev_l = df['Low'].shift(1)
prev_c = df['Close'].shift(1)
df['Pivot'] = (prev_h + prev_l + prev_c) / 3.0
df['BC'] = (prev_h + prev_l) / 2.0
df['TC'] = (df['Pivot'] - df['BC']) + df['Pivot']
df['CPR_Width_Pct'] = ((df['TC'] - df['BC']).abs() / df['Pivot']) * 100
df['Narrow_CPR'] = np.where(df['CPR_Width_Pct'] < 0.25, 1, 0)
return df
Get the Complete 100+ Python Quant & Automation Bundle
Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).
Buy Full Bundle on Gumroad ($19) →Frequently Asked Questions
Q: What CPR width percentage defines a Narrow CPR?
A CPR width below 0.25% of spot price on daily charts typically signals high compression and an upcoming trending session.