def driptest(numlist): ''' input: a list of numbers (nums corrsp to stocks listed in tckrlist) output: the value of a portfolio containing stocks specified in numlist starting on 9/19/2000 and ending on 7/15/2013 divs reinvested at price on close of pay dates ''' #TODO add semi-annual contributions, invested evenly between all stocks, every 130 days cashperstock = 50000/len(numlist) #start with $50,000 dollars divided by number of stocks holdingslist = [] for i in numlist: #buy initial shares price = mf.parse_yahoo_historical(mf.fetch_historical_yahoo(tckrlist[i], (2000, 9, 19), (2000, 9, 19), dividends=False), adjusted=False) iprice = price[0][2] ishares = round(cashperstock/iprice, 2) holdingslist.append(ishares) for i in fulldatelist: #for every day in window hcount = 0 p = i.split('/') d = int(p[1]) yahoodate = (int(p[2]), int(p[0]), d) # a yahoo fetch compatible date hcount = 0 for j in numlist: #look at every stock in the numlist paycount = 0 icurrprice = 0 d = int(p[1]) yahoodate = (int(p[2]), int(p[0]), d) for k in paydatemaster[j]: #for each stock, look at payment date master list k = k.strip() if k == i: #check if current day is a dividend pay day paymentmaster[j][paycount] = float(paymentmaster[j][paycount]) divpaymt = round(paymentmaster[j][paycount]*holdingslist[hcount], 2) try: currprice = mf.parse_yahoo_historical(mf.fetch_historical_yahoo(tckrlist[j], yahoodate, yahoodate, dividends=False), adjusted=False) icurrprice = currprice[0][2] holdingslist[hcount] += round(divpaymt/icurrprice, 3) #reinvest, using yahoo data, tckrlist[j] except: #sometimes paydates are on the weekend, in which case the next available day's close price is used while icurrprice == 0: d+= 1 try: yahoodate = (int(p[2]), int(p[0]), d) currprice = mf.parse_yahoo_historical(mf.fetch_historical_yahoo(tckrlist[j], yahoodate, yahoodate, dividends=False), adjusted=False) icurrprice = currprice[0][2] holdingslist[hcount] += round(divpaymt/icurrprice, 3) except: pass paycount += 1 hcount += 1 finaldate = (2013, 7, 15) count = 0 value = 0 for i in numlist: finalprice = mf.parse_yahoo_historical(mf.fetch_historical_yahoo(tckrlist[i], finaldate, finaldate, dividends=False), adjusted=False) ifinalprice = finalprice[0][2] value += round(ifinalprice*holdingslist[count], 2) #calculate final value count += 1 return value
def getData(self): self.dataLoaded = 1 fh = finance.fetch_historical_yahoo(self.name, self.startdate, self.enddate) self.data = mlab.csv2rec(fh); fh.close() self.data.sort()
def stockpricemasterlist(tckrlist): stockpricedates = [] stockpricemaster = [] for j in tckrlist: temporlist = [] temporlist2 = [] price = mf.fetch_historical_yahoo(j, (2000, 9, 19), (2013, 7, 15), dividends=False) for i in price: try: i =i.split(',') i[0] = i[0].split('-') if i[0][1][0] == str(0): m = i[0][1][1] else: m = i[0][1] if i[0][2][0] == str(0): d = i[0][2][1] else: d = i[0][2] y = i[0][0] date = str(m)+'/'+str(d)+'/'+str(y) temporlist.append(date) temporlist2.append(i[4]) except: pass stockpricedates.append(temporlist) stockpricemaster.append(temporlist2) return stockpricedates, stockpricemaster
def main(): if len(sys.argv) != 3: print('Usage {}: <symbol-file> <destination-dir>'.format(sys.argv[0])) exit(1) symbols = utils.load_symbol_list(sys.argv[1]) base_dir = sys.argv[2] for symbol in symbols: dest_dir = os.path.join(base_dir, symbol + '.csv') try: finance.fetch_historical_yahoo(symbol, (2000, 1, 1), (2014, 9, 21), cachename=dest_dir) except urllib2.URLError: print('Download failed for symbol: {}'.format(symbol)) time.sleep(30)
def get_stock_history(self): """ Fetches data using matplotlib's yahoo API (undocumented). A call to fetch_historical_yahoo returns a CSV file containing tuples: [(date, open, close, high, low, volume),...] Parameters ---------- None Returns ------- dates: array of historical dates prices: array of associated prices on those dates """ stock_history = finance.fetch_historical_yahoo(self.symbol, self.start, self.end) r = mlab.csv2rec(stock_history) r.sort() dates = r.date prices = r.adj_close # convert to epoch time for highcharts dates = [(int(time.mktime(time.strptime(date.strftime("%Y-%m-%d"), "%Y-%m-%d"))) - time.timezone)*1000 for date in dates] return dates, prices
def ticker_data(ticker): print ticker startdate = datetime.date(2012, 10, 28) today = enddate = datetime.date.today() pricesd = np.zeros(1000) datesd = np.zeros(1000) volumed = np.zeros(1000) try: fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # a numpy record array with fields: date, open, high, low, close, volume, adj_close) r = mlab.csv2rec(fh) fh.close() r.sort() prices = r.adj_close dates = r.date volume = r.volume except: prices = pricesd dates = datesd volume = volumed print "Unexpected error:" # raise return prices, dates, np.mean(volume), volume
def plotTicker(ticker, startdate, enddate, fillcolor): """ matplotlib.finance has fetch_historical_yahoo() which fetches stock price data the url where it gets the data from is http://ichart.yahoo.com/table.csv stores in a numpy record array with fields: date, open, high, low, close, volume, adj_close """ fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) r = mlab.csv2rec(fh) fh.close() r.sort() ### plot the relative strength indicator prices = r.adj_close ### plot the price and volume data ax.plot(r.date, prices, color=fillcolor, lw=2, label=ticker) ax.legend(loc='top right', shadow=True, fancybox=True) # set the labels rotation and alignment for label in ax.get_xticklabels(): # To display date label slanting at 30 degrees label.set_rotation(30) label.set_horizontalalignment('right') ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
def get_ticker_data_from_web_stend(ticker, startdate, enddate): """ return a numpy record array with fields: date, open, high, low, close, volume, adj_close) """ fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) r = mlab.csv2rec(fh); fh.close() r.sort() return r
def get_ticker_data_from_web(ticker): """ return a numpy record array with fields: date, open, high, low, close, volume, adj_close) """ startdate = datetime.date(1990,1,1) enddate = datetime.date.today() fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) r = mlab.csv2rec(fh); fh.close() r.sort() return r
def download_ticker_data(ticker): fullpath = data_folder + '/' + ticker today = enddate = datetime.date.today() + datetime.timedelta(days=1) startdate = enddate - datetime.timedelta(days=180) try: fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) shutil.copy2(fh.name, fullpath) fh.close() return True, None except Exception, e: return False,str(e)
def main(stock='aapl', start_date=None, end_date=None, training=None, time_series=None, animate=None, **kwargs): training = training or 20 end_date = end_date or datetime.today() start_date = start_date or end_date - timedelta(days=int( (training > 1 and training) or 30)) stock_data = mlab.csv2rec( finance.fetch_historical_yahoo(stock, start_date, end_date)) stock_prices = tuple(reversed(stock_data.adj_close)) dates = tuple(reversed(stock_data.date)) training_set = stock_prices[:int(training > 1 and training or training * len(stock_data.adj_close))] animate = (animate in {'foreground', 'background'} and get_animated_training(dates, animate == 'background')) \ or train def _time_series(window_size): neural_network = train_as_time_series(training_set, window_size, animate, **kwargs) network_samples = sliding_window(stock_prices, window_size) predicted_prices = list(network_samples[0][0]) predicted_prices.extend( chain.from_iterable( neural_network.feed(inputs) for inputs, _ in network_samples)) plot(stock_prices, predicted_prices, dates[len(training_set) - 1], x_axis=dates, title='Trained as Time Series', errors=neural_network.errors) def interpolation(): neural_network = train_as_function_interpolation( training_set, animate, **kwargs) predicted_prices = list( chain.from_iterable( imap(neural_network.feed, xrange(len(stock_prices))))) plot(stock_prices, predicted_prices, dates[len(training_set) - 1], x_axis=dates, title='Trained as Function Interpolation', errors=neural_network.errors) if time_series: _time_series(time_series) else: interpolation()
def get_quote_daily_matplotlib(ticker): startdate = datetime.date(2006, 1, 1) today = enddate = datetime.date.today() #ticker = 'SPY' fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # a numpy record array with fields: date, open, high, low, close, volume, adj_close r = mlab.csv2rec(fh) fh.close() r.sort() return r
def get_history_data(self, ticker, start, end): try: fh = finance.fetch_historical_yahoo(ticker, start, end) # a numpy record array with fields: date, open, high, low, close, volume, adj_close) raw = mlab.csv2rec(fh) fh.close() raw.sort() # get today data if end == datetime.date.today(): raw = self.get_realtime_data(ticker, raw) except: return 0 return raw
def main( stock='aapl', start_date=None, end_date=None, training=None, time_series=None, animate=None, **kwargs ): training = training or 20 end_date = end_date or datetime.today() start_date = start_date or end_date - timedelta(days=int((training > 1 and training) or 30)) stock_data = mlab.csv2rec(finance.fetch_historical_yahoo(stock, start_date, end_date)) stock_prices = tuple(reversed(stock_data.adj_close)) dates = tuple(reversed(stock_data.date)) training_set = stock_prices[:int(training > 1 and training or training * len(stock_data.adj_close))] animate = (animate in {'foreground', 'background'} and get_animated_training(dates, animate == 'background')) \ or train def _time_series(window_size): neural_network = train_as_time_series(training_set, window_size, animate, **kwargs) network_samples = sliding_window(stock_prices, window_size) predicted_prices = list(network_samples[0][0]) predicted_prices.extend(chain.from_iterable(neural_network.feed(inputs) for inputs, _ in network_samples)) plot( stock_prices, predicted_prices, dates[len(training_set) - 1], x_axis=dates, title='Trained as Time Series', errors=neural_network.errors ) def interpolation(): neural_network = train_as_function_interpolation(training_set, animate, **kwargs) predicted_prices = list(chain.from_iterable(imap(neural_network.feed, xrange(len(stock_prices))))) plot( stock_prices, predicted_prices, dates[len(training_set) - 1], x_axis=dates, title='Trained as Function Interpolation', errors=neural_network.errors ) if time_series: _time_series(time_series) else: interpolation()
def get_stock(ticker, startdate, enddate): fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # a numpy record array with fields: (date, open, high, low, close, volume, adj_close) r = mlab.csv2rec(fh) fh.close() r.sort() print 'the length of data:', len(r.close) get_stock_data = [] for i in xrange(0, len(r.close) - 1): if (r.volume[i] != 0): get_stock_data.append(r.close[i].tolist()) print 'after removing the datas with zero volume, the length of data:', len( get_stock_data) return get_stock_data
def mr_stock_price(ticker): now = datetime.datetime.now() earlier = now - datetime.timedelta(days = 4) price = mf.fetch_historical_yahoo(ticker, (earlier.year, earlier.month, earlier.day), (now.year, now.month, now.day), dividends=False) for r in price: r = r.split(',') try: float(r[-1]) mr_price= float(r[-1].strip()) break # shareprices.append(float(r[-1].strip())) except: continue return mr_price
def get_data_from_yahoo_api(ticker, startdate=datetime.date(1900, 1, 1), enddate=datetime.date.today()): fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) r = mlab.csv2rec(fh) fh.close() df = pd.DataFrame(data=r, columns=[ 'date', 'open', 'high', 'low', 'close', 'volume', 'adj_close' ]).sort('date') df['volume'] = df['volume'].astype(float) del df['adj_close'] print 'Available data: Ticker({ticker}) from {first_day} to {last_day}'.format( ticker=ticker, first_day=firstday(df), last_day=lastday(df)) return df, r
def get_benchmark(tick,y1,m1,d1,y2,m2,d2): startdate = datetime.date(y1, m1, d1) enddate = datetime.date(y2, m2, d2) ticker= tick benchmark_df= read_csv(finance.fetch_historical_yahoo(ticker, startdate, enddate),index_col='Date') benchmark_Index=benchmark_df['Close'] benchmark_Index=benchmark_Index.to_frame() benchmark_Index.columns=['values'] benchmark_Index=benchmark_Index.reset_index() benchmark_Index["New Date"]=benchmark_Index["Date"].map(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d').strftime('%d/%m/%y')) benchmark_Index["Date"]=benchmark_Index["New Date"].map(lambda x: datetime.datetime.strptime(x, '%d/%m/%y')) benchmark_Index=benchmark_Index.iloc[::-1] benchmark_Index=benchmark_Index.drop(["New Date"],1) benchmark_Index=benchmark_Index.set_index(["Date"]) benchmark_Index=benchmark_Index.divide(benchmark_Index.ix[0]) return benchmark_Index
def recuperarDados(ticker, startdate=None, enddate=None): if startdate is None and enddate is None: records = np.load(dadosPathMask % (ticker) + ".npy") else: if startdate is None: startdate = datetime.date(2007, 1, 1) if enddate is None: enddate = datetime.date.today() with finance.fetch_historical_yahoo(ticker, startdate, enddate) as fh: records = mlab.csv2rec(fh) records.sort() np.save(dadosPathMask % (ticker), records) return records
def plotSingleTickerWithVolume(ticker, startdate, enddate): global ax fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # a numpy record array with fields: # date, open, high, low, close, volume, adj_close r = mlab.csv2rec(fh) fh.close() r.sort() plt.rc('axes', grid=True) plt.rc('grid', color='0.78', linestyle='-', linewidth=0.5) axt = ax.twinx() prices = r.adj_close fcolor = 'darkgoldenrod' ax.plot(r.date, prices, color=r'#1066ee', lw=2, label=ticker) ax.fill_between(r.date, prices, 0, prices, facecolor='#BBD7E5') ax.set_ylim(0.5 * prices.max()) ax.legend(loc='upper right', shadow=True, fancybox=True) volume = (r.close * r.volume) / 1e6 # dollar volume in millions vmax = volume.max() axt.fill_between(r.date, volume, 0, label='Volume', facecolor=fcolor, edgecolor=fcolor) axt.set_ylim(0, 5 * vmax) axt.set_yticks([]) for axis in ax, axt: for label in axis.get_xticklabels(): label.set_rotation(30) label.set_horizontalalignment('right') axis.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
def _fetch_yahoo_data(self,startdate,enddate,dividends): ticker=self.full_ticker try: fh = finance.fetch_historical_yahoo(ticker, startdate, enddate,None,dividends) # From CSV to REACARRAY r = mlab.csv2rec(fh); fh.close() # Order by Desc r.sort() except: print "Error: %s,%s,%s" % (sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]) print "Error loading yahoo data for %s %s" % (self,str(startdate)) return None return r
def hist_data_collection(): conn = MySQLdb.connect(host = "127.0.0.1", user = "******", passwd = "sshravan91", db = "test1") c = conn.cursor(MySQLdb.cursors.DictCursor) c.execute("SELECT DISTINCT(Symbol) AS SYMBOL FROM HISTORY") symbol_data = c.fetchall() for var2 in xrange(c.rowcount): tempvar = symbol_data[var2]['SYMBOL'] print tempvar symbolslist.append(tempvar.lower()) c.close() del c conn.close() today = enddate = date.today() startdate = today - timedelta(1) conn = MySQLdb.connect(host = "127.0.0.1", user = "******", passwd = "sshravan91", db = "test1") for j in xrange(len(symbolslist)): ticker = str(symbolslist[j]) fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) r = mlab.csv2rec(fh); fh.close() r.sort() #print r query = "INSERT INTO HIST_STOCK (_DATE, NAME, OPEN, HIGH, LOW, CLOSE, VOLUME, ADJ_CLOSE) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)" x = conn.cursor() x.execute(query, (str(r.date[0]), ticker, str(r.open[0]), str(r.high[0]), str(r.low[0]), str(r.close[0]), str(r.volume[0]), str(r.adj_close[0]))) conn.commit() row = x.fetchall() x.close() del x conn.close()
def _fetch_yahoo_data(self, startdate, enddate, dividends): ticker = self.full_ticker try: fh = finance.fetch_historical_yahoo(ticker, startdate, enddate, None, dividends) # From CSV to REACARRAY r = mlab.csv2rec(fh) fh.close() # Order by Desc r.sort() except: print "Error: %s,%s,%s" % (sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) print "Error loading yahoo data for %s %s" % (self, str(startdate)) return None return r
def update_ticker_prices(): for ticker, in db.session.query(Securities.ticker).all(): data = fetch_historical_yahoo(ticker, datetime.now() - timedelta(days=7), datetime.now()) reader = csv.DictReader(data) for row in reader: new_record = SecurityPrices() for key in row: row[key.lower().replace('adj close', 'adjusted_close')] = row.pop(key) for column in inspect(SecurityPrices).attrs: if column.key == 'id': continue elif column.key == 'ticker': setattr(new_record, column.key, ticker) else: setattr(new_record, column.key, row[column.key]) try: db.session.add(new_record) db.session.commit() except IntegrityError: db.session.rollback()
def fetchStockData(cls, beg, end, ffile, yahoo=True, ticker="AAPL"): """Depending on the boolean parameter yahoo, it will fetch the stock data from yahoo finance, or from the file, given that the file contains date columns and price columns where ticker name is the header for prices for that security.""" if yahoo is True: p = fetch_historical_yahoo(ticker, beg, end) result = parse_yahoo_historical_ochl(p, False, True) return result else: result = os.path.exists(ffile) if result is False: raise FileNotFoundError("Error: {} file was not found.".format(ffile)) f = pd.read_csv(ffile) max = len(f.columns) for i in range(1, max): cur_name = f.columns.values[i] if cur_name == ticker: break prices = f.values[:, i] # f.values and f.iloc have actual data starting from 0 index. First is row. max_rows = len(f.values[:, 0]) """date, year, month, day, d, open, close, high, low, volume, adjusted_close""" results = [] for j in range(0, max_rows): dt = datetime.date(*[int(val) for val in f.values[j, 0].split('-')]) results.append((dt, 0, 0, 0, 0, 0, float(prices[j]), 0, 0, 0, float(prices[j]))) my_type = np.dtype( [(str('date'), object), (str('year'), np.int16), (str('month'), np.int8), (str('day'), np.int8), (str('d'), np.float), (str('open'), np.float), (str('close'), np.float), (str('high'), np.float), (str('low'), np.float), (str('volume'), np.float), (str('aclose'), np.float)]) d = np.array(results, dtype=my_type) return d.view(np.recarray) return result
def get_benchmark(tick, y1, m1, d1, y2, m2, d2): startdate = datetime.date(y1, m1, d1) enddate = datetime.date(y2, m2, d2) ticker = tick benchmark_df = read_csv(finance.fetch_historical_yahoo( ticker, startdate, enddate), index_col='Date') benchmark_Index = benchmark_df['Close'] benchmark_Index = benchmark_Index.to_frame() benchmark_Index.columns = ['values'] benchmark_Index = benchmark_Index.reset_index() benchmark_Index["New Date"] = benchmark_Index["Date"].map( lambda x: datetime.datetime.strptime(x, '%Y-%m-%d').strftime('%d/%m/%y' )) benchmark_Index["Date"] = benchmark_Index["New Date"].map( lambda x: datetime.datetime.strptime(x, '%d/%m/%y')) benchmark_Index = benchmark_Index.iloc[::-1] benchmark_Index = benchmark_Index.drop(["New Date"], 1) benchmark_Index = benchmark_Index.set_index(["Date"]) benchmark_Index = benchmark_Index.divide(benchmark_Index.ix[0]) return benchmark_Index
def div(stock_symbol): today = date.today() start = (today.year - year_to_cal, today.month, today.day) a = fetch_historical_yahoo(stock_symbol, start, today, cachename=None,dividends=True) dd = pd.DataFrame(list(a)) re1='.*?' # Non-greedy match on filler re2='([+-]?\\d*\\.\\d+)(?![-+0-9\\.])' # Float 1 rg = re.compile(re1+re2,re.IGNORECASE|re.DOTALL) sum = 0 #print dd[0][2] for i in xrange(dd.shape[0]): m = rg.search(dd[0][i]) if m: if m.group(1) == 'None': pass else: sum += float(m.group(1)) #print "("+float1+")"+"\n" return sum/year_to_cal
def download_ticker_US(ticker, sd=startdate, ed=enddate, market=''): fh = finance.fetch_historical_yahoo(ticker, sd, ed) # archive to disk # append to the existing files without the header # otherwise create it with the header f = 'data/' + market + '/' + ticker + '.txt' if not os.path.exists(f): fhWrite = open(f, 'w') header = fh.readline() fhWrite.write(header) else: fhWrite = open(f, 'a') # get the content, ignore the header fh.readline() r = fh.read() if r: # sort the records based on the dates r = '\r\n'.join(sorted(r.split())) fhWrite.write(r) # fhWrite.write('\r\n') fhWrite.close()
def download_ticker(ticker, sd=startdate, ed=enddate): """Download stocks open/close/high/close/volume from ASX and push to the central csv""" fh = finance.fetch_historical_yahoo(ticker, sd, ed) # archive to disk # append to the existing files without the header # otherwise create it with the header f = 'data/' + ticker + '.txt' if not os.path.exists(f): fhWrite = open(f, 'w') header = fh.readline() fhWrite.write(header) else: fhWrite = open(f, 'a') # get the content, ignore the header fh.readline() r = fh.read() r = '\r\n'.join(sorted(r.split())) fhWrite.write(r) fhWrite.write('\r\n') fhWrite.close()
def update_ticker_prices(): for ticker, in db.session.query(Securities.ticker).all(): start_date = datetime.now() - timedelta(days=7) end_date = datetime.now() data = fetch_historical_yahoo(ticker, start_date, end_date) reader = csv.DictReader(data) for row in reader: new_record = SecurityPrices() for key in list(row.keys()): key_name = key.lower().replace('adj close', 'adjusted_close') row[key_name] = row.pop(key) for column in inspect(SecurityPrices).attrs: if column.key == 'id': continue elif column.key == 'ticker': setattr(new_record, column.key, ticker) else: setattr(new_record, column.key, row[column.key]) try: db.session.add(new_record) db.session.commit() except IntegrityError: db.session.rollback()
def hist_data_collection(ticker_up): ticker=ticker_up.lower() today = enddate = date.today() startdate = datetime.date(2000,1,1) conn = MySQLdb.connect(host = "127.0.0.1", user = "******", passwd = "sshravan91", db = "test1") fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) r = mlab.csv2rec(fh); fh.close() r.sort() for j in xrange(len(r)): query = "INSERT INTO HIST_STOCK (_DATE, NAME, OPEN, HIGH, LOW, CLOSE, VOLUME, ADJ_CLOSE) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)" x = conn.cursor() x.execute(query, (str(r.date[j]), ticker.upper(), str(r.open[j]), str(r.high[j]), str(r.low[j]), str(r.close[j]), str(r.volume[j]), str(r.adj_close[j]))) conn.commit() row = x.fetchall() x.close() del x conn.close()
def getDataFromYahoo(self, symbol, startDate): hist = fi.fetch_historical_yahoo(symbol, startDate, datetime.today()) newEntries = [] # new container for i in hist: # split each line newEntries.append(i[:-1].replace("-","/").split(',')) try: col = newEntries.pop(0) for i in newEntries: # convert i[0] in datetime i[0] = datetime.strptime(i[0], '%Y/%m/%d') data = pd.DataFrame(newEntries, columns = col) except ValueError as e: logging.error('error in data set') return data
def ticker_data(ticker): print ticker startdate = datetime.date(2012,10,28) today = enddate = datetime.date.today() pricesd = np.zeros(1000) datesd = np.zeros(1000) volumed = np.zeros(1000) try: fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # a numpy record array with fields: date, open, high, low, close, volume, adj_close) r = mlab.csv2rec(fh); fh.close() r.sort() prices = r.adj_close dates = r.date volume = r.volume except: prices = pricesd dates = datesd volume = volumed print "Unexpected error:" # raise return prices, dates, np.mean(volume), volume
def weekendDiff(): ''' exercise. This function was in the handouts and looked very interesting. So I implemented it to see the inner workings. purpose of this function is to see weekend effect on the prices. :return: ''' tickerName = 'CSCO' begdate = datetime.date(2014, 1, 1) enddate = datetime.date.today() price = finance.fetch_historical_yahoo(tickerName, begdate, enddate) r = mlab.csv2rec(price) price.close() r.sort() r = r[-30:] # get the last 30 days fig, ax = plt.subplots() ax.plot(r.date, r.adj_close, 'o-', color='steelblue') ax.set_title('Normal Representation:' + tickerName + ' Plot with Weekend gaps') fig.autofmt_xdate() N = len(r) ind = np.arange(N) # the evenly spaced plot indices def formatDate(x, pos=None): thisind = np.clip(int(x + 0.5), 0, N - 1) return r.date[thisind].strftime('%Y-%m-%d') fig, ax = plt.subplots() ax.plot(ind, r.adj_close, 'd-', color='lightsalmon', lw=2) ax.set_title('New Representation:' + tickerName + ' Evenlyspaced out points') ax.xaxis.set_major_formatter(ticker.FuncFormatter(formatDate)) fig.autofmt_xdate() plt.show()
import matplotlib.finance as finance import matplotlib.dates as mdates import matplotlib.ticker as mticker import matplotlib.mlab as mlab import matplotlib.pyplot as plt import matplotlib.font_manager as font_manager stockArray = [[datetime.date(2006,1,1), datetime.date.today(), 'IBM'],[datetime.date(2006,1,1), datetime.date.today(), 'SPY']] startdate = datetime.date(2006,1,1) today = enddate = datetime.date.today() ticker = 'IBM' fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # a numpy record array with fields: date, open, high, low, close, volume, adj_close) r = mlab.csv2rec(fh); fh.close() r.sort() print r fh2 = finance.fetch_historical_yahoo('AAPL', startdate, enddate) r2 = mlab.csv2rec(fh2); fh2.close() r2.sort() def moving_average(x, n, type='simple'): """ compute an n period moving average. type is 'simple' | 'exponential'
def FinancialCharts(): import datetime import numpy as np import matplotlib.colors as colors import matplotlib.finance as finance import matplotlib.dates as mdates import matplotlib.ticker as mticker import matplotlib.mlab as mlab import matplotlib.pyplot as plt import matplotlib.font_manager as font_manager startdate = datetime.date(2006,1,1) today = enddate = datetime.date.today() ticker = 'SPY' fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # a numpy record array with fields: date, open, high, low, close, volume, adj_close) r = mlab.csv2rec(fh); fh.close() r.sort() 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 def relative_strength(prices, n=14): """ compute the n period relative strength indicator http://stockcharts.com/school/doku.php?id=chart_school:glossary_r#relativestrengthindex http://www.investopedia.com/terms/r/rsi.asp """ deltas = np.diff(prices) seed = deltas[:n+1] up = seed[seed>=0].sum()/n down = -seed[seed<0].sum()/n rs = up/down rsi = np.zeros_like(prices) rsi[:n] = 100. - 100./(1.+rs) for i in range(n, len(prices)): delta = deltas[i-1] # cause the diff is 1 shorter if delta>0: upval = delta downval = 0. else: upval = 0. downval = -delta up = (up*(n-1) + upval)/n down = (down*(n-1) + downval)/n rs = up/down rsi[i] = 100. - 100./(1.+rs) return rsi def moving_average_convergence(x, nslow=26, nfast=12): """ compute the MACD (Moving Average Convergence/Divergence) using a fast and slow exponential moving avg' return value is emaslow, emafast, macd which are len(x) arrays """ emaslow = moving_average(x, nslow, type='exponential') emafast = moving_average(x, nfast, type='exponential') return emaslow, emafast, emafast - emaslow plt.rc('axes', grid=True) plt.rc('grid', color='0.75', linestyle='-', linewidth=0.5) textsize = 9 left, width = 0.1, 0.8 rect1 = [left, 0.7, width, 0.2] rect2 = [left, 0.3, width, 0.4] rect3 = [left, 0.1, width, 0.2] fig = plt.figure(facecolor='white') axescolor = '#f6f6f6' # the axes background color ax1 = fig.add_axes(rect1, axisbg=axescolor) #left, bottom, width, height ax2 = fig.add_axes(rect2, axisbg=axescolor, sharex=ax1) ax2t = ax2.twinx() ax3 = fig.add_axes(rect3, axisbg=axescolor, sharex=ax1) ### plot the relative strength indicator prices = r.adj_close rsi = relative_strength(prices) fillcolor = 'darkgoldenrod' ax1.plot(r.date, rsi, color=fillcolor) ax1.axhline(70, color=fillcolor) ax1.axhline(30, color=fillcolor) ax1.fill_between(r.date, rsi, 70, where=(rsi>=70), facecolor=fillcolor, edgecolor=fillcolor) ax1.fill_between(r.date, rsi, 30, where=(rsi<=30), facecolor=fillcolor, edgecolor=fillcolor) ax1.text(0.6, 0.9, '>70 = overbought', va='top', transform=ax1.transAxes, fontsize=textsize) ax1.text(0.6, 0.1, '<30 = oversold', transform=ax1.transAxes, fontsize=textsize) ax1.set_ylim(0, 100) ax1.set_yticks([30,70]) ax1.text(0.025, 0.95, 'RSI (14)', va='top', transform=ax1.transAxes, fontsize=textsize) ax1.set_title('%s daily'%ticker) ### plot the price and volume data dx = r.adj_close - r.close low = r.low + dx high = r.high + dx deltas = np.zeros_like(prices) deltas[1:] = np.diff(prices) up = deltas>0 ax2.vlines(r.date[up], low[up], high[up], color='black', label='_nolegend_') ax2.vlines(r.date[~up], low[~up], high[~up], color='black', label='_nolegend_') ma20 = moving_average(prices, 20, type='simple') ma200 = moving_average(prices, 200, type='simple') linema20, = ax2.plot(r.date, ma20, color='blue', lw=2, label='MA (20)') linema200, = ax2.plot(r.date, ma200, color='red', lw=2, label='MA (200)') last = r[-1] s = '%s O:%1.2f H:%1.2f L:%1.2f C:%1.2f, V:%1.1fM Chg:%+1.2f' % ( today.strftime('%d-%b-%Y'), last.open, last.high, last.low, last.close, last.volume*1e-6, last.close-last.open ) t4 = ax2.text(0.3, 0.9, s, transform=ax2.transAxes, fontsize=textsize) props = font_manager.FontProperties(size=10) leg = ax2.legend(loc='center left', shadow=True, fancybox=True, prop=props) leg.get_frame().set_alpha(0.5) volume = (r.close*r.volume)/1e6 # dollar volume in millions vmax = volume.max() poly = ax2t.fill_between(r.date, volume, 0, label='Volume', facecolor=fillcolor, edgecolor=fillcolor) ax2t.set_ylim(0, 5*vmax) ax2t.set_yticks([]) ### compute the MACD indicator fillcolor = 'darkslategrey' nslow = 26 nfast = 12 nema = 9 emaslow, emafast, macd = moving_average_convergence(prices, nslow=nslow, nfast=nfast) ema9 = moving_average(macd, nema, type='exponential') ax3.plot(r.date, macd, color='black', lw=2) ax3.plot(r.date, ema9, color='blue', lw=1) ax3.fill_between(r.date, macd-ema9, 0, alpha=0.5, facecolor=fillcolor, edgecolor=fillcolor) ax3.text(0.025, 0.95, 'MACD (%d, %d, %d)'%(nfast, nslow, nema), va='top', transform=ax3.transAxes, fontsize=textsize) #ax3.set_yticks([]) # turn off upper axis tick labels, rotate the lower ones, etc for ax in ax1, ax2, ax2t, ax3: if ax!=ax3: for label in ax.get_xticklabels(): label.set_visible(False) else: for label in ax.get_xticklabels(): label.set_rotation(30) label.set_horizontalalignment('right') ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d') class MyLocator(mticker.MaxNLocator): def __init__(self, *args, **kwargs): mticker.MaxNLocator.__init__(self, *args, **kwargs) def __call__(self, *args, **kwargs): return mticker.MaxNLocator.__call__(self, *args, **kwargs) # at most 5 ticks, pruning the upper and lower so they don't overlap # with other ticks #ax2.yaxis.set_major_locator(mticker.MaxNLocator(5, prune='both')) #ax3.yaxis.set_major_locator(mticker.MaxNLocator(5, prune='both')) ax2.yaxis.set_major_locator(MyLocator(5, prune='both')) ax3.yaxis.set_major_locator(MyLocator(5, prune='both')) plt.show()
""" Name : 4375OS_07_15_Yahoo_price.py Book : Python for Finance Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 12/26/2013 email : [email protected] [email protected] """ import datetime import matplotlib.finance as finance import matplotlib.mlab as mlab ticker = 'IBM' begdate = datetime.date(2013,1,1) enddate = datetime.date.today() price = finance.fetch_historical_yahoo(ticker, begdate, enddate) r = mlab.csv2rec(price); price.close() r.sort()
def gen_data_vector(ticker, threeavg, divmonths): try: if os.path.isdir('/home/ubuntu/dgrpred/dump/temp_persistence/'+ticker):# if it's already there, skip it return None os.mkdir('/home/ubuntu/dgrpred/dump/temp_persistence/'+ticker) fredobjs = joblib.load('/home/ubuntu/dgrpred/dump/fred_obj_temp_dump') divmonth = divmonths[ticker] print '*********************' print ticker, divmonth if divmonth < 10: divmonth = '0'+str(divmonth) else: divmonth = str(divmonth) oldcsvs = os.listdir('/home/ubuntu/dgrpred/old_csv/') olddata = {} for i in oldcsvs: # print i olddata[i.split('.')[0]] = readcsv(i, False) url = 'http://financials.morningstar.com/ajax/exportKR2CSV.html?&callback=?&t='+ticker.upper()+'®ion=usa&culture=en-US&cur=USD&order=asc' opener = urllib2.build_opener() f = opener.open(url) data = f.read() with open('/home/ubuntu/dgrpred/new_csv/'+ticker.replace('/', '').upper()+'.csv', 'wb') as code: code.write(data) content = readcsv('/home/ubuntu/dgrpred/new_csv/'+ticker.upper()+'.csv', True) ##################fill in downloaded data with old data if new data is missing for i in content: for j in content[i]: if content[i][j] == '' or content[i][j] == '--': try: content[i][j] = olddata[ticker.upper()][i][j] except: pass # for i in olddata[ticker.upper()]: # print i, olddata[ticker.upper()][i] # print # exit() years = [] fye = [] for i in content['Profitability']: years.append(int(i.split('-')[0])) fye.append(int(i.split('-')[1])) years.sort() fye = list(set(fye))[0] if fye < 10: fye = '0'+str(fye) else: fye = str(fye) count = 0 content_cleaned = copy.deepcopy(content) for measure in content: # print measure for year in content[measure]: if content[measure][year] == '': del content_cleaned[measure] break else: try: content[measure][year] = float(content[measure][year]) except: del content_cleaned[measure] break print '---' # for measure in content_cleaned: # print measure # for year in content_cleaned[measure]: # print content_cleaned[measure][year] content = content_cleaned # exit() content['shareprices'] = {} count = 0 for x in range(int(years[0]), int(years[-1])+1): dd = 8 while calendar.weekday(x, 8, dd) >= 5: dd +=1 price = mf.fetch_historical_yahoo(ticker, (x, 8, dd), (x, 8, dd), dividends=False) for r in price: r = r.split(',') try: float(r[-1]) content['shareprices'][str(years[count])+'-'+str(fye)] = float(r[-1].strip()) # shareprices.append(float(r[-1].strip())) except: continue count += 1 content['divyoy'] = {} divs = [] for i in years: divs.append(content['Dividends USD'][str(i)+'-'+str(fye)]) divdelta= calc_delta(divs) count = 1 for i in divdelta: content['divyoy'][str(years[count])+'-'+str(fye)] = i count += 1 for i in content: for j in content[i]: if content[i][j] == '': print i, j cogs = [] # cogs_prior = [] count = 1 for i in years[1:]: print i eps_prior = float(content['Earnings Per Share USD'][str(years[count-1])+'-'+fye]) # print content['rev-Year over Year'][str(years[count-1])+'-'+fye] rev_prior = float(content['rev-Year over Year'][str(years[count-1])+'-'+fye]) roe_prior = float(content['Return on Equity %'][str(years[count-1])+'-'+fye]) cog = round((rev_prior/roe_prior)*eps_prior, 5) cogs_prior.append(cog) eps = float(content['Earnings Per Share USD'][str(years[count])+'-'+fye]) rev = float(content['rev-Year over Year'][str(years[count])+'-'+fye]) roe = float(content['Return on Equity %'][str(years[count])+'-'+fye]) cog = round((rev/roe)*eps, 5) cogs.append(cog) # count += 1 count = 1 fgs = [] # fgs_prior = [] for i in cogs_prior: fg = round((float(content['Earnings Per Share USD'][str(years[count-1])+'-'+fye]) - float(content['Dividends USD'][str(years[count-1])+'-'+fye]) - i), 5) fgs_prior.append(fg) fg = round((float(content['Earnings Per Share USD'][str(years[count])+'-'+fye]) - float(content['Dividends USD'][str(years[count-1])+'-'+fye]) - cogs[count-1]), 5) fgs.append(fg) count += 1 sc = [] # sc_prior = [] count = 0 print content['shareprices'] for i in fgs: sharep = float(content['shareprices'][str(years[count])+'-'+str(fye)]) t = round((i/sharep)*100, 5) sc.append(t) t = round((fgs_prior[count]/sharep)*100, 5) sc_prior.append(t) count += 1 pred = [] # pred_prior = [] count = 1 for i in sc: rev = float(content['rev-Year over Year'][str(years[count])+'-'+fye]) t = i + rev pred.append(t) rev_prior = float(content['rev-Year over Year'][str(years[count-1])+'-'+fye]) t = sc_prior[count-1] + rev_prior print sc_prior[count-1], rev_prior pred_prior.append(t) count += 1 x = [] y = [] count = 1 # feature_labels = ['eps_prior', 'eps_future', 'payoutratio_prior', 'divs_prior', 'cogs_prior', 'cogs_future', 'sharechange_prior', 'sharechange_future', 'funding_gap_prior', 'funding_gap_future', 'prediction_prior', 'prediction_future', 'revYOY_prior', 'revYOY_future', 'roe_prior', 'roe_future', 'shareprice_prior', 'three_yr_trailing_dgr', 'NAPMIImean', 'NAPMIIslope', 'CUUR0000SA0L2mean', 'CUUR0000SA0L2slope', 'IPFINALmean', 'IPFINALslope', 'CUUR0000SADmean', 'CUUR0000SADslope', 'HOUSTNEmean', 'HOUSTNEslope', 'MHHNGSPmean', 'MHHNGSPslope', 'WJFUELUSGULFmean', 'WJFUELUSGULFslope', 'CES1021000001mean', 'CES1021000001slope', 'PRRESCONSmean', 'PRRESCONSslope', 'PNRESCONSmean', 'PNRESCONSslope', 'DDURRG3M086SBEAmean', 'DDURRG3M086SBEAslope', 'DSERRG3M086SBEAmean', 'DSERRG3M086SBEAslope', 'IPNCONGDmean', 'IPNCONGDslope', 'IPCONGDmean', 'IPCONGDslope', 'HOUSTMWmean', 'HOUSTMWslope', 'USGOODmean', 'USGOODslope', 'ACOGNOmean', 'ACOGNOslope', 'PPICMMmean', 'PPICMMslope', 'NONREVSLmean', 'NONREVSLslope', 'IPFUELSmean', 'IPFUELSslope', 'IPMATmean', 'IPMATslope', 'BAAFFMmean', 'BAAFFMslope', 'M2REALmean', 'M2REALslope', 'IPDCONGDmean', 'IPDCONGDslope', 'IPBUSEQmean', 'IPBUSEQslope', 'USTRADEmean', 'USTRADEslope', 'PPIITMmean', 'PPIITMslope', 'CES3000000008mean', 'CES3000000008slope', 'BUSINVmean', 'BUSINVslope', 'CPIAUCSLmean', 'CPIAUCSLslope', 'NAPMNOImean', 'NAPMNOIslope', 'MCOILWTICOmean', 'MCOILWTICOslope', 'INDPROmean', 'INDPROslope', 'HOUSTmean', 'HOUSTslope', 'UNRATEmean', 'UNRATEslope', 'DGORDERmean', 'DGORDERslope', 'PPIFGSmean', 'PPIFGSslope', 'CPIMEDSLmean', 'CPIMEDSLslope', 'RSAFSmean', 'RSAFSslope', 'CPIAPPSLmean', 'CPIAPPSLslope', 'CPITRNSLmean', 'CPITRNSLslope', 'IPMANSICSmean', 'IPMANSICSslope', 'USCONSmean', 'USCONSslope', 'NAPMEImean', 'NAPMEIslope', 'NAPMPRImean', 'NAPMPRIslope', 'NAPMPImean', 'NAPMPIslope', 'NAPMSDImean', 'NAPMSDIslope', 'PPICRMmean', 'PPICRMslope'] fred_labels = ['NAPMIImean', 'NAPMIIslope', 'CUUR0000SA0L2mean', 'CUUR0000SA0L2slope', 'IPFINALmean', 'IPFINALslope', 'CUUR0000SADmean', 'CUUR0000SADslope', 'HOUSTNEmean', 'HOUSTNEslope', 'MHHNGSPmean', 'MHHNGSPslope', 'WJFUELUSGULFmean', 'WJFUELUSGULFslope', 'CES1021000001mean', 'CES1021000001slope', 'PRRESCONSmean', 'PRRESCONSslope', 'PNRESCONSmean', 'PNRESCONSslope', 'DDURRG3M086SBEAmean', 'DDURRG3M086SBEAslope', 'DSERRG3M086SBEAmean', 'DSERRG3M086SBEAslope', 'IPNCONGDmean', 'IPNCONGDslope', 'IPCONGDmean', 'IPCONGDslope', 'HOUSTMWmean', 'HOUSTMWslope', 'USGOODmean', 'USGOODslope', 'ACOGNOmean', 'ACOGNOslope', 'PPICMMmean', 'PPICMMslope', 'NONREVSLmean', 'NONREVSLslope', 'IPFUELSmean', 'IPFUELSslope', 'IPMATmean', 'IPMATslope', 'BAAFFMmean', 'BAAFFMslope', 'M2REALmean', 'M2REALslope', 'IPDCONGDmean', 'IPDCONGDslope', 'IPBUSEQmean', 'IPBUSEQslope', 'USTRADEmean', 'USTRADEslope', 'PPIITMmean', 'PPIITMslope', 'CES3000000008mean', 'CES3000000008slope', 'BUSINVmean', 'BUSINVslope', 'CPIAUCSLmean', 'CPIAUCSLslope', 'NAPMNOImean', 'NAPMNOIslope', 'MCOILWTICOmean', 'MCOILWTICOslope', 'INDPROmean', 'INDPROslope', 'HOUSTmean', 'HOUSTslope', 'UNRATEmean', 'UNRATEslope', 'DGORDERmean', 'DGORDERslope', 'PPIFGSmean', 'PPIFGSslope', 'CPIMEDSLmean', 'CPIMEDSLslope', 'RSAFSmean', 'RSAFSslope', 'CPIAPPSLmean', 'CPIAPPSLslope', 'CPITRNSLmean', 'CPITRNSLslope', 'IPMANSICSmean', 'IPMANSICSslope', 'USCONSmean', 'USCONSslope', 'NAPMEImean', 'NAPMEIslope', 'NAPMPRImean', 'NAPMPRIslope', 'NAPMPImean', 'NAPMPIslope', 'NAPMSDImean', 'NAPMSDIslope', 'PPICRMmean', 'PPICRMslope'] # feature_labels = ['cog', 'cog_prior', 'sc', 'sc_prior', 'funding gap', 'funding gap_prior', 'pred', 'pred_prior', 'rev', 'rev_prior', 'CPIAUCSL-mean','CPIAUCSL-slope','UNRATE-mean','UNRATE-slope','MCOILWTICO-mean','MCOILWTICO-slope','INDPRO-mean','INDPRO-slope', 'HOUST-mean','HOUST-slope', 'NAPMNOI-mean','NAPMNOI-slope', 'DGORDER-mean','DGORDER-slope', 'PPIFGS-mean','PPIFGS-slope', 'CPIMEDSL-mean','CPIMEDSL-slope', 'RSAFS-mean','RSAFS-slope', 'CPIAPPSL-mean','CPIAPPSL-slope', 'CPITRNSL-mean','CPITRNSL-slope', 'IPMANSICS-mean','IPMANSICS-slope', 'USCONS-mean','USCONS-slope', 'NAPMEI-mean','NAPMEI-slope', 'NAPMPRI-mean','NAPMPRI-slope', 'NAPMPI-mean','NAPMPI-slope', 'NAPMSDI-mean','NAPMSDI-slope', 'PPICRM-mean', 'PPICRM-slope'] # fseries = ['NAPM', 'CPIAUCSL','UNRATE','MCOILWTICO','INDPRO','HOUST','NAPMNOI','DGORDER','PPIFGS','CPIMEDSL','RSAFS','CPIAPPSL','CPITRNSL','IPMANSICS','USCONS','NAPMEI','NAPMPRI','NAPMPI','NAPMSDI','PPICRM'] labelsSaved= False for i in years[1:]: feature_labels =[] temp = [] ############################################# future (aka future prior data) temp.append(float(cogs[count-1])) feature_labels.append('cogs_future') temp.append(float(float(sc[count-1]))) feature_labels.append('sc_future') temp.append(float(fgs[count-1])) feature_labels.append('fg_future') temp.append(float(pred[count-1])) feature_labels.append('pred_future') temp.append(float(content['Return on Equity %'][str(years[count])+'-'+fye])) feature_labels.append('roe_future') rev = float(content['rev-Year over Year'][str(years[count])+'-'+fye]) temp.append(rev) feature_labels.append('revyoy_future') netmarg = float(content['Net Margin %'][str(years[count])+'-'+fye]) temp.append(netmarg) feature_labels.append('netmarg_future') temp.append(float(content['Earnings Per Share USD'][str(years[count])+'-'+fye])) feature_labels.append('eps_future') temp.append(float(content['Debt/Equity'][str(years[count])+'-'+str(fye)])) #d-to-e feature_labels.append('dtoe_future') if i == years[-1]: future_prior_data = copy.deepcopy(temp) print len(future_prior_data), '********' payout_future = float(content['Payout Ratio %'][str(years[count])+'-'+fye]) future_prior_data.append(payout_future) threeyravg = calc_delta(divs) mr_threeyravg = sum(threeyravg[-3:])/3 future_prior_data.append(mr_threeyravg) mr_price = mr_stock_price(ticker) future_prior_data.append(mr_price) print future_prior_data future_prior_data = np.array(future_prior_data, dtype=np.float32) print len(future_prior_data), '********' #############################################prior temp.append(float(cogs_prior[count-1])) feature_labels.append('cogs_prior') temp.append(float(float(sc_prior[count-1]))) feature_labels.append('sc_prior') temp.append(float(fgs_prior[count-1])) feature_labels.append('fg_prior') temp.append(float(pred_prior[count-1])) feature_labels.append('pred_prior') temp.append(float(content['Return on Equity %'][str(years[count-1])+'-'+fye])) feature_labels.append('roe_prior') rev_prior = float(content['rev-Year over Year'][str(years[count-1])+'-'+fye]) temp.append(rev_prior) feature_labels.append('revyoy_prior') netmarg_prior = float(content['Net Margin %'][str(years[count-1])+'-'+fye]) temp.append(netmarg_prior) feature_labels.append('netmarg_prior') temp.append(float(content['Earnings Per Share USD'][str(years[count-1])+'-'+fye])) feature_labels.append('eps_prior') temp.append(float(content['Debt/Equity'][str(years[count-1])+'-'+str(fye)])) #d-to-e prior year feature_labels.append('dtoe_prior') try: temp.append(float(content['Payout Ratio %'][str(years[count-1])+'-'+fye])) feature_labels.append('payout_prior') except: try: payoutr = float(content['Dividends USD'][str(years[count-1])+'-'+str(fye)]) / float(content['Earnings Per Share USD'][str(years[count-1])+'-'+fye]) temp.append(payoutr) feature_labels.append('payout_prior') except: raise TypeError temp.append(float(threeavg[ticker][str(i)])) feature_labels.append('thryravgdgr') temp.append(float(content['shareprices'][str(years[count-1])+'-'+str(fye)])) feature_labels.append('sp_prior') print len(temp), 'before first!' ############################################# combine: nd, glabels = gendata(temp, feature_labels) for q in glabels: feature_labels.append(q) for q in nd: temp.append(float(q)) print len(temp), 'before fred' for fo in fredobjs: # print fo.metric start = str(years[count-1])+'-'+divmonth+'-01' end = str(years[count])+'-'+divmonth+'-01' # print start, end mean, slope = calc_vals(start, end, fo.dates, fo.vals) temp.append(float(mean)) temp.append(float(slope)) # print mean, slope # print for label in fred_labels: feature_labels.append(label) nd, glabels = gendata(temp, feature_labels) for q in glabels: feature_labels.append(q) for q in nd: temp.append(float(q)) if labelsSaved == False: joblib.dump(feature_labels, '/home/ubuntu/dgrpred/dump/temp_persistence/'+ticker+'/'+ticker+'_featlabels') labelsSaved = True temp = np.array(temp, np.float32) x.append(temp) y.append(float(content['divyoy'][str(years[count])+'-'+fye])) count += 1 x = np.array(x, dtype = np.float32) y = np.array(y, dtype = np.float32) print years[-1] np.savetxt('/home/ubuntu/dgrpred/dump/temp_persistence/'+ticker+'/'+ticker+'_mr_year', np.array([years[-1]]), delimiter = ',') np.savetxt('/home/ubuntu/dgrpred/dump/temp_persistence/'+ticker+'/'+ticker+'_x', x, delimiter = ',') np.savetxt('/home/ubuntu/dgrpred/dump/temp_persistence/'+ticker+'/'+ticker+'_y', y, delimiter = ',') np.savetxt('/home/ubuntu/dgrpred/dump/temp_persistence/'+ticker+'/'+ticker+'_mr_price', np.array([mr_price], dtype= np.float32), delimiter=',') np.savetxt('/home/ubuntu/dgrpred/dump/temp_persistence/'+ticker+'/'+ticker+'_future_prior', future_prior_data, delimiter=',') np.savetxt('/home/ubuntu/dgrpred/dump/temp_persistence/'+ticker+'/'+ticker+'_mr_div', np.array([divs[-1]], dtype=np.float32), delimiter = ',') except: shutil.rmtree('/home/ubuntu/dgrpred/dump/temp_persistence/'+ticker) pass
import pandas as pd startdate = datetime.date(2014, 1, 1) today=enddate = datetime.date.today() ticker = "Nifty" ##str5 = "Nifty" ##print str5 ##int4 =raw_input("Enter the index Name(For Niftty :1,Bank Nifty2 ) :") ##if int4 == 1: ## str5 ="^NSEI" ##elif int4 == 2: ## str5 ="^NSEBANK" #fh=web.DataReader("^NSEI","yahoo", start , end) fh = finance.fetch_historical_yahoo("^NSEI" , startdate, enddate) # a numpy record array with fields: date, open, high, low, close, volume, adj_close) # new=pd.DataFrame({'Open':fh[:,0],'High':fh[:,1],'Low':fh[:,2],'Close':fh[:,3],'volume':fh[:,4]}) #new=pd.DataFrame(r) r = mlab.csv2rec(fh) fh.close() r.sort() def moving_average(x, n, type='simple'):
def __init__(self, ticker): gtk.VBox.__init__(self) startdate = datetime.date(2006, 1, 1) today = enddate = datetime.date.today() fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) r = mlab.csv2rec(fh) fh.close() r.sort() plt.rc("axes", grid=True) plt.rc("grid", color="0.75", linestyle="-", linewidth=0.5) textsize = 9 left, width = 0.1, 0.8 rect1 = [left, 0.7, width, 0.2] rect2 = [left, 0.3, width, 0.4] rect3 = [left, 0.1, width, 0.2] fig = Figure(facecolor="white", figsize=(5, 4), dpi=100) axescolor = "#f6f6f6" # the axies background color ax1 = fig.add_axes(rect1, axisbg=axescolor) # left, bottom, width, height ax2 = fig.add_axes(rect2, axisbg=axescolor, sharex=ax1) ax2t = ax2.twinx() ax3 = fig.add_axes(rect3, axisbg=axescolor, sharex=ax1) # plot the relative strength indicator prices = r.adj_close rsi = relative_strength(prices) fillcolor = "darkgoldenrod" ax1.plot(r.date, rsi, color=fillcolor) ax1.axhline(70, color=fillcolor) ax1.axhline(30, color=fillcolor) ax1.fill_between(r.date, rsi, 70, where=(rsi >= 70), facecolor=fillcolor, edgecolor=fillcolor) ax1.fill_between(r.date, rsi, 30, where=(rsi <= 30), facecolor=fillcolor, edgecolor=fillcolor) ax1.text(0.6, 0.9, ">70 = overbought", va="top", transform=ax1.transAxes, fontsize=textsize) ax1.text(0.6, 0.1, "<30 = oversold", transform=ax1.transAxes, fontsize=textsize) ax1.set_ylim(0, 100) ax1.set_yticks([30, 70]) ax1.text(0.025, 0.95, "RSI (14)", va="top", transform=ax1.transAxes, fontsize=textsize) ax1.set_title("%s daily" % ticker) # plot the price and volume data dx = r.adj_close - r.close low = r.low + dx high = r.high + dx deltas = np.zeros_like(prices) deltas[1:] = np.diff(prices) up = deltas > 0 ax2.vlines(r.date[up], low[up], high[up], color="black", label="_nolegend_") ax2.vlines(r.date[~up], low[~up], high[~up], color="black", label="_nolegend_") ma50 = moving_average(prices, 50, type="simple") ma100 = moving_average(prices, 100, type="simple") ma200 = moving_average(prices, 200, type="simple") linema50, = ax2.plot(r.date, ma50, color="blue", lw=2, label="MA (50)") linema100, = ax2.plot(r.date, ma100, color="green", lw=2, label="MA (100)") linema200, = ax2.plot(r.date, ma200, color="red", lw=2, label="MA (200)") last = r[-1] s = "%s O:%1.2f H:%1.2f L:%1.2f C:%1.2f, V:%1.1fM Chg:%+1.2f" % ( today.strftime("%d-%b-%Y"), last.open, last.high, last.low, last.close, last.volume * 1e-6, last.close - last.open, ) t4 = ax2.text(0.3, 0.9, s, transform=ax2.transAxes, fontsize=textsize) props = font_manager.FontProperties(size=10) leg = ax2.legend(loc="center left", shadow=True, fancybox=True, prop=props) leg.get_frame().set_alpha(0.5) volume = (r.close * r.volume) / 1e6 # dollar volume in millions vmax = volume.max() poly = ax2t.fill_between(r.date, volume, 0, label="Volume", facecolor=fillcolor, edgecolor=fillcolor) ax2t.set_ylim(0, 5 * vmax) ax2t.set_yticks([]) # compute the MACD indicator fillcolor = "darkslategrey" nslow = 26 nfast = 12 nema = 9 emaslow, emafast, macd = moving_average_convergence(prices, nslow=nslow, nfast=nfast) ema9 = moving_average(macd, nema, type="exponential") ax3.plot(r.date, macd, color="black", lw=2) ax3.plot(r.date, ema9, color="blue", lw=1) ax3.fill_between(r.date, macd - ema9, 0, alpha=0.5, facecolor=fillcolor, edgecolor=fillcolor) ax3.text( 0.025, 0.95, "MACD (%d, %d, %d)" % (nfast, nslow, nema), va="top", transform=ax3.transAxes, fontsize=textsize, ) # turn off upper axis tick labels, rotate the lower ones, etc for ax in ax1, ax2, ax2t, ax3: if ax != ax3: for label in ax.get_xticklabels(): label.set_visible(False) else: for label in ax.get_xticklabels(): label.set_rotation(30) label.set_horizontalalignment("right") ax.fmt_xdata = mdates.DateFormatter("%Y-%m-%d") canvas = FigureCanvas(fig) # a gtk.DrawingArea self.pack_start(canvas) toolbar = NavigationToolbar(canvas, win) self.pack_start(toolbar, False, False)
""" Name : 4375OS_07_27_volume_and_price_2_plots.py Book : Python for Finance Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 12/26/2013 email : [email protected] [email protected] """ from pylab import plotfile, show import matplotlib.finance as finance ticker = 'IBM' begdate = datetime.date(2013, 1, 1) enddate = datetime.date.today() x = finance.fetch_historical_yahoo(ticker, begdate, enddate) plotfile(x, (0, 5, 6)) show()
def manualtest(numlist): ''' input: a list of numbers (nums corrsp to stocks listed in tckrlist) output: the value of a portfolio containing stocks specified in numlist starting on 9/19/2000 and ending on 7/15/2013 using a manual reinvestment strategy ''' daycount = 0 cash = 0 transactions = 0 cashperstock = 50000/len(numlist) #start with $50,000 dollars divided by number of stocks holdingslist = [] for i in numlist: #buy initial shares price = mf.parse_yahoo_historical(mf.fetch_historical_yahoo(tckrlist[i], (2000, 9, 19), (2000, 9, 19), dividends=False), adjusted=False) iprice = price[0][2] ishares = round(cashperstock/iprice, 2) holdingslist.append(ishares) for i in fulldatelist: #for every day in window fiftydaylist = [] buyconditiontf =[] tendaylist= [] mrdaystckprice = [] hcount = 0 for j in numlist: #look at every stock in the randomly chosen numlist paycount = 0 for k in paydatemaster[j]: #for each stock, look at payment date master list k = k.strip() if k == i: #check if current day is a div pay day paymentmaster[j][paycount] = float(paymentmaster[j][paycount]) divpaymt = round(paymentmaster[j][paycount]*holdingslist[hcount], 2) cash += divpaymt # if it's a pay day, add the div payment to cash paycount += 1 hcount += 1 if daycount > 50: # if more than 50 days have passed for u in numlist: tempolist = [] dcount = 0 for p in stockdatemaster[u]: if p == i: tempolist = stockpricemaster[u][dcount:dcount+50] #stock prices for last 50 days dcount += 1 if len(tempolist) != 0: fstdv, favg = meanstdv(tempolist) #calculate fifty day moving average tstdv, tavg = meanstdv(tempolist[:10]) #calculate 10 day moving average fiftydaylist.append(favg) tendaylist.append(tavg) mrdaystckprice.append(tempolist[0]) fcount = 0 truelist = [] for s in mrdaystckprice: if float(s) < fiftydaylist[fcount] and float(s) < tendaylist[fcount] and tendaylist[fcount] < fiftydaylist[fcount]: #buy criteria defined buyconditiontf.append(True) truelist.append(fcount) # print i else: buyconditiontf.append(False) fcount += 1 fcount = 0 if cash != 0 and len(truelist) != 0: #if there is cash available and one or more buy criteria have been met if cash/len(truelist) > 500: #if there's more than $500 cash for e in buyconditiontf: if e == True: holdingslist[fcount] += (cash/len(truelist))/float(mrdaystckprice[fcount]) #for all stocks meeting buy criteria, add to holdings using available cash transactions += 1 #add one transaction to the list fcount += 1 cash = 0 #reset cash to zero; all cash was spent adding to stocks meeting buy criteria daycount += 1 finaldate = (2013, 7, 15) count = 0 value = 0 for i in numlist: finalprice = mf.parse_yahoo_historical(mf.fetch_historical_yahoo(tckrlist[i], finaldate, finaldate, dividends=False), adjusted=False) ifinalprice = finalprice[0][2] value += round((ifinalprice*holdingslist[count])+cash, 2) #calculate final value for each stock count += 1 return value, transactions
def run(): startdate = datetime.date(2000,1,1) today = enddate = datetime.date.today() ticker = 'SPY' fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # a numpy record array with fields: date, open, high, low, close, volume, adj_close) r = mlab.csv2rec(fh); fh.close() r.sort() plt.rc('axes', grid=True) plt.rc('grid', color='0.75', linestyle='-', linewidth=0.5) textsize = 9 left, width = 0.1, 0.8 rect1 = [left, 0.7, width, 0.2] rect2 = [left, 0.3, width, 0.4] rect3 = [left, 0.1, width, 0.2] fig = plt.figure(facecolor='white') axescolor = '#f6f6f6' # the axes background color ax1 = fig.add_axes(rect1, axisbg=axescolor) #left, bottom, width, height ax2 = fig.add_axes(rect2, axisbg=axescolor, sharex=ax1) ax2t = ax2.twinx() ax3 = fig.add_axes(rect3, axisbg=axescolor, sharex=ax1) ### plot the relative strength indicator prices = r.adj_close rsi = relative_strength(prices) fillcolor = 'darkgoldenrod' ax1.plot(r.date, rsi, color=fillcolor) ax1.axhline(70, color=fillcolor) ax1.axhline(30, color=fillcolor) ax1.fill_between(r.date, rsi, 70, where=(rsi>=70), facecolor=fillcolor, edgecolor=fillcolor) ax1.fill_between(r.date, rsi, 30, where=(rsi<=30), facecolor=fillcolor, edgecolor=fillcolor) ax1.text(0.6, 0.9, '>70 = overbought', va='top', transform=ax1.transAxes, fontsize=textsize) ax1.text(0.6, 0.1, '<30 = oversold', transform=ax1.transAxes, fontsize=textsize) ax1.set_ylim(0, 100) ax1.set_yticks([30,70]) ax1.text(0.025, 0.95, 'RSI (14)', va='top', transform=ax1.transAxes, fontsize=textsize) ax1.set_title('%s daily'%ticker) ### plot the price and volume data dx = r.adj_close - r.close low = r.low + dx high = r.high + dx deltas = np.zeros_like(prices) deltas[1:] = np.diff(prices) up = deltas>0 ax2.vlines(r.date[up], low[up], high[up], color='black', label='_nolegend_') ax2.vlines(r.date[~up], low[~up], high[~up], color='black', label='_nolegend_') ma20 = moving_average(prices, 20, type='simple') ma200 = moving_average(prices, 200, type='simple') linema20, = ax2.plot(r.date, ma20, color='blue', lw=2, label='MA (20)') linema200, = ax2.plot(r.date, ma200, color='red', lw=2, label='MA (200)') last = r[-1] s = '%s O:%1.2f H:%1.2f L:%1.2f C:%1.2f, V:%1.1fM Chg:%+1.2f' % ( today.strftime('%d-%b-%Y'), last.open, last.high, last.low, last.close, last.volume*1e-6, last.close-last.open ) t4 = ax2.text(0.3, 0.9, s, transform=ax2.transAxes, fontsize=textsize) props = font_manager.FontProperties(size=10) leg = ax2.legend(loc='center left', shadow=True, fancybox=True, prop=props) leg.get_frame().set_alpha(0.5) volume = (r.close*r.volume)/1e6 # dollar volume in millions vmax = volume.max() poly = ax2t.fill_between(r.date, volume, 0, label='Volume', facecolor=fillcolor, edgecolor=fillcolor) ax2t.set_ylim(0, 5*vmax) ax2t.set_yticks([]) ### compute the MACD indicator fillcolor = 'darkslategrey' nslow = 26 nfast = 12 nema = 9 emaslow, emafast, macd = moving_average_convergence(prices, nslow=nslow, nfast=nfast) ema9 = moving_average(macd, nema, type='exponential') ax3.plot(r.date, macd, color='black', lw=2) ax3.plot(r.date, ema9, color='blue', lw=1) ax3.fill_between(r.date, macd-ema9, 0, alpha=0.5, facecolor=fillcolor, edgecolor=fillcolor) ax3.text(0.025, 0.95, 'MACD (%d, %d, %d)'%(nfast, nslow, nema), va='top', transform=ax3.transAxes, fontsize=textsize) #ax3.set_yticks([]) # turn off upper axis tick labels, rotate the lower ones, etc for ax in ax1, ax2, ax2t, ax3: if ax!=ax3: for label in ax.get_xticklabels(): label.set_visible(False) else: for label in ax.get_xticklabels(): label.set_rotation(30) label.set_horizontalalignment('right') ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d') class MyLocator(mticker.MaxNLocator): def __init__(self, *args, **kwargs): mticker.MaxNLocator.__init__(self, *args, **kwargs) def __call__(self, *args, **kwargs): return mticker.MaxNLocator.__call__(self, *args, **kwargs) # at most 5 ticks, pruning the upper and lower so they don't overlap # with other ticks #ax2.yaxis.set_major_locator(mticker.MaxNLocator(5, prune='both')) #ax3.yaxis.set_major_locator(mticker.MaxNLocator(5, prune='both')) ax2.yaxis.set_major_locator(MyLocator(5, prune='both')) ax3.yaxis.set_major_locator(MyLocator(5, prune='both')) plt.show()
import datetime from matplotlib import verbose, get_cachedir from matplotlib.dates import date2num from matplotlib.cbook import iterable, mkdirs from matplotlib.collections import LineCollection, PolyCollection from matplotlib.colors import colorConverter from matplotlib.lines import Line2D, TICKLEFT, TICKRIGHT from matplotlib.patches import Rectangle from matplotlib.transforms import Affine2D if __name__ == '__main__': starttime = dt.date(2015,1,1) endtime = dt.date.today() ticker = 'SPY' fh = mpf.fetch_historical_yahoo(ticker, starttime, endtime) r = mlab.csv2rec(fh); fh.close() r.sort() df = pd.DataFrame.from_records(r) quotes = mpf.quotes_historical_yahoo_ohlc(ticker, starttime, endtime) fig, (ax1, ax2) = plt.subplots(2, sharex=True) tdf = df.set_index('date') cdf = tdf['close'] cdf.plot(label = "close price", ax=ax1) pd.rolling_mean(cdf, window=30, min_periods=1).plot(label = "30-day moving averages", ax=ax1) pd.rolling_mean(cdf, window=10, min_periods=1).plot(label = "10-day moving averages", ax=ax1) ax1.set_xlabel(r'Date') ax1.set_ylabel(r'Price') ax1.grid(True) props = font_manager.FontProperties(size=10)
from scipy.optimize import fsolve # (Year, month, day) tuples suffice as args for quotes_historical_yahoo date1 = (2004, 2, 1) date2 = (2004, 4, 12) mondays = WeekdayLocator(MONDAY) # major ticks on the mondays alldays = DayLocator() # minor ticks on the days weekFormatter = DateFormatter('%b %d') # e.g., Jan 12 dayFormatter = DateFormatter('%d') # e.g., 12 startdate = datetime.date(2013,4,1) today = enddate = datetime.date(2013,12,13)#datetime.date.today() matplotlib.dates.date2num(today) ticker = 'QQQ' #quotes = quotes_historical_yahoo(ticker, startdate, enddate)#fetch_historical_yahoo quotes_historical_yahoo # a numpy record array with fields: date, open, high, low, close, volume, adj_close) fh = fetch_historical_yahoo(ticker, startdate, enddate) r = mlab.csv2rec(fh); fh.close() r.sort() #quotes = quotes_historical_yahoo('INTC', date1, date2) quotes = [(matplotlib.dates.date2num(x[0]), x[1], x[2], x[3], x[4], x[5]) for x in r] v = 0.1207 # 0.2258 # 0.2472 k = 206 rate = 0.025/100 t0 = 72 #217-60 360/365 146 t1 = datetime.datetime(2014,2,24) # 2012,11,16 t2 = datetime.datetime(2015,2,2) # 2013,6,21 price = 1.12 # 77.78 def future_levels(begin_data, days_step):
import csv import codecs from datetime import date import sqlite3 import numpy as np import matplotlib.finance as finance import matplotlib.mlab as mlab import sys server = 'HKI.db' startdate = date(2016,6,1) enddate = date(2016,6,30) fh = finance.fetch_historical_yahoo('^HSCE', startdate, enddate)#, cachename=ticker + '.csv' # a numpy record array with fields: date, open, high, low, close, volume, adj_close) r = mlab.csv2rec(fh); fh.close() r.sort() r=r[r.volume>0] conn = sqlite3.connect('HKI.db') cursor = conn.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS HKITRI ( secid text NOT NULL, tradedate text, closeprice real,
up = (up*(n-1) + upval)/n down = (down*(n-1) + downval)/n rs = up/down rsi[i] = 100. - 100./(1.+rs) return rsi # preparing data startdate = datetime.date(2006,1,1) today = enddate = datetime.date.today() ticker = 'GS' fh = finance.fetch_historical_yahoo(ticker,startdate,today) # a numpy record array with fields: date, open, high, low, close, volume, adj_close r = mlab.csv2rec(fh); fh.close() r.sort() dates = r.date[9:] px = r.adj_close prices = px[9:] rsi = relative_strength(px, 10)[9:] dtcount = 0 pxdict = {} for date in dates: pxdict[date] = prices[dtcount] dtcount = dtcount + 1
from pylab import figure, show from matplotlib.finance import fetch_historical_yahoo from matplotlib.dates import YearLocator, MonthLocator, DateFormatter import datetime date1 = datetime.date(2012, 1, 1) date2 = datetime.date(2012, 11, 11) daysFmt = DateFormatter('%m-%d-%Y') quotes = fetch_historical_yahoo('MSFT', date1, date2) if len(quotes) == 0: raise SystemExit dates = [q[0] for q in quotes] opens = [q[1] for q in quotes] fig = figure() ax = fig.add_subplot(111) ax.plot_date(dates, opens, '-') # format the ticks ax.xaxis.set_major_formatter(daysFmt) ax.autoscale_view() # format the coords message box def price(x): return '$%1.2f' % x ax.fmt_xdata = DateFormatter('%Y-%m-%d')
def run(): startdate = datetime.date(2000, 1, 1) today = enddate = datetime.date.today() ticker = 'SPY' fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) # a numpy record array with fields: date, open, high, low, close, volume, adj_close) r = mlab.csv2rec(fh) fh.close() r.sort() plt.rc('axes', grid=True) plt.rc('grid', color='0.75', linestyle='-', linewidth=0.5) textsize = 9 left, width = 0.1, 0.8 rect1 = [left, 0.7, width, 0.2] rect2 = [left, 0.3, width, 0.4] rect3 = [left, 0.1, width, 0.2] fig = plt.figure(facecolor='white') axescolor = '#f6f6f6' # the axes background color ax1 = fig.add_axes(rect1, axisbg=axescolor) #left, bottom, width, height ax2 = fig.add_axes(rect2, axisbg=axescolor, sharex=ax1) ax2t = ax2.twinx() ax3 = fig.add_axes(rect3, axisbg=axescolor, sharex=ax1) ### plot the relative strength indicator prices = r.adj_close rsi = relative_strength(prices) fillcolor = 'darkgoldenrod' ax1.plot(r.date, rsi, color=fillcolor) ax1.axhline(70, color=fillcolor) ax1.axhline(30, color=fillcolor) ax1.fill_between(r.date, rsi, 70, where=(rsi >= 70), facecolor=fillcolor, edgecolor=fillcolor) ax1.fill_between(r.date, rsi, 30, where=(rsi <= 30), facecolor=fillcolor, edgecolor=fillcolor) ax1.text(0.6, 0.9, '>70 = overbought', va='top', transform=ax1.transAxes, fontsize=textsize) ax1.text(0.6, 0.1, '<30 = oversold', transform=ax1.transAxes, fontsize=textsize) ax1.set_ylim(0, 100) ax1.set_yticks([30, 70]) ax1.text(0.025, 0.95, 'RSI (14)', va='top', transform=ax1.transAxes, fontsize=textsize) ax1.set_title('%s daily' % ticker) ### plot the price and volume data dx = r.adj_close - r.close low = r.low + dx high = r.high + dx deltas = np.zeros_like(prices) deltas[1:] = np.diff(prices) up = deltas > 0 ax2.vlines(r.date[up], low[up], high[up], color='black', label='_nolegend_') ax2.vlines(r.date[~up], low[~up], high[~up], color='black', label='_nolegend_') ma20 = moving_average(prices, 20, type='simple') ma200 = moving_average(prices, 200, type='simple') linema20, = ax2.plot(r.date, ma20, color='blue', lw=2, label='MA (20)') linema200, = ax2.plot(r.date, ma200, color='red', lw=2, label='MA (200)') last = r[-1] s = '%s O:%1.2f H:%1.2f L:%1.2f C:%1.2f, V:%1.1fM Chg:%+1.2f' % ( today.strftime('%d-%b-%Y'), last.open, last.high, last.low, last.close, last.volume * 1e-6, last.close - last.open) t4 = ax2.text(0.3, 0.9, s, transform=ax2.transAxes, fontsize=textsize) props = font_manager.FontProperties(size=10) leg = ax2.legend(loc='center left', shadow=True, fancybox=True, prop=props) leg.get_frame().set_alpha(0.5) volume = (r.close * r.volume) / 1e6 # dollar volume in millions vmax = volume.max() poly = ax2t.fill_between(r.date, volume, 0, label='Volume', facecolor=fillcolor, edgecolor=fillcolor) ax2t.set_ylim(0, 5 * vmax) ax2t.set_yticks([]) ### compute the MACD indicator fillcolor = 'darkslategrey' nslow = 26 nfast = 12 nema = 9 emaslow, emafast, macd = moving_average_convergence(prices, nslow=nslow, nfast=nfast) ema9 = moving_average(macd, nema, type='exponential') ax3.plot(r.date, macd, color='black', lw=2) ax3.plot(r.date, ema9, color='blue', lw=1) ax3.fill_between(r.date, macd - ema9, 0, alpha=0.5, facecolor=fillcolor, edgecolor=fillcolor) ax3.text(0.025, 0.95, 'MACD (%d, %d, %d)' % (nfast, nslow, nema), va='top', transform=ax3.transAxes, fontsize=textsize) #ax3.set_yticks([]) # turn off upper axis tick labels, rotate the lower ones, etc for ax in ax1, ax2, ax2t, ax3: if ax != ax3: for label in ax.get_xticklabels(): label.set_visible(False) else: for label in ax.get_xticklabels(): label.set_rotation(30) label.set_horizontalalignment('right') ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d') class MyLocator(mticker.MaxNLocator): def __init__(self, *args, **kwargs): mticker.MaxNLocator.__init__(self, *args, **kwargs) def __call__(self, *args, **kwargs): return mticker.MaxNLocator.__call__(self, *args, **kwargs) # at most 5 ticks, pruning the upper and lower so they don't overlap # with other ticks #ax2.yaxis.set_major_locator(mticker.MaxNLocator(5, prune='both')) #ax3.yaxis.set_major_locator(mticker.MaxNLocator(5, prune='both')) ax2.yaxis.set_major_locator(MyLocator(5, prune='both')) ax3.yaxis.set_major_locator(MyLocator(5, prune='both')) plt.show()
import matplotlib.colors as colors import matplotlib.finance as finance import matplotlib.dates as mdates import matplotlib.ticker as mticker import matplotlib.mlab as mlab import matplotlib.pyplot as plt import matplotlib.font_manager as font_manager import time, sys from sklearn import cross_validation startdate = datetime.date(2012, 1, 1) today = enddate = datetime.date.today() ticker = '^DJI' # a numpy record array with fields: date, open, high, low, close, volume, adj_close) fh = finance.fetch_historical_yahoo(ticker, startdate, enddate) r = mlab.csv2rec(fh) fh.close() r.sort() view_length = 3000 # Plot data plt.style.use('ggplot') plt.figure(1) plt.subplot(211) plt.title(ticker) plt.xlabel('Date') plt.ylabel('Closing Price') plt.plot(r.date[-view_length:], r.close[-view_length:]) # Transform time series to difference returns
# these are the constants required by the various tools. macdFast = 8 #Rule#1:8 def:12 macdSlow = 17 #Rule#1:17 def:26 macdSmoothing = 9 #Rule#1:9 def:9 stoLength = 14 #stoKPeriods=1#%K period stoDPeriods = 5 # %D periods Rule#1 recommends 5, default is 3 movAvePeriods = 10 # number of periods for a moving average fs = 6 if macdFast > macdSlow: exit('ERROR: MACDFast>MACDSlow') for ticIdx in range(np.size(tickers)): fh = finance.fetch_historical_yahoo(tickers[ticIdx], startDate, endDate) # a numpy record array with fields: date, open, high, low, close, volume, adj_close) finData = mlab.csv2rec(fh) fh.close() finData.sort() timeDelt = finData.date[-1] - finData.date[1] if timeDelt.days <= (macdSlow + macdSmoothing + 1): # not enough data to calculate the MACD exit( sprintf( 'ERROR: not enough days to calculate the MACD timeDelta=:%i days', int(timeDelt.days))) #MACD, EMAfast, EMAslow, MACDsign, MACDdiff=ff2.MACD(finData, n_slow, n_fast,macdSmoothing): rule1Orders, ordersTechnik1, ordersTechnik2, priceTechnik2, macdDiff, fastStok, stoD, ma, ma20, ma50, ma200 = ff2.buyOrSell(