Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Support Resistance Algorithm - Technical analysis

Tags:

algorithm

I have an intra-day chart and I am trying to figure out how to calculate support and resistance levels, anyone knows an algorithm for doing that, or a good starting point?

like image 867
Yaron Avatar asked Dec 21 '11 08:12

Yaron


People also ask

How is support and resistance used in technical analysis?

Support is a price point below the current market price that indicate buying interest. Resistance is a price point above the current market price that indicate selling interest. S&R can be used to identify targets for the trade. For a long trade, look for the immediate resistance level as the target.

How do you find the support and resistance algorithm?

this is simple. Take the list of N turning points and compute the Y-distance between it and each of the other discovered turning points. If the distance is less than a fixed constant then you have found two "close" turning points, indicating possible support/resistance.


1 Answers

Yes, a very simple algorithm is to choose a timeframe, say 100 bars, then look for local turning points, or Maxima and Minima. Maxima and Minima can be computed from a smoothed closing price by using the 1st and second derivative (dy/dx and d^2y/dx). Where dy/dx = zero and d^y/dx is positive, you have a minima, when dy/dx = zero and d^2y/dx is negative, you have a maxima.

In practical terms this could be computed by iterating over your smoothed closing price series and looking at three adjacent points. If the points are lower/higher/lower in relative terms then you have a maxima, else higher/lower/higher you have a minima. You may wish to fine-tune this detection method to look at more points (say 5, 7) and only trigger if the edge points are a certain % away from the centre point. this is similar to the algorithm that the ZigZag indicator uses.

Once you have local maxima and minima, you then want to look for clusters of turning points within a certain distance of each other in the Y-Direction. this is simple. Take the list of N turning points and compute the Y-distance between it and each of the other discovered turning points. If the distance is less than a fixed constant then you have found two "close" turning points, indicating possible support/resistance.

You could then rank your S/R lines, so two turning points at $20 is less important than three turning points at $20 for instance.

An extension to this would be to compute trendlines. With the list of turning points discovered now take each point in turn and select two other points, trying to fit a straight line equation. If the equation is solvable within a certain error margin, you have a sloping trendline. If not, discard and move on to the next triplet of points.

The reason why you need three at a time to compute trendlines is any two points can be used in the straight line equation. Another way to compute trendlines would be to compute the straight line equation of all pairs of turning points, then see if a third point (or more than one) lies on the same straight line within a margin of error. If 1 or more other points does lie on this line, bingo you have calculated a Support/Resistance trendline.

I hope this helps. No code examples sorry, I'm just giving you some ideas on how it could be done. In summary:

Inputs to the system

  • Lookback period L (number of bars)
  • Closing prices for L bars
  • Smoothing factor (to smooth closing price)
  • Error Margin or Delta (minimum distance between turning points to constitute a match)

Outputs

  • List of turning points, call them tPoints[] (x,y)
  • List of potential trendlines, each with the line equation (y = mx + c)

EDIT: Update

I recently learned a very simple indicator called a Donchian Channel, which basically plots a channel of the highest high in 20 bars, and lowest low. It can be used to plot an approximate support resistance level. But the above - Donchian Channel with turning points is cooler ^_^

like image 101
Dr. Andrew Burnett-Thompson Avatar answered Nov 09 '22 07:11

Dr. Andrew Burnett-Thompson