def ret_f(ticker, startDate, endDate): p = finance.quotes_historical_yahoo(ticker, startDate, endDate, asobject=True, adjusted=True) return ((p.aclose[1:] - p.aclose[0:-1]) / p.aclose[:-1])
def calc_stats(ticker): """calculates basic statistical quantites for the daily returns of the stock with ticker Arguments ========= ticker: string containg the ticker Returns ====== None Saves statistical quantities to 'TICKER_daily_returns_stats.txt' """ # start your code here5 now = datetime.datetime.now() sp = finance.quotes_historical_yahoo(ticker, (1900, 1, 1), (now.year, now.month, now.day), asobject=True, adjusted=False) returns = np.array([(sp.open[i+1] - sp.open[i]) / sp.open[i] for i in range(len(sp.open) - 1)]) mean = np.mean(returns) median = np.median(returns) std = np.std(returns) skewness = stats.skew(returns) kurtosis = stats.kurtosis(returns) resultStr = "Statistical Properties of Daily Returns For: {0}\nMean = {1} Median = {2} Standard Deviation = {3} Skewness = {4} Kurtosis = {5}".format(*(ticker, mean, median, std, skewness, kurtosis)) result = open(ticker + "_daily_returns_stats.txt", 'w') result.write(resultStr) return None
def candlestickExample(): # (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 quotes = quotes_historical_yahoo('INTC', date1, date2) if len(quotes) == 0: raise SystemExit fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2) ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(weekFormatter) #ax.xaxis.set_minor_formatter(dayFormatter) #plot_day_summary(ax, quotes, ticksize=3) candlestick(ax, quotes, width=0.6) ax.xaxis_date() ax.autoscale_view() plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') print quotes plt.show()
def __init__(self, ticker): gtk.VBox.__init__(self) startdate = datetime.date(2001, 1, 1) today = enddate = datetime.date.today() date1 = datetime.date(2011, 1, 1) date2 = datetime.date.today() mondays = WeekdayLocator(MONDAY) # major ticks on the mondays alldays = DayLocator() # minor ticks on the days weekFormatter = DateFormatter("%b %d") # Eg, Jan 12 dayFormatter = DateFormatter("%d") # Eg, 12 quotes = quotes_historical_yahoo(ticker, date1, date2) if len(quotes) == 0: raise SystemExit fig = Figure(facecolor="white", figsize=(5, 4), dpi=100) fig.subplots_adjust(bottom=0.2) ax = fig.add_subplot(111) ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(weekFormatter) candlestick(ax, quotes, width=0.6) ax.xaxis_date() ax.autoscale_view() pylab.setp(pylab.gca().get_xticklabels(), rotation=45, horizontalalignment="right") canvas = FigureCanvas(fig) # a gtk.DrawingArea self.pack_start(canvas) toolbar = NavigationToolbar(canvas, win) self.pack_start(toolbar, False, False)
def test(self): #1. Get close prices. today = datetime.date.today() start = (today.year - 1, today.month, today.day) quotes = quotes_historical_yahoo('AAPL', start, today) close = numpy.array([q[4] for q in quotes]) #2. Get positive log returns. logreturns = numpy.diff(numpy.log(close)) pos = logreturns[logreturns > 0] #3. Get frequencies of returns. counts, values = numpy.histogram(pos,bins=5) values = values[:-1] + (values[1] - values[0])/2 freqs = 1.0/counts freqs = numpy.log(freqs) #4. Fit the frequencies and returns to a line. p = numpy.polyfit(values,freqs, 1) #5. Plot the results. matplotlib.pyplot.plot(values, freqs, 'o') matplotlib.pyplot.plot(values, p[0] * values + p[1])
def get_close(ticker): today = date.today() start = (today.year - 1, today.month, today.day) quotes = quotes_historical_yahoo(ticker, start, today) return np.array([q[4] for q in quotes])
def best_fit_gaussian(ticker): """determines the best fit gaussian for the distribution of daily returns for stock with ticker and makes a plot of the distribution and the best-fit gaussian ========= ticker: string for the stock ticker Returns ====== None Saves plot to 'TICKER_gaussian_fit.pdf' """ # start your code here stock_data = finance.quotes_historical_yahoo(ticker, (1950, 1, 1), (2014, 9, 30), asobject=True, adjusted=True) returns = np.array([(stock_data.open[i+1] - stock_data.open[i]) / stock_data.open[i] for i in range(len(stock_data.open) - 1)]) counts, bin_edges = np.histogram(returns, density=True, bins=200) bin_centers = bin_edges[:-1]+(bin_edges[1]-bin_edges[0])/2.0 popt, pcov = curve_fit(gaussian, bin_centers, counts) counts_fit = gaussian(bin_centers, *popt) blue_dot = plt.plot(bin_centers, counts, label="Daily Returns", color = 'blue', linewidth = 0, marker = 'o', markersize=3, markeredgewidth = 0, markeredgecolor='none') green_line = plt.plot(bin_centers, counts_fit, label="Gaussian", color='green') x_axis = plt.xticks()[0] text_x_pos = x_axis[math.ceil(len(x_axis) * 0.6)] y_axis = plt.yticks()[0] text_y_pos = y_axis[math.ceil(len(y_axis) * 0.4)] plt.legend(loc="upper right") plt.text(text_x_pos, text_y_pos, 'mu={0:.03f}\nsigma={1:.03f}'.format(*(popt[0], popt[1]))) plt.xlabel('Daily Returns') plt.ylabel('Probability Density') plt.title(ticker) plt.savefig(ticker + '_gaussian_fit.pdf') return None
def initialize1( self,stockTicker, date1, date2, interval, resolution, dataFeedType): self.stockTicker = stockTicker; if(dataFeedType =="yahoo"): self.quotes = quotes_historical_yahoo(self.stockTicker, date1, date2) self.N = self.quotes.__len__(); self.vDateInt = zeros(self.N) self.vDate = empty(self.N, dtype=object); self.vOpen = zeros(self.N) self.vClose = zeros(self.N) self.vHigh = zeros(self.N) self.vLow = zeros(self.N) self.vVolume = zeros(self.N) index = 0; for line in self.quotes: self.vDateInt[index]= line [0]; self.vDate[index] = date.fromordinal( int( line [0] ) ) self.vOpen[index] = line [1]; self.vClose[index] = line [2]; self.vHigh[index] = line [3]; self.vLow[index] = line [4]; self.vVolume[index] = line [5]; index = index +1; elif (dataFeedType == "google"): self.vDateInt, self.vOpen, self.vHigh, self.vLow, self.vClose, self.vVolume = quotes_historical_google.getData(symbol=self.stockTicker, startDate=date1, endDate=date2, resolution=resolution); self.N = size(self.vDateInt); self.vDate = empty(self.N, dtype=object); index = 0; for d in self.vDateInt: self.vDate[index] = date.fromordinal( int( d) ); index = index + 1;
def get_close(ticker): today = date.today() start = (today.year - 1, today.month, today.day) quotes = quotes_historical_yahoo(ticker, start, today) return numpy.array([q[4] for q in quotes])
def test(self): # 2011 to 2012 start = datetime.datetime(2011, 01, 01) end = datetime.datetime(2012, 01, 01) symbols = ["AA", "AXP", "BA", "BAC", "CAT"] quotes = [ finance.quotes_historical_yahoo(symbol, start, end, asobject=True) for symbol in symbols ] close = numpy.array([q.close for q in quotes]).astype(numpy.float) dates = numpy.array([q.date for q in quotes]) data = {} for i in xrange(len(symbols)): data[symbols[i]] = numpy.diff(numpy.log(close[i])) df = pandas.DataFrame(data, index=dates[0][:-1], columns=symbols) print df print df.corr() df.plot() legend(symbols) show()
def get_quotes(): import urllib2 from socket import error as SocketError import errno fi = open('china.txt') lines = fi.readlines() symbol_dict={} for mar, idx in Markets.iteritems(): for k, v in gen_symbols(lines, mar,idx): symbol_dict[k] = v quotes=[] symbols = [] #symbol e.g.: u'603099.ss',u'002281.sz' for symbol in symbol_dict.keys(): try: q = finance.quotes_historical_yahoo(symbol, d1, d2, asobject=True) #q.readlines(), return format: Date,Open,High,Low,Close,Volume,Adj Close except Exception, e: print symbol, e symbol_dict.pop(symbol) if None != q: quotes.append(q) symbols.append(symbol)
def main(): win = gtk.Window() win.connect('destroy', gtk.main_quit) win.set_title('Cursors') vbox = gtk.VBox() win.add(vbox) # Get data from Yahoo Finance enddate = datetime.date.today() startdate = enddate + datetime.timedelta(days=-72) quotes = finance.quotes_historical_yahoo('GOOG', startdate, enddate) qa = array(quotes) f = create_figure(quotes) a = f.gca() vbox.pack_start(gtk.Label('No Blit'), False, False) vbox.pack_start(f.canvas) cursor1 = SnaptoCursor(a, qa[:,0], qa[:,2], useblit=False) f.canvas.mpl_connect('motion_notify_event', cursor1.mouse_move) f = create_figure(quotes) a = f.gca() vbox.pack_start(gtk.Label('Use Blit'), False, False) vbox.pack_start(f.canvas) cursor2 = SnaptoCursor(a, qa[:,0], qa[:,2], useblit=True) f.canvas.mpl_connect('motion_notify_event', cursor2.mouse_move) win.show_all() gtk.main()
def sample(): ############################################################################### # Downloading the data date1 = datetime.date(2012, 1, 1) # start date date2 = datetime.date(2012, 12, 1) # end date # get quotes from yahoo finance quotes = quotes_historical_yahoo("INTC", date1, date2) if len(quotes) == 0: raise SystemExit # unpack quotes dates = np.array([q[0] for q in quotes], dtype=int) close_v = np.array([q[2] for q in quotes]) # volume = np.array([q[5] for q in quotes])[1:] # take diff of close value # this makes len(diff) = len(close_t) - 1 # therefore, others quantity also need to be shifted diff = 100 * (np.exp(np.log(close_v[1:]) - np.log(close_v[:-1])) - 1) dates = dates[1:] close_v = close_v[1:] print diff # pack diff and volume for training # X = np.column_stack([diff, volume]) X = np.column_stack([diff]) return X, dates, close_v
def test(self): #Get close prices. today = datetime.date.today() start = (today.year - 1, today.month, today.day) quotes = quotes_historical_yahoo('AAPL', start, today) close = numpy.array([q[4] for q in quotes]) #2. Get log returns. logreturns = numpy.diff(numpy.log(close)) #3. Calculate breakout and pullback freq = 1 / float(50) breakout = scipy.stats.scoreatpercentile(logreturns, 100 * (1 - freq)) pullback = scipy.stats.scoreatpercentile(logreturns, 100 * freq) print breakout print pullback print logreturns #4. Generate buys and sells buys = numpy.compress(logreturns < pullback, close) sells = numpy.compress(logreturns > breakout, close) profit = sells.sum() - buys.sum() print 'profit: from log_returns = %s' % profit #5. Plot a histogram of the log returns matplotlib.pyplot.hist(logreturns)
def get_data_yahoo_orig(start_date, end_date, fut_code="INTC", format="candle"): try: quotes = quotes_historical_yahoo(fut_code, start_date, end_date) except: print("Unable to get quotes for %s", fut_code) return [] if quotes == None: print("Unable to get quotes for %s", fut_code) return [] if len(quotes) == 0: print("Unable to get quotes for %s", fut_code) return [] else: if(quotes[0][0] < float(datetime.datetime.toordinal(start_date)) or \ quotes[len(quotes)-1][0] > float(datetime.datetime.toordinal(end_date))): print "unnable to get yahoo quotes for ticker %s for required dates" % fut_code return None if (format == "candle"): return quotes else: dt, op, cl, hi, lo, vol = zip(*quotes) return zip(dt, cl)
def get_data_yahoo(start_date, end_date, fut_code="INTC", format = "candle"): try: quotes = quotes_historical_yahoo(fut_code, start_date, end_date) except: print("Unable to get quotes for %s", fut_code) return [] if quotes == None: print("Unable to get quotes for %s", fut_code) return [] if len(quotes)==0: print("Unable to get quotes for %s", fut_code) return [] else: if(quotes[0][0] < float(datetime.datetime.toordinal(start_date)) or \ quotes[len(quotes)-1][0] > float(datetime.datetime.toordinal(end_date))): print "unnable to get yahoo quotes for ticker %s for required dates" %fut_code return None if(format=="candle"): dt, op, cl, hi, lo, vol = zip(*quotes) dt2 = [] for d in dt: dt2.append(datetime.datetime.fromordinal(int(d))) quotes = zip(dt2, op, cl, hi, lo, vol) return quotes else: dt, op, cl, hi, lo, vol = zip(*quotes) return zip(dt, cl)
def stock_monthly_returns(): """create a plot of the monthly returns for a stock Arguments ========= None Prompts the user for a stock ticker Returns ====== None Saves plot to 'TICKER_monthly_returns.pdf' """ # start your code here stock = raw_input('Input a stock ticker: ') now = datetime.datetime.now() sp = finance.quotes_historical_yahoo(stock, (1900, 1, 1), (now.year, now.month, now.day), asobject=True, adjusted=False) graphDate = sp.date[30:] graphPrice = [0] * (len(sp.date) - 30) for idx, val in enumerate(graphPrice): graphPrice[idx] = (sp.close[idx + 30] - sp.close[idx]) / sp.close[idx] plt.plot(graphDate, graphPrice) plt.xlabel('Dates') plt.ylabel('Returns') plt.title('Monthy return for stock ' + stock) plt.savefig(stock + '_monthly_returns.pdf') return None
def test(self): # 2011 to 2012 start = datetime.datetime(2011, 01, 01) end = datetime.datetime(2012, 01, 01) symbols = ["AA", "AXP", "BA", "BAC", "CAT"] quotes = [finance.quotes_historical_yahoo(symbol, start, end, asobject=True) for symbol in symbols] close = numpy.array([q.close for q in quotes]).astype(numpy.float) dates = numpy.array([q.date for q in quotes]) data = {} for i in xrange(len(symbols)): data[symbols[i]] = numpy.diff(numpy.log(close[i])) df = pandas.DataFrame(data, index=dates[0][:-1], columns=symbols) print df print df.corr() df.plot() legend(symbols) show()
def get_data(id, start, end): try: res = quotes_historical_yahoo(id, start, end) except urllib2.HTTPError: return None else: return res
def test(self): # Download AAPL data for 2011 to 2012 start = datetime.datetime(2011, 01, 01) end = datetime.datetime(2012, 01, 01) symbol = "AAPL" quotes = finance.quotes_historical_yahoo(symbol, start, end, asobject=True) # Create date time index dt_idx = pandas.DatetimeIndex(quotes.date) #Create data frame df = pandas.DataFrame(quotes.close, index=dt_idx, columns=[symbol]) # Resample with monthly frequency resampled = df.resample('M', how=numpy.mean) print resampled # Plot df.plot() resampled.plot() show()
def main(): win = gtk.Window() win.connect('destroy', gtk.main_quit) win.set_title('Cursors') vbox = gtk.VBox() win.add(vbox) # Get data from Yahoo Finance enddate = datetime.date.today() startdate = enddate + datetime.timedelta(days=-72) quotes = finance.quotes_historical_yahoo('GOOG', startdate, enddate) qa = array(quotes) f = create_figure(quotes) a = f.gca() vbox.pack_start(gtk.Label('No Blit'), False, False) vbox.pack_start(f.canvas) cursor1 = SnaptoCursor(a, qa[:, 0], qa[:, 2], useblit=False) f.canvas.mpl_connect('motion_notify_event', cursor1.mouse_move) f = create_figure(quotes) a = f.gca() vbox.pack_start(gtk.Label('Use Blit'), False, False) vbox.pack_start(f.canvas) cursor2 = SnaptoCursor(a, qa[:, 0], qa[:, 2], useblit=True) f.canvas.mpl_connect('motion_notify_event', cursor2.mouse_move) win.show_all() gtk.main()
def plot_opening_closing_prices(self): if len(self.ticker_symbol_input_line.text()) == 0 or len(self.start_date_input_line.text()) == 0 or len(self.end_date_input_line.text()) == 0: return start_date = date(*[int(x) for x in self.start_date_input_line.text().split('-')]) end_date = date(*[int(x) for x in self.end_date_input_line.text().split('-')]) quotes = quotes_historical_yahoo(str(self.ticker_symbol_input_line.text()), start_date, end_date) if len(quotes) == 0: return dates, opening_prices, closing_prices = zip(*[[q[0], q[1], q[2]] for q in quotes]) self.axes.cla() self.axes.grid(b=True, which='both') self.axes.set_title('Historical Opening/Closing Prices') self.axes.set_xlabel('Date') self.axes.set_ylabel('Price') opening_plot, = self.axes.plot_date(dates, opening_prices, 'b-') closing_plot, = self.axes.plot_date(dates, closing_prices, 'r-') self.axes.legend([opening_plot, closing_plot], ['Opening Price', 'Closing Price'], title='Ticker Symbol: ' + str(self.ticker_symbol_input_line.text()).upper(), loc=2) years = YearLocator() years_format = DateFormatter('%Y') months = MonthLocator() self.axes.xaxis.set_major_locator(years) self.axes.xaxis.set_major_formatter(years_format) self.axes.xaxis.set_minor_locator(months) self.axes.autoscale_view() self.figure.autofmt_xdate() self.axes.fmt_xdata = DateFormatter('%Y-%m-%d') self.axes.fmt_ydata = lambda x: '$%1.2f' % x self.canvas.draw()
def ret_f(ticker, begdate, enddate): p = quotes_historical_yahoo(ticker, begdate, enddate, asobject=True, adjusted=True) return ((p.open[1:] - p.open[:-1]) / p.open[1:])
def annualBeta(): ''' 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 calculate beta of a stock. :return: ''' def ret_f(ticker, startDate, endDate): p = finance.quotes_historical_yahoo(ticker, startDate, endDate, asobject=True, adjusted=True) return ((p.aclose[1:] - p.aclose[0:-1]) / p.aclose[:-1]) startDate = (1990, 1, 1) endDate = (2014, 12, 31) # Pandas Series for Oracle's Data y0 = pd.Series(ret_f('ORCL', startDate, endDate)) # Pandas Series for S&P500 Data x0 = pd.Series(ret_f('^GSPC', startDate, endDate)) # Historical Date values of S&P500 dateVal = finance.quotes_historical_yahoo('^GSPC', startDate, endDate, asobject=True, adjusted=True).date[0:-1] lag_year = dateVal[0].strftime("%Y") y1, x1, beta, index0 = [], [], [], [] # Calculate Beta for each year for i in range(1, len(dateVal)): year = dateVal[i].strftime("%Y") if (year == lag_year): x1.append(x0[i]) y1.append(y0[i]) else: model = pd.ols(y=pd.Series(y1), x=pd.Series(x1)) print(lag_year, round(model.beta[0], 4)) beta.append(model.beta[0]) index0.append(lag_year) x1 = [] y1 = [] lag_year = year # Plot the main graph plt.plot(beta, c='firebrick', label='ORCL Beta w.r.t S&P500') plt.hlines(y=1, xmin=0, xmax=25, label='Perfect Correlation', lw=2, color='steelblue') plt.legend() plt.show()
def vol_annual(ticker): x = quotes_historical_yahoo(ticker, begdate, enddate, asobject=True, adjusted=True) ret = (x.aclose[1:] - x.aclose[:-1]) / x.aclose[:-1] return (std(ret))
def download_data(symbol, days_delta=60): finish_date = datetime.today() start_date = finish_date - timedelta(days=days_delta) stocks_raw = quotes_historical_yahoo(symbol, start_date, finish_date) stocks_df = pd.DataFrame(stocks_raw, columns=["n_date", "open", "close", "high", "low", "volume"]) return stocks_df
def download_data(symbol, finish_date, start_date): stocks_raw = quotes_historical_yahoo(symbol, start_date, finish_date) stocks_df = pd.DataFrame( stocks_raw, columns=["TimeStamp", "Open", "Close", "High", "Low", "Volume"]) stocks_df["TimeStamp"] = stocks_df["TimeStamp"].astype(np.int32) stocks_df["TimeStamp"] = stocks_df["TimeStamp"].apply(datetime.fromordinal) return stocks_df
def download_data(symbol, finish_date, start_date): stocks_raw = quotes_historical_yahoo(symbol, start_date, finish_date) stocks_df = pd.DataFrame(stocks_raw, columns=["TimeStamp", "Open", "Close", "High", "Low", "Volume"]) stocks_df["TimeStamp"] = stocks_df["TimeStamp"].astype(np.int32) stocks_df["TimeStamp"] = stocks_df["TimeStamp"].apply(datetime.fromordinal) return stocks_df
def get_close(symbol): today = date.today() start = (today.year - 1, today.month, today.day) quotes = quotes_historical_yahoo(symbol, start, today) quotes = numpy.array(quotes) return quotes.T[4]
def getQuotes(symbol, start, end): quotes = fin.quotes_historical_yahoo(symbol, start, end) dates, open, close, high, low, volume = zip(*quotes) data = {"open": open, "close": close, "high": high, "low": low, "volume": volume} dates = Index([datetime.fromordinal(int(d)) for d in dates]) return DataFrame(data, index=dates)
def ret_annual(ticker): x = quotes_historical_yahoo(ticker, begdate, enddate, asobject=True, adjusted=True) logret = log(x.aclose[1:] / x.aclose[:-1]) return (exp(sum(logret)) - 1)
def get_close(symbol): today = date.today() start = (today.year - 1, today.month, today.day) quotes = quotes_historical_yahoo(symbol, start, today) quotes = np.array(quotes) return quotes.T[4]
def getQuotes(symbol, start, end): '''getQuotes documentation''' quotes = fin.quotes_historical_yahoo(symbol, start, end) dates, opn, close, high, low, volume = zip(*quotes) data = {'open': opn, 'close': close, 'high' : high, 'low' : low, 'volume': volume} dates = Index([datetime.fromordinal(int(d)) for d in dates]) return DataFrame(data, index=dates)
def ret_annual(ticker,begdate,enddte): x=quotes_historical_yahoo(ticker,begdate,enddate,asobject=True,adjusted=True) logret = log(x.aclose[1:]/x.aclose[:-1]) date=[] d0=x.date for i in range(0,size(logret)): date.append(d0[i].strftime("%Y")) y=pd.DataFrame(logret,date,columns=[ticker]) return exp(y.groupby(y.index).sum())-1
def ret_monthly(ticker): x = quotes_historical_yahoo(ticker,(begYear,1,1),(endYear,12,31),asobject=True,adjusted=True) logret=log(x.aclose[1:]/x.aclose[:-1]) date=[] d0=x.date for i in range(0,size(logret)): date.append(''.join([d0[i].strftime("%Y"),d0[i].strftime("%m")])) y=pd.DataFrame(logret,date,columns=[ticker]) return y.groupby(y.index).sum()
def gpcs_trade(series, trade): """Function to plot a candle graph from a given series""" """takes as input the security code, default TY""" from matplotlib.dates import DateFormatter, WeekdayLocator, HourLocator, \ DayLocator, MONDAY from matplotlib.finance import quotes_historical_yahoo, candlestick,\ plot_day_summary, candlestick2 mondays = WeekdayLocator(MONDAY) # major ticks on the mondays alldays = DayLocator() # minor ticks on the days monthFormatter = DateFormatter('%b %d') # Eg, Jan 12 dayFormatter = DateFormatter('%d') # Eg, 12 #quotes contains a list of tuples: #(date, open, close, high, low, volume) quotes = series if len(quotes) == 0: today = datetime.datetime.now() date2 = (today.year, today.month, today.day) date1 = ( today.year -1, today.month, 1) quotes = quotes_historical_yahoo('fut_code', date1, date2) if len(quotes) == 0: raise SystemExit fig = figure() fig.subplots_adjust(bottom=0.2) ax = fig.add_subplot(111) #ax.xaxis.set_major_locator(mondays) #ax.xaxis.set_minor_locator(mondays) #ax.xaxis.set_major_formatter(monthFormatter) #ax.xaxis.set_minor_formatter(dayFormatter) #plot_day_summary(ax, quotes, ticksize=3) dt, o, c, h, l, v = zip(*quotes) dt2 = [] for d in dt: dt2.append(datetime.datetime.fromordinal(int(d))) ser = DataFrame(data = {0:o, 1:c, 2:h, 3:l, 4:v}, index = dt2) tmdelta = len(ser[trade.trend.a:]) ax.plot([trade.trend.a, max(ser.index)], [trade.trend.c_reg, (tmdelta*trade.trend.m_reg + trade.trend.c_reg)], color='r', linestyle='-', linewidth=2) ax.plot([trade.trend.a, max(ser.index)], [trade.trend.c_reg+trade.trend.stdev_p_reg, (trade.trend.stdev_p_reg +tmdelta*trade.trend.m_reg + trade.trend.c_reg)], color='g', linestyle='-', linewidth=2) ax.plot([trade.trend.a, max(ser.index)], [trade.trend.c_reg-trade.trend.stdev_p_reg, (-trade.trend.stdev_p_reg +tmdelta*trade.trend.m_reg + trade.trend.c_reg)], color='g', linestyle='-', linewidth=2) tmdelta = len(ser[trade.trend.a:trade.trend.e]) ax.plot([trade.trend.a, trade.trend.e], [trade.trend.c_reg, (tmdelta*trade.trend.m_reg + trade.trend.c_reg)], color='b', linestyle='-', linewidth=2) candlestick(ax, quotes, width=0.6) #ax.xaxis_date() ax.autoscale_view() setp( gca().get_xticklabels(), rotation=45, horizontalalignment='right') show()
def ret_f(ticker, begdate, enddate): p = quotes_historical_yahoo(ticker, begdate, enddate, asobject=True, adjusted=True) ret = (p.aclose[1:] - p.aclose[:-1]) / p.aclose[1:] date_ = p.date return pd.DataFrame(data=ret, index=date_[:-1], columns=['ret'])
def getYahooData(ticker, date1, date2): data = quotes_historical_yahoo(ticker, date1, date2, asobject=True, adjusted=True) data.date = [datetime.date.fromordinal(int(dt)) for dt in data.date] data = zip(data.date, data.open, data.close, data.high, data.low, data.volume) data = np.array(data, dtype = [('Date', datetime.date), ('Open', float), ('Close', float), ('High', float), ('Low', float), ('Volume', float)]) idx = np.where(data['Volume'] > 0) data = data[idx] return data
def GetStockDataWithThreading(symbol, index, d1, d2, quotes, symbols, names, fullSymbols, fullNames, badIndx): try: quote = finance.quotes_historical_yahoo(symbol, d1, d2, asobject=True) #print("Succesfully Loaded %s, %s\n"%(fullSymbols[index], fullNames[index])) quotes[index] = [quote] symbols[index] = fullSymbols[index] names[index] = fullNames[index] badIndx[index] = -1 #Filter these out before we apply the mask except: #print("Failed to Load %s, %s\n"%(fullSymbols[index],fullNames[index])) badIndx[index] = index
def download_history(self, ticker): date1 = datetime.date(2015,1,1) date2 = datetime.date.today() try: quotes = quotes_historical_yahoo(ticker, date1, date2) datafile = os.path.join(us_dir, ticker) output = open(datafile, "wb") pickle.dump(quotes, output) output.close() except Exception, e: print e
def test(self): #1. Download price data # 2011 to 2012 start = datetime.datetime(2011, 01, 01) end = datetime.datetime(2012, 01, 01) quotes = finance.quotes_historical_yahoo('AAPL', start, end, asobject=True) close = numpy.array(quotes.close).astype(numpy.float) self.assertAlmostEqual(normal_ad(numpy.diff(numpy.log(close)))[0], 0.57, 2) self.assertAlmostEqual(normal_ad(numpy.diff(numpy.log(close)))[1], 0.13, 2)
def _redraw(): _redraw.f.clf() quotes = quotes_historical_yahoo( getInputs(), date1, date2) if len(quotes) == 0: raise SystemExit dates = [q[0] for q in quotes] opens = [q[1] for q in quotes] fig =Figure(figsize=(5,4), dpi=100) ax = _redraw.f.add_subplot(111) ax.plot_date(dates, opens, '-') _redraw.canvas.show() _redraw.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
def dataAccess(self): # access data from yahoo # return: date, open, close, high, low, volume quotes = quotes_historical_yahoo(self.stockName, self.startDate, self.endDate) # if error if len(quotes) == 0: print self.stockName raise SystemExit #quotes = np.array(map(list, quotes)) quotes = np.array(quotes) return quotes
def ret_annual(ticker, begdate, enddte): x = quotes_historical_yahoo(ticker, begdate, enddate, asobject=True, adjusted=True) logret = log(x.aclose[1:] / x.aclose[:-1]) date = [] d0 = x.date for i in range(0, size(logret)): date.append(d0[i].strftime("%Y")) y = pd.DataFrame(logret, date, columns=[ticker]) return exp(y.groupby(y.index).sum()) - 1
def plot_funds(tickerlist, initial, start, end): '''Plot a fund by its ticker symbol, normalized to a given initial value. ''' numdays = (end - start).days daysinyear = 365.0 print '%9s %9s %9s %9s' % ('Ticker', 'daily', 'CC', 'abs') # For testing, use something like # FUSVX = quotes_historical_yahoo('FUSVX', datetime.datetime(2012, 10, 1), # datetime.datetime(2013, 4, 1), # asobject=True) for i, ticker in enumerate(tickerlist): # This gives a runtime warning for SCAL, and all the aclose vals # come out zero. Catching a RuntimeWarning isn't as simple as try; # http://stackoverflow.com/questions/10519237/python-how-to-avoid-runtimewarning-in-function-definition # http://stackoverflow.com/questions/9349434/how-do-i-check-for-numeric-overflow-without-getting-a-warning-in-python fund_data = quotes_historical_yahoo(ticker, start, end, asobject=True) # Guard against failures of quotes_historical_yahoo; # without this check you'll see more uncatchable RuntimeWarnings. if fund_data['aclose'][0] == 0: print ticker, ": First adjusted close is 0!" continue # Calculate effective daily-compounded interest rate fixed_pct = fund_data['aclose'][-1] / fund_data['aclose'][0] - 1. Rcc = daysinyear / numdays * \ numpy.log(fund_data['aclose'][-1] / fund_data['aclose'][0]) # Convert CC return to daily-compounded return: Rdaily = daysinyear * (math.exp(Rcc / daysinyear) - 1.) # Another attempt to compute the daily rate, but it's wrong. # Reff = daysinyear * (math.exp(math.log(fund_data['aclose'][-1] # - fund_data['aclose'][0]) # /numdays) - 1) print "%9s %9.2f %9.2f %9.2f" % (ticker, Rdaily * 100, Rcc * 100, fixed_pct * 100) # Normalize to the initial investment: fund_data['aclose'] *= initial / fund_data['aclose'][0] # and plot ax1.plot_date(x=fund_data['date'], y=fund_data['aclose'], fmt=pick_color(i), label=ticker)
def ret_vol_f(stocks, begtime, endtime): ret = [] vol = [] for i in stocks: price = finance.quotes_historical_yahoo(i, begtime, endtime, asobject=True, adjusted=True) close = price.aclose log_ret = np.log(close[1:] / close[:-1]) ret.append(np.exp(log_ret.sum()) - 1) vol.append(np.std(log_ret)) return (ret, vol)
def get_stock_data(id, startdate, enddate): import matplotlib.finance as finance try: # fh = finance.fetch_historical_yahoo(id, startdate, enddate) # (date, open, high, low, close, volume, adj_close) # rows = mlab.csv2rec(fh) # fh.close() rows = finance.quotes_historical_yahoo(id, startdate, enddate, adjusted=True) # (d, open, close, high, low, volume) except urllib2.HTTPError as ex: logger.debug('Cant get data for %s %s' % (id, str(ex))) return [] return rows
def getquotes(symbol, start, end): quotes = fin.quotes_historical_yahoo(symbol, start, end) dates, open, close, high, low, volume = lzip(*quotes) data = { 'open': open, 'close': close, 'high': high, 'low': low, 'volume': volume } dates = pa.Index([dt.datetime.fromordinal(int(d)) for d in dates]) return pa.DataFrame(data, index=dates)
def getquotes(symbol, start, end): quotes = fin.quotes_historical_yahoo(symbol, start, end) dates, open, close, high, low, volume = lzip(*quotes) data = { 'open' : open, 'close' : close, 'high' : high, 'low' : low, 'volume' : volume } dates = pa.Index([dt.datetime.fromordinal(int(d)) for d in dates]) return pa.DataFrame(data, index=dates)
def getquotes(symbol, start, end): # Taken from the no-longer-existent pandas.examples.finance quotes = fin.quotes_historical_yahoo(symbol, start, end) dates, open, close, high, low, volume = lzip(*quotes) data = { 'open' : open, 'close' : close, 'high' : high, 'low' : low, 'volume' : volume } dates = pd.Index([dt.datetime.fromordinal(int(d)) for d in dates]) return pd.DataFrame(data, index=dates)
def get_basic_records(tickers, d1, d2, option): """ This function is to get daily basic records. Args: tickers (list) : a list containing company tickers d1 (datetime) : start date d2 (datetime) : end date option (string) : a string of feature name. Available: "open","close","volume","return","return rate". Return: results (array) : an numpy array containing requested features. Array with shape m,d. m: number of instances; d: number of features. """ quotes = [ finance.quotes_historical_yahoo(ticker, d1, d2, asobject=True) for ticker in tickers ] if option == "open": results = np.array([q.open for q in quotes]).astype(np.float) elif option == "close": results = np.array([q.close for q in quotes]).astype(np.float) elif option == "volume": results = np.array([q.volume for q in quotes]).astype(np.float) elif option == "return": opens = np.array([q.open for q in quotes]).astype(np.float) closes = np.array([q.close for q in quotes]).astype(np.float) results = closes - opens elif option == "return rate": opens = np.array([q.open for q in quotes]).astype(np.float) closes = np.array([q.close for q in quotes]).astype(np.float) results = (closes - opens) / opens else: print "Option Not Found!" results = None return results
def getYahooData(ticker, date1, date2): data = quotes_historical_yahoo(ticker, date1, date2, asobject=True, adjusted=True) data.date = [datetime.date.fromordinal(int(dt)) for dt in data.date] data = zip(data.date, data.open, data.close, data.high, data.low, data.volume) data = np.array(data, dtype=[('Date', datetime.date), ('Open', float), ('Close', float), ('High', float), ('Low', float), ('Volume', float)]) idx = np.where(data['Volume'] > 0) data = data[idx] return data
def test(self): #1. Download price data # 2011 to 2012 start = datetime.datetime(2011, 01, 01) end = datetime.datetime(2012, 01, 01) quotes = finance.quotes_historical_yahoo('AAPL', start, end, asobject=True) close = numpy.array(quotes.close).astype(numpy.float) self.assertAlmostEqual( normal_ad(numpy.diff(numpy.log(close)))[0], 0.57, 2) self.assertAlmostEqual( normal_ad(numpy.diff(numpy.log(close)))[1], 0.13, 2)
def get(symbol, daysBack, fileWrite): if __debug__ and resolution != 1: print "get_yahoo_hist_data:get - Does not support resolution other then 1" date1 = date.today() - timedelta(days=daysBack) # end date2 = date.today() quotes = quotes_historical_yahoo(symbol, date1, date2) if (fileWrite == 1): f = open(symbol + ".csv", 'w') for line in quotes: for val in line: f.write(str(val)) f.write(',') f.write('\n') f.close()