Skip to main content

Support and Resistance Trend lines Calculator for Financial Analysis

Project description

trendln

Support and Resistance Trend lines Calculator for Financial Analysis

PyPI - Python Version PyPI - Version PyPI - Status PyPI - Downloads PyPI - License PyPI - Implementation GitHub stars

Note

This library can calculate and plot trend lines for any time series, not only for its primary intended purpose of financial analysis.

Changelog »


==> Check out this article on Programmatic Identification of Support/Resistance Trend lines with Python or alternatively here for details on how the library and its features are implemented and work.


Quick Start

Calculation Only

The calc_support_resistance function will calculate all support and resistance information including local extrema, average and their trend lines using several different methods:

import trendln
# this will serve as an example for security or index closing prices
import yfinance as yf # requires yfinance - pip install yfinance
tick = yf.Ticker('^GSPC') # S&P500
hist = tick.history(period="max", rounding=True)
minimaIdxs, maximaIdxs, pmin, pmax, mintrend, maxtrend, minwindows, maxwindows =
	calc_support_resistance(hist[-1000:].Close)
minimaIdxs, maximaIdxs, pmin, pmax, mintrend, maxtrend, minwindows, maxwindows =
	calc_support_resistance(
	# list of data as float
	hist,

	# METHOD_NAIVE - any local minima or maxima only for a single interval (currently requires pandas)
	# METHOD_NAIVECONSEC - any local minima or maxima including those for consecutive constant intervals (currently requires pandas)
	# METHOD_NUMDIFF (default) - numerical differentiation determined local minima or maxima (requires findiff)
	extmethod = METHOD_NUMDIFF,
	
	# METHOD_NCUBED - simple exhuastive 3 point search (slowest)
	# METHOD_NSQUREDLOGN (default) - 2 point sorted slope search (fast)
	# METHOD_HOUGHPOINTS - Hough line transform optimized for points
	# METHOD_HOUGHLINES - image-based Hough line transform (requires scikit-image)
	# METHOD_PROBHOUGH - image-based Probabilistic Hough line transform (requires scikit-image)
	method=METHOD_NSQUREDLOGN,
	
	# window size when searching for trend lines prior to merging together
	window=125,
	
	# maximum percentage slope standard error
	errpct = 0.005,
	
	# for all METHOD_*HOUGH*, the smallest unit increment for discretization e.g. cents/pennies 0.01
	hough_scale=0.01
	
	# only for METHOD_PROBHOUGH, number of iterations to run
	hough_prob_iter=10,
	
	# sort by area under wrong side of curve, otherwise sort by slope standard error
	sortError=False)
# minimaIdxs - sorted list of indexes to the local minima
# maximaIdxs - sorted list of indexes to the local maxima
# pmin - [slope, intercept] of average best fit line through all local minima points
# pmax - [slope, intercept] of average best fit line through all local maxima points
# mintrend - sorted list containing (points, result) for local minima trend lines
# maxtrend - sorted list containing (points, result) for local maxima trend lines
	# points - list of indexes to points in trend line
	# result - (slope, intercept, SSR, slopeErr, interceptErr, areaAvg)
		# slope - slope of best fit trend line
		# intercept - y-intercept of best fit trend line
		# SSR - sum of squares due to regression
		# slopeErr - standard error of slope
		# interceptErr - standard error of intercept
		# areaAvg - Reimann sum area of difference between best fit trend line
		#   and actual data points averaged per time unit
# minwindows - list of windows each containing mintrend for that window
# maxwindows - list of windows each containing maxtrend for that window

Plotting Calculations

The plot_support_resistance function will calculate and plot the average and top 2 support and resistance lines, along with marking extrema used with a maximum history length, and otherwise identical arguments to the calculation function.

fig = plot_support_resistance(hist[-1000:].Close) # requires matplotlib - pip install matplotlib
fig = plot_support_resistance(
	hist,
	xformatter = None, #x-axis data formatter turning numeric indexes to display output
	  # e.g. ticker.FuncFormatter(func) otherwise just display numeric indexes
	numbest = 2, #number of best support and best resistance lines to display
	fromwindows = True, #draw numbest best from each window, otherwise draw numbest across whole range
	pctbound = 0.1, # bound trend line based on this maximum percentage of the data range above the high or below the low
	extmethod = METHOD_NUMDIFF,
	method=METHOD_NSQUREDLOGN,
	window=125,
	errpct = 0.005,
	hough_prob_iter=10,
	sortError=False)
# fig - returns matplotlib.pyplot.gcf() or the current figure
plt.savefig('suppres.svg', format='svg')
plt.show()

fig = plot_sup_res_date(hist[-1000:].Close, hist[-1000:].index) #requires pandas
fig = plot_sup_res_date( #automatic date formatter based on US trading calendar
	hist,
	idx, #date index from pandas
	numbest = 2,
	fromwindows = True,
	pctbound = 0.1,
	extmethod = METHOD_NUMDIFF,
	method=METHOD_NSQUREDLOGN,
	window=125,
	errpct = 0.005,
	hough_scale=0.01,
	hough_prob_iter=10,
	sortError=False)

plot_sup_res_learn( #draw learning figures, included for reference material only
	curdir, #base output directory for png and svg images, will be saved in 'data' subfolder
	hist) #pandas DataFrame containing Close and date index

Example output of plotting support resistance

Installation

Install trendln using pip:

$ pip install trendln --upgrade --no-cache-dir

Install trendln using conda:

$ conda install -c GregoryMorse trendln

Installation sanity check:

import trendln
#requires yfinance library install, not a package requirement, but used to assist with sanity check
#pip install yfinance
directory = '.' # a 'data' folder will be created here if not existing to store images
trendln.test_sup_res(directory) #simple tests that all methods are executing correct, assertion or other error indicates problem

Requirements

  • Python >= 2.7, 3.4+
  • numpy >= 1.15
  • findiff >= 0.7.0 (if using default numerical differentiation method)
  • scikit-image >= 0.14.0 (if using image-based Hough line transform or its probabilistic variant)
  • pandas >= 0.23.1 (if using date plotting function, or using naive minima/maxima methods)
  • matplotlib >= 3.1.0 (if using any plotting function)

License

trendln is distributed under the MIT License. See the LICENSE file in the release for details.

Support

Any questions, issues or ideas can kindly be submitted for review.

Gregory Morse gregory.morse@live.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trendln-0.1.8.tar.gz (20.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page