Created
September 30, 2020 08:51
-
-
Save yuyasugano/6238fdd90cf8d0b68bc8075004ffe6c3 to your computer and use it in GitHub Desktop.
A support and resistence line sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def supres(ltp, n): | |
""" | |
This function takes a numpy array of last traded price | |
and returns a list of support and resistance levels | |
respectively. n is the number of entries to be scanned. | |
""" | |
from scipy.signal import savgol_filter as smooth | |
# converting n to a nearest even number | |
if n%2 != 0: | |
n += 1 | |
n_ltp = ltp.shape[0] # length of ltp | |
# smoothening the curve | |
# scipy.signal.savgol_filter(x, window_length, polyorder, deriv=0, delta=1.0, axis=- 1, mode='interp', cval=0.0) | |
# window_lengthint | |
# The length of the filter window (i.e., the number of coefficients). | |
# polyorderint | |
# The order of the polynomial used to fit the samples. | |
ltp_s = smooth(ltp, (n+1), 3) # the polynomial is 3 in this sample | |
print('length of ltp_s: {}'.format(len(ltp_s))) | |
# taking a simple derivative | |
ltp_d = np.zeros(n_ltp) | |
ltp_d[1:] = np.subtract(ltp_s[1:], ltp_s[:-1]) | |
print('length of ltp_d: {}'.format(len(ltp_d))) | |
resistance = [] | |
support = [] | |
for i in range(n_ltp - n): | |
arr_sl = ltp_d[i:(i+n)] | |
first = arr_sl[:int((n/2))] # first half | |
last = arr_sl[int((n/2)):] # second half | |
r_1 = np.sum(first > 0) | |
r_2 = np.sum(last < 0) | |
s_1 = np.sum(first < 0) | |
s_2 = np.sum(last > 0) | |
# local maxima detection | |
if (r_1 == (n/2)) and (r_2 == (n/2)): | |
resistance.append(ltp[i+(int(n/2)-1)]) | |
# local minima detection | |
if (s_1 == (n/2)) and (s_2 == (n/2)): | |
support.append(ltp[i+(int(n/2)-1)]) | |
return support, resistance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment