def get_stock_data(symbol='^VIX', start_date='2020-01-01'): today = str(pendulum.today().date()) stock = yf(symbol) prices = stock.get_historical_price_data(start_date, today, 'daily') prices = pd.json_normalize(prices) prices = pd.json_normalize(prices[('%s.prices' % (symbol))][0]) return prices
def main(): # generate a list of symbols to get the data for file_name='IPO_symbols.csv' symbol_list=import_data(file_name) #for testing use one symbol # ~ symbol_list=np.array([['FB','2018-01-01','2018-03-01'],['SNAP','2018-01-01','2018-03-01']]) print(symbol_list) #Update the each companies data print('now updating data') total_syms=len(symbol_list) i=0 #use below to update subset of rows # ~ for row in doc.index[23:]: #use below to update all rows for i in range(total_syms): print('updating data for company '+str(symbol_list[i][0])+' row '+str(i+1)+' of '+str(total_syms)) sym=symbol_list[i][0] data=yf(sym) historical_prices = data.get_historical_price_data(symbol_list[i][1], symbol_list[i][2], 'daily') print(historical_prices[sym]['firstTradeDate']['formatted_date']) print(type(historical_prices)) #format the date in a simple format # ~ doc['Date'] = [today.date() for today in doc['Date']] #Save the data to csv file print('saving data')
def get_data(ticker): data = yf(ticker) stock = data.get_historical_price_data(Start, End, 'weekly') keys = list(stock.keys()) stock1 = stock[keys[0]] list1 = list(stock1.keys()) a1 = stock1[list1[5]] num_a1 = len(a1) i = 0 price = [] date = [] for i in range(num_a1): data1 = a1[i] data1_keys = list(data1.keys()) p = data1[data1_keys[6]] d = data1[data1_keys[7]] price.append(p) date.append(d) i += 1 df = pd.DataFrame({"Date": date, "Close": price}) df = df.set_index("Date", drop=False) del df["Date"] # df = df.dropna() return df
def YahooPython(): #setting time list b = 8 c = 30 ti = [] for i in range(8, 16): time_ = r'{}:{}'.format(b, c) ti.append(time_) b += 1 tickers = ['MSFT', 'AAPL'] #configure login parameters for connection to Twilio yfinancials = yf(tickers) account_si = str(input("Insert SI: ")) auth_token = str(input("Insert Token: ")) from_ = str(input("Insert sending telephone: ")) to = str(input("Insert receiving telephone: ")) #ping Twilio authentication cli = Client(account_si, auth_token) #loop continuously with a 60 second loop delay and 30 second timer delay to reduce load time_end = datetime.now().strftime("%d") + timedelta(days=5) while datetime.now().strftime("%d") < time_end: timenow = datetime.now().strftime("%H:%M") if timenow in ti: mess = cli.messages.create(from_, body='Daily Stocks ' + str(yfinancials.get_current_price()), to) print('Message Sent at: ', stimenow) time.sleep(20)
def get_stock_data(tickers, start_date='2020-01-01', end_date='2021-01-26', freq='daily'): '''This function takes the list of stock ticker and get historical OHLC data Parameters ---------- tickers : list/iterable Iterable object containing ticker symbols as strings start_date : str, optional Takes start date of data, format = 'yyyy-mm-dd' end_date : str, optional Takes end date of data, format = 'yyyy-mm-dd' Returns ------- pandas DataFrame containing pricing data and list containing tickers whose data was not found ''' ticker_not_found=[] for ticker in tickers: yf_engine = yf(ticker) price = yf_engine.get_historical_price_data(start_date,end_date,freq) #store the data in DataFrame try: ticker_data = pd.DataFrame(price[ticker]['prices']) ticker_data = ticker_data.drop('date', axis=1) # We will use formatted_date columns instead except: ticker_not_found.append(ticker) continue return ticker_data, ticker_not_found
def __init__(self, ticker='AAPL'): self.ticker = ticker self.YF = yf(ticker) self.PF = Equity(ticker) self.date_str = dt.date.today().strftime('%Y-%m-%d') self.prices_dir = 'prices/prices_{}'.format(self.date_str) self.info_dir = 'info/info_{}'.format(self.date_str)
def getTopStocks(top_stocks_no): global stock_list global url # Gets the top 30 stock identifiers data = requests.get(url) if data.status_code != 200: print('Error! Status code was {}.'.format(data.status_code)) html_data = BeautifulSoup(data.content, 'html.parser') temp = list(html_data.children)[1] temp = list(temp.children)[1] temp = list(temp.children)[2] html = str(list(temp.children)[0]) stock_list = html[html.find('"pageCategory":"YFINANCE:') + 25:html.find('","fallbackCategory":')].split(',') # Removes any identifiers with a fifth letter(which won't work with our program) remove_list = [] for id in stock_list: if len(id) > 4: if len(stock_list) > top_stocks_no: # print(':Removing {} from stock_list because it has a fifth-letter identifier.'.format(id)) remove_list.append(id) for i in range(len(remove_list)): stock_list.remove(remove_list[i]) stock_list = stock_list[0:top_stocks_no] #stock_list = html[html.find('"pageCategory":"YFINANCE:') + 25:html.find('","fallbackCategory":')].split(',')[0:num_of_stocks] # list indexed starts with meta data object, so add 1 to index because index[0] is removed print(':Top {} stocks: {}.'.format(top_stocks_no, stock_list)) fields = ['Company Name', 'Company Symbol'] rows = [] for i in range(len(stock_list)): tstock = yf(stock_list[i]) get_name = tstock.get_stock_quote_type_data() rows.append([stock_list[i], get_name[stock_list[i]]['longName']], ) filename = "StockList.csv" with open(filename, 'w') as csvfile: # creating a csv writer object csvwriter = csv.writer(csvfile) # writing the fields csvwriter.writerow(fields) # writing the data rows csvwriter.writerows(rows)
def get_data1(ticker, Start, End): data = yf(ticker) stock = data.get_historical_price_data(Start, End, 'weekly') keys = list(stock.keys()) stock1 = stock[keys[0]] list1 = list(stock1.keys()) a1 = stock1[list1[5]] num_a1 = len(a1) i = 0 price, date = list(), list() for i in range(num_a1): data1 = a1[i] data1_keys = list(data1.keys()) p = data1[data1_keys[6]] d = data1[data1_keys[7]] price.append(p) date.append(d) i += 1 return price, date
def main(): # import the data from an existing file # generate the latest date in the file ##### # Change the symbol here ##### file_name='^RUT.csv' sym='^RUT' recent_date,today_date,df=import_data(file_name) # ~ print(recent_date,today_date) #Update the companies data print('now updating data') data=yf(sym) historical_prices = data.get_historical_price_data(recent_date, today_date, 'daily') new_data_df=pd.DataFrame(historical_prices[sym]['prices']) new_data_df=new_data_df[['formatted_date','open','high','low','close','adjclose','volume']] new_data_df=new_data_df.rename(index=str,columns={'formatted_date':'Date','open':'Open','high':'High','low':'Low','close':'Close','adjclose':'Adj Close','volume':'Volume'}) #need to make this part better, can get repeated dates on here final_df=df.append(new_data_df[1:]) # ~ print(df.tail()) # ~ print(new_data_df.tail()) # ~ print(final_df.tail()) #Save the data to csv file print('saving data') print(str(len(new_data_df)-1)+' rows added to file. Old num rows = '+str(len(df))+'. New num rows = '+str(len(final_df))) print('Old end date: '+str(df['Date'].iloc[-1])+'. New end date: '+str(final_df['Date'].iloc[-1])) final_df.to_csv(file_name, index=False, float_format='%.5f')
def download_data(symbol, start_date, end_date, time_interval="daily", dirname="data"): """Download data given ticker symbols.""" try: from yahoofinancials import YahooFinancials as yf tick = yf(symbol) data = tick.get_historical_price_data(start_date=start_date, end_date=end_date, time_interval=time_interval) df = pd.DataFrame.from_dict(data[symbol]["prices"]) df["date"] = pd.to_datetime(df["formatted_date"]) df = df[["date", "adjclose", "close", "high", "low", "open", "volume"]] if dirname is not None: df.to_csv(os.path.join(dirname, f"{symbol}.csv"), index=False) else: return df except KeyError: print(f"... Data not found for {symbol}")
if __name__ == "__main__": # Get list of companies to be compared tickerList = [] n = int(input('Enter the number of stocks to be compared: ')) for i in range(0, n): ele = input('Enter a ticker: ') tickerList.append(ele) # Show user the company list print(tickerList) print('\nFetching market data...\n') # Get stock data's key stats company = yf(tickerList) data = company.get_key_statistics_data() dataframe = pd.DataFrame(data) useful_data = dataframe.take( [1, 3, 7, 20, 13, 21, 23, 26, 31, 35, 36, 40, 43, 45], 0) # Print useful data print(useful_data) # Query user for creation of report reportQuery = input('\nWould you like to save this in a CSV file? (Y/N)\n') reportQuery = str(reportQuery.upper()) if (reportQuery == 'Y'): print('Report will be created.') dataframe_report = useful_data.transpose() dataframe_report.to_csv('report.csv')
def stock_page(stock_info_id): stock_dic = mongo.db.stock_info.find_one({"_id": ObjectId(stock_info_id)}) for key, value in stock_dic.items(): # get the sort stock name from db if key == "stock_name_short": stock_name = value # get the whole stock name from db if key == "stock_name": stock_title = value # get the company description from db if key == "description": stock_description = value # get the amount of free cash of the user cash_of_user = mongo.db.users.find_one({"username": session["user"]})["cash"] # variable to get the id of the stock get_stock_id = mongo.db.stock_info.find_one( {"stock_name_short": stock_name})["_id"] yf2 = yf(stock_name) # get the stock price stock_price = round(si.get_live_price(stock_name), 2) # get max amount of stocks you can buy max_amount = math.floor(int(cash_of_user) / stock_price) # the market close price of a stock close_price = yf2.get_prev_close_price() # the absoluut price change since market opening change_price = yf2.get_current_change() # the relate percent change since market opening change_percent_price = round(change_price * 100 / close_price, 2) # market status market_status = si.get_market_status() # get the stock data for in the table stock_table = si.get_quote_table(stock_name) stock_info_first_part = {} stock_info_second_part = {} # make 2 dict of the data for index, (k, v) in enumerate(stock_table.items()): if index <= 8: add_info = {k: v} stock_info_first_part.update(add_info) else: add_info = {k: v} stock_info_second_part.update(add_info) # code to buy the stocks if request.method == "POST": # define variables # the criteria for when the stock is allready bought by the user data_find = { "bought_by": session["user"], "stock_name_short": stock_name } # the criteria to extract the price of the stock # of the users free cashflow user_of_db = {"username": session["user"]} # the criteria to find the user 'admin' user_is_admin = {"username": "******"} # get the number of stocks bought get_stock_amount = int(request.form.get("stock_total")) # get the number of stocks bought in a string to valide it get_stock_amount_str = request.form.get("stock_total") # get to purchase value of the stocks excl fee price_change = round(stock_price * get_stock_amount, 2) # get amount spend on fee by purchasing a stock spend_on_fee = round(0.5 + (0.003 * price_change), 2) total_spend_stock = round(price_change + spend_on_fee, 2) # check if the number of stock the user wants to buy is valide if get_stock_amount_str == "" or not check_stock(get_stock_amount_str): flash("Enter a valide number.") return redirect(url_for("stock_page", stock_info_id=get_stock_id)) # if user already has the stock, execute this if statement if mongo.db.stocks_bought.count_documents(data_find) == 1: # get the amount of stocks the user allready has stocks_user_has = mongo.db.stocks_bought.find_one(data_find) stocks_owned = stocks_user_has["stock_amount"] # get the price of the amount of stocks the user allready has price_user_paid = mongo.db.stocks_bought.find_one(data_find) stocks_price_own = price_user_paid["stock_price"] # get the changed price per stock changed_price_per_stock = (stocks_price_own + price_change) / ( stocks_owned + get_stock_amount) # update the new purchase to the db mongo.db.stocks_bought.update_one( data_find, { '$inc': { "stock_price": price_change, "stock_amount": get_stock_amount }, '$set': { "price_per_stock": changed_price_per_stock } }) # extract the stock buy price of the free cash of user and # add the cash spend on fee to the total spend on fees mongo.db.users.update_one( user_of_db, { '$inc': { "cash": -total_spend_stock, "total_spend_fees": spend_on_fee } }) # add the fee value to the admin profile to see # how much the website has made so far mongo.db.users.update_one( user_is_admin, {'$inc': { "total_income_business": spend_on_fee }}) flash(f"You successfully bought {get_stock_amount}" + f" {stock_name} stocks for ${price_change}") return redirect(url_for("portfolio")) else: # get the price per stock price_per_stock = price_change / get_stock_amount # buy the new stock and upload the data to the db stock_bought = { "stock_name_short": stock_name, "stock_name": stock_title, "bought_by": session["user"], "stock_price": price_change, "stock_amount": get_stock_amount, "price_per_stock": price_per_stock } # add new stock to database mongo.db.stocks_bought.insert_one(stock_bought) # extract the stock buy price of the free cash of user and # add the cash spend on fee to the total spend on fees mongo.db.users.update_one( user_of_db, { '$inc': { "cash": -total_spend_stock, "total_spend_fees": spend_on_fee } }) # add the fee value to the admin profile to see # how much the website has made so far mongo.db.users.update_one( user_is_admin, {'$inc': { "total_income_business": spend_on_fee }}) flash(f"You successfully bought {get_stock_amount} " + f"{stock_name} stocks for ${price_change}") return redirect(url_for("portfolio")) return render_template("stock.html", stock_info_first_part=stock_info_first_part, stock_info_second_part=stock_info_second_part, stock_price=stock_price, change_percent_price=change_percent_price, stock_title=stock_title, max_amount=max_amount, stock_dic=stock_dic, stock_name=stock_name, market_status=market_status, stock_description=stock_description)
def update_quote(symbols): pull_data = yf(symbols) all_data = pull_data.get_stock_price_data(reformat=True) current_price = [all_data[sym]['regularMarketPrice'] for sym in symbols] return current_price
def maincall(ticker_path): ticker = pd.read_csv(ticker_path) ticker = pd.DataFrame(ticker) ticker.sort_values(by=["Ticker"], ascending=True, inplace=True) toke = 'H4WiYj0CVcFpz7hZeFpj9uxKJxcJ' btoke = r'Bearer {}'.format(toke) column_list = [ 'symbol', 'root_symbol', 'description', 'strike', 'bid', 'ask', 'option_type', 'open_interest', 'volume', 'expiration_date' ] expiry = getdate() import_chain2 = pd.DataFrame(data=None, columns=column_list) for num, tick_ in enumerate(ticker["Ticker"]): print((num + 1) + "//" + len(ticker["Ticker"])) for f in expiry: try: res = requests.get( 'https://sandbox.tradier.com/v1/markets/options/chains', params={ 'symbol': tick_, 'expiration': f, 'greeks': 'false' }, headers={ 'Authorization': btoke, 'Accept': 'application/json' }) json_response = res.json() print(res.status_code) #print(json_response) if json_response['options'] == None: raise Exception #ideally best not to call from 2 api's but tradier not working yf1 = yf(tick_) ticker_price = yf1.get_current_price() nest1 = json_response['options']['option'] import_chain = pd.DataFrame.from_dict(nest1) import_chain = import_chain[import_chain.columns.intersection( column_list)] import_chain.sort_values(by='strike', ascending=True) import_chain["center_strike"] = abs(import_chain["strike"] - ticker_price) min_strike = import_chain["center_strike"].min() mid1 = import_chain[import_chain["center_strike"] == min_strike].strike.index[0] mid1 = int(mid1) mid2 = mid1 + 1 import_chain2 = import_chain2.append(import_chain.loc[mid1 - 12:mid1]) import_chain2 = import_chain2.append( import_chain.loc[mid2:mid2 + 12]) del import_chain del import_chain2["center_strike"] del import_chain2["description"] del import_chain2["open_interest"] except: continue print(import_chain2) return import_chain2
if((datetime.datetime.today()-date).days>56): dateBeg = date + datetime.timedelta(days=-56) yearB = dateBeg.strftime('%Y') monthB = dateBeg.strftime('%m') dayB = dateBeg.strftime('%d') dateBeg = yearB + "-" + monthB + "-" + dayB dateEnd = date + datetime.timedelta(days=56) yearE = dateEnd.strftime('%Y') monthE = dateEnd.strftime('%m') dayE = dateEnd.strftime('%d') dateEnd = yearE + "-" + monthE + "-" + dayE ticker = stock[1] ytick = yf(ticker) prices = ytick.get_historical_price_data(dateBeg,dateEnd,time_interval='daily') if(prices[ticker] != None): if('prices' in prices[ticker].keys() and prices[ticker]['prices']!=None): df = pd.DataFrame(prices[ticker]['prices']) mid = 0; while(mid<df.shape[0] and str(date.date()) != df.iloc[mid,7]): mid+=1; if(mid<df.shape[0]): df = df.iloc[mid-30:mid+31,6:] stock = []