def SearchPERatio(self): for x in range(8,1503,5): # for x in range(8,self.soup.select('.basic2').__len__(),5): if self.soup.select('.basic2')[x].text != '-': if float(self.soup.select('.basic2')[x].text) <=float(self.TargetPERatio) : QueryPrice = Share(self.soup.select('.basic2')[x-2].text+'.TW') #Query the API if QueryPrice.get_earnings_share()!=None and float(QueryPrice.get_earnings_share())>=self.TargetEPS: # Get EPS self.AllData.append(self.soup.select('.basic2')[x-2].text) # StockNumber self.AllData.append(QueryPrice.get_open()) # Get FinalPrice self.AllData.append(self.soup.select('.basic2')[x].text) # Get PERatio self.AllData.append(QueryPrice.get_earnings_share())
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 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 fundamentalStats(stock): try: stokOutput = Share(stock) openPrice = stokOutput.get_open() closePrice = stokOutput.get_prev_close() dailyDelta = stokOutput.get_change() earningsShare = stokOutput.get_earnings_share() fiddyDay = stokOutput.get_50day_moving_avg() priceBook = stokOutput.get_price_book() peeEee = stokOutput.get_price_earnings_ratio() pegRatio = stokOutput.get_price_earnings_growth_ratio() if (float(priceBook) < 1.5 and float(peeEee) < 50 and float(pegRatio) < 2 and float(peeEee) > 0): csvList = [stock, "open price:", openPrice, "previous close:", closePrice, "daily deltas:", dailyDelta, "earnings per share:", earningsShare, "50 day moving avg:", fiddyDay, "price/book:", priceBook, "price/earnings:", peeEee, "peg:", pegRatio, "\n"] print (stock, "will be imported to Excel.") stockPicks = open("picks.csv", "a", newline='') writeCSV = csv.writer(stockPicks, dialect='excel') for stock in csvList: writeCSV.writerow([stock]) else: print (stock, "does not meet criteria.") except: print(stock, "is missing a defined key statistic.")
def tesfile(self,event): count =0 f = open("export.csv","r") for row in csv.reader(f): # print(row) count+=1 if count >=4 and (row[2]!='-' and float(row[2])<=self.TargetPERatio): QueryPrice=Share(row[0]+'.TW') # volumetemp = self.InTimeObject.GetInTimeStockInfo(row[0]) # print(volumetemp) if QueryPrice.get_earnings_share()!=None and float(QueryPrice.get_earnings_share())>=self.TargetEPS and float: #float(QueryPrice.get_earnings_share())>=self.TargetEPS: if float(QueryPrice.get_volume())>=self.TargetVolume and float(QueryPrice.get_price())<=self.TargetPrice: information = row[0] + row[1]+"\t" + QueryPrice.get_earnings_share() +" "+ QueryPrice.get_volume()+" " + QueryPrice.get_price() self.ThreemenBox.insert(END,information) if count==50: break
def SelectStock(self): wb = Workbook() ws = wb.active with open('export.csv',newline='') as f: reader = csv.reader(f) index = 2 title = ["代號","名稱","股價","本益比","EPS"] for x in range(0,5,1): ws.cell(row=1,column=x+1,value=title[x]) for row in reader: if row!=[]: try: if int(row[0]) < 10000: # print(row[2]) if "-" not in (row[2]) and float(row[2]) < 20: # print(row) Query = Share(row[0]+'.TW') #print(float(Query.get_earnings_share())) #print(float(Query.get_open())) #print("******************") if float(Query.get_open()) >= 20 and float(Query.get_open()) <= 90 and float(Query.get_earnings_share())>1.2: print(row) ws.cell(row=index , column=1 , value = row[0]) ws.cell(row=index , column=2 , value = row[1]) ws.cell(row=index , column=3 , value = Query.get_open()) ws.cell(row=index , column=4 , value = row[2]) ws.cell(row=index , column=5 , value = Query.get_earnings_share()) index = index + 1 except: pass wb.save("Final.xlsx")
def report_current_from_yahoo(symbol): yahoo = Share(symbol) price_cur = yahoo.get_price() eps = yahoo.get_earnings_share() pe = yahoo.get_price_earnings_ratio() print 'yahoo: price=%s eps=%s, pe=%s'%(price_cur, eps, pe)
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 stocks(dict_response): message = "" try: query_text = dict_response['_text'].lower() if query_text.find("stocks ") != -1: query_text = query_text[7:] y = Share(query_text) message += "Trading information for " + y.get_name( ) + " (" + query_text + ") :\n" message += "Opened: " + y.get_open() + "\n" message += "Current: " + y.get_price() + "\n" message += "Earnings share: " + y.get_earnings_share() + "\n" message += "Short ratio: " + y.get_short_ratio() + "\n" message += "Previous close: " + y.get_prev_close() + "\n" except: message = technical_issues() return message
def get_company_info(ticker): try: s = Share(ticker) data = { 'Market_cap': s.get_market_cap(), 'Average_volume': s.get_avg_daily_volume(), 'EPS': s.get_earnings_share(), 'Short_ratio': s.get_short_ratio(), 'PE': s.get_price_earnings_ratio(), 'PEG': s.get_price_earnings_growth_ratio(), } return DataFetcher._extract_company_info(data) except YQLQueryError: logger.error("Company info not found for {}".format(ticker)) except Exception as e: logger.error("Unexpected error occured: {}".format(e)) return {}
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 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
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 main(): # this adds commas to all numbers greater than one thousand locale.setlocale(locale.LC_ALL, 'en_US') # if statement that checks for args. error/help message will appear if no args if (len(sys.argv) == 1): print "\nPlease supply one or more tickers. Example: python stephan_s_stock_quote.py GOOG\n" else: for counter in range(1, len(sys.argv)): # this is where we fetch our stocks from y = Share(sys.argv[counter]) # this is the output along with a message regarding the CSV file print "\nSymbol: " + str(sys.argv[counter]) print "Company Name: " + str(y.get_name()) print "Market Capitalization: $" + str(y.get_market_cap()) print "Earnings Per Share: $" + str( locale.format( "%d", float(y.get_earnings_share()), grouping=True)) print "P/E Ratio: " + str(y.get_price_earnings_ratio()) print "Average Volume: " + str( locale.format( "%d", float(y.get_avg_daily_volume()), grouping=True)) print "Today's Volume: " + str( locale.format("%d", float(y.get_volume()), grouping=True)) print "Today's Closing Price: $" + str(y.get_price()) print "Percent Change: " + str(y.get_percent_change()) + "\n" print "A CSV file of your selected stock tickers has been downloaded to your computer under the name 'stocks.csv'. " + "\n" print "The CSV file will be downloaded to the same folder that this program was stored in." + "\n"
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")
def stock_quote_get(): print(request.args.get('symbol')) symbol = str(request.args.get('symbol')) # get all the relevant data from the Yahoo Finance API stock = Share(symbol) stock_name = stock.get_name() stock_symbol = stock.symbol stock_price = stock.get_price() stock_change = stock.get_change() stock_change_pct = stock.get_percent_change() prev_close = stock.get_prev_close() open = stock.get_open() day_range = stock.get_days_range() year_range = stock.get_year_range() volume = stock.get_volume() avg_volume = stock.get_avg_daily_volume() market_cap = stock.get_market_cap() pe_ratio = stock.get_price_earnings_ratio() eps = stock.get_earnings_share() dividend = stock.get_dividend_share() dividend_yld = stock.get_dividend_yield() dividend_ex_date = stock.get_ex_dividend_date() yr_target = stock.get_one_yr_target_price() historical = stock.get_historical('2017-01-01', date.isoformat(date.today())) # put the data into the DynamoDB database table = dynamodb.Table('Stocks') response = table.put_item( Item={ 'symbol': symbol, 'date': date.isoformat(date.today()), 'prev_close': prev_close, 'open': open, 'day_range': day_range, 'year_range': year_range, 'volume': volume, 'avg_volume': avg_volume, 'market_cap': market_cap, 'pe_ratio': pe_ratio, 'eps': eps, 'dividend': dividend, 'dividend_yld': dividend_yld, 'dividend_ex_date': dividend_ex_date, 'yr_target': yr_target, }) close_history = [] for point in historical: close_date = point['Date'] close_date = int( time.mktime(datetime.strptime(close_date, "%Y-%m-%d").timetuple())) close_price = point['Adj_Close'] close_price = float(close_price) close_history.append([close_date, close_price]) return render_template("stock/stock_detail.html", stock_name=stock_name, stock_symbol=stock_symbol, stock_price=stock_price, stock_change=stock_change, stock_change_pct=stock_change_pct, prev_close=prev_close, open=open, day_range=day_range, year_range=year_range, volume=volume, avg_volume=avg_volume, market_cap=market_cap, pe_ratio=pe_ratio, eps=eps, dividend=dividend, dividend_yld=dividend_yld, dividend_ex_date=dividend_ex_date, yr_target=yr_target, close_history=close_history)
class Symbol(object): def __init__(self, symbol, s_date=None, e_date=None): self.log=Logging() self.name=symbol self.created=datetime.datetime.utcnow() self.log.info("created {}".format(self.name)) try: self.share=Share(symbol) except: self.log.error("platform is offline or not connecting") if s_date and e_date: self.begin=s_date self.end=e_date try: self.share=Share(symbol) self.data=self.share.get_historical(self.begin, self.end) self.log.refresh("{} data collected".format(self.name)) except: self.log.error("platform is offline or not connecting") def refresh_data(self, s_date=None, e_date=None): if s_date and e_date: try: share=Share(self.name) self.begin=s_date self.end=e_date share.get_historical(s_date, e_date) self.log.refresh("{} data collected".format(self.name)) except: self.log.error("platform is offline or not connecting") def market_cap(self): try: self.market_cap = self.share.get_market_cap() self.log.info("{} market cap refreshed".format(self.name)) except: self.log.error("platform is offline or not connecting") def earnings_per_share(self): try: self.eps = self.share.get_earnings_share() self.log.info("{} eps refreshed".format(self.name)) except: self.log.error("platform is offline or not connecting") def moving_average_50(self): try: self.moving_average_50 = self.share.get_50day_moving_average() self.log.info("{} 50 day moving ave refreshed".format(self.name)) except: self.log.error("platform is offline or not connecting") #implement TODO def nday_moving_average(self, n): try: self.moving_average_n = None self.log.info("{} {} day moving ave refreshed".format(self.name, n)) except: self.log.error("platform is offline or not connecting") def price_earnings_ratio(self): try: self.price_to_earnings = self.share.get_price_earnings_ratio() self.log.info("{} price to earnings refreshed".format(self.name)) except: self.log.error("platform is offline or not connecting") def book_value(self): try: self.book = self.share.get_price_book() self.log.info("{} book value refreshed".format(self.name)) except: self.log.error("platform is offline or not connecting") def year_high(self): try: self.year_high = self.share.get_change_from_year_high() self.log.info("{} year high change refreshed".format(self.name)) except: self.log.error("platform is offline or not connecting") def year_low(self): try: self.year_low = self.share.get_change_from_year_low() self.log.info("{} year low change refreshed".format(self.name)) except: self.log.error("platform is offline or not connecting") def target_price(self): try: self.year_target = self.share.get_change_from_year_high() self.log.info("{} year target change refreshed".format(self.name)) except: self.log.error("platform is offline or not connecting") def year_range(self): try: self.year_range = self.share.get_change_from_year_high() self.log.info("{} year range change refreshed".format(self.name)) except: self.log.error("platform is offline or not connecting")
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: russell3000.set_value(s,'Year high',shy.get_year_high()) except: pass try: russell3000.set_value(s,'Year low',shy.get_year_low()) except: pass try: russell3000.set_value(s,'50 days MA',shy.get_50day_moving_avg()) 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()
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 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 )
print "The CSV file will be downloaded to the same folder that this program was stored in." + "\n" # code that creates the CSV file with open('stocks.csv', 'w') as fp: outputFile = csv.writer(fp) data1 = [[ 'Symbol', 'Company Name', 'Market Capitalization', 'Earnings Per Share', 'P/E Ratio', 'Average Volume', 'Today\'s Volume', 'Today\'s Closing Price', 'Percent Change' ]] outputFile.writerows(data1) for counter in range(1, len(sys.argv)): y = Share(sys.argv[counter]) data2 = [[ str(sys.argv[counter]), str(y.get_name()), str(y.get_market_cap()), str(y.get_earnings_share()), str(y.get_price_earnings_ratio()), str(y.get_avg_daily_volume()), str(y.get_volume()), str(y.get_price()), str(y.get_percent_change()) ]] outputFile.writerows(data2) if __name__ == '__main__': main()
print getopen if myargs.getbook is True: getbook = stock.get_book_value() print getbook if myargs.dividendshare is True: getdiv = stock.get_dividend_share() print getdiv if myargs.dividendyield is True: dividendyield = stock.get_dividend_yield() print dividendyield if myargs.eps is True: eps = stock.get_earnings_share() print eps if myargs.dayh is True: dayhigh = stock.get_days_high() 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:
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))
# 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( )
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()
def populateStock(shareString, sqlConnection): timedout = True curShare = None while timedout: try: curShare = Share(shareString) timedout=False except: timedout=True time.sleep(5) print('Stock querry timed out, retrying') if curShare.get_price() == 0: return detailShare = None try: detailShare = stockretriever.get_current_info([shareString]) pprint(detailShare) except: print('Yql error caught, continuing excluding analyst estimates for ' + shareString) EPSEstimateNextYear = None EPSEstimateCurrentYear = None EPSEstimateNextQuarter = None if(detailShare != None): if(detailShare['PriceBook']==None): print('No data on this stock, will not populate table.') return if(detailShare['EPSEstimateNextYear']!=None): EPSEstimateNextYear = float(detailShare['EPSEstimateNextYear']) if(detailShare['EPSEstimateCurrentYear']!=None): EPSEstimateCurrentYear = float(detailShare['EPSEstimateCurrentYear']) if(detailShare['EPSEstimateNextQuarter']!=None): EPSEstimateNextQuarter = float(detailShare['EPSEstimateNextQuarter']) sqlStatement = 'default statement' if EPSEstimateNextYear != None and EPSEstimateCurrentYear != None and EPSEstimateNextQuarter != None: sqlStatement = ("REPLACE INTO SM_Stock_Data \n" "VALUES('%s',\"%f\",%i,%i,\"%f\",\"%f\",'%s',\"%f\",\"%f\",\"%f\");" % (shareString, moneyStringToFloat(curShare.get_price()), moneyStringToInt(curShare.get_volume()), moneyStringToInt(curShare.get_market_cap()), moneyStringToFloat(curShare.get_earnings_share()), moneyStringToFloat(curShare.get_dividend_share()), curShare.get_trade_datetime(), EPSEstimateNextYear, EPSEstimateCurrentYear, EPSEstimateNextQuarter)) else: sqlStatement = ("REPLACE INTO SM_Stock_Data (ticker,price,volume,mktcap,eps,dividend,refreshdate) \n" "VALUES('%s',\"%f\",%i,%i,\"%f\",\"%f\",'%s');" % (shareString, moneyStringToFloat(curShare.get_price()), moneyStringToInt(curShare.get_volume()), moneyStringToInt(curShare.get_market_cap()), moneyStringToFloat(curShare.get_earnings_share()), moneyStringToFloat(curShare.get_dividend_share()), curShare.get_trade_datetime())) print('Populating stock with command: ' + sqlStatement) sqlConnection.execute(("REPLACE INTO SM_Stock \n" "VALUES('%s','%s')") % (shareString, doubleApost(nameForTicker(shareString)))) sqlConnection.execute(sqlStatement)
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)
def get_stock_info(): share = Share('AAPL') opening = share.get_open() dividend_yeild = share.get_earnings_share() print share.get_short_ratio()