def _DownloadDividendsCSVFromYahooFinance(self, symbols: List[Text]) -> List[Optional[Text]]: csv_data = [] today = datetime.datetime.now().strftime("%Y-%m-%d") all_assets = YahooFinancials(symbols) all_dividends = all_assets.get_daily_dividend_data( start_date=config.DEFAULT_MIN_DATE, end_date=today) for symbol in symbols: dividends = all_dividends[symbol] if dividends is None: csv_data.append(None) continue lines = ['Date,Amount'] # Header for e in dividends: if e['amount'] is None: continue lines.append(f'{e["formatted_date"]},{e["amount"]}') lines.append('') # This will add a last line break. csv_data.append("\n".join(lines)) return csv_data
def aux_data(tickers, start_date, end_date): freq = 'daily' # Construct yahoo financials objects for data extraction financial = {} aux_daily = {} for ticker in tickers: financial[ticker] = YahooFinancials(ticker) tick = financial[ticker].get_historical_price_data( start_date, end_date, freq)[ticker]['prices'] tick = pd.DataFrame(clean_stock_data(tick))[['volume', 'open']] aux_daily[ticker] = tick.rename(columns={'open': f'{ticker}_open', \ 'volume': f'{ticker}_volume'}) # Join into one daily master dataset aux = pd.DataFrame() for ticker in tickers: aux = aux.merge(aux_daily[ticker], how='outer', left_index=True, right_index=True) #Find correlation between opening price of pair vols = pd.DataFrame() opens = pd.DataFrame() vol_dict = {} open_dict = {} for ticker in tickers: vols[ticker] = aux[f'{ticker}_volume'] vol_dict[f'{ticker}_std'] = np.std(aux[f'{ticker}_volume']) vol_dict[f'{ticker}_avg'] = np.mean(aux[f'{ticker}_volume']) vol_cor = np.mean(np.mean(vols.corr())) aux_data = [vol_cor, list(vol_dict.values())] return aux_data
def get_map_of_date_to_price(symbol, start_date, end_date, period, indexing_flag): yahoo_stock = YahooFinancials(symbol) json = (yahoo_stock.get_historical_price_data(start_date, end_date, period)) map_of_prices = {} x = 0 for row in json[symbol]["prices"]: price_today = row['close'] date = row['formatted_date'] if indexing_flag == True: map_of_prices[x] = [date, price_today] else: map_of_prices[date] = price_today x = x + 1 return map_of_prices
def multistocks_yahoofinancials(stocks, start, end, period_number, stock_value): periods = ["daily", "weekly", "monthly"] stock_value = ['high', 'adjclose'] prices = pd.DataFrame() #end_date=(datetime.date.today()).strftime('%Y-%m-%d') #start_date=(datetime.date.today()-datetime.timedelta(1825)).strftime('%Y-%m-%d') for ticker in stocks: #create a object stock_name = YahooFinancials(ticker) data = stock_name.get_historical_price_data(start, end, periods[period_number]) value = data[ticker]["prices"] value_period = periods[period_number] #print(value_period) temp = pd.DataFrame(value)[["formatted_date", "high"]] temp.set_index("formatted_date", inplace=True) temp.dropna(inplace=True) prices[ticker] = temp["high"] return prices
def stock_statements(tickers, frequency, statement_type): # data_list = [] # for ticker in tickers: yahoo_financials = YahooFinancials(ticker) ass = yahoo_financials.get_financial_stmts(frequency, statement_type, reformat=True) if statement_type is 'cash' and frequency is 'quarterly': data = ass['cashflowStatementHistoryQuarterly'] if statement_type is 'cash' and frequency is 'annual': data = ass['cashflowStatementHistory'] if statement_type is 'income' and frequency is 'quarterly': data = ass['incomeStatementHistoryQuarterly'] if statement_type is 'income' and frequency is 'annual': data = ass['incomeStatementHistory'] if statement_type is 'balance' and frequency is 'quarterly': data = ass['balanceSheetHistoryQuarterly'] if statement_type is 'balance' and frequency is 'annual': data = ass['balanceSheetHistory'] return data
def update_true_values(): client = pymongo.MongoClient("mongodb+srv://kmhatre:" + PASSWORD + "@crypto.j5hw0.mongodb.net/" + 'predictions' + "?retryWrites=true&w=majority") db = client['predictions'] col = db['predictions'] pred = {} for crypto in CRYPTO_LIST.keys(): yf = YahooFinancials(CRYPTO_LIST[crypto]) today = str( (dt.datetime.now(timezone('EST')) - dt.timedelta(days=1)))[:10] yesterday = str( (dt.datetime.now(timezone('EST')) - dt.timedelta(days=1)))[:10] prices = yf.get_historical_price_data(yesterday, today, 'daily') price = prices[CRYPTO_LIST[crypto]]['prices'][0]['open'] pred[crypto + '_true'] = str(price) query = {'date': today} newvalues = {"$set": pred} col.update_one(query, newvalues) client.close()
def get_data(cls, self): """Get data from yahoo finance.""" treasuries = YahooFinancials(self.tickers) tdata = treasuries.get_current_price() # Add current timestamp tdata['time'] = datetime.datetime.now() # Append data to existing data frame df = self.df.append(tdata, ignore_index=True) """ # Remove time from columns for data conversion try: self.cols.remove('time') except ValueError: pass # Convert cols to float 16s df[self.cols] = df[self.cols].astype(np.float16) """ df.reset_index(inplace=True, drop=True) return df
def calc_earnings_yield(tickers): """ calc earnings to yield ratio :param tickers: :return: """ print('Fetching Earnings to Yield Data from Yahoo for 100 Tickers...') total_data = {} for index, ticker in enumerate(tickers): if index % 1 == 0: print('Fetching Earnings to Yield Data for %d out of %d' % (index, len(tickers))) try: yahoo_financials = YahooFinancials(ticker) total_data[ticker] = 1 / yahoo_financials.get_pe_ratio() except KeyError: print('Could not calc. PE for %s' % ticker) total_data[ticker] = None continue return total_data
def post(self) -> asset_returns: args = request.json print('Processing price request for', args) try: ticker = args['ticker'].upper() start_date = dateparser.parse( args['start_date']).strftime('%Y-%m-%d') end_date = dateparser.parse(args['end_date']).strftime('%Y-%m-%d') yf = YahooFinancials(ticker) prices = yf.get_historical_price_data(start_date, end_date, args['interval']) ret_prices = [{ 'date': datetime.utcfromtimestamp(price['date']).strftime('%Y-%m-%d'), 'close': price['close'] } for price in prices[ticker]['prices'] if price['close'] is not None] return {'prices': ret_prices, 'ticker': ticker} except Exception as e: return {'message': 'Unable to retrive prices'}, 500
def extraction(output_data, WATCHLIST): # Run Yahoo API with 1 indexes at a time. # can run multiple as well n = 1 for category in WATCHLIST: category_name = category.pop(0) output_data.append(category_name) while len(category) > 0: index_to_search = category[0:n] category = category[n:] process_from_yahoo = YahooFinancials(index_to_search) result_data = process_from_yahoo.get_stock_price_data() extract(output_data, result_data) # append an empty column to use that to add a row in excel output_data.append(" ")
def _get_ohlcv(self, params): yf = YahooFinancials([params['symbol']]) hist_price = yf.get_historical_price_data( start_date=params['start_date'].strftime("%Y-%m-%d"), end_date=params['end_date'].strftime("%Y-%m-%d"), time_interval=params['frequency'], )[params['symbol']]['prices'] df = pd.DataFrame.from_dict(hist_price) df = df[params['req_data_fields']] df.columns = params['data_fields'] df.set_index('DATE', drop=True, inplace=True) df.index = pd.to_datetime(df.index) for column in df.columns: try: df[column] = pd.to_numeric(df[column]) except: print(column) return self._adjust_ohlcv(df) if params['adjusted'] == True else df
def update_tdata_history(ticker, colab): for line in reversed(list(open(c.ROOT_DIR_Google + "Tdata_out.csv"))): last = line break token = last.split(",") min_date = token[0] # print(min_date) time = datetime.datetime.now() time = datetime.datetime.strftime(time, "%H:%M:%S") if time < "16:00:00": max_date = (datetime.datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d') else: max_date = datetime.datetime.today().strftime('%Y-%m-%d') # print(max_date) if (max_date > min_date): yahoo_financials = YahooFinancials(ticker) historical_stock_prices = yahoo_financials.get_historical_price_data( min_date, max_date, 'daily') nya_data = historical_stock_prices[ticker] price_data = nya_data['prices'] if colab: csv_out = open(c.ROOT_DIR_Google + 'Tdata_out.csv', mode='a') # opens csv file else: csv_out = open('Technical_data/Tdata_out.csv', mode='a') # opens csv file writer = csv.writer(csv_out) # create the csv writer object for line in price_data: # writes a row and gets the fields from the json object writer.writerow([ line.get('formatted_date'), line.get('high'), line.get('low'), line.get('open'), line.get('close'), line.get('volume'), line.get('adjclose') ]) csv_out.close()
def fill_in_summary_block(self): self.get_ticker_selection() self.initialize_worksheet(sheet_type="backend") self.ws.Range("market_cap").Value = YahooFinancials( self.ticker).get_market_cap() self.ws.Range("ttm_eps").Value = YahooFinancials( self.ticker).get_earnings_per_share() self.ws.Range("ttm_pe").Value = YahooFinancials( self.ticker).get_pe_ratio() self.ws.Range("current_vol").Value = YahooFinancials( self.ticker).get_current_volume() self.ws.Range("av_vol_10_day").Value = YahooFinancials( self.ticker).get_ten_day_avg_daily_volume() self.ws.Range("av_vol_3_months").Value = YahooFinancials( self.ticker).get_three_month_avg_daily_volume()
def retrieve_yahoo_data(stock, start, end, method=1): """ Function used to retrieve Yahoo Finance stock data """ if method == 1: stock = yf.Ticker(stock) hist = stock.history(start=start, end=end, auto_adjust=False) prices = [] for index, day in hist.iterrows(): day_info = (index, day['Open'], day['High'], day['Low'], day['Close'], day['Volume'], day['Adj Close']) prices.append(day_info) return prices if method == 2: stock_2 = YahooFinancials(stock) stock_info = stock_2.get_historical_price_data( start, end.strftime('%Y-%m-%d'), 'daily') prices = [] prices_ = stock_info[stock]['prices'] for p in prices_: day_info = (pd.Timestamp(p['formatted_date']), round(p['open'], 2), round(p['high'], 2), round(p['low'], 2), round(p['close'], 2), p['volume'], round(p['adjclose'], 2)) prices.append(day_info) return prices if method == 3: yf.pdr_override() data = pdr.get_data_yahoo([stock], start=start, end=end) prices = [] for index, day in data.iterrows(): day_info = (index, round(day['Open'], 2), round(day['High'], 2), round(day['Low'], 2), round(day['Close'], 2), day['Volume'], round(day['Adj Close'], 2)) prices.append(day_info) return prices
async def stock(ctx, ticker, info): yahoo_financials = YahooFinancials(ticker) if (info == "current"): await ctx.send(ticker.upper() + " current share price: $" + str(yahoo_financials.get_current_price())) if (info == "open"): await ctx.send(ticker.upper() + " share price at opening: $" + str(yahoo_financials.get_open_price())) if (info == "prevclose"): await ctx.send(ticker.upper() + " share priced at previous close: $" + str(yahoo_financials.get_prev_close_price())) if (info == "cap"): await ctx.send(ticker.upper() + " market cap: $" + str("{:,}".format(yahoo_financials.get_market_cap()))) if (info == "dailylow"): await ctx.send(ticker.upper() + " daily low: $" + str(yahoo_financials.get_daily_low())) if (info == "dailyhigh"): await ctx.send(ticker.upper() + " daily high: $" + str(yahoo_financials.get_daily_high())) if (info == "yearlow"): await ctx.send(ticker.upper() + " yearly low: $" + str(yahoo_financials.get_yearly_low())) if (info == "yearhigh"): await ctx.send(ticker.upper() + " yearly high: $" + str(yahoo_financials.get_yearly_high())) if (info == "rev"): await ctx.send(ticker.upper() + " total revenue: $" + str("{:,}".format(yahoo_financials.get_total_revenue())) ) if (info == "net"): await ctx.send(ticker.upper() + " net income: $" + str("{:,}".format(yahoo_financials.get_net_income()))) if (info == "op"): await ctx.send( ticker.upper() + " operating income: $" + str("{:,}".format(yahoo_financials.get_operating_income()))) if (info == "profit"): await ctx.send(ticker.upper() + " gross profit: $" + str("{:,}".format(yahoo_financials.get_gross_profit())))
def create_retrieve_thread(tickers, metric, file_name, data_dict, batch_no): start_loop = time.time() print( f"Batch/thread {batch_no + 1}: Tickers to be retrieved are: {tickers}") yahoo_financials = YahooFinancials(tickers) if metric == "balance": print(f"Retrieving annual balance sheets from Yahoo Finance...") financial_statement = yahoo_financials.get_financial_stmts( 'annual', 'balance')['balanceSheetHistory'] elif metric == "income": print( f"Retrieving annual income statement history from Yahoo Finance..." ) financial_statement = yahoo_financials.get_financial_stmts( 'annual', 'income')['incomeStatementHistory'] elif metric == "cap": print(f"Retrieving market cap information from Yahoo Finance...") financial_statement = yahoo_financials.get_market_cap() else: print("Metric entered is not recognized.") financial_statement = {} dict_lock.acquire() data_dict.update(financial_statement) end_loop = time.time() print(f"Saving batch {batch_no + 1} to JSON file...") json.dump(data_dict, open(file_name + '.json', 'w')) dict_lock.release() print( f"Time elapsed for batch {batch_no + 1}: {end_loop - start_loop}, metric: {metric}" ) print()
def load_data(self): ''' idYahoo: Indicated of company from yahoo finance startHistorical: Start date of the history in format 'yyyy-MM-dd' endHistorical: End date of the history in format 'yyyy-MM-dd' return: dataframe with data history ''' def __create_new_vars(df): # Get difference between high and low of each day and get difference between open and close of each day df['range_hl'] = df['high'] - df['low'] df['range_oc'] = df['open'] - df['close'] # Add a column 'order_day' to indicate the order of the rows by date df['order_day'] = [x for x in list(range(len(df)))] return df trainingModel_ml.__createDirectory(self) yahoo_financials = YahooFinancials(self.idYahoo) data = yahoo_financials.get_historical_price_data( start_date=self.startHistorical, end_date=self.endHistorical, time_interval='daily') df = pd.DataFrame(data[self.idYahoo]['prices']) df['Date'] = df.formatted_date df = df.drop(['date', 'formatted_date'], axis=1) # convert Date column to datetime df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d') df['month'] = df['Date'].dt.month df = df.drop_duplicates() # sort by datetime df.sort_values(by='Date', inplace=True, ascending=True) df = __create_new_vars(df) self.df = df
def yahoo2pandas(tickers, from_date="1900-01-01", to_date="2100-12-31", frequency="daily", allow_null=False): """Download raw stocks data from yahoo and return a pandas dataframe. allow_null parameter allow you to fetch data even by dates where some tickers don't exist. """ results = {} yahoo_financials = YahooFinancials(tickers) historical_stock_prices = yahoo_financials.get_historical_price_data( from_date, to_date, frequency) for ticker in historical_stock_prices: df = pd.DataFrame(historical_stock_prices[ticker]["prices"]) df["formatted_date"] = (df["formatted_date"]).astype("datetime64[ns]") df = df.set_index("formatted_date") results[ticker] = df["close"].loc[~df["close"].index.duplicated( keep="first")] return pd.DataFrame.from_dict(results).dropna( ) if not allow_null else pd.DataFrame.from_dict(results)
def download_yahoo(ticker: str, base_dir: str = default_data_dir): try: yf = YahooFinancials(ticker) data = yf.get_historical_price_data(dt_to_str(start_date), dt_to_str(end_date), 'daily') except Exception as err: print(f'Unable to read data for {ticker}: {err}') return pd.DataFrame({}) if data.get(ticker) is None or data[ticker].get('prices') is None or \ data[ticker].get('timeZone') is None or len(data[ticker]['prices']) == 0: print(f'Yahoo: no data for {ticker}') return pd.DataFrame({}) prices = {} for rec in sorted(data[ticker]['prices'], key=lambda r: r['date']): date = datetime.strptime(rec['formatted_date'], '%Y-%m-%d') dic_with_prices(prices, ticker, date, rec['open'], rec['high'], rec['low'], rec['close'], rec['volume']) if 'dividends' in data[ticker]['eventsData']: for date, rec in sorted( data[ticker]['eventsData']['dividends'].items(), key=lambda r: r[0]): date = datetime.strptime(date, '%Y-%m-%d') dic_with_div(prices, ticker, date, rec['amount']) if 'splits' in data[ticker]['eventsData']: for date, rec in sorted(data[ticker]['eventsData']['splits'].items(), key=lambda r: r[0]): date = datetime.strptime(date, '%Y-%m-%d') print(f"{ticker} has split {rec['splitRatio']} for {date}") frame = pd.DataFrame.from_dict( prices, orient='index', columns=['Open', 'High', 'Low', 'Close', 'Volume', 'Dividend']) save_csv(base_dir, ticker, frame, 'yahoo')
def tickerIdentifier(self, text): # Initialize SQL connection = sqlite3.connect('BrokeAz.db') cursor = connection.cursor() # Retrieve cashtag and allcap words with length 3-5 and remove any special characters try: potentialTickers = [ word for word in text.split() if word.isupper() or "$" in word] potentialTickers = [''.join(e for e in word if e.isalnum()) for word in potentialTickers] potentialTickers = [ word.upper() for word in potentialTickers if len(word) >= 3 and len(word) <= 5 and word.isalpha()] # Insertion Process: Add to Dictionary for ticker in potentialTickers: # Check if it is a Real Stock Symbol by checking the DB # if not in word_exclusion table proceed to cross check with YahooFinancials to ensure its a valid Ticker (Line 108-109) # if invalid YahooFinancials(ticker).get_stock_quote_type_data() will return { {Ticker} : None } (Line 111) # insert to dictionary otherwise insert capitalized word to DB if not a valid ticker (Line 112/114) cursor.execute("SELECT word FROM word_exclusion where word='{}'".format(ticker.upper())) if cursor.fetchone() == None: test = YahooFinancials(ticker).get_stock_quote_type_data() for key, val in test.items(): if val != None: self.tickers[ticker] = self.tickers.get(ticker, 0) + 1 else: cursor.execute("INSERT INTO word_exclusion (word) VALUES ('{}')".format(ticker.upper())) connection.commit() else: # Skip to the next ticker continue return self.tickers except Exception as e: traceback.print_exc(file=sys.stdout) logger.error(e)
def process(self): start_date = self.conf.get('start_date') end_date = self.conf.get('end_date') end_date_future = datetime.datetime.strptime( end_date, "%Y-%m-%d") + datetime.timedelta(days=self.conf.get('n_ahead')) end_date_future = str(end_date_future.date()) date_range = Helper.get_spec_date(start_date, end_date) date_range_future = Helper.get_spec_date(start_date, end_date_future) diff_len = len(date_range_future) - len(date_range) values = pd.DataFrame({'Dates': date_range_future}) values['Dates'] = pd.to_datetime(values['Dates']) selected_tickers = self.conf.get('tickers_selected') for i in selected_tickers: raw_data = YahooFinancials(i) raw_data = raw_data.get_historical_price_data( start_date, end_date_future, "daily") df = pd.DataFrame( raw_data[i]['prices'])[['formatted_date', 'adjclose']] df.columns = ['Dates1', i] df['Dates1'] = pd.to_datetime(df['Dates1']) values = values.merge(df, how='left', left_on='Dates', right_on='Dates1') values = values.drop(labels='Dates1', axis=1) values = values.fillna(method="ffill", axis=0) values = values.fillna(method="bfill", axis=0) cols = values.columns.drop('Dates') values[cols] = values[cols].apply(pd.to_numeric, errors='coerce').round(decimals=3) values.set_index('Dates', inplace=True) self.data = values[:-diff_len] self.data_future = values[-diff_len:]
def get_data_from_yahoo(reload_sp500=False): if reload_sp500: tickers = save_sp500_tickers() else: with open("sp500tickers.pickle", "rb") as f: tickers = pickle.load(f) if not os.path.exists('stock_dfs'): os.makedirs('stock_dfs') start = str(dt.datetime(2010, 1, 1).date()) end = str(dt.datetime.now().date()) time_period = 'daily' errors = [] for ticker in tickers: try: if not os.path.exists('stock_dfs/{}.csv'.format(ticker)): TKR = YahooFinancials(ticker) fin_dict = TKR.get_historical_price_data( start, end, time_period) df = pd.DataFrame(fin_dict[ticker]['prices']) df.reset_index(inplace=True) df.set_index("date", inplace=True) df.to_csv('stock_dfs/{}.csv'.format(ticker)) #else: #print('Already have {}'.format(ticker)) except: print('No data available for {} ticker'.format(ticker)) errors.append(ticker) pass print(errors) for e in errors: tickers.remove(e) print(tickers) with open("sp500tickers.pickle", "wb") as f: pickle.dump(tickers, f)
def get_stock_price(self, stock_name, t=None): stock = YahooFinancials(self.stickers[stock_name]) if t is None: price = stock.get_current_price() else: data = stock.get_historical_price_data(start_date=str(t)[:10], end_date=str( datetime.date.today()), time_interval='daily') data = pd.DataFrame(data[self.stickers[stock_name]]['prices']) data.rename(columns={ 'formatted_date': 'ds', 'close': 'y' }, inplace=True) if not (data.empty) and str(t)[:10] in data.ds.unique(): price = data[data.ds == str(t)[:10]]['y'].values[0] else: price = np.nan return price
def retrieve_day_bars_yahoo(self, dates, symbol, req_data_length, arr, return_format=["close", "close"]): symbol_yahoo_finance_interactor = YahooFinancials(symbol) print("Getting data for dates between: %s and %s" % (dates[0].strftime('%Y-%m-%d'), dates[1].strftime('%Y-%m-%d'))) earnings_days_data = symbol_yahoo_finance_interactor.get_historical_price_data( dates[0].strftime('%Y-%m-%d'), dates[1].strftime('%Y-%m-%d'), 'daily') # earnings_days_data = json.loads(earnings_days_data) try: price1 = earnings_days_data[symbol]["prices"][0][return_format[0]] price2 = earnings_days_data[symbol]["prices"][1][return_format[1]] print("Success!") arr[0] = price1 arr[1] = price2 except: # Right now I can only see this coming up because of holidays print("Failure.") return False
def getData(tickers, start_date, end_date): ''' The function to download data from Yahoo Finance API. It takes a list of tickers, start date and end date, which is the last trading date. The return is a dataframe containing the tickers' adjusted close price and volume. Parameters: tickers: A list of tickers. start_date: The start date for fetching the data. end_date: The last date to fetch the data. Returns: df: Dataframe that contains the tickers' adjusted close price and volume. ''' df = pd.DataFrame() print(tickers) if (len(tickers) > 0): for ticker in tickers: yahoo_financials = YahooFinancials(str(ticker)) daily = yahoo_financials.get_historical_price_data( start_date, end_date, 'daily') daily = daily.get(ticker).get('prices') df_temp = pd.DataFrame.from_records(daily) df_temp = df_temp.drop(['close', 'date', 'high', 'low', 'open'], axis=1) df_temp = df_temp.rename( columns={ 'formatted_date': 'date', 'adjclose': ticker, 'volume': ticker + '_volume' }) df_temp['date'] = pd.to_datetime(df_temp['date']) df_temp.set_index('date', inplace=True) df = pd.concat([df, df_temp], axis=1) return df
def fetch(meta_path='data/meta.yaml'): logger = logging.getLogger(__name__) with open(meta_path) as stream: metas = yaml.load(stream) for fname, meta in metas.items(): code = meta['code'] start = str(meta['start']) end = datetime.datetime.today().strftime('%Y-%m-%d') yahoo_finance = YahooFinancials(code) daily_price = yahoo_finance.get_historical_price_data( start, end, 'daily') df = json_normalize(daily_price[code]['prices']) df = df.drop(['date'], axis=1) df = df.rename(index=str, columns={ 'formatted_date': 'Date', 'open': 'Open', 'high': 'High', 'low': 'Low', 'close': 'Close', 'adjclose': 'Adj Close', 'volume': 'Volume', }) df = df.round(decimals=2) df.to_csv(path_or_buf='data/history_price/%s' % fname, index=False, columns=[ 'Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume' ]) logger.info('Download: {fname}'.format(fname=fname)) logger.debug('Meta: {meta}'.format(meta=meta))
def financials(): # financeform = CheckFinancials() # return render_template('finance.html', title='Finance', form=financeform) ticker = 'AMZN' yahoo_financials = YahooFinancials(ticker) balance_sheet_data_qt = yahoo_financials.get_financial_stmts( 'quarterly', 'balance') appended_data = [] indices = [] # for item in balance_sheet_data_qt['balanceSheetHistoryQuarterly']['AMZN']: # # print(item.keys()) # col = next(iter(item)) # indices = item[col].keys() # values = item[col].values() # interim = {col: values} # print(interim) # appended_data.append(interim) #interim = pd.DataFrame({col: values}, index=indices) #pd.concat([d, interim], axis=1, sort=False) #d.append(pd.DataFrame(item)) #print(d) #d = pd.DataFrame(data=appended_data, index=indices) #print(d) #d = pd.DataFrame(data=balance_sheet_data_qt['balanceSheetHistoryQuarterly']['AMZN']) d = {'col1': [1, 2], 'col2': [3, 4]} df = pd.DataFrame(data=d) return render_template("dataframe.html", name="dataframe", data=df.to_html())
def get_ma_map(index_symbol,ma_days): yahoo_stock = YahooFinancials(index_symbol) json = (yahoo_stock.get_historical_price_data('2006-01-01', '2020-10-30', 'daily')) map_of_prices = {} x = 0 for row in json[index_symbol]["prices"]: price_today = row['close'] date = row['formatted_date'] map_of_prices[x] = [date,price_today] x = x+ 1 date_to_ma_with_index = calculate_ma(50, map_of_prices) map_date_to_ma = dict() for x in date_to_ma_with_index : map_date_to_ma[date_to_ma_with_index[x][0]] = [date_to_ma_with_index[x][1]] return map_date_to_ma
def downloader(symbol): """downloads symbol price data using oanda REST API :param symbol: the symbol name :type symbol: str """ yf = YahooFinancials(symbol) res = yf.get_historical_price_data(str(start_date), str(end_date), granularity) if not res or symbol not in res or 'prices' not in res[symbol]: ValueError('Fetching price data for "{}" failed.'.format(symbol)) prices = res[symbol]['prices'] df = pd.DataFrame( { 'open': [p['open'] for p in prices], 'close': [p['close'] for p in prices], 'low': [p['low'] for p in prices], 'high': [p['high'] for p in prices], 'volume': [p['volume'] for p in prices], }, index=[pd.Timestamp(d['formatted_date']) for d in prices]) if 'dividend' in prices: df['dividend'] = [p['dividend'] for p in prices] else: df['dividend'] = 0 if 'split' in prices: df['split'] = [p['split'] for p in prices] else: df['split'] = 1 print(df.head(3)) return df
def update_figure(n_clicks, selected_ticker, start_date, end_date): trace1 = [] for i in selected_ticker: yahoo_financials = YahooFinancials(i) stock_price_data = yahoo_financials.get_historical_price_data( start_date, end_date, 'daily') df = pd.DataFrame(stock_price_data[i]['prices'][0:]) df = df[['close', 'formatted_date']] trace1.append( go.Scatter(x=df['formatted_date'], y=df['close'], mode='lines', name='{}'.format(i))) return { 'data': trace1, 'layout': go.Layout(title='S&P 500 Closing Prices (Daily)', xaxis={'title': 'Date'}, yaxis=dict(title='Price')) }