How to Calculate Pivot Points in Python
Pivot point is a leading indicator used to assess directional movement of an asset and potential support and resistance level .
- it is calculated using the previous day’s high , low and close prices .
- traders use pivot points to predict where price may reverse or breakout .
- pivot point work well in intraday trading because they give a quick view of market sentiment .

How to calculate pivot points.
pivot point(p) =(high+low+close)%3
then we calculate supports and resistance levels.
First resistance = (2*p)-low
second resistance = p + (high-low)
third resistance = high + 2* (p-low)
First support = (2*p) - high
second support = p- (high-low)
third support = low - 2*(high-p)
How to trade using pivot points
1. Range trading strategy
- if price approaches support s1 or s2 , look for bullish candlesticks signals (pin bar , hammer) , go for buy .
- if price approaches first resistance or second resistance , look for bearish candlestick signals (shooting star ) , go for sell .
2.Breakout strategy
- if price breaks above resistance1 with strong volume , it may continue to second resistance and third resistance , go for long .
- if price breaks below first support with strong volume , it may continue to second support and third support , go for short .
3. How to Implement pivot point using python
from kiteconnect import KiteConnect
import pandas as pd
import datetime as dt
import os
import numpy as np
cwd = os.chdir("D:\GenAi\python\python basic\Strategy")
#generate trading session
access_token = open("access_token.txt",'r').read()
key_secret = open("api_key.txt",'r').read().split()
kite = KiteConnect(api_key=key_secret[0])
kite.set_access_token(access_token)
#get dump of all NSE instruments
instrument_dump = kite.instruments("NSE")
instrument_df = pd.DataFrame(instrument_dump)
def instrumentLookup(instrument_df,symbol):
try:
return instrument_df[instrument_df.tradingsymbol==symbol].instrument_token.values[0]
except:
return -1
def fetchOHLC(ticker,interval,duration):
instrument = instrumentLookup(instrument_df,ticker)
data = pd.DataFrame(kite.historical_data(instrument,dt.date.today()-dt.timedelta(duration), dt.date.today(),interval))
data.set_index("date",inplace=True)
return data
def levels(df):
high = round(df["high"][-1],2)
low = round(df["low"][-1],2)
close = round(df["close"][-1],2)
pivot = round((high + low + close)/3,2)
r1 = round((2*pivot - low),2)
r2 = round((pivot + (high - low)),2)
r3 = round((high + 2*(pivot - low)),2)
s1 = round((2*pivot - high),2)
s2 = round((pivot - (high - low)),2)
s3 = round((low - 2*(high - pivot)),2)
return (pivot,r1,r2,r3,s1,s2,s3)
df= fetchOHLC("INFY","day",30)
pp_levels = levels(df.iloc[:-1,:])
Explanation of above code
def levels(df):
high = round(df["high"][-1],2)
- Defines a function called
levelswhich takesohlc_dayas input. - df is expected to be a dataset (like a dictionary or DataFrame) containing
"high","low", and"close"prices. - The docstring tells us the purpose: it returns pivot point and support/resistance levels.
- Gets the last high price (
[-1]means last value in the list/series). - Rounds it to 2 decimal places.
low = round(ohlc_day["low"][-1],2)
close = round(ohlc_day["close"][-1],2)
- Gets the last low price, rounded to 2 decimals.
- Gets the last closing price, rounded to 2 decimals.
pivot = round((high + low + close)/3,2)
- Calculates the pivot point.
- Formula:
(High + Low + Close) / 3 - Pivot is the central reference price for next day’s trading.
r1 = round((2*pivot - low),2)
- Resistance 1 (R1) level.
- Formula:
2*Pivot - Low - First level where price might face resistance if it goes up.
r2 = round((pivot + (high - low)),2)
- Resistance 2 (R2) level.
- Formula:
Pivot + (High - Low) - Second, stronger resistance level.
r3 = round((high + 2*(pivot - low)),2)
- Resistance 3 (R3) level.
- Formula:
High + 2*(Pivot - Low) - Strongest resistance level.
s1 = round((2*pivot - high),2)
- Support 1 (S1) level.
- Formula:
2*Pivot - High - First support level (price may stop falling here).
s2 = round((pivot - (high - low)),2)
- Support 2 (S2) level.
- Formula:
Pivot - (High - Low) - Second support level.
s3 = round((low - 2*(high - pivot)),2)
- Support 3 (S3) level.
- Formula:
Low - 2*(High - Pivot) - Strongest support level.
return (pivot,r1,r2,r3,s1,s2,s3)
Returns all calculated levels as a tuple.
Output looks like:(Pivot, R1, R2, R3, S1, S2, S3)