├── Images ├── AAPL.png ├── AAPL_sr.png ├── AAPL_locals.png ├── AAPL_smooth.png ├── AAPL_smooth_sr.png └── AAPL_normal_smooth.png ├── requirements.txt ├── support_resistance.py └── README.md /Images/AAPL.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sohandillikar/SupportResistance/HEAD/Images/AAPL.png -------------------------------------------------------------------------------- /Images/AAPL_sr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sohandillikar/SupportResistance/HEAD/Images/AAPL_sr.png -------------------------------------------------------------------------------- /Images/AAPL_locals.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sohandillikar/SupportResistance/HEAD/Images/AAPL_locals.png -------------------------------------------------------------------------------- /Images/AAPL_smooth.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sohandillikar/SupportResistance/HEAD/Images/AAPL_smooth.png -------------------------------------------------------------------------------- /Images/AAPL_smooth_sr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sohandillikar/SupportResistance/HEAD/Images/AAPL_smooth_sr.png -------------------------------------------------------------------------------- /Images/AAPL_normal_smooth.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sohandillikar/SupportResistance/HEAD/Images/AAPL_normal_smooth.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | scipy==1.5.4 2 | numpy==1.19.4 3 | pandas==1.1.4 4 | matplotlib==3.3.3 5 | scikit-learn==0.23.2 6 | pandas-datareader==0.9.0 7 | -------------------------------------------------------------------------------- /support_resistance.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | from math import sqrt 4 | import matplotlib.pyplot as plt 5 | import pandas_datareader as web 6 | from scipy.signal import savgol_filter 7 | from sklearn.linear_model import LinearRegression 8 | 9 | def pythag(pt1, pt2): 10 | a_sq = (pt2[0] - pt1[0]) ** 2 11 | b_sq = (pt2[1] - pt1[1]) ** 2 12 | return sqrt(a_sq + b_sq) 13 | 14 | def regression_ceof(pts): 15 | X = np.array([pt[0] for pt in pts]).reshape(-1, 1) 16 | y = np.array([pt[1] for pt in pts]) 17 | model = LinearRegression() 18 | model.fit(X, y) 19 | return model.coef_[0], model.intercept_ 20 | 21 | def local_min_max(pts): 22 | local_min = [] 23 | local_max = [] 24 | prev_pts = [(0, pts[0]), (1, pts[1])] 25 | for i in range(1, len(pts) - 1): 26 | append_to = '' 27 | if pts[i-1] > pts[i] < pts[i+1]: 28 | append_to = 'min' 29 | elif pts[i-1] < pts[i] > pts[i+1]: 30 | append_to = 'max' 31 | if append_to: 32 | if local_min or local_max: 33 | prev_distance = pythag(prev_pts[0], prev_pts[1]) * 0.5 34 | curr_distance = pythag(prev_pts[1], (i, pts[i])) 35 | if curr_distance >= prev_distance: 36 | prev_pts[0] = prev_pts[1] 37 | prev_pts[1] = (i, pts[i]) 38 | if append_to == 'min': 39 | local_min.append((i, pts[i])) 40 | else: 41 | local_max.append((i, pts[i])) 42 | else: 43 | prev_pts[0] = prev_pts[1] 44 | prev_pts[1] = (i, pts[i]) 45 | if append_to == 'min': 46 | local_min.append((i, pts[i])) 47 | else: 48 | local_max.append((i, pts[i])) 49 | return local_min, local_max 50 | 51 | symbol = 'AAPL' 52 | df = web.DataReader(symbol, 'yahoo', '2019-01-01', '2019-04-01') 53 | series = df['Close'] 54 | series.index = np.arange(series.shape[0]) 55 | 56 | month_diff = series.shape[0] // 30 57 | if month_diff == 0: 58 | month_diff = 1 59 | 60 | smooth = int(2 * month_diff + 3) 61 | 62 | pts = savgol_filter(series, smooth, 3) 63 | 64 | local_min, local_max = local_min_max(pts) 65 | 66 | local_min_slope, local_min_int = regression_ceof(local_min) 67 | local_max_slope, local_max_int = regression_ceof(local_max) 68 | support = (local_min_slope * np.array(series.index)) + local_min_int 69 | resistance = (local_max_slope * np.array(series.index)) + local_max_int 70 | 71 | plt.title(symbol) 72 | plt.xlabel('Days') 73 | plt.ylabel('Prices') 74 | plt.plot(series, label=symbol) 75 | plt.plot(support, label='Support', c='r') 76 | plt.plot(resistance, label='Resistance', c='g') 77 | plt.legend() 78 | plt.show() 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SupportResistance 2 | A python script that estimates the support and resistance lines of a stock's prices or a period 3 | ## Prerequisites 4 | 1. Have `Python3` and `pip` installed on PC. (Install from https://python.org) 5 | 2. Have `git` installed. (Install from https://git-scm.com/downloads) 6 | 7 | ## Setup 8 | 1. `git clone` the repository 9 | ```sh 10 | git clone https://github.com/lil-zohee/SupportResistance.git 11 | ``` 12 | 2. Move in the new directory 13 | ```sh 14 | cd SupportResistance/ 15 | ``` 16 | 3. Install all necessary modules 17 | ```sh 18 | pip3 install -r requirements.txt 19 | ``` 20 | 4. Now you can run the program 21 | ```sh 22 | python3 support_resistance.py 23 | ``` 24 | In the python script, I tried to predict the support and resistance for AAPL stock from the dates of January 1, 2019 to April 1, 2019. However, you can change that in lines 51 and 52 of the program. 25 | ```python 26 | symbol = 'AAPL' 27 | df = web.DataReader(symbol, 'yahoo', '2019-01-01', '2019-04-01') 28 | ``` 29 | 30 | ## How it works 31 | The closing prices of the Bank Of America stock from January 1, 2019 to April 1, 2019 look like this. 32 |
33 |
34 |
50 |
51 |
54 |
55 |
60 |
61 |
86 |
87 |
90 |
91 |