Pivot Points calculation in Python for day trading | by Gianluca Malato | Towards Data Science

Day trading is very different from swing trading. It’s more difficult and it needs some particular tools to be profitable. Pivot Points are some of the tools that day traders can use.
In this article, I explain how they work.
Pivot points are price levels often used in intraday trading (but they can be used even in swing trading). They are considered “natural” supports and resistances for the price during a daily market session, so they can be quite useful for day traders because the market often behaves in a non-trivial way when it gets closer to them.
There are several types of pivot points, but the classic pivot points are usually defined starting from the last day’s high, low, and close price. Applying some mathematical calculations to these prices on the previous market day, a trader can calculate the pivot levels for the current market day and use them. In this article, I’ll focus on classical pivot points.
Classical pivot points usually define the main pivot level. The main idea is that if the price is above the pivot level, the market is bullish. If the price is below the main pivot level, the market is bearish.
The main pivot level is often followed by some support and resistance levels, that are used as natural key levels for the intraday market session.
The following figure shows the Facebook stock price at 15 minutes timeframe. The horizontal levels are the pivot supports (S1, S2, S3) and resistances (R1, R2, R3), while the green horizontal lines are the pivot levels (P).
As you can see, the market often seems to reject those levels just like they were acting as real supports and resistances. Even the Pivot level itself acts in this way. So, it’s easy to understand the importance of such levels in day trading.
A day trader could look for trading opportunities where the price is near a pivot level, waiting for a pullback or a breakout and using the nearest pivot level as a target, for example.
The classical pivot levels are calculated according to the following formulas, taken from Tradingview knowledge base (https://www.tradingview.com/support/solutions/43000521824-pivot-points-standard/), applied to the last day’s High, Low and Close prices:
PP = (High + Low + Close) / 3
R1 = 2 * PP - Low
S1 = 2 * PP - High
R2 = PP + (High - Low)
S2 = PP - (High - Low)
R3 = PP + 2 * (High - Low)
S3 = PP - 2 * (High - Low)
PP is the main pivot level. S1, S2 and S3 are the support levels. R1, R2 and R3 are the resistance levels.
Let’s see how to calculate these levels in Python. For this example, I’ll focus on Facebook stock data. The whole notebook can be found here: https://github.com/gianlucamalato/machinelearning/blob/master/Calculation_of_daily_pivot_levels.ipynb
First of all, we must get stock data using yfinance library.
!pip install yfinance
Then, we can import the libraries we need.
import numpy as npimport pandas as pdimport yfinance
We can now import Facebook data into a Pandas dataframe.
ticker = yfinance.Ticker("FB")df = ticker.history(interval="1d")
The last record of this dataframe is the last market day. We are going to use its price levels in order to calculate the pivot levels we can use today.
last_day = df.tail(1).copy()
Now, according to the pivot formulas, we can calculate the levels.
last_day['Pivot'] = (last_day['High'] + last_day['Low'] + last_day['Close'])/3last_day['R1'] = 2*last_day['Pivot'] - last_day['Low']last_day['S1'] = 2*last_day['Pivot'] - last_day['High']last_day['R2'] = last_day['Pivot'] + (last_day['High'] - last_day['Low'])last_day['S2'] = last_day['Pivot'] - (last_day['High'] - last_day['Low'])last_day['R3'] = last_day['Pivot'] + 2*(last_day['High'] - last_day['Low'])last_day['S3'] = last_day['Pivot'] - 2*(last_day['High'] - last_day['Low'])
The result is:
Pivot points are very used in day trading and they are very easy to calculate in Python. The only need a single market day data, so they don’t need too many historical records. A day trader should try to create a trading strategy according to these levels (or other kinds of pivot levels, like Fibonacci, Woodie, Camarilla) and according to a strategy type (i.e. breakout or pullback).