python - Matplotlib and Numpy Math -
i'm trying traction matplotlib , numpy not easy.
i'm doing mini project start dealing matplotlib , numpy i'm stuck...
here code:
# modules import datetime import numpy np import matplotlib.finance finance import matplotlib.mlab mlab import matplotlib.pyplot plot # define quote startdate = datetime.date(2010,10,1) today = enddate = datetime.date.today() ticker = 'uso' # catch csv fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # csv reacarray r = mlab.csv2rec(fh); fh.close() # order desc r.sort() ### methods begin def moving_average(x, n, type='simple'): """ compute n period moving average. type '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() = np.convolve(x, weights, mode='full')[:len(x)] a[:n] = a[n] return ### methods end prices = r.adj_close dates = r.date ma20 = moving_average(prices, 20, type='simple') ma50 = moving_average(prices, 50, type='simple') # when ma20 crosses ma50 equal = np.round(ma20,1)==np.round(ma50,1) dates_cross = (dates[equal]) prices_cross = (prices[equal]) # when ma20 > ma50 ma20_greater_than_ma50 = np.round(ma20,1) > np.round(ma50,1) dates_ma20_greater_than_ma50 = (dates[ma20_greater_than_ma50]) prices_ma20_greater_than_ma50 = (prices[ma20_greater_than_ma50]) print dates_ma20_greater_than_ma50 print prices_ma20_greater_than_ma50
now need this:
store price of "price_cross" see if 1 day after "ma20_greater_than_ma50" statment true, if true store price "price of 1 day after" "next price_cross" - "price of 1 day after" (price2 - price1) occurences
how can math , more important. how can traction matplotlib , numpy. books should buy?
give me clues.
best regards,
i agree josh, wanted add matplotlib gallery:
http://matplotlib.sourceforge.net/gallery.html
most of plots start off directly copying close want, , modifying fit needs. matplotlib gallery has many such examples.
Comments
Post a Comment