http://stackoverflow.com/questions/8587047/support-resistance-algorithm-technical-analysis
Has anyone translated this type of Java code into Quantopian?
http://stackoverflow.com/questions/8587047/support-resistance-algorithm-technical-analysis
Has anyone translated this type of Java code into Quantopian?
Trend lines
A trend line is a line among a number of so-called pivot points on a stock chart. As the name suggests, the line's trend portrays the trend of the price development. In the past, traders drew trend lines on paper; but, nowadays, we can let a computer draw it for us. In this tutorial, we shall resort to a very simple approach that is probably not very useful in real life, but it should clarify the principle well.
Time for action – drawing trend lines
Follow the ensuing steps to draw trend lines:
1. Determine the pivots: First, we need to determine the pivot points. We shall
pretend they are equal to the arithmetic mean of the high, low, and close price:
h, l, c = numpy.loadtxt('data.csv', delimiter=',', usecols=(4, 5,
6), unpack=True)
pivots = (h + l + c) / 3
print "Pivots", pivots
From the pivots, we can deduce the so-called resistance and support levels. The
support level is the lowest level at which the price rebounds. The resistance
level is the highest level at which the price bounces back. These are not natural
phenomena, mind you, they are merely estimates. Based on these estimates, it is
possible to draw support and resistance trend lines. We will define the daily spread
to be the difference of the high and low price.
2. Fit data to a line: Define a function to fit line to data to a line where y = at + b.
The function should return a and b. This is another opportunity to apply the lstsq
function of the NumPy linalg package. Rewrite the line equation to y = Ax, where
A = [t 1] and x = [a b]. Form A with the NumPy ones and vstack function:
def fit_line(t, y):
A = numpy.vstack([t, numpy.ones_like(t)]).))]).T
return numpy.linalg.lstsq(A, y)[0]
3. Determine the support and resistance levels: Assuming that support levels are one
daily spread below the pivots, and that resistance levels are one daily spread above
the pivots, fi t the support and resistance trend lines:
t = numpy.arange(len(c))
sa, sb = fit_line(t, pivots - (h - l))
ra, rb = fit_line(t, pivots + (h - l))
support = sa * t + sb
resistance = ra * t + rb
4. Analyze the bands: At this juncture, we have all the necessary informati on to
draw the trend lines, however, it is wise to check how many points fall between
the support and resistance levels. Obviously, if only a small percentage of the
data is between the trend lines, then this setup is of no use to us. Make up a
conditi on for points between the bands and select with the where functi on
based on the conditi on:
condition = (c > support) & (c < resistance)
print "Condition", condition
between_bands = numpy.where(condition)
These are the condition values:
Condition [False False True True True True True False False
True False False
False False False True False False False True True True True
False False True True True False True]
Double-check the values:
print support[between_bands]
print c[between_bands]
print resistance[between_bands]
The array returned by the where function has rank 2, so call the ravel functi on
before calling the len function:
between_bands = len(numpy.ravel(between_bands))))
print "Number points between bands", between_bands
print "Ratio between bands", float(between_bands)/len(c)
You will get the following result:
Number points between bands 15
Ratio between bands 0.5
As an extra bonus, we gained a predictive model. Extrapolate the next day resistance
and support levels:
print "Tomorrows support", sa * (t[-1] + 1) + sb
print "Tomorrows resistance", ra * (t[-1] + 1) + rb
This results in:
Tomorrows support 349.389157088
Tomorrows resistance 360.749340996
Another approach to fi gure out how many points are between the support and
resistance esti mates is to use [] and intersect1d. Defi ne selecti on criteria in the
[] operatpr and intersect the results with the intersect1d functi on.
a1 = c[c > support]
a2 = c[c < resistance]
print "Number of points between bands 2nd approach" ,len(numpy.
intersect1d(a1, a2))
Not surprisingly, we get:
Number of points between bands 2nd approach 15
For More Information:
www.packtpub.com/numpy-1-5-using-real-world-examplesbeginners-
guide/book
Chapter 3
[ 77 ]
5. Plot the bands: Once more, we will plot the results:
plot(t, c)
plot(t, support)
plot(t, resistance)
show()
In the preceding plot, we have the price data and the corresponding support and
resistance lines.
What just happened?
We drew trend lines without having to mess around with rulers, pencils, and paper charts.
We defi ned a functi on that can fi t data to a line with the NumPy vstack, ones, and lstsq
functi ons. We fi t the data in order to defi ne support and resistance trend lines. Then we
fi gured out how many points are within the support and resistance range. We did this using
two separate methods that produced the same result.
The first method used the where functi on with a Boolean condition. The second method
made use of the [] operator and the intersect1d function. The intersect1d functi on
returns an array of common elements from two arrays.