[Numpy-discussion] How to get the prices of Moving Averages Crosses?
Christopher Barker
Chris.Barker@noaa....
Tue Mar 1 13:17:02 CST 2011
On 3/1/11 10:55 AM, Andre Lopes wrote:
> I'm not okay with linear interpolation.
Well, odds are that the crossing point won't be exactly at a data point,
so you need to do SOME kind of interpolation.
You could use a higher order interpolation (cubic spline, etc): see the
interpolation routines in scipy.
However, for this use, perhaps rather than the crossing point, you could
use the next time after the crossing point as your metric.
What's appropriate depends entirely on your purpose.
Do note that that key here is that you need to find a point where the
difference changes sign -- it's not likely to be zero at any point.
One more thought -- you're looking at a moving average anyway -- with
that smoothing already in place linear interpolation is probably fine,
after all the exact crossing point is going to be a function of your
smoothing parameters.
You'll also want to think about what it means when the prices cross,
then cross right back at the next time step, or even match, then the one
that was higher goes back up...
-Chris
> Can you suggest me some books
> around this subject. I will mainly try to build some indicator for the
> stock market.
>
> If you can give me a clue I would be appreciated.
>
> Best Regards,
>
>
>
> On Tue, Mar 1, 2011 at 5:23 PM, Joe Kington <jkington@wisc.edu
> <mailto:jkington@wisc.edu>> wrote:
>
> Hi Andre,
>
> Assuming that you want the exact point (date and value) where each
> crossing occurs, you'll need to interpolate where they cross.
>
> There are a number of different ways to do so, but assuming you're
> okay with linear interpolation, and everything's sampled on the same
> dates, you can simply do something like this:
>
> import numpy as np
> import matplotlib.pyplot as plt
>
> def main():
> x = np.linspace(0, 2*np.pi, 20)
> y1 = np.sin(2*x)
> y2 = np.cos(x)
> crossings = find_crossings(x, y1, y2)
> cross_x, cross_y = crossings.T
> plt.plot(x, y1, 'bx-')
> plt.plot(x, y2, 'gx-')
> plt.plot(cross_x, cross_y, 'ro')
> plt.show()
>
> def find_crossings(x, y1, y2):
> diff = np.diff(np.sign(y1 - y2))
> indicies, = np.nonzero(diff)
> crossings = [interpolate_crossing(i, x, y1, y2) for i in indicies]
> return np.array(crossings)
>
> def interpolate_crossing(i, x, y1, y2):
> slope = ( (y1[i] - y2[i])
> / ((y2[i+1] - y2[i]) - (y1[i+1] - y1[i])))
> x = x[i] + slope * (x[i+1] - x[i])
> y = y1[i] + slope * (y1[i+1] - y1[i])
> return x, y
>
> main()
>
> VXsqp.png
>
> On Tue, Mar 1, 2011 at 10:07 AM, Andre Lopes <lopes80andre@gmail.com
> <mailto:lopes80andre@gmail.com>> wrote:
>
> Hi,
>
> I'm new to Numpy. I'm doing some tests with some Stock Market Quotes
>
> My struggle right now is "how to get the values of the moving
> averages
> crosses", I send an image in attach to illustrate what I'm trying to
> get.
>
> I'm using the this computation to get when the moving averages
> crosses, but when I look at the graph, the values doesn't seem ok.
>
> [quote]
> # Get when the ma20 cross ma50
> equal = np.round(ma20,2)==np.round(ma50,2)
> dates_cross = (dates[equal])
> prices_cross = (prices[equal])
> [/quote]
>
>
> The full code is this:
> [quote]
> # Modules
> import datetime
> import numpy as np
> import matplotlib.finance as finance
> import matplotlib.mlab as mlab
> import matplotlib.pyplot as plot
>
> # Define quote
> startdate = datetime.date(2008,10,1)
> today = enddate = datetime.date.today()
> ticker = 'uso'
>
> # Catch CSV
> fh = finance.fetch_historical_yahoo(ticker, startdate, enddate)
>
> # From CSV to REACARRAY
> r = mlab.csv2rec(fh); fh.close()
> # Order by Desc
> r.sort()
>
>
> ### Methods Begin
> def moving_average(x, n, type='simple'):
> """
> compute an n period moving average.
>
> type is 'simple' | 'exponential'
>
> """
> x = np.asarray(x)
> if type=='simple':
> weights = np.ones(n)
> else:
> weights = np.exp(np.linspace(-1., 0., n))
>
> weights /= weights.sum()
>
>
> a = np.convolve(x, weights, mode='full')[:len(x)]
> a[:n] = a[n]
> return a
> ### Methods End
>
>
> prices = r.adj_close
> dates = r.date
> ma20 = moving_average(prices, 20, type='simple')
> ma50 = moving_average(prices, 50, type='simple')
>
> # Get when the ma20 cross ma50
> equal = np.round(ma20,2)==np.round(ma50,2)
> dates_cross = (dates[equal])
> prices_cross = (prices[equal])
>
> # Ver se a ma20 > ma50
> # ma20_greater_than_ma50 = np.round(ma20,2) > np.round(ma50,2)
> # dates_ma20_greater_than_ma50 = (dates[ma20_greater_than_ma50])
> # prices_ma20_greater_than_ma50 = (prices[ma20_greater_than_ma50])
>
> print dates_cross
> print prices_cross
> #print dates_ma20_greater_than_ma50
> #print prices_ma20_greater_than_ma50
>
>
> plot.plot(prices)
> plot.plot(ma20)
> plot.plot(ma50)
> plot.show()
> [/quote]
>
> Someone can give me some clues?
>
> Best Regards,
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org <mailto:NumPy-Discussion@scipy.org>
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org <mailto:NumPy-Discussion@scipy.org>
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov
More information about the NumPy-Discussion
mailing list