Calculating Resistance and Pivot points with Python

Harsh
Code for Cause
Published in
5 min readJul 2, 2020

--

Python has now extended it’s reach to almost all sectors of industry and finance sector is no different. BuyUcoin — a Vist India based global cryptocurrency exchange wallet and I , collaborated on a project to make a feature rich program , that sends you an email when there is a sudden change in the trend of stock prices , all automated and predicted with python.

BuyUcoin

In this article I will talk about how you can can predict the change in market value accurately and mark the pivot points and their dates accordingly

Before we dive into this , let’s understand a little bit about the stock market. What does support and resistance mean in a stock price chart. In stock market technical analysis, support and resistance are certain predetermined levels of the price of a security at which it is thought that the price will tend to stop and reverse. These levels are denoted by multiple touches of price without a breakthrough of the level

support and resistance graph

Since we are only talking about resistance here , we shall shift our focus towards that. Analysts in this field have been working on this for a long time now , hence yahoo came up with it’s library yfinance that lets you fetch the stock prices of any listed stock for any time frame required and customize it accordingly to you own needs. Hence we will be using matplotlib and yfinance library to help us visualize the data for starters.

After you are done with importing all the relevant libraries , we can start writing our code. Start by activating yahoo finance workaround and setting the starting and the end points of the dataframe. You can do this with the following code.

here Yf.pdr_override() , will activate your workaround conditions and the rest will set your dataframe.

Now we are done with all the prerequisites for the main program and we can jump directly into it. So let’s start by deciding the math behind the decision making of — How do you decide the value of the pivot points ?

The answer is pretty simple and straight forward we take. we are going to use a variable ( counter ) to count to 5 and that’s because when we look at a stock chart , we want to identify a local maximum and as for me , i would recommend using a period of 10 days , so we want a to find a stock that is a local maximum within these 10 days. So it needs 5 days before to the left and 5 days after to the right , that is how we will characterize a pivot point.

so will make an array called Range in which we will store all our 10 periods of days and their values. We shall also make an array called daterange which will store the dates for the same. so each day we will add a new date and get rid of the previous value so we can keep rotating the dates and move ahead in the chart.

we will start by initiating a loop and finding the current max of the array and appending those values in the range array. Now if the current max is the max value in the array the counter will add +1 and if is not , the counter will be set as zero and it will look for a new maximum. we need another if statement to check if the pivot has been found , so if the counter == 5 , last pivot will become new pivot as all 5 points before and after it are lower than it

we will set the date location as index of the last pivot and we will get the corresponding date as well. print out this much to figure out if you have done everything right till the point. Make sure you convert the pivots and dates into strings before printing the value. after printing it you will get all the values of the pivot points. Now we will iterate through these arrays and print them out in couple so the user can see which pivot occurred on which date. It will look something like this

Pivot points and the their dates ( Tesla stock)

Now all that is left is marking these points on our tesla stock price which currently looks something like this: ( we are working on Tesla stock price charts , hence the figure represents the same )

we can plot it with the help of the following code

Note that you can choose your linemarker and style as you wish. Now that our lines are plotted we can look at all the pivot points.

Finally we have plotted our pivot points and now we can use it and modify it however we want.

In the next article , we will look at the trends in resistance and how to predict it. repository for this project — https://github.com/hahaharsh7/support-and-resistance-plotting/blob/master/Untitled4.ipynb

--

--