def plot_returns(tickers): if len(tickers) > 1 and type(tickers) != str: data = yf.Tickers(tickers) df = data.history(period=start_date) returns = df['Close'].resample('M').ffill().pct_change() returns1 = (returns + 1).cumprod() fig = go.Figure() for column in returns1: fig.add_trace(go.Scatter(x=returns1.index, y=returns1[column], mode='lines', name=column)) fig.update_layout( title='Cumulative Returns on $1 Investment' ) st.write(fig) elif len(tickers) == 1 and type(tickers) != str: ticker = tickers[0] data = yf.Ticker(ticker) df = data.history(period=start_date) returns = df['Close'].resample('M').ffill().pct_change() returns1 = (returns + 1).cumprod() returns1 = returns1.to_frame('Close') fig = px.line(returns1, x=returns1.index, y=returns1['Close']) st.write(fig) elif type(tickers) == str: ticker = tickers data = yf.Ticker(ticker) df = data.history(period=start_date) returns = df['Close'].resample('M').ffill().pct_change() returns1 = (returns + 1).cumprod() returns1 = returns1.to_frame('Close') fig = px.line(returns1, x=returns1.index, y=returns1['Close'], title='Cumulative Returns on $1 Investment') st.write(fig)
def getData(): stocks = yf.Tickers(['FB', 'AMZN', 'NFLX', 'GOOGL', 'AAPL']) time_period = 'max' #can change this df = stocks.history(period=time_period) df = df['Close'] df = df.reset_index() df = df[df.notna().all(axis=1)] df.to_csv("prices.csv", index=False) # df = pd.read_csv("prices.csv") df["Date"] = pd.to_datetime( df["Date"]) # convert string to datetime format stocks = ['FB', 'AMZN', 'NFLX', 'GOOGL', 'AAPL'] scaler = StandardScaler() df[stocks] = scaler.fit_transform(df[stocks]) train_size = int(df.shape[0] * 0.80) val_size = int(df.shape[0] * 0.90) a = df.iloc[:train_size, :] b = df.iloc[train_size:val_size, :] c = df.iloc[val_size:, :] a = a.drop("Date", axis=1) b = b.drop("Date", axis=1) c = c.drop("Date", axis=1) # dfRecommendations = pd.read_csv("recommendations.csv")# Read from cache to reduce time taken stocks = ['FB', 'AMZN', 'NFLX', 'GOOGL', 'AAPL'] masterlist = [] for each in stocks: x = yf.Ticker(each) y = x.recommendations y['stock_symbol'] = each y = y.reset_index() masterlist.append(y) dfRecommendations = pd.concat(masterlist, axis=0) dfRecommendations["Date"] = pd.to_datetime( dfRecommendations["Date"]) # convert string to datetime format return df, a, b, c, dfRecommendations
def stock_info_all_at_once(): tick_list = new_scraper() request_string = '' for x in tick_list: request_string = request_string + x + ' ' all_info = yf.Tickers(request_string) stockInfos = [] for tk in tick_list: strCMD = 'all_info.tickers.' + tk + '.info' print(tk) stockInfos.append(exec(strCMD)) if len(stockInfos) < 450: print('NOT ENOUGH INFO') return stockInfos else: print("DONE!") #PUT TO EXCEL stockInfos = sorted(stockInfos, key=lambda x: x['marketCap'], reverse=True) df = pandas.DataFrame(stockInfos) df.to_excel(stock_info_excel_path) #DUMP TO FILE with open('Stock Picker/Results/stockInfo.json', 'w') as fp: fp.write(json.dumps(stockInfos, indent=4)) return stockInfos
def load_data(): stocks = yf.Tickers(['FB','AMZN','NFLX','GOOGL','AAPL']) time_period = 'max'#can change this df = stocks.history(period = time_period) df = df['Close'] df = df.reset_index() df = df[df.notna().all(axis = 1)] # df.to_csv("prices.csv", index=False) # df = pd.read_csv("prices.csv") df["Date"] = pd.to_datetime(df["Date"]) # convert string to datetime format # stocks = ['FB','AMZN','NFLX','GOOGL','AAPL'] # masterlist = [] # for each in stocks: # x = yf.Ticker(each) # y = x.recommendations # y['stock_symbol'] = each # y = y.reset_index() # masterlist.append(y) # dfRecommendations = pd.concat(masterlist,axis= 0) # dfRecommendations["Date"] = pd.to_datetime(dfRecommendations["Date"]) # convert string to datetime format # dfRecommendations.to_csv('recommendations.csv',index = False) dfRecommendations = pd.read_csv("recommendations.csv")# Read from cache to reduce time taken dfRecommendations["Date"] = pd.to_datetime(dfRecommendations["Date"]) # convert string to datetime format dfRecommendations["Date"] = dfRecommendations["Date"].dt.date return df, dfRecommendations
def get_data(tickers_list, period): """ :param tickers: str: stock ticker string :param period: str: valid date period for comparison :return: temp_string, delta: str, float: stock printing statements and ratio are returned """ pairs = dict() temp_string = "" tickers = " ".join([x.upper() for x in tickers_list]).strip() stocks = yf.Tickers(tickers) data = stocks.history(env.get('PERIOD', period)) for ticker in tickers_list: close = data.Close[ticker][-1] close_date = data.index[-1] temp_string += "{} Close {}: {}\n".format(ticker, str(close_date), str(close)) high = max(data.Close[ticker]) temp_string += "{} {}-High: {}\n".format(ticker, env.get('PERIOD', period), str(high)) delta = calc_stock(high, close) pairs[ticker] = delta temp_string += "{} Delta: {}\n".format(ticker, str(delta)) + "\n" return temp_string, pairs
def main(): symbols_list = ['SPY','QQQ','IWM','GLD','UVXY','TLT','TSLA','NVDA','NFLX','AMC'] #df=pd.read_csv(url) #symbols_list.extend(list(df.Symbol.values)) final_list = [] for x in np.arange(0,len(symbols_list),100): try: symbols = symbols_list[x:x+100] print(symbols) ticker_list = yf.Tickers(' '.join(symbols)) history = ticker_list.history(period="max") for ticker in ticker_list.tickers: try: mydf = history[[('Close',ticker),('Volume',ticker)]] arr = etl(mydf) if arr.shape[0] > total_days: tmp_list = chunckify(arr) final_list.extend(tmp_list) except: traceback.print_exc() raise ValueError() time.sleep(2) except: traceback.print_exc() raise ValueError() Y = np.array([x[1].T for x in final_list]) X = np.array([x[0] for x in final_list]) np.save('X.npy', X) np.save('Y.npy', Y)
def lambda_handler(event, context): stocks = [ 'FB', 'SHOP', 'BYND', 'NFLX', 'PINS', 'SQ', 'TTD', 'OKTA', 'SNAP', 'DDOG' ] data = yf.Tickers(stocks) hist = data.history(start="2020-05-14", end="2020-05-15", interval="1m", group_by='tickers') output = [] for stock in stocks: for index, price in hist[stock].iterrows(): output.append({ 'high': price['High'], 'low': price['Low'], 'ts': index.strftime('%Y-%m-%d %H:%M:%S'), 'name': stock + "Cg==" }) as_jsonstr = json.dumps(output) fh = boto3.client("firehose", "us-east-2") fh.put_record(DeliveryStreamName="delivery-system", Record={"Data": as_jsonstr.encode('utf-8')}) return {'statusCode': 200, 'body': json.dumps(f'Done: {as_jsonstr}')}
def lambda_handler(event, context): symbols = [ 'FB', 'SHOP', 'BYND', 'NFLX', 'PINS', 'SQ', 'TTD', 'OKTA', 'SNAP', 'DDOG' ] tickers = yf.Tickers(' '.join(symbols)) res = tickers.history(start="2020-05-14", end="2020-05-15", period='1d', interval='1m', group_by='row') records = [] for st in symbols: for i in range(len(res)): ss = res.iloc[i].loc[st, ['High', 'Low']] ss.reset_index(level=0, drop=True, inplace=True) ss.rename({'High': 'high', "Low": 'low'}, inplace=True) ss['ts'] = str(res.index[i]) ss['name'] = st records.append(ss.to_json()) fh = boto3.client("firehose", "us-east-2") for r in records: fh.put_record(DeliveryStreamName="DataTransformer", Record={"Data": r.encode('utf-8')}) return {'statusCode': 200, 'body': json.dumps(f'Done! Records')}
def lambda_handler(event, context): # initialize boto3 client fh = boto3.client("firehose", "us-east-2") #leverage the power of yfinance.history stock_symbol = ['FB','SHOP','BYND','NFLX','PINS','SQ','TTD','OKTA','SNAP','DDOG'] data = yf.Tickers(stock_symbol) history = data.history(start="2020-05-14", end="2020-05-15",interval="1m",group_by = 'tickers') req = [] for stock in stock_symbol: for index, value in history[stock].iterrows(): dataf = {'high':value['High'],'low':value['Low'],'ts':index.strftime('%Y-%m-%d %H:%M:%S'),'name':stock} #convert it into JSON as_jsonstr = json.dumps(dataf) # this actually pushed to our firehose datastream # we must "encode" in order to convert it into the # bytes datatype as all of AWS libs operate over # bytes not strings fh.put_record( DeliveryStreamName="STA9760-delivery-yfinance-stream", Record={"Data": as_jsonstr.encode('utf-8')}) req.append(dataf) # return return { 'statusCode': 200, 'body': json.dumps(f'Done! Recorded: {as_jsonstr}') }
def allStocks(): global all_stocks symbolList = getSymbols() #query = buildQuery(symbolList) #response = json.loads(urlopen(query).read()) # return jsonify(response["query"]) response = yf.Tickers(symbolList) #TODO: parse safely #quotes = response["query"]["results"]["quote"] for ticker in symbolList: #symbol = quote["Symbol"] #price = quote["LastTradePriceOnly"] #name = quote["Name"] #change = quote["Change"] #time = quote["LastTradeTime"] name = getattr(response.tickers, ticker).info["longName"] symbol = getattr(response.tickers, ticker).info["symbol"] price = getattr(response.tickers, ticker).info["regularMarketPrice"] change = getattr(response.tickers, ticker).info["SandP52WeekChange"] time = getattr(response.tickers, ticker).info["exchangeTimezoneShortName"] all_stocks[symbol] = { "name": name, "symbol": symbol, "price": price, "change": change, "time": time } #return render_template("index.html",mainController.stocks=flask.jsonify(all_stocks)) return flask.jsonify(all_stocks)
def get_data(tickers_list, period, company_names=all_company_names): """ :param tickers: str: stock ticker string :param period: str: valid date period for comparison :return: temp_string, delta: str, float: stock printing statements and ratio are returned """ pairs = dict() temp_string = "" tickers = " ".join([x.upper() for x in tickers_list]).strip() stocks = yf.Tickers(tickers) data = stocks.history(env.get('PERIOD', period))['Close'] print(data.head()) print(data.tail()) for ticker in tickers_list: try: df = data[ticker] df.dropna(inplace=True) close = df[-1] high = max(df) delta = calc_stock(high, close) pairs[ticker] = delta except Exception as e: print(f"Couldn't find {ticker} in data") return pairs
def analyze(self): # Load data from file, generate data by running the `ticker_counts.py` script data_directory = Path('./data') input_path = data_directory / f'{dt.date.today()}_tick_df.csv' df_tick = pd.read_csv(input_path).sort_values( by=['Mentions', 'Ticker'], ascending=False) columns = [ 'Ticker', 'Name', 'Industry', 'PreviousClose', 'Low5d', 'High5d', 'ChangePercent1d', 'ChangePercent5d', 'ChangePercent1mo' ] # Activate all tickers' info in parallel self.tickers = yf.Tickers(df_tick['Ticker'].tolist()) with concurrent.futures.ThreadPoolExecutor() as executor: executor.map(lambda t: t.info, self.tickers.tickers) self.data = self.tickers.download(period='1mo', group_by='ticker', progress=True) fin_data = [self.get_ticker_info(tick) for tick in df_tick['Ticker']] df_best = pd.DataFrame(fin_data, columns=columns) # Save to file to load into yahoo analysis script output_path = data_directory / f'{dt.date.today()}_financial_df.csv' df_best.to_csv(output_path, index=False)
def current_price(): companies = get_companies() # TODO: remember to remove '.SA' if the source file already # records codes with it company_codes = list( map(lambda c: companies[c]['codes'] + '.SA', companies)) company_codes_str = ' '.join(company_codes) tickers = yfinance.Tickers(company_codes_str).tickers current_prices = {} i = 0 # to iterate over company_codes for ticker in tickers: LOG.info('Obtaining data from %s', company_codes[i]) try: ticker_current_price = ticker.info['previousClose'] LOG.info('Price: %s', ticker_current_price) except KeyError as e: if e.args[0] == 'regularMarketOpen': LOG.error('No regularMarketOpen found, skipping it.') continue except ValueError as e: LOG.error(e) continue current_prices[company_codes[i]] = ticker_current_price i += 1 LOG.info('Number of obtained last close price: %d', len(current_prices)) LOG.info(current_prices) return current_prices
def load_data_history(tickers, start_date): if len(tickers) > 1 and type(tickers) != str: ticker_data = yf.Tickers(tickers) data = ticker_data.history(period=start_date) for ticker in tickers: fig = go.Figure() fig = px.line(data, x=data.index, y=data['Close'][ticker]) fig.update_xaxes(title='Date') fig.update_yaxes(title='Price') st.subheader(ticker + "'s Price History") st.plotly_chart(fig) elif len(tickers) == 1 and type(tickers) != str: ticker = tickers[0] ticker_data = yf.Ticker(ticker) data = ticker_data.history(period=start_date) fig = go.Figure() fig = px.line(data, x=data.index, y=data['Close']) fig.update_xaxes(title='Date') fig.update_yaxes(title='Price') st.subheader(ticker + "'s Price History") st.plotly_chart(fig) elif type(tickers) == str: ticker = tickers ticker_data = yf.Ticker(ticker) data = ticker_data.history(period=start_date) fig = go.Figure() fig = px.line(data, x=data.index, y=data['Close']) fig.update_xaxes(title='Date') fig.update_yaxes(title='Price') st.subheader(ticker + "'s Price History") st.plotly_chart(fig)
def getDetail(): symbol = flask.request.get_json() #.encode('ascii') symbol = symbol[10:-1] print(symbol) response = yf.Tickers(symbol) hist = getattr(response.tickers, symbol).history('3mo') open_data = hist.iloc[:, 0] model = AR(open_data) model_fit = model.fit(disp=0) print(model_fit.summary()) predictions = model_fit.predict(start=len(open_data), end=len(open_data) + 9, dynamic=False) dates = open_data.index.tolist() final_date = dates[-1].to_pydatetime() dates = date_unix(dates) open_data = list(open_data) l = [] for i in range(1, 11): l.append(final_date + timedelta(days=i)) predict_dates = list(map(lambda x: time.mktime(x.timetuple()), l)) predict_open = list(predictions) actual = list(zip(dates, open_data)) predict = list(zip(predict_dates, predict_open)) data = {"actual": actual, "predict": predict} return flask.jsonify(data)
def analyze(self, best_n=25): # Load data from file, generate data by running the `ticker_counts.py` script data_directory = Path('./data') input_path = data_directory / f'{dt.date.today()}_tick_df.csv' df_tick = pd.read_csv(input_path).sort_values( by=['Mentions', 'Ticker'], ascending=False) columns = [ 'Name', 'Industry', 'Previous Close', '5d Low', '5d High', '1d Change (%)', '5d Change (%)', '1mo Change (%)' ] df_best = df_tick.head(best_n) # Add ASX code to tickers df_best['Ticker'] = [v + '.AX' for v in df_best['Ticker'].tolist()] # Activate all tickers' info in parallel self.tickers = yf.Tickers(df_best['Ticker'].tolist()) with concurrent.futures.ThreadPoolExecutor() as executor: executor.map(lambda t: t.info, self.tickers.tickers) #self.data = self.tickers.download(period='1mo', group_by='ticker', progress=True) self.data = yf.download( [v.info['symbol'] for v in self.tickers.tickers], period='1mo', group_by='ticker', progress=True) df_best[columns] = df_best['Ticker'].apply(self.get_ticker_info) # Save to file to load into yahoo analysis script output_path = data_directory / f'df_best_{best_n}.csv' df_best.to_csv(output_path, index=False) print(df_best.head())
def allSelected(tickerSymbol, date, close, volume, dividens): if len(tickerSymbol) == 0: st.info( "Use the menu on the left to select the stocks that you want to compare" ) return elif len(tickerSymbol) == 1: tickerData = yf.Ticker(tickerSymbol[0]) else: tickerData = yf.Tickers(tickerSymbol) st.write("Data range: " + date) tickerDf = tickerData.history(date) # Open High Low Close Volume Dividends Stock Splits if close: st.write(""" ## Close price""") st.area_chart(tickerDf.Close) if volume: st.write(""" ## Volume""") st.line_chart(tickerDf.Volume) if dividens: st.write(""" ## Dividends""") st.line_chart(tickerDf.Dividends) if not close and not volume and not dividens: st.info("Select the data to be shown in the left bar")
def returnStockTickers(): if WEEKDAY < 5: tickers = yf.Tickers("YEXT FCN ACN") finalString = "How did ye ole house do on the market today? \n\n" + str( tickers.history(period="1d")) return finalString else: return False
def home(): tickers = yf.Tickers('msft aapl goog nok tlry pltr') # tickers = yf.Tickers('aapl') data = [] for key in tickers.tickers: data.append(tickers.tickers[key].info) return render_template("homepage.html", data=data)
def get_yf_tickers(self, tickers: {str, list}): """ Returns an iterable of yfinance.Ticker objects :param tickers: str, list :return: yfinance.Tickers.tickers """ return yf.Tickers( tickers=self.get_multiple_ticker_str(tickers)).tickers
def fetch_historicals( self, symbols: str or list = None, time_period: str = "ytd", aggregation="Close", ): """ Returns a dictionary mapping each ticker (in the user's holdings) to a pandas dataframe-interpreted dictionary of the stock's historical data """ logger.info("Logged Into Robinhood") # Fetch build holding symbols/tickers if symbols is None: if self.build_holdings is None: self.build_holdings = self.fetch_build_holdings() symbols = [ x.replace(".", "-") for x in list(self.build_holdings.keys()) ] # Fetch historical data from yfinance # could also get from robinhood, but yfinance is faster and more accurate response = yf.Tickers(" ".join(symbols)) # fetch_instrument_to_stock_map() rh_instruments = self.rh_conn.stocks.get_instruments_by_symbols( symbols) instrument_id_to_symbol = {} for instrument_data in rh_instruments: instrument_id_to_symbol[ instrument_data["id"]] = instrument_data["symbol"] tickers = response.tickers historical_data = {} macd_data = {} for ticker, ticker_obj in tickers.items(): df = None try: df = ticker_obj.history(period=time_period) except Exception: try: df = ticker_obj.history(period="ytd") except Exception as e: logger.error(f"{e.__class__.__name__}: {e}") logger.error(f"No historical data found for {ticker}") if df is not None and not df.empty: logger.info(f"Historical DF ================== \n{df}") macd_d3_data = self.generate_macd(df, aggregation) df.index = df.index.map(str) ticker_historicals = df.to_dict("index") historical_data[ticker.replace("-", ".")] = ticker_historicals macd_data[ticker.replace("-", ".")] = macd_d3_data else: logger.info(f"Skipping {ticker} historical data") return historical_data, macd_data, instrument_id_to_symbol
def getReferrer(): tickers = yf.Tickers('FB SHOP BYND NFLX PINS SQ TTD OKTA SNAP DDOG') hist = tickers.history(start="2020-12-01", end="2020-12-02", interval = "5m") hist=hist.stack() hist.reset_index(inplace=True) hist['Datetime']= hist['Datetime'].astype(str) hist=hist.rename(columns={'High': 'high', 'Low': 'low', 'Datetime': 'ts', 'level_1':'name'}) hist=hist[['high', 'low', 'ts', 'name']] return hist.to_json(orient="records", lines = True)
def get_multi_price_history(self, stocks, period='1mo'): tickers = yf.Tickers(' '.join(stocks)) res = {} for t in tickers.tickers: his = t.history(period=period) if len(his) == 0: continue res[t.ticker] = his return res
def multiple_tickers(): ''' using Tickers module to obtain info about several companies at the same time ''' tickers = yf.Tickers('bmbl afrm') company_info = tickers.tickers.BMBL.info print(company_info)
def see_all(stock_list, chat_id, last_prices): current = 0 tickers = yf.Tickers(stock_list).tickers new_prices = [] for t in tickers: stock = t.get_info() new_prices.append( make_alert_if_needed(stock, last_prices[current], chat_id)) current += 1 return new_prices
def get_port_info_values(syms): names = syms if type(syms) == list else syms.tolist() tickers = yf.Tickers(names) dict_list = [] for n in tqdm.tqdm(names): d = tickers.tickers[n].get_info() d['symbol'] = n dict_list.append(d) df_info_values = pd.DataFrame(dict_list) return df_info_values
def __init__(self, keyword_list: list) -> None: """Initialization of DesignerFinanceCollector Args: keyword_list (list): Keyword-list with the tickers to search for. """ self.keyword_list = keyword_list self.tickers = yf.Tickers(" ".join(self.keyword_list)) self.df = pd.DataFrame() self.dict = {}
def get_stock_price(tickers): stocks = yf.Tickers(tickers).tickers prices = { ticker: stock.info["regularMarketPrice"] for ticker, stock in stocks.items() if stock.info["regularMarketPrice"] != None } return dumps(prices, indent=4)
def multiple_tickets(): tickers = yf.Tickers('msft aapl goog') # ^ returns a named tuple of Ticker objects # access each ticker using (example) tickers.tickers.MSFT.info tickers.tickers.AAPL.history(period="1mo") tickers.tickers.GOOG.actions data = yf.download("SPY AAPL", start="2017-01-01", end="2017-04-30")
def get(self, request): symbols = [] dict = {} queryset = Stock.objects.all() for query in queryset: symbols.append(query.name) tickers = yf.Tickers(symbols) for s in symbols: dict[s] = tickers.tickers[s].info['regularMarketPrice'] return Response(dict)