Hi Andre,<br><br>Assuming that you want the exact point (date and value) where each crossing occurs, you'll need to interpolate where they cross.<br><br>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:<br>
<br><div style="margin-left: 40px;"><span style="font-family: courier new,monospace;">import numpy as np</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import matplotlib.pyplot as plt</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def main():</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    x = np.linspace(0, 2*np.pi, 20)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    y1 = np.sin(2*x)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    y2 = np.cos(x)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    crossings = find_crossings(x, y1, y2)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    cross_x, cross_y = crossings.T</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    plt.plot(x, y1, 'bx-')</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    plt.plot(x, y2, 'gx-')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    plt.plot(cross_x, cross_y, 'ro')</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    plt.show()</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def find_crossings(x, y1, y2):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    diff = np.diff(np.sign(y1 - y2))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    indicies, = np.nonzero(diff)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    crossings = [interpolate_crossing(i, x, y1, y2) for i in indicies]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    return np.array(crossings)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def interpolate_crossing(i, x, y1, y2):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    slope = (       (y1[i] - y2[i])</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">           / ((y2[i+1] - y2[i]) - (y1[i+1] - y1[i])))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    x = x[i] + slope * (x[i+1] - x[i])</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    y = y1[i] + slope * (y1[i+1] - y1[i])</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    return x, y</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">main()</span><br>
</div><br><img title="VXsqp.png" alt="VXsqp.png" src="http://i.imgur.com/VXsqp.png"><br><br><div class="gmail_quote">On Tue, Mar 1, 2011 at 10:07 AM, Andre Lopes <span dir="ltr"><<a href="mailto:lopes80andre@gmail.com" target="_blank">lopes80andre@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Hi,<br>
<br>
I'm new to Numpy. I'm doing some tests with some Stock Market Quotes<br>
<br>
My struggle right now is "how to get the values of the moving averages<br>
crosses", I send an image in attach to illustrate what I'm trying to<br>
get.<br>
<br>
I'm using the this computation to get when the moving averages<br>
crosses, but when I look at the graph, the values doesn't seem ok.<br>
<br>
[quote]<br>
# Get when the ma20 cross ma50<br>
equal = np.round(ma20,2)==np.round(ma50,2)<br>
dates_cross  = (dates[equal])<br>
prices_cross = (prices[equal])<br>
[/quote]<br>
<br>
<br>
The full code is this:<br>
[quote]<br>
# Modules<br>
import datetime<br>
import numpy as np<br>
import matplotlib.finance as finance<br>
import matplotlib.mlab as mlab<br>
import matplotlib.pyplot as plot<br>
<br>
# Define quote<br>
startdate = datetime.date(2008,10,1)<br>
today = enddate = datetime.date.today()<br>
ticker = 'uso'<br>
<br>
# Catch CSV<br>
fh = finance.fetch_historical_yahoo(ticker, startdate, enddate)<br>
<br>
# From CSV to REACARRAY<br>
r = mlab.csv2rec(fh); fh.close()<br>
# Order by Desc<br>
r.sort()<br>
<br>
<br>
### Methods Begin<br>
def moving_average(x, n, type='simple'):<br>
    """<br>
    compute an n period moving average.<br>
<br>
    type is 'simple' | 'exponential'<br>
<br>
    """<br>
    x = np.asarray(x)<br>
    if type=='simple':<br>
        weights = np.ones(n)<br>
    else:<br>
        weights = np.exp(np.linspace(-1., 0., n))<br>
<br>
    weights /= weights.sum()<br>
<br>
<br>
    a =  np.convolve(x, weights, mode='full')[:len(x)]<br>
    a[:n] = a[n]<br>
    return a<br>
### Methods End<br>
<br>
<br>
prices = r.adj_close<br>
dates = r.date<br>
ma20 = moving_average(prices, 20, type='simple')<br>
ma50 = moving_average(prices, 50, type='simple')<br>
<br>
# Get when the ma20 cross ma50<br>
equal = np.round(ma20,2)==np.round(ma50,2)<br>
dates_cross  = (dates[equal])<br>
prices_cross = (prices[equal])<br>
<br>
# Ver se a ma20 > ma50<br>
# ma20_greater_than_ma50 = np.round(ma20,2) > np.round(ma50,2)<br>
# dates_ma20_greater_than_ma50  = (dates[ma20_greater_than_ma50])<br>
# prices_ma20_greater_than_ma50 = (prices[ma20_greater_than_ma50])<br>
<br>
print dates_cross<br>
print prices_cross<br>
#print dates_ma20_greater_than_ma50<br>
#print prices_ma20_greater_than_ma50<br>
<br>
<br>
plot.plot(prices)<br>
plot.plot(ma20)<br>
plot.plot(ma50)<br>
plot.show()<br>
[/quote]<br>
<br>
Someone can give me some clues?<br>
<br>
Best Regards,<br>
<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br>