def updateTrend(stock_and_price): print("============Updating Graphical Trend===========") tend = [] for price, stock in zip(stock_and_price['stock value'], stock_and_price['Stock']): print("Updating : " + stock) try: mm50 = yf.Share(stock).get_50day_moving_avg() mm200 = yf.Share(stock).get_200day_moving_avg() except: for _ in range(100): try: mm50 = yf.Share(stock).get_50day_moving_avg() mm200 = yf.Share(stock).get_200day_moving_avg() break except: continue mm50 = float(mm50) mm200 = float(mm200) price = float(price) if price > mm50 and price > mm200: tend.append('up') elif price < mm50 and price < mm200: tend.append('down') elif price < mm50 and price > mm200: tend.append('neutral up') elif price > mm50 and price < mm200: tend.append('neutral down') return tend
def release_info(release_dates): """ INPUT: TIME DATA FRAME OUTPUT: TIME FEATURES """ df_ts = pd.DataFrame() df_CPI_entertainment = pd.read_csv( '../data/CPI-UrbanConsumers-AdmissionMoviesTheatersConcerts.csv') df_CPI = pd.read_csv('../data/CPIAUCSL.csv') CPI = dict(zip(df_CPI['DATE'], df_CPI['CPIAUCSL'])) CPI_E = dict( zip(df_CPI_entertainment['DATE'], df_CPI_entertainment['CUSR0000SS62031'])) S = yahoo_finance.Share('^GSPC') N = yahoo_finance.Share('^IXIC') df_ts['keys'] = release_dates['dt_obj'].apply(process_date) df_ts['CPI'] = [CPI[x] for x in df_ts['keys']] df_ts['CPI_E'] = [CPI_E[x] for x in df_ts['keys']] df_ts['SP500'] = [market_date(x, S) for x in release_dates['dt_obj']] df_ts['NASDAQ'] = [market_date(x, N) for x in release_dates['dt_obj']] df_ts.to_csv('market_data.csv') return df_ts
def yahoo_finance(): aapl = yf.Share('AAPL') print(aapl.get_open()) print( aapl.get_historical(yahoo_date(dt.datetime(2017, 1, 3)), yahoo_date(dt.datetime(2017, 10, 1))))
def loadData(self, tickerSymbol, startDate, endDate, reloadData=False, fileName='stockData.csv'): self.tickerSymbol = tickerSymbol self.startDate = startDate self.endDate = endDate if reloadData: #get data from yahoo finance fo tickerSymbol data = pd.DataFrame( yahoo.Share(tickerSymbol).get_historical(startDate, endDate)) # save as CSV to stop blowing up their API data.to_csv(fileName) # save then reload as the yahoo finance date doesn't load right in Pandas data = pd.read_csv(fileName) else: data = pd.read_csv(fileName) # Due to differing markets and timezones, public holidays etc (e.g. BHP being an Australian stock, # ASX doesn't open on 26th Jan due to National Holiday) there are some gaps in the data. # from manual inspection and knowledge of the dataset, its safe to take the previous days' value data.fillna(method='ffill', inplace=True) self.data = data
def load_data(path): log.debug('Unpickling data from %s' % path) data = pd.read_pickle(path) log.debug('Unpickled data from %s' % path) aapl = None vals = list() stock_data = list() if LOCAL: aapl = pd.read_csv('./table.csv', index_col=0) else: aapl = yahoo_finance.Share('AAPL') log.debug('Stock downloader ready') for i in range(len(data.index)): date = transform_date(data.index[i]) try: s = None if LOCAL: try: s = aapl.loc[date] except KeyError: # log.error('%s' % date) continue else: log.debug('Donwloading %s %s' % ('AAPL', date)) s = aapl.get_historical(date, date)[0] logging.debug('Donwloaded %s %s' % ('AAPL', date)) stock_data.append(0 if s['Close'] > s['Open'] else 1) except yahoo_finance.YQLResponseMalformedError: continue row = data.iloc[i] vals.append(row.values) return np.array(vals, dtype=np.float32), np.array(stock_data, dtype=np.int32)
def updatePrices(stocks): value = [] print("============Updating Prices===========") for stock in stocks: print("Updating : " + stock) try: val = yf.Share(stock).get_price() except: for _ in range(100): try: val = yf.Share(stock).get_price() break except: continue value.append(float(val)) return value
def historical(symbol,startdate,enddate): share=yf.Share(symbol) history=share.get_historical(startdate,enddate) hist_prices=[] for day in history: hist_prices.append(day.get('Close')) return hist_prices
def download_base_data(symbols,startDate,endDate): dataFrames = [] counter = 1 for symbol in symbols: print('LOADING DATA FOR ' + symbol) print('Finished ' + str(counter) + ' out of ' + str(len(symbols))) counter += 1 try: shareInfo = yahoo_finance.Share(symbol) print('MadeIt1') rawData = shareInfo.get_historical(startDate,endDate) print('MadeIt2') pdDf = pd.DataFrame(rawData) print('MadeIt3') if len(pdDf.axes[0])>0: print('MadeIt4') pdDf = TradeDataFormatting.formatDataFrame(pdDf) print('MadeIt5') filterCols = ['Date','Symbol','Volume','Close','Open','Low','High','Adj_Volume','Adj_Close','Adj_Open','Adj_Low','Adj_High'] print('MadeIt6') pdDf = pdDf[filterCols] print('MadeIt7') dataFrames.append(pdDf) except Exception as e: print('LOADING ERROR For ' + symbol + ': ' + str(e)) combinedData = pd.concat(dataFrames) combinedData = combinedData.dropna() return combinedData
def get_hist_data(self, ticker, startdate, stopdate, column="Adj_Close"): # get historical data # Inputs # 1. ticker - ticker sympol of desired equite. Ex. 'SPY' # 2. startdate - start date to start collecting data from. Ex. startdate = '2016-08-20' # 3. stopdate - stop date to stop collecting data from. Ex. endDate = '2016-09-16' # 4. column - this is the column in the dataframe to use to get price information from. Default is 'Adj_Close' # Returns # 1. dataframe containing data for the specified inputs # # Get a dataframe with data between the two dates for the specified ticker. This will automatically load # the historical data into the local _df variable. # Get the historical data and load into the dataframe variable. Return the historical data to the calling # function for the user to cycle through it to generate trade signals. #self._df = GetHistoricalStockData(ticker, startdate, stopdate) # Get the data from yahoo finance, reorder the data, and then put the data into a dataframe for easy use. yahoo = yfinance.Share(ticker) data = yahoo.get_historical(start_date=startdate, end_date=stopdate) # data comes in reversed order. Put it in ascending order. data = data[::-1] # Put the data into a dataframe df = pd.DataFrame(data=data) # Load historical data and initialize other values self.load_hist_data(ticker, df, column) return df
def getYahooStockData(ticker, startDate, endDate, v=0): import yahoo_finance symbol = yahoo_finance.Share(ticker) import sys try: stockData = symbol.get_historical(startDate, endDate) df1 = pd.DataFrame(stockData) dates = df1['Date'].values df2 = df1[['Adj_Close', 'Close', 'High', 'Low', 'Open', 'Volume']].values dfs = pd.DataFrame( df2, index=dates, columns=['Adj_Close', 'Close', 'High', 'Low', 'Open', 'Volume']) dfs.sort_index(ascending=True, inplace=True) except: print( 'Exception: Double check your ticker, startDate and endDate and make sure there is data available.' ) print('ticker = ', ticker) print('startDate = ', startDate) print('endDate =', endDate) e = sys.exc_info()[0] print(e)
def DownloadDailyData(self, output_tocsv=False): if self.DateFind(self.Startdate) != 'default': Startdate = self.DateFind(self.Startdate) else: Startdate = '2000-01-01' if self.DateFind(self.Enddate) != 'deafult': Enddate = self.DateFind(self.Enddate) else: Enddate = datetime.date.today().strftime('%y-%m-%d') # print Startdate, Enddate for stock in self.stock_list: locals()['%s_df' % stock] = pd.DataFrame( yahoo_finance.Share('%s' % stock).get_historical( Startdate, Enddate)) locals()['%s_df' % stock] = locals()['%s_df' % stock].iloc[::-1] self.stock_price_dict['%s' % stock] = locals()['%s_df' % stock] if output_tocsv == True: locals()['%s_df' % stock].to_csv("%s_stock_data.csv" % stock, index=False) print '%s download completed' % stock else: pass return self.stock_price_dict
def get_yearly_data(self, stock, years=15): share = YFin.Share(stock) if share is not None: td = pd.datetime.today() ed = str(td.date()) sd = str(td.year - years) + '-01-01' return share.get_historical(sd, ed)
def __init__(self, quandl_key): """quandl_key : the api key of your quandl account""" ql.ApiConfig.api_key = quandl_key self.external = False self.sentiment = False self.sp500 = False sp = yf.Share(symbol='^GSPC')
def updateDividend(stocks): print("============Updating Dividend===========") div = [] for stock in stocks: print("Updating : " + stock) try: d = yf.Share(stock).get_dividend_yield() except: for _ in range(100): try: d = yf.Share(stock).get_dividend_yield() break except: continue div.append(d) return div
def get_historical_data(self, stock_sym, count=10): share = YFin.Share(stock_sym) if share is not None: today = pd.datetime.today() end_date = str(today.date()) start_date = str((today - BDay(count)).date()) return share.get_historical(start_date, end_date)
def updatePER(stocks): print("============Updating PER===========") PER = [] for stock in stocks: print("Updating : " + stock) try: b = yf.Share(stock).get_price_earnings_ratio() except: for _ in range(100): try: b = yf.Share(stock).get_price_earnings_ratio() break except: continue PER.append(b) return PER
def updateBNPA(stocks): print("============Updating BNPA===========") bnpa = [] for stock in stocks: print("Updating : " + stock) try: b = yf.Share(stock).get_earnings_share() except: for _ in range(100): try: b = yf.Share(stock).get_earnings_share() break except: continue bnpa.append(float(b)) return bnpa
def updateChanges(stocks): print("============Updating changes===========") prev_value = [] for stock in stocks: print("Updating : " + stock) try: prev_val = yf.Share(stock).get_change() except: for _ in range(100): try: prev_val = yf.Share(stock).get_change() break except: continue prev_value.append(float(prev_val)) return prev_value
def retrieveStockData(): try: if reloadData: #get data from yahoo finance fo tickerSymbol historical = yahoo.Share(ticker).get_historical(startDate, endDate) data = pd.DataFrame(historical) # save as CSV to stop blowing up their API data.to_csv(fileName, index=False, parse_dates=['Date']) # save then reload as the yahoo finance date doesn't load right in Pandas data = pd.read_csv(fileName) else: # read the existing csv data = pd.read_csv(fileName) except: print("Error") #Date and Symbol columns not required data.drop(['Symbol'], axis=1, inplace=True) pd.to_datetime(data['Date']) # make date as an index for pandas data frame #data.set_index('Date',inplace=True) #Forward and backfll blank data, better option for this dataset than dropping nulls data.fillna(method='ffill', inplace=True) data.fillna(method='bfill', inplace=True) return data
def populate_post_ipo_yahoo(days=30): try: PostIPOPriceYahoo.__table__.create(bind=savant.db.create_engine()) except: savant.db.session.rollback() #ipos = session.query(Company, HistoricalIPO).filter(Company.id == HistoricalIPO.company_id).filter(HistoricalIPO.first_day_volume != None).filter(Company.symbol == 'TRTLU').all() ipos = session.query( Company, HistoricalIPO).filter(Company.id == HistoricalIPO.company_id).filter( HistoricalIPO.first_day_volume != None).all() for ipo in ipos: sym = ipo.Company.symbol print sym stock = yahoo.Share(sym) ipo_date = ipo.HistoricalIPO.ipo_date sdate = ipo_date.strftime('%Y-%m-%d') edate = (ipo_date + datetime.timedelta(days)).strftime('%Y-%m-%d') result = stock.get_historical(sdate, edate) #the result contains: adj_close and close. THe adj_close should have considered divident or split. we use close here. if len(result) == 0: print "cannot download historical data from Yahoo Finance for", sym continue try: if sdate != result[-1]["Date"]: print "historical data on ipo date for", sym, "is not found" continue except: print "result for ", sym, 'does not contain Date:', result[-1] continue if len(result) < 12: print sym, 'contains only', len(result), 'daily bar!' continue for res in result: try: rec = { "open": float(res["Open"]), "close": float(res["Close"]), "high": float(res["High"]), "low": float(res["Low"]), "volume": int(res["Volume"]), "date": datetime.datetime.strptime(res["Date"], '%Y-%m-%d') } except: print "invalide result for", sym, res break post_ipo_price = PostIPOPriceYahoo(**rec) #print post_ipo_price #post_ipo_price.datetime = price.name # post_ipo_price.date = price.name.split(' ')[0] #post_ipo_price.date = res["Date"] post_ipo_price.company_id = ipo.Company.id savant.db.session.add(post_ipo_price) try: savant.db.session.commit() except: savant.db.session.rollback() print "cannot save ", sym
def get_data(self): for i in self.syms: self.share = yahoo_finance.Share(i) self.dataset = self.share.data_set self.dataset["TIMESTAMP"] = timenow() self.data.append(self.dataset) print self.dataset print "DATA: ", self.data
def get_stock_color(): DOW = yf.Share('DOW') open_price = DOW.get_open() cur_price = DOW.get_price() if (open_price > cur_price): return 'ff0000' else: return '00ff00'
def update_prices(self, start_date=None): """ Retrieve online prices for the commodity: - for currencies, it will get from quandl the exchange rates between the currency and its base_currency - for stocks, it will get from yahoo the daily closing prices expressed in its base_currency Args: start_date (:class:`datetime.date`): prices will be updated as of the start_date. If None, start_date is today - 7 days. .. note:: if prices are already available in the GnuCash file, the function will only retrieve prices as of the max(start_date, last quoted price date) .. todo:: add some frequency to retrieve prices only every X (week, month, ...) """ if self.book is None: raise GncPriceError("Cannot update price for a commodity not attached to a book") # get last_price updated last_price = self.prices.order_by(-Price.date).limit(1).first() if start_date is None: start_date = datetime.datetime.today().date() + datetime.timedelta(days=-7) if last_price: start_date = max(last_price.date.date() + datetime.timedelta(days=1), start_date) if self.namespace == "CURRENCY": # get reference currency (from book.root_account) default_currency = self.base_currency if default_currency == self: raise GncPriceError("Cannot update exchange rate for base currency") # through Quandl for exchange rates quotes = quandl_fx(self.mnemonic, default_currency.mnemonic, start_date) for q in quotes: p = Price(commodity=self, currency=default_currency, date=datetime.datetime.strptime(q.date, "%Y-%m-%d"), value=str(q.rate)) else: symbol = self.mnemonic share = yahoo_finance.Share(symbol) currency = self.book.currencies(mnemonic=share.data_set["Currency"]) # get historical data for q in share.get_historical("{:%Y-%m-%d}".format(start_date), "{:%Y-%m-%d}".format(datetime.date.today()), ): day, close = q["Date"], q["Close"] Price(commodity=self, currency=currency, date=datetime.datetime.strptime(day, "%Y-%m-%d"), value=Decimal(close), type='last')
def main(): yahoo = yh.Share('VXX') historical_prices = yahoo.get_historical('2016-09-01', '2016-09-10') price_frame = pd.DataFrame(index= [d['Date'] for d in historical_prices], columns=['Adj_Close']) for price_dict in historical_prices: price_frame.loc[price_dict['Date'], 'Adj_Close'] = price_dict['Adj_Close'] price_frame = price_frame.sort_index() print price_frame
def agentGetPrices(sector,sectorTxt,startDate,endDate): print sectorTxt, " Agent is running..." prices=[] for ticker in sector: tickerPrices = y.Share(ticker).get_historical(startDate,endDate) cleanPrices =[] for j in tickerPrices: cleanPrices.append(float(j['Adj_Close'])) prices.append(cleanPrices) return prices
def test_fcn(self, stock_sym): share = YFin.Share(stock_sym) # print dir(share) if share is not None: # pprint(share.get_historical('2015-1-1', '2015-1-2')) # pprint(share.get_days_range()) # pprint(share.get_earnings_share()) # pprint(share.get_market_cap()) pprint(share.get_one_yr_target_price())
def main(): symbol = yahoo_finance.Share("GOOG") google_data = symbol.get_historical("2015-01-01", "2016-06-30") google_df = pd.DataFrame(google_data) # Output data into CSV google_df.to_csv( "C:\Users\Divya K\Desktop\Project SVM\DataSet\Data\google_stock_data.csv" ) print
def __init__(self, tablename = "trading", symbollist = "./symbols.txt"): self.symbollist = symbollist l = yahoo_finance.Share("YHOO") s = l.data_set self.db = db_dict() self.db.create_dict_table(tablename,s) self.syms = [] with open(self.symbollist, "r") as s: for line in s: if line != None and line != "" and line != "\n": self.syms.append(line.strip("\n")) print self.syms for i in self.syms: share = yahoo_finance.Share(i) s = share.data_set self.db.insert_dict_values(tablename,s) self.db.conn.commit() self.db.query("*", "*")
def __init__(self, stock, start_date, end_date): """The init of this class converts all of the downloaded data into usable lists which can then be analysed or plotted through the use of other functions and modules """ try: self.data = yahoo_finance.Share(stock).get_historical(start_date, end_date) self.close = [dic['Close'] for dic in self.data] self.open = [dic['Open'] for dic in self.data] self.date = [dic['Date'] for dic in self.data] except Exception, error_StockClass__init__: print 'error_StockClass__init__: ', error_StockClass__init__
def scrap_daily_bar(symbol, sdate, edate, ): stock = yahoo.Share(symbol) results = stock.get_historical(sdate, edate) #the result contains: adj_close and close. THe adj_close should have considered divident or split. we use close here. print len(results) if len(results) == 0: print "cannot download historical data from Yahoo Finance for", symbol nInserted = 0 for res in results: if insert_daily_bar(symbol, datetime.datetime.strptime(res["Date"], '%Y-%m-%d'), res["Open"], res["High"], res["Low"], res["Close"], res["Volume"]): nInserted += 1 print nInserted, "records were added"