def fecth_stock_info(strTickerlist): strList = strTickerlist.split() # df_temp = pd.DataFrame(columns=['TickerID', 'TickerShortName', 'TickerType']) # strList = strList.split() rows_list = [] result = db.session.query(Ticker.TickerID).group_by(Ticker.TickerID).all() dbTickerList = [] if result is not None and len(result) > 0: dbTickerList = [temp[0] for temp in result] for ticker in strList: tempdict = {} if ticker in dbTickerList: continue tempdict['TickerID'] = ticker tempdict['TickerShortName'] = yf.Ticker(ticker).info['shortName'] tempdict['TickerType'] = yf.Ticker(ticker).info['quoteType'] rows_list.append(tempdict) df_temp = pd.DataFrame(rows_list) df_temp.to_sql(name="Ticker", con=db.session.bind, if_exists='append', index=False, dtype={ 'TickerID': sqlalchemy.types.VARCHAR(length=20), 'TickerShortName': sqlalchemy.types.VARCHAR(length=80), 'TickerType': sqlalchemy.types.VARCHAR(length=20) })
def get_prices(from_date, ticker): #y, m, d = from_date #start = datetime.datetime(y, m, d) end = datetime.datetime.now() tic = yf.Ticker(ticker) index = yf.download(ticker, start=from_date, end=end) name = tic.info['shortName'] prices = index['Close'] prices = prices.dropna() return prices, name
def FindTicker(text): #this regex finds anything that looks like a stock ticker so pretty much anything upper case 1-5 characters long #it's honestly kind of a nightmare 'cause I don't get regex temp = re.findall("(?:(?<=\A)|(?<=\s)|(?<=[$]))([A-Z]{1,5})(?=\s|$|[^a-zA-z])", text) #iterates over temp checking if each ticker is on the blacklist not_tickers if the ticker is not blacklisted it is #added to tickers #also #tests each ticker by making a lookup call to fix_yahoo_finance if the call is not empty it adds the ticker #it also checks that the ticker is tradable to avoid adding words that people capitalized that happen to be obscure #non tradeable tickers for ticker in temp: if ticker in not_tickers: temp.remove(ticker) else: test = yf.Ticker(ticker) if test.info != {}: tickers.add(ticker) if test.info['tradeable'] == False: tickers.remove(ticker) if temp ==[]: return tickers #this just removes any duplicates from the temp list temp = set(temp) #iterates over temp first making sure that the ticker is valid then looking at the comment the ticker was found in #and finding any words on the positive and negative lists then using that to add or subtract from the outlook value for ticker in temp: if ticker in tickers: #prints the ticker and the comment it was in so you can check wtf it's doing print("gonna do shit with " + ticker + ' based on \n' + text) if ticker not in outlook: #format is [faith, for, against, symbol, for + against] outlook[ticker]=[0, 0, 0, ticker, 0] if ticker in outlook: for word in negative: if word in text.lower(): outlook[ticker][0] += -1 outlook[ticker][2] += 1 outlook[ticker][4] += 1 for word in positive: if word in text.lower(): outlook[ticker][0] += 1 outlook[ticker][1] += 1 outlook[ticker][4] += 1 return tickers, outlook
def get_realtime_data(cls, symbol): """ Obtener información en tiempo real de un mercado :param symbol: símbolo del mercado en yahoo finance :return: datos en tiempo real en formato JSON """ ticker = yf.Ticker(symbol) info = ticker.info key = {'_id': symbol} data = { '_id': symbol, 'shortName': info['shortName'], 'regularMarketOpen': info['regularMarketOpen'], 'regularMarketDayHigh': info['regularMarketDayHigh'], 'regularMarketDayLow': info['regularMarketDayLow'], 'regularMarketVolume': info['regularMarketVolume'], } return data
def get_stocks_returns(_symbol, _start_date, _end_date, _price_label): _symbol = _symbol _start_date = _start_date _end_date = _end_date _price_label = _price_label try: _stock = yf.Ticker(_symbol) _data = _stock.history(start=_start_date, end=_end_date) used_data = _data[_price_label] ret = [] for i in range(len(used_data) - 1): result = (used_data[i + 1] - used_data[i]) / used_data[i] if not np.isnan([result]).any(): ret.append(result) return ret except: return []
import fix_yahoo_finance as TickerData import json # Used to put data in JSON file # Why do indexes need the carrot top infront of them lol indices = ["^DJI", "^GSPC", "^IXIC", "^RUT", "CL=F", "GC=F", "SI=F", "EURUSD=X", "^TNX", "^VIX"] indices_data = [] for index in indices: # Getting info for every index needed indices_data.append(TickerData.Ticker(index).info) with open("featuredindicies.json", "w") as outfile: # Basically same script as the other featured one json.dump(indices_data, outfile, indent=4, sort_keys=True)
import fix_yahoo_finance as TickerData import os import sys import requests import json # print("Beginning script for autocomplete") # I don't know how the person on the stack overflow got this link :) stocks = requests.get( "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={}®ion=1&lang=en" .format(sys.argv[1])).json()['ResultSet']['Result'] data = [] # First we autocomple for specified string in command line, then we get the info of every stock for stock in stocks: data.append(TickerData.Ticker(stock["symbol"]).info) # Making sure to open the absolute path because ran by swift with open('../../Data/AutoComplete/autocomplete.json', 'w') as outfile: json.dump( data, outfile, indent=4, sort_keys=True ) # Saving autocomplete file, ran with swift so I need the full absolute path # print("Successfully autocompleted data for {}".format(sys.argv[1]))
def add_company(self, ticker): s = yf.Ticker(ticker) self.tick_items[ticker] = s
# -*- coding: utf-8 -*- # Global imports import pandas as pd import fix_yahoo_finance as yf from update_gsheets import Table s_name = 'MSFT' s = yf.Ticker(s_name) # Dummy classes for Indices and Portfolios. Will fill in later class Index: def __repr__(self): return 'Index object {}'.format(self.name) def __init__(self, name): self.name = name self.tick_items = {} def add_company(self, ticker): s = yf.Ticker(ticker) self.tick_items[ticker] = s def remove_company(self, ticker): self.tick_items.pop(ticker)
from Robinhood import Robinhood import json import csv import shelve #from rh_SMA import SMA import decimal import fix_yahoo_finance as yf import requests msft = yf.Ticker("DIS190510C00135000") # get stock info print(print(msft.info)) # get historical market data hist = msft.history(period="max") # show actions (dividends, splits) msft.actions # show dividends msft.dividends # show splits msft.splits ''' ''' def getcloseandopen(trader,stocksymbol): #stocksymbol string, trader = robinhood object applequotedata = trader.quote_data(stocksymbol)
import fix_yahoo_finance as TickerData import json stocks = ["AAPL", "FB", "GOOG", "MSFT", "GE", "TSLA", "INTC", "IBM"] stocks_data = [] for stock in stocks: stocks_data.append(TickerData.Ticker(stock).info) with open('featuredstocks.json', 'w') as outfile: json.dump(stocks_data, outfile, indent=4, sort_keys=True)
def download_returns(ticker, period="max"): if isinstance(period, _pd.DatetimeIndex): p = {"start": period[0]} else: p = {"period": period} return _yf.Ticker(ticker).history(**p)['Close'].pct_change()
def Generate(): #jinja2 setup stuff #template file name template_filename = "display.html" #output file name rendered_filename = "index.html" #these are the vars that are used by Jinja to generate the html document render_vars = { #each of these blocks corosponds to one row of the chart in the generated HTML the format for the puts[][][] is #above 'put1_sym': puts[0][1][3], 'put1_chn': str(yf.Ticker(puts[0][1][3]).info['regularMarketChangePercent'])[:5], 'put1_total': puts[0][1][0], 'put1_up': puts[0][1][1], 'put1_down': puts[0][1][2], 'put2_sym': puts[1][1][3], 'put2_chn': str(yf.Ticker(puts[1][1][3]).info['regularMarketChangePercent'])[:5], 'put2_total': puts[1][1][0], 'put2_up': puts[1][1][1], 'put2_down': puts[1][1][2], 'put3_sym': puts[2][1][3], 'put3_chn': str(yf.Ticker(puts[2][1][3]).info['regularMarketChangePercent'])[:5], 'put3_total': puts[2][1][0], 'put3_up': puts[2][1][1], 'put3_down': puts[2][1][2], 'put4_sym': puts[3][1][3], 'put4_chn': str(yf.Ticker(puts[3][1][3]).info['regularMarketChangePercent'])[:5], 'put4_total': puts[3][1][0], 'put4_up': puts[3][1][1], 'put4_down': puts[3][1][2], 'put5_sym': puts[4][1][3], 'put5_chn': str(yf.Ticker(puts[4][1][3]).info['regularMarketChangePercent'])[:5], 'put5_total': puts[4][1][0], 'put5_up': puts[4][1][1], 'put5_down': puts[4][1][2], 'call1_sym': calls[0][1][3], 'call1_chn': str(yf.Ticker(calls[0][1][3]).info['regularMarketChangePercent'])[:5], 'call1_total': calls[0][1][0], 'call1_up': calls[0][1][1], 'call1_down': calls[0][1][2], 'call2_sym': calls[1][1][3], 'call2_chn': str(yf.Ticker(calls[1][1][3]).info['regularMarketChangePercent'])[:5], 'call2_total': calls[1][1][0], 'call2_up': calls[1][1][1], 'call2_down': calls[1][1][2], 'call3_sym': calls[2][1][3], 'call3_chn': str(yf.Ticker(calls[2][1][3]).info['regularMarketChangePercent'])[:5], 'call3_total': calls[2][1][0], 'call3_up': calls[2][1][1], 'call3_down': calls[2][1][2], 'call4_sym': calls[3][1][3], 'call4_chn': str(yf.Ticker(calls[3][1][3]).info['regularMarketChangePercent'])[:5], 'call4_total': calls[3][1][0], 'call4_up': calls[3][1][1], 'call4_down': calls[3][1][2], 'call5_sym': calls[4][1][3], 'call5_chn': str(yf.Ticker(calls[4][1][3]).info['regularMarketChangePercent'])[:5], 'call5_total': calls[4][1][0], 'call5_up': calls[4][1][1], 'call5_down': calls[4][1][2], 'pop1': popular[0][1][3], 'pop2': popular[1][1][3], 'pop3': popular[2][1][3], 'pop4': popular[3][1][3], 'pop5': popular[4][1][3], 'pop6': popular[5][1][3], 'pop7': popular[6][1][3], 'pop8': popular[7][1][3], 'pop9': popular[8][1][3], 'pop10': popular[9][1][3], #number of mentions for each popular stock well not quite mentions more of each bad or good word 'val1': popular[0][1][4], 'val2': popular[1][1][4], 'val3': popular[2][1][4], 'val4': popular[3][1][4], 'val5': popular[4][1][4], 'val6': popular[5][1][4], 'val7': popular[6][1][4], 'val8': popular[7][1][4], 'val9': popular[8][1][4], 'val10': popular[9][1][4], } #Jinja stuff that I don't understand I borrowed it from a tutorial and it works script_path = os.path.dirname(os.path.abspath(__file__)) template_file_path = os.path.join(script_path, template_filename) rendered_file_path = os.path.join(script_path, rendered_filename) environment = jinja2.Environment(loader=jinja2.FileSystemLoader(script_path)) output_text = environment.get_template(template_filename).render(render_vars).encode("utf-8") #the .encode bit is vital if you want unicode support #output the generated html to the index.html file with open(rendered_file_path, "wb") as result_file: #the "wb" option is vital for unicode support result_file.write(output_text)
# -*- coding: utf-8 -*- """ Created on Wed Apr 17 16:34:03 2019 @author: user """ import fix_yahoo_finance as yf msft = yf.Ticker("MSFT") # get stock info msft.info # get historical market data hist = msft.history(period="max") # show actions (dividends, splits) msft.actions # show dividends msft.dividends # show splits msft.splits
list_status='L', fields='ts_code,symbol,name,area,industry,list_date') # save stock information stock_df = dual_tickers.join(stock_info.set_index('ts_code'), on='A_Ticker') stock_df.to_csv(os.path.join(data_dir, 'stock_info.csv'), index=False, encoding='utf_8_sig') period = 10 # in integral year for tickers in (list(dual_tickers['H_Ticker'].str.slice(start=1)), list(dual_tickers['A_Ticker'].str.replace('.SH', '.SS'))): for t in tickers: try: ticker = yf.Ticker(t) hist_data = ticker.history(period=str(period) + 'y') hist_data = hist_data.reset_index() hist_data.to_csv(os.path.join(data_dir, '{}.csv'.format(t)), index=False) logger.info('Saved daily price of {}.'.format(t)) except ValueError as e: logger.error('Error occurred while getting {}: {} '.format( t, str(e))) # compute hkd cny exchange rate look_back = datetime.today() - timedelta(days=period * 365) # get hkd/usd logger.info('Fetching HKD/USD rates...') hkd_usd = get_fx('DEXHKUS', look_back.strftime('%Y-%m-%d')) # get cny/usd
def start(company): dividends = {} error = False try: data = AV.data(symbol=company, function='DA') except TypeError: print(f"{company} caused an Error") error = True if error == False: # pull data # pull company name if len(data["high"]) < 2000: time.sleep(60) print(company) try: data = AV.data(symbol=company, function='DA') except TypeError: print(f"{company} caused an Error") error = True if len(data["high"]) > 2000: #check against wrong api requests data_yahoo = yf.Ticker(company) shortname = True try: name = data_yahoo.info["shortName"] except KeyError: shortname = False if shortname != False: if " " in name: name = name.replace(" ", "-") if "." in name: name = name.replace(".", "") if "'" in name: name = name.replace("'", "") if "/" in name: name = name.replace("/", "") if "(" in name: name = name.replace("(", "") if ")" in name: name = name.replace(")", "") print(name, company) currency = data_yahoo.info["currency"] currency = currency.upper() market = data_yahoo.info["market"] highs = data["high"] lows = data["low"] closes = data["close"] dates = data["date"] index = check_2001(dates) if index == None: index = 0 highs = rebuild_lists(highs, index) lows = rebuild_lists(lows, index) closes = rebuild_lists(closes, index) dates = rebuild_lists(dates, index) dividend_amounts = data_yahoo.dividends dividend_days = data_yahoo.dividends.keys() div_index = check_same_beginning(dividend_days, dates) dividend_amounts = dividend_amounts[div_index:] dividend_days = dividend_days[div_index:] temp_list = [] for i in dividend_days: temp_list.append(str(i)[0:10]) dividend_days = temp_list days = [] temp_dividends = {} highs = check_for_null(highs, "first") lows = check_for_null(lows, "first") closes = check_for_null(closes, "first") if currency != "EUR" and currency != "ILS": print(currency) with open( f"currencies/foreign-eur/{currency}EUR.FOREX.csv", "r") as currency_data: table = csv.reader(currency_data) cur_dates = [] cur_close = [] for row in table: cur_dates.append(row[0]) cur_close.append(row[4]) #print(type(high)) for i in range(len(highs)): conversion_amount = find_cur_date( dates[i], cur_dates, cur_close) high = highs[i] low = lows[i] close = closes[i] high = round(float(high) * float(conversion_amount), 2) low = round(float(low) * float(conversion_amount), 2) close = round( float(close) * float(conversion_amount), 2) days.append(Day(dates[i], high, low, close)) for n in range(len(dividend_days)): conversion_amount = find_cur_date( dividend_days[n], cur_dates, cur_close) dividend_amount = round( float(dividend_amounts[n]) * float(conversion_amount), 2) temp_dividends[dividend_days[n]] = dividend_amount if currency == "ILS": print(currency) with open(f"currencies/foreign-eur/fx_daily_ILS_EUR.csv", "r") as currency_data: table = csv.reader(currency_data) cur_dates = [] cur_close = [] for row in table: cur_dates.append(row[0]) cur_close.append(row[4]) #print(type(high)) for i in range(len(highs)): conversion_amount = find_cur_date( dates[i], cur_dates, cur_close) high = highs[i] low = lows[i] close = closes[i] high = round(float(high) * float(conversion_amount), 2) low = round(float(low) * float(conversion_amount), 2) close = round( float(close) * float(conversion_amount), 2) days.append(Day(dates[i], high, low, close)) for n in range(len(dividend_days)): conversion_amount = find_cur_date( dividend_days[n], cur_dates, cur_close) dividend_amount = round( float(dividend_amounts[n]) * float(conversion_amount), 2) temp_dividends[dividend_days[n]] = dividend_amount if currency == "EUR": for i in range(len(highs)): days.append(Day(dates[i], highs[i], lows[i], closes[i])) for n in range(len(dividend_days)): temp_dividends[dividend_days[n]] = dividend_amounts[n] dividends[company] = temp_dividends # create Days with date, high, low print("step1 done") calc = DividendCalculator(days) #start calculations # get_prices(calc, company, name, dividends, market, currency)