1import trendln
2# this will serve as an example for security or index closing prices, or low and high prices
3import yfinance as yf # requires yfinance - pip install yfinance
4tick = yf.Ticker('^GSPC') # S&P500
5hist = tick.history(period="max", rounding=True)
6mins, maxs = calc_support_resistance(hist[-1000:].Close)
7minimaIdxs, pmin, mintrend, minwindows = calc_support_resistance((hist[-1000:].Low, None)) #support only
8mins, maxs = calc_support_resistance((hist[-1000:].Low, hist[-1000:].High))
9(minimaIdxs, pmin, mintrend, minwindows), (maximaIdxs, pmax, maxtrend, maxwindows) = mins, maxs
10
11(minimaIdxs, pmin, mintrend, minwindows), (maximaIdxs, pmax, maxtrend, maxwindows) = \
12 calc_support_resistance(
13 # list/numpy ndarray/pandas Series of data as bool/int/float and if not a list also unsigned
14 # or 2-tuple (support, resistance) where support and resistance are 1-dimensional array-like or one or the other is None
15 # can calculate only support, only resistance, both for different data, or both for identical data
16 h,
17
18 # METHOD_NAIVE - any local minima or maxima only for a single interval (currently requires pandas)
19 # METHOD_NAIVECONSEC - any local minima or maxima including those for consecutive constant intervals (currently requires pandas)
20 # METHOD_NUMDIFF (default) - numerical differentiation determined local minima or maxima (requires findiff)
21 extmethod = METHOD_NUMDIFF,
22
23 # METHOD_NCUBED - simple exhuastive 3 point search (slowest)
24 # METHOD_NSQUREDLOGN (default) - 2 point sorted slope search (fast)
25 # METHOD_HOUGHPOINTS - Hough line transform optimized for points
26 # METHOD_HOUGHLINES - image-based Hough line transform (requires scikit-image)
27 # METHOD_PROBHOUGH - image-based Probabilistic Hough line transform (requires scikit-image)
28 method=METHOD_NSQUREDLOGN,
29
30 # window size when searching for trend lines prior to merging together
31 window=125,
32
33 # maximum percentage slope standard error
34 errpct = 0.005,
35
36 # for all METHOD_*HOUGH*, the smallest unit increment for discretization e.g. cents/pennies 0.01
37 hough_scale=0.01
38
39 # only for METHOD_PROBHOUGH, number of iterations to run
40 hough_prob_iter=10,
41
42 # sort by area under wrong side of curve, otherwise sort by slope standard error
43 sortError=False,
44
45 # accuracy if using METHOD_NUMDIFF for example 5-point stencil is accuracy=3
46 accuracy=1)
47# if h is a 2-tuple with one value as None, then a 2-tuple is not returned, but the appropriate tuple instead
48# minimaIdxs - sorted list of indexes to the local minima
49# pmin - [slope, intercept] of average best fit line through all local minima points
50# mintrend - sorted list containing (points, result) for local minima trend lines
51 # points - list of indexes to points in trend line
52 # result - (slope, intercept, SSR, slopeErr, interceptErr, areaAvg)
53 # slope - slope of best fit trend line
54 # intercept - y-intercept of best fit trend line
55 # SSR - sum of squares due to regression
56 # slopeErr - standard error of slope
57 # interceptErr - standard error of intercept
58 # areaAvg - Reimann sum area of difference between best fit trend line
59 # and actual data points averaged per time unit
60# minwindows - list of windows each containing mintrend for that window
61
62# maximaIdxs - sorted list of indexes to the local maxima
63# pmax - [slope, intercept] of average best fit line through all local maxima points
64# maxtrend - sorted list containing (points, result) for local maxima trend lines
65 #see for mintrend above
66# maxwindows - list of windows each containing maxtrend for that window
67