def get_balancesheets(self, ref_symbol): """ Get financials from IEX @params: ref_symbol - Required : symbol (Str) """ stock = Stock(ref_symbol) balancesheet = stock.balancesheet_table(last=1, period="quarter", token=IEX_TOKEN) #Remove unnecesary data balancesheet = balancesheet.loc[:, balancesheet.columns.isin([ "reportDate", "shareholderEquity" ])] #Add symbol name if not balancesheet.empty: balancesheet_len = len(balancesheet.index) balancesheet_arr = [ref_symbol] * balancesheet_len balancesheet.insert(loc=0, column='symbol', value=balancesheet_arr) #Reorder dataframe balancesheet = self.set_column_sequence(balancesheet, ["symbol", "reportDate"]) return balancesheet
def get_stats(self, ref_symbol): """ Get stats from IEX @params: ref_symbol - Required : symbol (Str) """ stock = Stock(ref_symbol) stats = stock.stats_table(token=IEX_TOKEN) #Remove unnecesary data stats = stats.loc[:, stats.columns.isin(["sharesOutstanding"])] #Add symbol and date if not stats.empty: stats_len = len(stats.index) stats_arr = [ref_symbol] * stats_len stats.insert(loc=0, column='symbol', value=stats_arr) #Get current date currDate = datetime.datetime.now().strftime("%Y-%m-%d") stats_arr = [currDate] * stats_len stats.insert(loc=0, column='date', value=stats_arr) #Reorder dataframe stats = self.set_column_sequence( stats, ["symbol", "date", "sharesOutstanding"]) return stats
def get_earnings(self, ref_symbol): """ Get earnings from IEX @params: ref_symbol - Required : symbol (Str) """ stock = Stock(ref_symbol) earnings = stock.earnings_table(last="1", period="annual", token=IEX_TOKEN) #Remove unnecesary data earnings.drop([ "consensusEPS", "estimatedEPS", "numberOfEstimates", "EPSSurpriseDollar", "yearAgoChangePercent", "estimatedChangePercent", "symbolId" ], axis=1, errors='ignore', inplace=True) #Add symbol name if not earnings.empty: earnings_len = len(earnings.index) earnings_arr = [ref_symbol] * earnings_len earnings.insert(loc=0, column='symbol', value=earnings_arr) #Reorder dataframe earnings = self.set_column_sequence(earnings, [ "symbol", "actualEPS", "announceTime", "EPSReportDate", "fiscalPeriod", "fiscalEndDate" ]) return earnings
def get_chart(self, ref_symbol, ref_range='1m'): """ Get charts from IEX @params: ref_symbol - Required : symbol (Str) ref_range - Optional : date range (Str) """ stock = Stock(ref_symbol) chart = stock.chart_table(ref_range, chartByDay=True, chartCloseOnly=True, token=IEX_TOKEN) print(chart) #Remove unnecesary data chart.drop([ "volume", "change", "changePercent", "changeOverTime", "high", "label", "low", "open", "uClose", "uHigh", "uLow", "uOpen", "uVolume" ], axis=1, errors='ignore', inplace=True) #Add symbol name column if not chart.empty: chart_len = len(chart.index) chart_arr = [ref_symbol] * chart_len chart.insert(loc=0, column='symbol', value=chart_arr) #Reorder dataframe chart = self.set_column_sequence(chart, ["symbol", "date"]) return chart
def update_ticker(self, ticker, conn, current_time, test=False): """ Creates a new row in the database for the specified ticker and time :type ticker: string :param ticker: The ticker to be inserted :type conn: :param conn: The connection to the database :type current_time: datetime :param current_time: The current time to be inserted :rtype: void """ s = Stock(str(ticker)) ticker_info = Stock(ticker).quote() c = conn.cursor() values = "('{}', '{}', '{}', '{}', '{}', '{}', {}, {})".format( current_time, ticker, ticker_info['low'], ticker_info['high'], ticker_info['open'], ticker_info['close'], ticker_info['latestPrice'], ticker_info['latestVolume']) if (test): test_file = open('test_fetch.txt', 'a+') test_file.write(values + '\n') test_file.close() cmd = ' INSERT INTO StockData VALUES ' + values c.execute(cmd) conn.commit()
def get_dividends(self, ref_symbol, ref_range='1m'): """ Get dividends from IEX @params: ref_symbol - Required : symbol (Str) ref_range - Optional : date range (Str) """ stock = Stock(ref_symbol) dividends = stock.dividends_table(ref_range, token=IEX_TOKEN) #print( ref_symbol ) #print( dividends ) #Remove unnecesary data dividends.drop(["recordDate", "declaredDate", "flag"], axis=1, errors='ignore', inplace=True) #Add symbol name column if not dividends.empty: dividends_len = len(dividends.index) dividends_arr = [ref_symbol] * dividends_len dividends.insert(loc=0, column='symbol', value=dividends_arr) #Reorder dataframe dividends = self.set_column_sequence( dividends, ["symbol", "exDate", "paymentDate", "amount"]) return dividends
def watchlistQuotes(watchList): #init empty security objects for i in watchList: i = security(str(i), 0, 0, 0, 0, 0, 0) #init blank list of security objects stocks = [] #loop through watchlist for i in watchList: #get price price = Stock(i).price() #get open ohlc = Stock(i).ohlc() openn = ohlc['open']['price'] #get high high = ohlc['high'] #get low low = ohlc['low'] #get close closee = ohlc['close']['price'] #calculate net change change = closee - openn netChange = '{:+.2f}'.format(change) #set i = security(str(i), price, openn, high, low, closee, netChange) stocks.append(i) #loop though list of security object for i in range(0, len(stocks)): print(stocks[i - 1])
def update_stock_info(self,ticker): """ updates stock information in database for argument ticker """ want = ['low', 'high', 'open', 'close', 'latestPrice', 'latestVolume'] sys.stdout = open(os.devnull, 'w') updatedInfo = Stock(ticker).quote() sys.stdout = sys.__stdout__ thevalues = {key:value for key, value in updatedInfo.items() if key in want} detime = time.strftime("%H:%M") conn = sqlite3.connect(self.db) c = conn.cursor() print("Opened database successfully") print(f"the values to insert...\n{thevalues}") print(type(thevalues['latestVolume'])) c.execute("INSERT INTO STOCKDATA VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (detime, ticker, thevalues['low'], thevalues['high'], thevalues['open'], thevalues['close'], thevalues['latestPrice'], thevalues['latestVolume'])) conn.commit() conn.close()
def update_stock_info(self,ticker): """ updates stock information in database for argument ticker :type ticker : string :param ticker : ticker name """ want = ['low', 'high', 'open', 'close', 'latestPrice', 'latestVolume'] sys.stdout = open(os.devnull, 'w') updatedInfo = Stock(ticker).quote() sys.stdout = sys.__stdout__ thevalues = {key:value for key, value in updatedInfo.items() if key in want} detime = time.strftime("%H:%M") conn = sqlite3.connect(self.db) c = conn.cursor() c.execute("INSERT INTO StockData VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (detime, ticker, thevalues['low'], thevalues['high'], thevalues['open'], thevalues['close'], thevalues['latestPrice'], thevalues['latestVolume'])) conn.commit() conn.close()
def get_company(self, ref_symbol): """ Get company information from IEX @params: ref_symbol - Required : symbols ([Str]) """ stock = Stock(ref_symbol) company = stock.company_table(token=IEX_TOKEN) #Remove unnecesary data company.drop([ "website", "CEO", "primarySicCode", "employees", "address", "address2", "state", "city", "zip", "country", "phone" ], axis=1, errors='ignore', inplace=True) #Reorder dataframe if not company.empty: company = self.set_column_sequence(company, [ "symbol", "companyName", "exchange", "industry", "description", "securityName", "issueType", "sector" ]) #print( company ) return company
def print_stocks(): fit = float(Stock("FIT").price()) tes = float(Stock("TSLA").price()) if fit > 5: telegram_send.send(messages=['FIT: ' + str(fit)]) flag1 = 1 if tes > 200: telegram_send.send(messages=['TSLA: ' + str(tes)]) flag2 = 1 if flag1 == 1: time.sleep(300) flag1 = 0 flag2 = 0
def save_tickers(n, fileName): #URL = r.get("https://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download").text tickerList = [] pure = r.get("https://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&pagesize=150").text #get pure text from the website i =0 #counter #print(URL) baseStockUrl = "https://api.iextrading.com/1.0/stock" for curr in pure.splitlines(): #splits lines at new line if i == n: break else: try: subst = curr[curr.rfind("symbol/")::1] # parse all lines until 'symbol/' is found https://www.nasdaq.com/symbol/asps/stock-report"> ticker = subst[subst.find("/")+1::1] # parse subst which contains 'asps/stock-report">' ticker2 = ticker[:ticker.find("/")] #parse to just get the symbol if ticker2.find("\"") == -1 and ticker2 != "": outputRedirect = io.StringIO() #redirects output so it doesnt print to console sys.stdout = outputRedirect #sets standard out to output redirect Stock(ticker2).price() ## checks if valid ticker, if its not found go to except sys.stdout = sys.__stdout__ #resets standard out back to stdout i = i +1 tickerList.append(ticker2) except: pass file = open(fileName,"w") #creates ticker.text to write to it. file.write("\n".join(tickerList)) #puts everything that was in the list created with tickers on a new line file.close() #closes file
def get_book(ticker): """ returns stock information in the form of a dicitonary """ book = Stock(ticker).quote() return book
def update_tickers(ticker_filename, graph_filename, info_filename): info = open(info_filename, "a") with open(graph_filename, 'at', newline='') as file: writer = csv.writer(file, delimiter=',') file = open(ticker_filename, "r") for line in file: symbol = line.replace("\n", "") stock = Stock(symbol).quote() currentDT = datetime.datetime.now() currTime = currentDT.strftime("%H:%M") column = [currTime, stock['symbol'], stock['latestPrice'], stock['latestVolume'], stock['close'], stock['open'], stock['low'], stock['high']] writer.writerow(i for i in column) info_counter = 0 for data in column: info.write(str(data)) if info_counter == 2 or info_counter == 3: info.write("\t") info.write("\t") info_counter = info_counter + 1 info.write("\n") info.write("\n")
def get_tickers_from_url(self, url, currentTickers): """Gets stock tickers from a specified NASDAQ url. :Details: Function retrieves a maximum amount of tickers from a specified URL. A *NASDAQ URL* is expected. The maximum quantity takes in to account the current amount of tickers already found outside the function. The function will return whenever the maximum is reached or the page tickers are exhausted. The intent is to feed this function each successive page URL. :param str url: URL to retrieve stock data from. :param int currentTickers: The current amount of tickers already retrieved. :return: Retrieved tickers. :rtype: set of str """ # Get Initial Web Page response = requests.get(url) # Harvest Tickers tickers = set(re.findall(self.__PATTERN_SYMBOLS, response.text)) # Check Tickers for Validity & Limit validTickers = set() for ticker in tickers: if len(validTickers)+currentTickers >= self.maximum: print(len(validTickers)+currentTickers,"Valid Ticker(s) Found. User Specified Limit Reached.") break try: Stock(ticker).price() validTickers.add(ticker.upper()) except: print("Invalid Ticker:", ticker) return validTickers
def saveTickers(n, filename): if int(n) > 150: raise Exception( "You need to give me a number less than or equal to 150!") # Create request with 150 item url request = requests.get(url=pull150ItemsURL()) parser = PyQuery(request.text) table = parser("#CompanylistResults") table_parser = PyQuery(table) symbols = table_parser("h3") symbol_list = [symbol for symbol in symbols.text().split()] for i, ticker in enumerate(symbol_list): try: if i <= int(n): Stock(symbol=ticker).price() else: break except: symbol_list.remove(ticker) f = open(filename, "w") for i, symbol in enumerate(symbol_list): if i <= int(n): f.write(symbol + '\n') else: break f.close()
def test_tables(): ibm = Stock("GM") assert ibm.chart_table().empty == False assert ibm.dividends_table().empty == False assert ibm.effective_spread_table().empty == False assert ibm.financials_table().empty == False assert ibm.volume_by_venue_table().empty == False
def get_financials(self, ref_symbol): """ Get financials from IEX @params: ref_symbol - Required : symbol (Str) """ stock = Stock(ref_symbol) financials = stock.financials_table(period="annual", token=IEX_TOKEN) #Add symbol name if not financials.empty: financials_len = len(financials.index) financials_arr = [ref_symbol] * financials_len financials.insert(loc=0, column='symbol', value=financials_arr) #Reorder dataframe financials = self.set_column_sequence(financials, ["symbol", "reportDate"]) return financials
def save_tickers(self): """ Uses selenium to crawl webpage and access up to 110 valid tickers using price() from iex API to verify validity, then writes them to 'tickers.txt' with one ticker per line """ driver = webdriver.Chrome('./chromedriver') link = "http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQrender=download" driver.get(link) driver.find_element_by_xpath('//*[@id="main_content_lstpagesize"]/option[3]').click() link = driver.current_url tickers = [] d = requests.get(link) html_file = open('html.txt', 'w') html_file.write(d.text) html_file = open('html.txt') tickerbaselink = "https://www.nasdaq.com/symbol/" i = 1 for line in html_file.readlines(): x = re.search(tickerbaselink, line) if x is not None: if i is 1: t = os.path.basename(x.string).upper() t = t[:-3] tickers.append(t) if i is 3: i = 0 i = i + 1 ticker_filename = "tickers.txt" f = open(ticker_filename, 'w') n = 0 validTickers = [] for x in tickers: if n == int(self.ticker_count): break else: try: sys.stdout = open(os.devnull, 'w') Stock(x).price() sys.stdout = sys.__stdout__ validTickers.append(x) n += 1 f.write('{0}\n'.format(x)) except Exception: print("not valid") os.remove("./html.txt")
def update_info(ticker, f): """ Gets quote data for specific ticker and writes to output file in a csv format """ stock = Stock(ticker).quote() args = [ time.strftime('%H:%M', time.localtime(stock['latestUpdate'] // 1000)), stock['symbol'], stock['latestPrice'], stock['latestVolume'], stock['close'], stock['open'], stock['low'], stock['high'] ] f.write(",".join(map(str, args))) f.write('\n')
def check_if_valid(self): """ check_if_valid() function returns true only if price is found """ try: price = Stock(self.ticker).price() if price: return True else: return False except (Exception, TypeError): return False
def update_stock_info(self, ticker, stop): """ update_stock_info function grabs corresponding ticker data and stores into sqlite database Args: ticker what ticker to look for stop when to stop grabbing info """ now = datetime.datetime.now() if (now.time() > stop): return False conn = sqlite3.connect(self.db) c = conn.cursor() sys.stdout = open(os.devnull, "w") c.execute( 'INSERT INTO StockData(Time, Ticker, Low, High, Open, Close, Price, Volume) VALUES(?, ?, ?, ?, ?, ?, ?, ?)', (str(now.strftime("%H:%M")), str(ticker), str(Stock(ticker).quote().get('low')), str(Stock(ticker).quote().get('high')), str(Stock(ticker).quote().get('open')), str(Stock(ticker).quote().get('close')), str(Stock(ticker).quote().get('latestPrice')), str(Stock(ticker).quote().get('latestVolume')))) conn.commit() c.close() conn.close() sys.stdout = sys.__stdout__ return True
def check_if_valid(ticker): ''' uses iex-api-python to check if ticker has valid price function Args: ticker: fetched ticker from NASDAQ website ''' try: price = Stock(ticker).price() if price: return True else: return False except(Exception,TypeError): return False
def is_valid(symbol): """ args: symbol: the ticker symbol that is being checked use: checks to see if the ticker symbol is a valid stock in the API. If price() is False, it will not be writen to the file. """ try: Stock(symbol).price() return True except: return False
def getQuotes(): x = input( 'If you would like additional quotes, enter a symbol at any time: ') x = x.upper() #get price price = Stock(x).price() #get open ohlc = Stock(x).ohlc() openn = ohlc['open']['price'] #get high high = ohlc['high'] #get low low = ohlc['low'] #get close closee = ohlc['close']['price'] #calculate net change change = closee - openn netChange = '{:+.2f}'.format(change) #set y = security(str(x), price, openn, high, low, closee, netChange) print('\n') print(y)
def get_quote(self, ref_symbol): """ Get quote from IEX @params: ref_symbol - Required : symbol (Str) """ stock = Stock(ref_symbol) quote = stock.quote_table(token=IEX_TOKEN) #Remove unnecesary data quote = quote.loc[:, quote.columns.isin([ "symbol", "close", "closeTime", "marketCap", "peRatio" ])] if not quote.empty: #Don't return if any are null #Actually still need stocks with null mcap and pe for performance #if (quote['symbol'] != 'SPY').any() and quote.isnull().values.any(): # return pandas.DataFrame() if quote[['close', 'closeTime']].isnull().values.any(): return pandas.DataFrame() #Change date format date = pandas.Timestamp(quote["closeTime"].iloc[0], unit='ms').strftime('%Y-%m-%d') quote.drop(["closeTime"], axis=1, errors='ignore', inplace=True) quote["date"] = date #Reorder dataframe quote = self.set_column_sequence( quote, ["symbol", "date", "close", "marketCap", "peRatio"]) return quote
def writeToFile(ticker, info_writer, endTime): currentDT = datetime.datetime.now() hour = str(currentDT.hour) minute = currentDT.minute if minute < 10: minute = '0' + str(minute) else: minute = str(minute) # If function exceeds its endTime then exit the module if currentDT >= endTime: return ticker_info = Stock(symbol=ticker).quote() info_writer.writerow([ hour + ':' + minute, ticker, ticker_info['low'], ticker_info['high'], ticker_info['open'], ticker_info['close'], ticker_info['latestPrice'], ticker_info['latestVolume'] ])
def update_stock(self, ticker, c): """ args: ticker: the ticker to update c: cursor to execute sql commands use: request info of the ticker and update the database with the info """ t = datetime.time(datetime.now()).isoformat(timespec='minutes') q = Stock(ticker).quote() stock = [ t, q['symbol'], q['low'], q['high'], q['open'], q['close'], q['latestPrice'], q['latestVolume'] ] insert = "insert into StockData values (?, ?, ?, ?, ?, ?, ?, ?)" c.execute(insert, stock) print(stock)
def fetch(timeLimit, tickerFname, infoFname): # Collect Stock Tickers tickerSet = getTickers(tickerFname) # Setup for Print File exists = os.path.isfile(infoFname) outfile = open(infoFname, "a") if not exists: # Initialize File if New outfile.write( "Time,Ticker,latestPrice,latestVolume,Close,Open,low,high\n") outfile.flush() # Timed Execution endTime = time.time() + timeLimit currentMinute = datetime.datetime.now().minute firstPass = True while time.time() < endTime: if firstPass or (datetime.datetime.now().minute != currentMinute): # if not exists and firstPass: print("Retrieving Stock Data") firstPass = False else: print("Updating Stock Data at", datetime.datetime.now().time()) # Execute Update Here for ticker in tickerSet: tickInfo = Stock(ticker).quote() tickStr = str( makeTimeString(datetime.datetime.now().hour, datetime.datetime.now().minute) + "," + ticker + "," + str(tickInfo["latestPrice"]) + ",") tickStr = tickStr + str(tickInfo["latestVolume"]) + "," + str( tickInfo["close"]) + "," + str(tickInfo["open"]) + "," tickStr = tickStr + str(tickInfo["low"]) + "," + str( tickInfo["high"]) + "\n" outfile.write(tickStr) currentMinute = datetime.datetime.now().minute outfile.flush() print("Time Limit Has Expired at", datetime.datetime.now().time()) outfile.close()
def confirm_ticker(t): """ Uses iex.Stock().price to check if a ticker has a listed price """ try: #blocking std output from Stock().price() blockPrint() #checking if ticker has price Stock(t).price() #Restoring std output enablePrint() return True except: #print("Unexpected error:", sys.exc_info()[0]) #print(f"Ticker: {t} not found") return False