def loadKeyStatistics (cls, companyID = 'A'): ''' dataset df= pd.DataFrame(columns=['marketCapital','bookValue','ebitda','dividentShare','DividentYield','earningShare', 'BookPrice','SalesPrice','earningsGrowth','earningsRatio', 'symbol', 'date']) ''' yahoo = Share(companyID) yahoo.refresh() try: a = re.search('[a-zA-Z]+', yahoo.get_market_cap()) b = re.search('[a-zA-Z]+', yahoo.get_ebitda()) if a.group(0) is not None: p = re.split('[a-zA-Z]+', yahoo.get_market_cap()) if a.group(0) in 'B': marketCap = float(p[0]) * 10 ** 9 elif a.group(0) in 'M': marketCap = float(p[0]) * 10 ** 6 else: marketCap = -1 print ('Market cap: ' + yahoo.get_market_cap()) else: marketCap = yahoo.get_market_cap() if b.group(0) is not None: p = re.split('[a-zA-Z]+', yahoo.get_ebitda()) if b.group(0) in 'B': ebitda = float(p[0]) * 10 ** 9 elif b.group(0) in 'M': ebitda = float(p[0]) * 10 ** 6 else: ebitda = -1 print ('Ebitda: ' +yahoo.get_ebitda()) else: ebitda = yahoo.get_ebitda() except (TypeError, AttributeError): print ('Missing :' + companyID) e = sys.exc_info()[0] print( "<p>Error: %s</p>" % e ) ebitda = -1.0 marketCap = -1.0 try: company = LoadYahooFinance(symbol = companyID, marketCap = marketCap, bookValue = float(yahoo.get_book_value()), ebitda = ebitda, dividentShare = float(yahoo.get_dividend_share()), dividentYield = float(yahoo.get_dividend_yield()), earningShare = float(yahoo.get_earnings_share()), bookPrice = float(yahoo.get_price_book()), salesPrice = float(yahoo.get_price_sales()), earningsGrowth = float(yahoo.get_price_earnings_growth_ratio()), earningRatio = float(yahoo.get_price_earnings_ratio())) return company except TypeError: print ('Missing :' + companyID) e = sys.exc_info()[0] print( "<p>Error: %s</p>" % e )
def updateInfos(): print("Updating Infos!") with open('static/sp100.json', 'rb') as f: ls = json.load(f) for i in ls: timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") print (i['name']) symbol = Share(i['name']) item = { 'name': i['name'], 'price': symbol.get_price(), 'time': timestamp, 'prev_close': symbol.get_prev_close(), 'open': symbol.get_open(), 'volume': symbol.get_volume(), 'pe': symbol.get_price_earnings_ratio(), 'eps': symbol.get_earnings_share(), 'price_sales': symbol.get_price_sales(), 'ebitda': symbol.get_ebitda(), 'hotness': ms.hotness_function(i['name']), 'BS': ms.bs_function(i['name'])} db.infos.update( {"name": i['name']}, { "$push": {"data": item} } ) print('Collection Infos Updated.') return Response('Collection Infos Updated.')
def createInfos(): if db.infos.count() == 0: print("Creating Infos!!") with open('static/sp100.json', 'rb') as f: ls = json.load(f) for i in ls: timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") symbol = Share(i['name']) item = { 'name': i['name'], 'price': symbol.get_price(), 'time': timestamp, 'prev_close': symbol.get_prev_close(), 'open': symbol.get_open(), 'volume': symbol.get_volume(), 'pe': symbol.get_price_earnings_ratio(), 'eps': symbol.get_earnings_share(), 'price_sales': symbol.get_price_sales(), 'ebitda': symbol.get_ebitda(), 'hotness': ms.hotness_function(i['name']), 'BS': ms.bs_function(i['name'])} db.infos.insert_one({ "name": i['name'], "sector": i['sector'], "data": [item] }) print('Collection Infos Created.') return Response('Collection Infos Created.')
def stock_summary(request, symbol=None): if symbol == None: symbol = request.POST['symbol'] current_stock = Stock() stock = Share(symbol) current_stock.symbol = symbol.upper() current_stock.price = stock.get_price() current_stock.change = stock.get_change() current_stock.volume = stock.get_volume() current_stock.prev_close = stock.get_prev_close() current_stock.stock_open = stock.get_open() current_stock.avg_daily_volume = stock.get_avg_daily_volume() current_stock.stock_exchange = stock.get_stock_exchange() current_stock.market_cap = stock.get_market_cap() current_stock.book_value = stock.get_book_value() current_stock.ebitda = stock.get_ebitda() current_stock.dividend_share = stock.get_dividend_share() current_stock.dividend_yield = stock.get_dividend_yield() current_stock.earnings_share = stock.get_earnings_share() current_stock.days_high = stock.get_days_high() current_stock.days_low = stock.get_days_low() current_stock.year_high = stock.get_year_high() current_stock.year_low = stock.get_year_low() current_stock.fifty_day_moving_avg = stock.get_50day_moving_avg() current_stock.two_hundred_day_moving_avg = stock.get_200day_moving_avg() current_stock.price_earnings_ratio = stock.get_price_earnings_ratio() current_stock.price_earnings_growth_ratio = stock.get_price_earnings_growth_ratio() current_stock.price_sales = stock.get_price_sales() current_stock.price_book = stock.get_price_book() current_stock.short_ratio = stock.get_short_ratio() date_metrics = [] url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+symbol+'/chartdata;type=quote;range=1y/csv' page = urllib2.urlopen(url).read() pagebreaks = page.split('\n') for line in pagebreaks: items = line.split(',') if 'Company-Name:' in line: current_stock.company_name = line[13:len(line)] current_stock.save() if 'values' not in items: if len(items)==6: hd = HistoricalData( stock_id = Stock.objects.get(id=int(current_stock.id)).id, date = items[0][4:6]+'/'+items[0][6:9]+'/'+items[0][0:4], close = items[1][0:(len(items[1])-2)], high = items[2][0:(len(items[2])-2)], price_open = items[3][0:(len(items[3])-2)], low = items[4][0:(len(items[4])-2)], volume = items[5][0:-6]+","+items[5][-6:-3]+","+items[5][-3:len(items[5])]) hd.save() date_metrics.append(hd) del date_metrics[0] return render(request, "stock_summary.html", {'current_stock': current_stock, 'date_metrics': date_metrics})
def getAllStockData(ticker): '''Get a few random tickers.''' stock = Share(ticker) stock.refresh() data = { 'name': stock.get_name(), 'price': stock.get_price(), 'change': stock.get_change(), 'volume': stock.get_volume(), 'prev_close': stock.get_prev_close(), 'open': stock.get_open(), 'avg_daily_volume': stock.get_avg_daily_volume(), 'stock_exchange': stock.get_stock_exchange, 'market_cap': stock.get_market_cap(), 'book_value': stock.get_book_value(), 'ebitda': stock.get_ebitda(), 'dividend_share': stock.get_dividend_share(), 'dividend_yield': stock.get_dividend_yield(), 'earnings_share': stock.get_earnings_share(), 'days_high': stock.get_days_high(), 'days_low': stock.get_days_low(), 'year_high': stock.get_year_high(), 'year_low': stock.get_year_low(), '50day_moving_avg': stock.get_50day_moving_avg(), '200day_moving_avg': stock.get_200day_moving_avg(), 'price_earnings_ratio': stock.get_price_earnings_ratio(), 'price_earnings_growth_ratio': stock.get_price_earnings_growth_ratio(), 'get_price_sales': stock.get_price_sales(), 'get_price_book': stock.get_price_book(), 'get_short_ratio': stock.get_short_ratio(), 'trade_datetime': stock.get_trade_datetime(), 'percent_change_from_year_high': stock.get_percent_change_from_year_high(), 'percent_change_from_year_low': stock.get_percent_change_from_year_low(), 'change_from_year_low': stock.get_change_from_year_low(), 'change_from_year_high': stock.get_change_from_year_high(), 'percent_change_from_200_day_moving_average': stock.get_percent_change_from_200_day_moving_average(), 'change_from_200_day_moving_average': stock.get_change_from_200_day_moving_average(), 'percent_change_from_50_day_moving_average': stock.get_percent_change_from_50_day_moving_average(), 'change_from_50_day_moving_average': stock.get_change_from_50_day_moving_average(), 'EPS_estimate_next_quarter': stock.get_EPS_estimate_next_quarter(), 'EPS_estimate_next_year': stock.get_EPS_estimate_next_year(), 'ex_dividend_date': stock.get_ex_dividend_date(), 'EPS_estimate_current_year': stock.get_EPS_estimate_current_year(), 'price_EPS_estimate_next_year': stock.get_price_EPS_estimate_next_year(), 'price_EPS_estimate_current_year': stock.get_price_EPS_estimate_current_year(), 'one_yr_target_price': stock.get_one_yr_target_price(), 'change_percent_change': stock.get_change_percent_change(), 'divended_pay_date': stock.get_dividend_pay_date(), 'currency': stock.get_currency(), 'last_trade_with_time': stock.get_last_trade_with_time(), 'days_range': stock.get_days_range(), 'years_range': stock.get_year_range() } return data
def rec(p): yahoo = Share(p) a = yahoo.get_prev_close() b = yahoo.get_year_high() c = yahoo.get_year_low() d = yahoo.get_open() e = yahoo.get_ebitda() f = yahoo.get_market_cap() g = yahoo.get_avg_daily_volume() h = yahoo.get_dividend_yield() i = yahoo.get_earnings_share() j = yahoo.get_days_low() k = yahoo.get_days_high() l = yahoo.get_50day_moving_avg() m = yahoo.get_200day_moving_avg() n = yahoo.get_price_earnings_ratio() o = yahoo.get_price_earnings_growth_ratio() print p print "Previous Close: ", a print "Year High", b print "Year Low", c print "Open:", d print "EBIDTA", e print "Market Cap", f print "Average Daily Volume", g print "Dividend Yield", h print "Earnings per share", i print "Days Range:", j, "-", k print "50 Days Moving Average", l print "200 Days Moving Average", m print "Price Earnings Ratio", n print "Price Earnings Growth Ratio", o import MySQLdb db = MySQLdb.connect(host="127.0.0.1", user="******", passwd="1111", db="stocks", local_infile=1) cur = db.cursor() cur.execute( """ INSERT INTO stockapp_info (symbol, prev_close, year_high, year_low, open_price , ebidta, market_cap, avg_daily_vol , dividend_yield, eps , days_low ,days_high, moving_avg_50, moving_avg_200, price_earnings_ratio, price_earnings_growth_ratio) VALUES (%s, %s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s) """, (p, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)) db.commit() cur.close()
def rec(p): yahoo = Share(p) a=yahoo.get_prev_close() b=yahoo.get_year_high() c=yahoo.get_year_low() d=yahoo.get_open() e=yahoo.get_ebitda() f=yahoo.get_market_cap() g=yahoo.get_avg_daily_volume() h=yahoo.get_dividend_yield() i=yahoo.get_earnings_share() j=yahoo.get_days_low() k=yahoo.get_days_high() l=yahoo.get_50day_moving_avg() m=yahoo.get_200day_moving_avg() n=yahoo.get_price_earnings_ratio() o=yahoo.get_price_earnings_growth_ratio() print p print "Previous Close: ",a print "Year High",b print "Year Low",c print "Open:",d print "EBIDTA",e print "Market Cap",f print "Average Daily Volume",g print "Dividend Yield",h print "Earnings per share",i print "Days Range:", j ,"-",k print "50 Days Moving Average",l print "200 Days Moving Average",m print"Price Earnings Ratio", n print"Price Earnings Growth Ratio",o import MySQLdb db = MySQLdb.connect(host="127.0.0.1", user="******",passwd="1111", db="stocks",local_infile = 1) cur=db.cursor() cur.execute (""" INSERT INTO stockapp_info (symbol, prev_close, year_high, year_low, open_price , ebidta, market_cap, avg_daily_vol , dividend_yield, eps , days_low ,days_high, moving_avg_50, moving_avg_200, price_earnings_ratio, price_earnings_growth_ratio) VALUES (%s, %s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s) """, (p,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o)) db.commit() cur.close()
def getStat(stockTicker): StatDict = dict() ticker = Share(stockTicker) mktCap = ticker.get_market_cap() if (mktCap != None and mktCap != 0): StatDict["Market cap"] = mktCap ebitda = ticker.get_ebitda() if (ebitda != None and ebitda != 0): StatDict["EBITDA"] = ebitda peR = ticker.get_price_earnings_ratio() if (peR != None and peR != 0): StatDict["Price Earning Ratio"] = peR EPS = ticker.get_earnings_share() if (EPS != None and EPS != 0): StatDict["EPS"] = EPS divYield = ticker.get_dividend_yield() if (divYield != None and divYield != 0): StatDict["Dividend Yield"] = divYield return StatDict
def getStockInfo(): messageBody = request.values.get('Body') try: """Splitting the text input into action and ticker""" actionMessage = messageBody.split(" ")[0] stockTicker = messageBody.split(" ")[1].upper() """Using the Yahoo api to respond to user requests""" stock = Share(stockTicker) if actionMessage.upper() == "QUOTE": message = "Last Trading Price: "+ stock.get_price() elif actionMessage.upper() == "VOLUME": message = "Last Trading VOLUME: "+ stock.get_volume() elif actionMessage.upper() == "MCAP": message = "MarketCap: "+ stock.get_market_cap() elif actionMessage.upper() == "EBITDA": message = "EBITDA: "+ stock.get_ebitda() except: message = "Wrong input, please try again" resp = twilio.twiml.Response() resp.sms(message) return str(resp)
def getStockInfo(): messageBody = request.values.get('Body') try: """Splitting the text input into action and ticker""" actionMessage = messageBody.split(" ")[0] stockTicker = messageBody.split(" ")[1].upper() """Using the Yahoo api to respond to user requests""" stock = Share(stockTicker) if actionMessage.upper() == "QUOTE": message = "Last Trading Price: " + stock.get_price() elif actionMessage.upper() == "VOLUME": message = "Last Trading VOLUME: " + stock.get_volume() elif actionMessage.upper() == "MCAP": message = "MarketCap: " + stock.get_market_cap() elif actionMessage.upper() == "EBITDA": message = "EBITDA: " + stock.get_ebitda() except: message = "Wrong input, please try again" resp = twilio.twiml.Response() resp.sms(message) return str(resp)
def get_symbol_yahoo_stats_yql(symbols, exclude_name=False): """ Get the symbols' basic statistics from Yahoo Finance. Input: symbols - a list of symbol strings, e.g. ['AAPL'] Output: stats in Pandas DataFrame. This function is ported from pandas_datareader/yahoo/components.py """ sym_list = str2list(symbols) if sym_list == None: return DataFrame() # Yahoo Finance tags, refer to http://www.financialwisdomforum.org/gummy-stuff/Yahoo-data.htm tags = ['Symbol'] if not exclude_name: tags += ['Name'] tags += ['Exchange', 'MarketCap', 'Volume', 'AverageDailyVolume', 'BookValue', 'P/E', 'PEG', 'Price/Sales', 'Price/Book', 'EBITDA', 'EPS', 'EPSEstimateNextQuarter', 'EPSEstimateCurrentYear', 'EPSEstimateNextYear', 'OneyrTargetPrice', 'PriceEPSEstimateCurrentYear', 'PriceEPSEstimateNextYear', 'ShortRatio', 'Dividend/Share', 'DividendYield', 'DividendPayDate', 'ExDividendDate'] lines = [] for sym in sym_list: stock = Share(sym) line = [sym] if not exclude_name: line += [stock.get_name()] line += [stock.get_stock_exchange(), str2num(stock.get_market_cap(), m2b=True), str2num(stock.get_volume()), str2num(stock.get_avg_daily_volume()), str2num(stock.get_book_value()), str2num(stock.get_price_earnings_ratio()), str2num(stock.get_price_earnings_growth_ratio()), str2num(stock.get_price_sales()), str2num(stock.get_price_book()), str2num(stock.get_ebitda()), str2num(stock.get_earnings_share()), str2num(stock.get_EPS_estimate_next_quarter()), str2num(stock.get_EPS_estimate_current_year()), str2num(stock.get_EPS_estimate_next_year()), str2num(stock.get_one_yr_target_price()), str2num(stock.get_price_EPS_estimate_current_year()), str2num(stock.get_price_EPS_estimate_next_year()), str2num(stock.get_short_ratio()), str2num(stock.get_dividend_share()), str2num(stock.get_dividend_yield()), stock.get_dividend_pay_date(), stock.get_ex_dividend_date()] lines.append(line) stats = DataFrame(lines, columns=tags) stats = stats.drop_duplicates() stats = stats.set_index('Symbol') return stats
except: pass try: russell3000.set_value(s,'Average daily volume',shy.get_avg_daily_volume()) except: pass try: russell3000.set_value(s,'Market cap',shy.get_market_cap()) except: pass try: russell3000.set_value(s,'Book value',shy.get_book_value()) except: pass try: russell3000.set_value(s,'Ebitda',shy.get_ebitda()) except: pass try: russell3000.set_value(s,'Dividend share',shy.get_dividend_share()) except: pass #try: # russell3000.set_value(s,'Divident yield',shy.get_dividend_yield()) #except: # pass try: russell3000.set_value(s,'Earnings share',shy.get_earnings_share()) except: pass try:
# -*- coding: utf-8 -*- """ Created on Thu Jul 21 15:17:46 2016 @author: SMALLON """ from yahoo_finance import Share yahoo = Share('YHOO') goog = Share('GOOG') print(yahoo.get_ebitda()) print(yahoo.get_earnings_share()) print(yahoo.get_price_book()) print(goog.get_ebitda()) print(goog.get_earnings_share()) print(goog.get_price_book()) from yahoo_finance import Share IBB = Share('IBB') print(IBB.get_ebitda()) from yahoo_finance import Share li = ['YHOO','GOOG'] yahoo = Share('YHOO') goog = Share('GOOG') print(yahoo.get_book_value()) for i in li: t = Share(i) print(i) print(t.get_book_value())
import glob, pandas as pd from yahoo_finance import Share res = [] for f in glob.glob('data/*.csv'): market = f.replace("data/", "").replace(".csv", "") df = pd.read_csv(f) for line in df.iterrows(): res.append((market, line[1].Symbol, line[1].Name)) for (market, symbol, name) in res: if market == 'nyse': x = Share(symbol) print x.get_book_value() print x.get_ebitda() print x.get_earnings_share() print x.get_price_sales()
etfResult = StockInfo.objects.values('ticker', 'name') #for etfIdx in range(0, len(etfResult)) : tickerStr = etfResult[0]['ticker'] share = Share(tickerStr) dateStr = share.get_trade_datetime()[0:11].replace('-','') ma_200Str = convert(share.get_200day_moving_avg()) ma_50Str = convert(share.get_50day_moving_avg()) book_valueStr = convert(share.get_book_value()) volume_avgStr = convert(share.get_avg_daily_volume()) ebitdaStr = convert(share.get_ebitda()) dividend_yieldStr = convert(share.get_dividend_yield()) market_capStr = convert(share.get_market_cap()) year_highStr = convert(share.get_year_high()) year_lowStr = convert(share.get_year_low()) print tickerStr, dateStr, ma_200Str, ma_50Str, book_valueStr, volume_avgStr, ebitdaStr, dividend_yieldStr, market_capStr, year_highStr, year_lowStr # print share.get_change() # print share.get_days_high() # print share.get_days_low() # print share.get_dividend_share() # print share.get_info() # print share.get_open() # print share.get_prev_close()
# Determine functionality from yahoo_finance import Share tesla = Share('TSLA') print tesla.get_price() print tesla.get_market_cap() print "get_book_value:", tesla.get_book_value() print "get_ebitda:", tesla.get_ebitda() print "get_dividend_share:", tesla.get_dividend_share() print "get_dividend_yield:", tesla.get_dividend_yield() print "get_earnings_share:", tesla.get_earnings_share() print "get_days_high:", tesla.get_days_high() print "get_days_low:", tesla.get_days_low() print "get_year_high:", tesla.get_year_high() print "get_year_low:", tesla.get_year_low() print "get_50day_moving_avg:", tesla.get_50day_moving_avg() print "get_200day_moving_avg:", tesla.get_200day_moving_avg() print "get_price_earnings_ratio:", tesla.get_price_earnings_ratio() print "get_price_earnings_growth_ratio:", tesla.get_price_earnings_growth_ratio( ) print "get_price_sales:", tesla.get_price_sales() print "get_price_book:", tesla.get_price_book() print "get_short_ratio:", tesla.get_short_ratio() print "get_trade_datetime:", tesla.get_trade_datetime() # "a:", print tesla.get_historical(start_date, end_date) # "a:", print tesla.get_info() print "get_name:", tesla.get_name() print "refresh:", tesla.refresh() print "get_percent_change_from_year_high:", tesla.get_percent_change_from_year_high( )
def on_message(self, message): print_logger.debug("Received message: %s" % (message)) if "ValidateTicker" in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed ticker validation request") self.write_message("ValidationFailed:Malformed") return ticker = message[1] if validate_ticker(ticker): self.write_message("ValidationSucceeded:%s" % ticker) print_logger.debug("Ticker was valid") else: self.write_message("ValidationFailed:%s" % ticker) print_logger.debug("Ticker was bad") return elif "GetCompanyName" in message: print_logger.debug("You got here") message = message.split(":") company_ticker = message[1] company_name = get_company_title(company_ticker) self.write_message("CompanyName:%s" % company_name) elif "GetStockData" in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed Message from Client") return ticker = message[1] # Get ticker information share_data = Share(ticker) price = share_data.get_price() percent_change = share_data.get_change() previous_close = share_data.get_prev_close() open_price = share_data.get_open() volume = share_data.get_volume() pe_ratio = share_data.get_price_earnings_ratio() peg_ratio = share_data.get_price_earnings_growth_ratio() market_cap = share_data.get_market_cap() book_value = share_data.get_price_book() average_volume = share_data.get_avg_daily_volume() dividend_share = share_data.get_dividend_share() dividend_yield = share_data.get_dividend_yield() earnings_per_share = share_data.get_earnings_share() ebitda = share_data.get_ebitda() fifty_day_ma = share_data.get_50day_moving_avg() days_high = share_data.get_days_high() days_low = share_data.get_days_low() year_high = share_data.get_year_high() year_low = share_data.get_year_low() two_hundred_day_ma = share_data.get_200day_moving_avg() # Build a string to send to the server containing the stock data share_string = "price:" + str(price) + "|"\ + "percentChange:" + str(percent_change) + "|"\ + "previousClose:" + str(previous_close) + "|"\ + "openPrice:" + str(open_price) + "|"\ + "volume:" + str(volume) + "|"\ + "peRatio:" + str(pe_ratio) + "|"\ + "pegRatio:" + str(peg_ratio) + "|"\ + "marketCap:" + str(market_cap) + "|"\ + "bookValue:" + str(book_value) + "|"\ + "averageVolume:" + str(average_volume) + "|"\ + "dividendShare:" + str(dividend_share) + "|"\ + "dividendYield:" + str(dividend_yield) + "|"\ + "earningsPerShare:" + str(earnings_per_share) + "|"\ + "ebitda:" + str(ebitda) + "|"\ + "50DayMa:" + str(fifty_day_ma) + "|"\ + "daysHigh:" + str(days_high) + "|"\ + "daysLow:" + str(days_low) + "|"\ + "yearHigh:" + str(year_high) + "|"\ + "yearLow:" + str(year_low) + "|"\ + "200DayMa:" + str(two_hundred_day_ma) + "|" self.write_message("StockData;%s" % (share_string)) print_logger.debug("Sending Message: StockData;%s" % (share_string)) elif "GetCompanyDesc" in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed Message from Client") return ticker = message[1] description = update_description_oneoff(ticker) self.write_message("CompanyDescription:%s" % str(description)) elif "GetCompanyDividend" in message and "Record" not in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed Message from Client") return ticker = message[1] # Grab the dividend data from dividata.com dividend_url = "https://dividata.com/stock/%s/dividend" % ticker # This should potentially be a dividend_data = requests.get(dividend_url) dividend_soup = BeautifulSoup(dividend_data.text, 'html5lib') if len(dividend_soup.find_all("table")) > 0: dividend_soup = dividend_soup.find_all("table")[0] else: dividend_soup = "<h3>No dividend history found.</h3>" # Send this div up to the server self.write_message("DividendHistoryData:" + str(dividend_soup)) elif "GetCompanyDividendRecord" in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed Message from Client") return ticker = message[1] # Get the dividend record html for the table and send it up #dividend_record = strip_dividends(ticker, req_proxy) #print_logger.debug("Writing message: " + str(dividend_record)) #self.write_message("DividendRecord:" + str(dividend_record)) elif "GetBollinger" in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed Message from Client") return ticker = message[1] # Switch into the tmp directory old_dir = os.getcwd() os.chdir(TEMP_DIR) # Update the historical data for the ticker symbol YAHOO_FINANCE_HISTORICAL_OBJECT.read_ticker_historical(ticker) bands = BollingerBandStrategy(data_storage_dir="%s/historical_stock_data" % TEMP_DIR\ , ticker_file="%s/stock_list.txt" % TEMP_DIR, filtered_ticker_file=\ "%s/filtered_stock_list.txt" % TEMP_DIR) # Save the graph so that we can show it on the website bands.save_stock_chart(ticker, "%s" % TEMP_DIR) # Also let the server know that we found an answer result = bands.test_ticker(ticker) if result is not None: print_logger.debug("BB:GoodCandidate") self.write_message("BB:GoodCandidate") else: print_logger.debug("BB:BadCandidate") self.write_message("BB:BadCandidate") elif "CheckRobinhoodLogin" in message: print "HELLO WORLD!!! HELLO WORLD!!! HELLO WORLD!!!%s" % ROBINHOOD_INSTANCE if ROBINHOOD_INSTANCE.is_logged_in() is True: self.write_message("RobinhoodLoggedIn:%s" % ROBINHOOD_INSTANCE.username) else: self.write_message("RobinhoodNotLoggedIn") elif "GetPosition" in message: ticker = message.replace("GetPosition:", "") account_positions = ROBINHOOD_INSTANCE.get_position_history(active=True) user_owns_stock = False position_string = "" for position in account_positions: # Get data about the position, including current price. position_data = requests.get(position["instrument"]) position_data = json.loads(position_data._content) position.update(position_data) if position["symbol"] != ticker: continue quote_data = requests.get(position["quote"]); quote_data = json.loads(quote_data._content) position.update(quote_data) position_string = json.dumps(position) user_owns_stock = True if user_owns_stock is True: self.write_message("Position:%s" % position_string) else: self.write_message("Position:None")
from yahoo_finance import Share, Currency yahoo = Share('AAPL') yahoo.refresh() print yahoo.get_info() print yahoo.get_avg_daily_volume() print yahoo.get_stock_exchange() print yahoo.get_book_value() print yahoo.get_ebitda() print yahoo.get_dividend_share() print yahoo.get_price_earnings_ratio() print yahoo.get_short_ratio() print yahoo.get_price_book() # f = open('nasdaqlisted.txt', 'r') # print (f.readline()) # print (f.readline())
try: change = str( float(history[day]['Close']) - float(history[day]['Open'])) chngList.append(change) stockChange.append({ "Date": (str(history[day]['Date'])), "Start": (str(history[day]['Open'])), "End": (str(history[day]['Close'])), "Change": (change[:5]), }) except IndexError: #prevents the code failing when there are fewer than the max range entries in the response. pass #Test for the other yahoo_finance methods print(stock.get_ebitda()) #Calculates the number of entries in the newly updated variable for use in later for loop. numberOfEntries = (len(stockChange)) #Calculate the total change in stock across the date range set. chngInRng = str( float(stockChange[0]['End']) - float(stockChange[numberOfEntries - 1]['Start'])) print("Change from " + start + " to " + end + " = " + str(chngInRng[:5])) #Deletes the content of outputJson before updating the file. with open(outputJson, 'w') as stockDel: stockDel.truncate()
print dayhigh if myargs.dayl is True: daylow = stock.get_days_low() print daylow if myargs.yearhigh is True: yearhigh = stock.get_year_high() print yearhigh if myargs.yearlow is True: yearlow = stock.get_year_low() print yearlow if myargs.ebitda is True: ebitda = stock.get_ebitda() print ebitda if myargs.ps is True: ps = stock.get_price_sales() print ps if myargs.peg is True: peg = stock.get_price_earnings_growth_ratio() print peg if myargs.percentchange is True: change = stock.get_change() getopen = stock.get_open() percentchange = yahoofinancecalc.getPercentChange(stock, change, getopen) print str(percentchange) + "%"
def on_message(self, message): print_logger.debug("Received message: %s" % (message)) self.write_message("Test Message") if "ValidateTicker" in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed ticker validation request") self.write_message("ValidationFailed:Malformed") return ticker = message[1] # The file I have stored didn't end up being a good validation # option as it does not contain a complete list of all # securities. I have to acquire the data from yahoo # finance anyway, so just use that. The Share function # call will throw a NameError exception if the ticker doesn't exist # isValid = current_stock_list.is_valid_stock(ticker) isValid = True try: test = Share(str(ticker)) if test.get_price() is None: isValid = False except NameError: isValid = False if isValid: self.write_message("ValidationSucceeded:%s" % ticker) print_logger.debug("Ticker was valid") else: self.write_message("ValidationFailed:%s" % ticker) print_logger.debug("Ticker was bad") return elif "GetCompanyName" in message: print_logger.debug("You got here") message = message.split(":") company_ticker = message[1] company_name = "" try: company_info="../task_1/google_search_program/cleaned_data/" + company_ticker + "/company_info" company_name = " " f = open(company_info, "r") line = f.readlines() company_name = line[0].split(",") company_name = company_name[0] company_name = company_name.title() if '(' not in company_name: company_name = company_name + " (%s)" % company_ticker except Exception: company_name = get_company_title_proxied(company_ticker) self.write_message("CompanyName:%s" % company_name) elif "ExecuteQuery" in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed input query") self.write_message("QueryResult:Error") data = current_solr_object.issue_query(str(message[1])) data = current_solr_object.recover_links(data) final_string = "QueryResult" for link in data: final_string = final_string + ":" + str(link) self.write_message(final_string) elif "GetStockData" in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed Message from Client") return ticker = message[1] # Get ticker information share_data = Share(ticker) price = share_data.get_price() percent_change = share_data.get_change() previous_close = share_data.get_prev_close() open_price = share_data.get_open() volume = share_data.get_volume() pe_ratio = share_data.get_price_earnings_ratio() peg_ratio = share_data.get_price_earnings_growth_ratio() market_cap = share_data.get_market_cap() book_value = share_data.get_price_book() average_volume = share_data.get_avg_daily_volume() dividend_share = share_data.get_dividend_share() dividend_yield = share_data.get_dividend_yield() earnings_per_share = share_data.get_earnings_share() ebitda = share_data.get_ebitda() fifty_day_ma = share_data.get_50day_moving_avg() days_high = share_data.get_days_high() days_low = share_data.get_days_low() year_high = share_data.get_year_high() year_low = share_data.get_year_low() two_hundred_day_ma = share_data.get_200day_moving_avg() # Build a string to send to the server containing the stock data share_string = "price:" + str(price) + "|"\ + "percentChange:" + str(percent_change) + "|"\ + "previousClose:" + str(previous_close) + "|"\ + "openPrice:" + str(open_price) + "|"\ + "volume:" + str(volume) + "|"\ + "peRatio:" + str(pe_ratio) + "|"\ + "pegRatio:" + str(peg_ratio) + "|"\ + "marketCap:" + str(market_cap) + "|"\ + "bookValue:" + str(book_value) + "|"\ + "averageVolume:" + str(average_volume) + "|"\ + "dividendShare:" + str(dividend_share) + "|"\ + "dividendYield:" + str(dividend_yield) + "|"\ + "earningsPerShare:" + str(earnings_per_share) + "|"\ + "ebitda:" + str(ebitda) + "|"\ + "50DayMa:" + str(fifty_day_ma) + "|"\ + "daysHigh:" + str(days_high) + "|"\ + "daysLow:" + str(days_low) + "|"\ + "yearHigh:" + str(year_high) + "|"\ + "yearLow:" + str(year_low) + "|"\ + "200DayMa:" + str(two_hundred_day_ma) + "|" self.write_message("StockData;%s" % (share_string)) print_logger.debug("Sending Message: StockData;%s" % (share_string)) elif "GetCompanyDesc" in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed Message from Client") return ticker = message[1] # Read in the company description description = "" try: f = open("../task_1/google_search_program/cleaned_data/%s/company_description" % str(ticker), "r") description = f.read() except Exception: # If the file does not exist, get the data manually description = update_description_oneoff(ticker) self.write_message("CompanyDescription:%s" % str(description)) elif "GetCompanyDividend" in message and "Record" not in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed Message from Client") return ticker = message[1] # Grab the dividend data from dividata.com dividend_url = "https://dividata.com/stock/%s/dividend" % ticker # This should potentially be a dividend_data = requests.get(dividend_url) dividend_soup = BeautifulSoup(dividend_data.text, 'html5lib') if len(dividend_soup.find_all("table")) > 0: dividend_soup = dividend_soup.find_all("table")[0] else: dividend_soup = "<h3>No dividend history found.</h3>" # Send this div up to the server self.write_message("DividendHistoryData:" + str(dividend_soup)) elif "GetCompanyDividendRecord" in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed Message from Client") return ticker = message[1] # Get the dividend record html for the table and send it up dividend_record = strip_dividends(ticker, req_proxy) print_logger.debug("Writing message: " + str(dividend_record)) self.write_message("DividendRecord:" + str(dividend_record)) elif "GetBollinger" in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed Message from Client") return ticker = message[1] # Get the bollinger band history along with the 5 day moving average close, lower_band, five_day_ma = calculate_bands(ticker) last_5_days_5_day_ma = [] last_5_days_bb = [] last_5_days_close = [] for i in range(0, 5): last_5_days_5_day_ma.append(five_day_ma[i]) last_5_days_bb.append(lower_band[i]) last_5_days_close.append(close[i]) condition_1 = False condition_2 = False # Condition 1: Has the stock price at close been below the lower bollinger band # at market close within the last 5 days for i in range(0, 5): if last_5_days_close[i] < last_5_days_bb[i]: condition_1 = True # Condition 2: Has the current stock price been above the 5 day moving average sometime in the last 3 days for i in range(0, 3): if last_5_days_close[i] > last_5_days_5_day_ma[i]: condition_2 = True if condition_1 is True and condition_2 is True: self.write_message("BB:GoodCandidate") else: self.write_message("BB:BadCandidate") elif "GetSentiment" in message: message = message.split(":") if len(message) != 2: print_logger.error("Malformed Message from Client") return ticker = message[1] # Lists of sentiment based words good_words = ["buy", "bull", "bullish", "positive", "gain", "gains", "up"] bad_words = ["sell", "bear", "bearish", "negative", "loss", "losses", "down"] DATA_DIRECTORY = "../task_1/google_search_program/cleaned_data/%s" % ticker.upper() positive_file_stats = [] negative_file_stats = [] positive_files = 0 negative_files = 0 neutral_files = 0 trump_count = 0 files_examined = 0 for root, dirs, files in os.walk(DATA_DIRECTORY): path = root.split(os.sep) print((len(path) - 1) * '---', os.path.basename(root)) for file in files: if "article" in file: f = open('/'.join(path) + '/' + file) title = f.readline() article_text = " ".join(f.readlines()) if article_text.count("trump") > 0: trump_count = trump_count + 1 positive_word_count = 0 negative_word_count = 0 files_examined = files_examined + 1 for word in good_words: if word in article_text: positive_word_count = positive_word_count + article_text.count(word) print "Word: %s, %s" % (word, article_text.count(word)) for word in bad_words: if word in article_text: negative_word_count = negative_word_count + article_text.count(word) if positive_word_count > negative_word_count: positive_ratio = float(positive_word_count) / float(negative_word_count + positive_word_count) if positive_ratio > 0.7: positive_files = positive_files + 1 positive_file_stats.append((positive_word_count, negative_word_count)) else: neutral_files = neutral_files + 1 elif positive_word_count == negative_word_count: neutral_files = neutral_files + 1 else: negative_ratio = float(negative_word_count) / float(negative_word_count + positive_word_count) if negative_ratio > 0.7: negative_files = negative_files + 1 negative_file_stats.append((positive_word_count, negative_word_count)) else: neutral_files = neutral_files + 1 print_logger.debug("Sentiment:" + str(positive_files) + ":" + str(negative_files) +\ ":" + str(neutral_files) + ":" + str(trump_count) + ":" + str(files_examined)) self.write_message("Sentiment:" + str(positive_files) + ":" + str(negative_files) +\ ":" + str(neutral_files) + ":" + str(trump_count) + ":" + str(files_examined))
import glob, pandas as pd from yahoo_finance import Share res = [] for f in glob.glob('data/*.csv'): market = f.replace("data/","").replace(".csv","") df = pd.read_csv(f) for line in df.iterrows(): res.append((market, line[1].Symbol, line[1].Name)) for (market,symbol,name) in res: if market=='nyse': x = Share(symbol) print x.get_book_value() print x.get_ebitda() print x.get_earnings_share() print x.get_price_sales()
from yahoo_finance import Share #yahoo = Share('YHOO') #yahoo = Share('SPXC') yahoo = Share('TFM') #yahoo = Share('INDU') #INDEXSP #yahoo = Share('NDX') print yahoo print yahoo.get_open() #'36.60' print yahoo.get_price() print yahoo.get_price_earnings_ratio() print 'get_dividend_share: ',yahoo.get_dividend_share() print 'get_dividend_yield: ',yahoo.get_dividend_yield() print 'get_earnings_share: ',yahoo.get_earnings_share() print 'get_price_earnings_ratio: ',yahoo.get_price_earnings_ratio() print 'get_price_earnings_growth_ratio: ',yahoo.get_price_earnings_growth_ratio() print 'get_year_high: ',yahoo.get_year_high() print 'get_year_low: ',yahoo.get_year_low() print 'get_days_high: ',yahoo.get_days_high() print 'get_days_low: ',yahoo.get_days_low() print 'get_ebitda: ',yahoo.get_ebitda() print 'get_book_value: ',yahoo.get_book_value() #'36.84' #print yahoo.get_trade_datetime() #'2014-02-05 20:50:00 UTC+0000' #get_avg_daily_volume()
def write_technical_files(stock_code, start_time, end_time): # """ Experiment on quandl """ # print('quandl data') # mydata = quandl.get("FRED/GDP") # print(mydata) # print('hello') # data = quandl.get("WIKI/FB.11", start_date="2014-01-01", end_date="2014-12-31", collapse="monthly", transform="diff") # print(data) stock = Share(stock_code) print('stock.get_info()') print(stock.get_info()) print('get_price()') print(stock.get_price()) print('get_change()') print(stock.get_change()) print('get_stock_exchange()') print(stock.get_stock_exchange()) print('get_market_cap()') print(stock.get_market_cap()) print('get_book_value()') print(stock.get_book_value()) print('get_ebitda()') print(stock.get_ebitda()) print('get_dividend_share()') print(stock.get_dividend_share()) print('get_dividend_yield()') print(stock.get_dividend_yield()) print('get_earnings_share()') print(stock.get_earnings_share()) print('get_50day_moving_avg()') print(stock.get_50day_moving_avg()) print('get_200day_moving_avg()') print(stock.get_200day_moving_avg()) print('get_price_earnings_ratio()') print(stock.get_price_earnings_ratio()) print('get_price_earnings_growth_ratio()') print(stock.get_price_earnings_growth_ratio()) print('get_price_sales()') print(stock.get_price_sales()) print('get_price_book()') print(stock.get_price_book()) print('get_short_ratio()') print(stock.get_short_ratio()) print('historical_data') print(stock.get_historical(start_time, end_time)) historical_data = stock.get_historical(start_time, end_time) info_text = "Symbol\t" + "Stock Exchange\t" + "Price\t" + "Market Cap\t" + "Book Value\t" + "EBITDA\t" + "50d Moving Avg\t" + "100d Moving Avg\n" info_text += str(stock.get_info()['symbol']) + "\t" + str(stock.get_stock_exchange()) + "\t" + str(stock.get_price()) + "\t" + str(stock.get_market_cap()) + "\t" + str(stock.get_book_value()) + "\t"; info_text += str(stock.get_ebitda()) + "\t" + str(stock.get_50day_moving_avg()) + "\t" + str(stock.get_200day_moving_avg()) + "\n"; info_directory = '/data/info.tsv' write_to_file(info_directory, info_text) high_low_text = "date\t" + "High\t" + "Low\n" open_close_text = "date\t" + "Open\t" + "Close\n" volume_text = "date\t" + "Volume\n" for index, value in enumerate(historical_data): date = str(historical_data[len(historical_data) - 1 - index]['Date']) date = date.replace('-','') stock_high = str(historical_data[len(historical_data) - 1 - index]['High']) stock_low = str(historical_data[len(historical_data) - 1 - index]['Low']) stock_open = str(historical_data[len(historical_data) - 1 - index]['Open']) stock_close = str(historical_data[len(historical_data) - 1 - index]['Close']) stock_volume = str(int(historical_data[len(historical_data) - 1 - index]['Volume']) / 1000) high_low_text += date + "\t" + stock_high + "\t" + stock_low + "\n" open_close_text += date + "\t" + stock_open + "\t" + stock_close + "\n" volume_text += date + "\t" + stock_volume + "\n" high_low_directory = '/data/highlow.tsv' open_close_directory = '/data/openclose.tsv' volume_directory = '/data/volume.tsv' write_to_file(high_low_directory, high_low_text) write_to_file(open_close_directory, open_close_text) write_to_file(volume_directory, volume_text) ratio_text = "name\t" + "value\n" if stock.get_change() != None: name = "Change" value = str(stock.get_change()) ratio_text += name + "\t" + value + "\n" if stock.get_dividend_share() != None: name = "Dividend Share" value = str(stock.get_dividend_share()) ratio_text += name + "\t" + value + "\n" if stock.get_dividend_yield() != None: name = "Divident Yield" value = str(stock.get_dividend_yield()) ratio_text += name + "\t" + value + "\n" if stock.get_earnings_share() != None: name = "Earning Share" value = str(stock.get_earnings_share()) ratio_text += name + "\t" + value + "\n" if stock.get_price_earnings_ratio() != None: name = "Price Earning" value = str(stock.get_price_earnings_ratio()) ratio_text += name + "\t" + value + "\n" if stock.get_price_earnings_growth_ratio() != None: name = "Price Earning Growth" value = str(stock.get_price_earnings_growth_ratio()) ratio_text += name + "\t" + value + "\n" if stock.get_price_sales() != None: name = "Price Sales" value = str(stock.get_price_sales()) ratio_text += name + "\t" + value + "\n" if stock.get_price_book() != None: name = "Price Book" value = str(stock.get_price_book()) ratio_text += name + "\t" + value + "\n" if stock.get_short_ratio() != None: name = "Short" value = str(stock.get_short_ratio()) ratio_text += name + "\t" + value + "\n" ratio_directory = '/data/ratio.tsv' write_to_file(ratio_directory, ratio_text)