def get_stock_price_and_volume(symbol, start_date, end_date, dates): share = Share(symbol) hist_data = share.get_historical(start_date, end_date) hist_data.reverse() volume = [] price = [] i = 0 for d in dates: if i < len(hist_data): if (hist_data[i]['Date'] == d): # Weekday price.append(hist_data[i]['Close']) volume.append(hist_data[i]['Volume']) i += 1 else: # Weekend price.append(0) volume.append(0) else: # Get the current price and volume instead from historical data price.append(share.get_price()) volume.append(share.get_volume()) if len(dates) != len(volume) and len(dates) != len(price): print 'Dates and volume and/or price lists are not of same lenght!' return [price, volume]
def get_quote(symbol): share = Share(symbol) if not share.get_price(): return {} change_f = float(share.get_change()) change_str = '+%.02f' % change_f if change_f >= 0 else '%.02f' % change_f change_percent_f = change_f / float(share.get_open()) * 100 change_percent = '+%.02f' % change_percent_f if change_percent_f >= 0 else '%.02f' % change_percent_f return { 'price': share.get_price(), 'change': change_str, 'change_percent': change_percent, 'open_price': share.get_open(), 'market_cap': share.get_market_cap(), 'year_low': share.get_year_low(), 'year_high': share.get_year_high(), 'day_low': share.get_days_low(), 'day_high': share.get_days_high(), 'volume': share.get_volume(), 'pe_ratio': share.get_price_earnings_ratio() or '-' }
def getTrending(self): trending = [] k = 0 retry = 0 maxRetries = 3 while k < len(self.bySymbol.keys()): ticker = self.bySymbol.keys()[k] try: sData = Share(ticker) sData.get_avg_daily_volume() avgVolume = float(sData.get_avg_daily_volume()) pVolume = float(sData.get_volume()) sdVolume = (avgVolume)**0.5 trendingP = (pVolume - avgVolume) / sdVolume trending.append((ticker, trendingP)) k += 1 except: time.sleep(0.05) retry += 1 if retry >= maxRetries: retry = 0 k += 1 trending.sort(key=lambda x: x[1], reverse=True) trending = [s for s in trending if s[1] > 0] return trending
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 data_frame_companies_amount(list_companies,porcentaje_volumnen_diario,max_var_pvd): # Es una funcion que toma una lista de empresas (codificadas por yahoo.finance), el porcentaje (tanto por uno) # sobre el volumen diario a considerar y la maxima variacion en porcentaje (tanto por uno) sobre este porcentaje. list_index=["ask5","ask4","ask3","ask2","ask1","bid1","bid2","bid3","bid4","bid5"] companies_amount=pd.DataFrame(index=list_index) for i in list_companies: company = Share(i) volume=company.get_volume() cantidad_per_ba=float(company.get_avg_daily_volume())*porcentaje_volumnen_diario num=cantidad_per_ba*max_var_pvd # variacion maxima respecto al 1% del volumen diario vec_list_amount=[] for h in range(0,len(list_index)): vec_list_amount.append(round(cantidad_per_ba+np.random.randint(-num,num,1))) vec_amount=np.array(vec_list_amount) companies_amount[i]=vec_amount return companies_amount
def getStock(name_of_company): global company_name,company_symbol stock = [] k=0 stock.append([]) stock.append("NA") stock.append("NA") stock.append("NA") stock.append("NA") stock.append("NA") stock.append("NA") stock.append("NA") j=0 for i in company_symbol: if i == name_of_company: break j=j+1 print "j is "+str(j)+"link is " stock[0]=company_name[j] yahoo = Share(name_of_company) stock[1] = yahoo.get_open() stock[2] = yahoo.get_price() stock[3] = yahoo.get_trade_datetime() stock[4] = company_symbol[j] stock[5] = yahoo.get_volume() stock[6] = yahoo.get_dividend_yield() stock[7] = google_links[j] print stock conn = mysql.connect() cur = conn.cursor() if 'username' in session: username = session['username'] cur.execute("SELECT purse FROM user WHERE username = %s;", [username]) print username for row in cur.fetchall(): for lol in row: purse=lol companystock = [dict( name=stock[0], open=stock[1], lasttradeprice=stock[2], lasttradetime=stock[3], stocksymbol=stock[4], MarketCapital=stock[5], dividend=stock[6], link=stock[7] )] cur.execute("SELECT stock FROM user WHERE username = %s;", [username]) print username for row in cur.fetchall(): for lol in row: newarray = lol.split(',') currentstock = newarray[j] print purse return companystock,stock,purse,currentstock
def stock_search(symbol="YHOO"): stock = Share(symbol) open_price = stock.get_open() close_price = stock.get_prev_close() price = stock.get_price() high = stock.get_days_high() low = stock.get_days_low() volume = stock.get_volume() return stock, open_price, close_price, price, high, low, volume
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 andrews_algo(): """ Various metrics for a given ticker :param tickers: :return: """ global ticker_list for ticker in ticker_list: stock = Share(ticker) print(line_separator) print(stock.get_name()) print("Current Price: " + str(stock.get_price())) # Dollar volume == momentum dollar_volume = float(stock.get_price()) * float(stock.get_volume()) if dollar_volume > 20000000.0: print("High Trading Liquidity, dollar volume: " + num_to_short_text(dollar_volume)) else: print("Low Trading Liquidity, dollar volume: " + num_to_short_text(dollar_volume)) # PEG is apparently inaccurate need to implement checks/also check conditional logic peg = float(stock.get_price_earnings_growth_ratio()) if peg > 1.5: print("Undervalued, Large Growth Potential, PEG ratio: " + str(peg)) elif peg < 1: print("Overvalued, High Risk Potential, PEG ratio: " + str(peg)) else: print("Fairly Priced, Low Growth Potential, PEG ratio: " + str(peg)) # TODO: ROE (increasing ROE signals regular profit generation) # TODO: Beta value to determine volatility # TODO: Actual EPS vs next quarter estimated EPS (to predict imminent stock jump or crash) # TODO: Formula to calculate future theoretical earnings # Converting textual numbers to floats market_cap = short_text_to_float(stock.get_market_cap()) if market_cap > 200000000000.0: print("Mega Cap") elif market_cap > 10000000000.0: print("Large Cap") elif market_cap > 2000000000.0: print("Mid Cap") elif market_cap > 300000000.0: print("Small Cap") elif market_cap > 50000000.0: print("Micro Cap") else: print("Nano Cap") print("Market Capitalization: " + num_to_short_text(market_cap)) print(line_separator)
def get_stock_info(ticker): temp_share = Share(ticker) real_price = temp_share.get_price() adj_price = (float(temp_share.get_open()) + float(temp_share.get_close()) + float(temp_share.get_high()) + float(temp_share.get_low())) / 4 real_vol = temp_share.get_volume() avg_vol = temp_share.get_avg_daily_volume() avg_50_day = temp_share.get_50day_moving_avg() avg_200_day = temp_share.get_200day_moving_avg() return real_price, adj_price, real_vol, avg_vol, avg_50_day, avg_200_day
def getCurrent(ticker): """ Return the most recent stock data through yahoo_finance. """ stock = Share(ticker) stock.refresh() #refresh the query open_ = stock.get_open() volume = stock.get_volume() high = stock.get_days_high() price = stock.get_price() time = stock.get_trade_datetime() low = stock.get_days_low() return (open_, high, volume, price, time, low)
def getStockInfoNow(request): if request.method == 'GET': sid = request.GET['sid'] try: s = Share(sid+'.TW') p = float(s.get_price()) c = float(s.get_change()) v = float(s.get_volume())/1000 pc = float(s.get_prev_close()) except: return JsonResponse({"success": 'false', 'error': 'wrong sid'}) return JsonResponse({'price': p, 'change': c, 'volume': v, 'prev_close': pc}) else: return JsonResponse({"success": 'false', 'error': 'wrong method'})
def process_symbol(net, symbol): settings = load(symbol+'.set') if(len(settings)==0): return yahoo = Share(symbol) mp = 2.0*settings['maxc'] p = float(yahoo.get_price())/mp d = yahoo.get_trade_datetime() wd = datetime.datetime.strptime(d[:10],"%Y-%m-%d").weekday()/6.0 v = float(yahoo.get_volume())/(2*settings['maxv']) ts = UnsupervisedDataSet(3,) ts.addSample((wd,p,v),) ret = net.activate([wd,p,v]) print "IK, K, V ", ret
def fetch(): for share in Share.objects.all(): try: sh = YahooShare(share.share) except: print('exception occurred while fetching stock...') continue sv, created = ShareValue.objects.get_or_create(share=share, price=sh.get_price(), open=sh.get_open(), volume=sh.get_volume(), time=datetime.strptime(sh.get_trade_datetime(), '%Y-%m-%d %H:%M:%S %Z%z')) if not created: print('%s market is closed' % sv.share)
def quote(request): stockSymbol = request.GET['stock'] stock = Share(stockSymbol) form = StockQuoteForm() context = { "form": form, "stock": stockSymbol, "price": stock.get_price(), "change": stock.get_change(), "volume": stock.get_volume(), "cap": stock.get_market_cap() } template = "quote.html" return render(request, template, context)
def realTime(request): company_name = str(request.GET.get('name')) stock = Share(company_name) rowlist = [] d = collections.OrderedDict() cursor = connection.cursor() cursor.execute('select max(id) from stockPrediction_onedaystock') idd = cursor.fetchone()[0] print idd d["id"] = idd d["time"] = datetime.now() d["price"] = stock.get_price() d["volume"] = stock.get_volume() rowlist.append(d) return Response(rowlist)
def realTime(request): company_name = str(request.GET.get('name')) stock = Share(company_name) rowlist = [] d = collections.OrderedDict() cursor = connection.cursor() cursor.execute('select max(id) from stockPrediction_onedaystock') idd = cursor.fetchone()[0] print idd d["id"] = idd d["time"] = datetime.now() d["price"] = stock.get_price() d["volume"] = stock.get_volume() rowlist.append(d) return Response(rowlist)
def get_price_YAHOO(ticker): #for ticker in list_of_symbols: try: from yahoo_finance import Share yahoo = Share(ticker) yahoo.refresh() price_finance = yahoo.get_price() print("IN->", ticker) print("Open -> ", yahoo.get_open()) print("Current Price -> ", yahoo.get_price()) print("Ave Volume -> ", yahoo.get_avg_daily_volume()) print("Volume -> ", yahoo.get_volume()) print("Time -> ", yahoo.get_trade_datetime()) return float(price_finance) except: get_price_YAHOO(ticker)
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 main(): PATH_OF_DATA = 'data' error_log_file = open('error.log', 'a') index_lists = [f[:-4] for f in listdir(PATH_OF_DATA) if f[-4:] == '.csv'] skipFlag = True if len(sys.argv) > 1 else False tillFlag = True if len(sys.argv) > 2 else False for stock_index in index_lists: if skipFlag: if stock_index != sys.argv[1]: continue else: skipFlag = False if tillFlag: if stock_index == sys.argv[2]: break filename = join(PATH_OF_DATA, stock_index + '.csv') if isfile(filename): # 如果已經有檔案,就讀出最後一行然後插入在後面 lastline = get_last_row(filename) print 'lastline = ', lastline try: st = Share(stock_index + '.tw') if not time_after(lastline[0], st.get_trade_datetime()[:10]): print 'time : ', st.get_trade_datetime()[:10] fo = open(filename, 'ab') cw = csv.writer(fo, delimiter=',') # 更新當天資料 cw.writerow([ st.get_trade_datetime()[:10], st.get_open(), st.get_days_high(), st.get_days_low(), st.get_price(), st.get_volume(), '0.0' ]) print "更新一筆!" else: print "不需要更新" except: print stock_index, "update error!" error_log_file.write('%s, Update Error\n' % (stock_index))
def get_today_stock_data(ticker): try: s = Share(ticker) data = { 'Open': float(s.get_open()), 'Close': float(s.get_prev_close()), 'High': float(s.get_days_high()), 'Low': float(s.get_days_low()), 'Volume': int(s.get_volume()), 'Date': datetime.date.today(), } return data except YQLQueryError: logger.error("Daily data not found for {}".format(ticker)) except Exception as e: logger.error("Unexpected error occurred: {}".format(e)) return {}
def get_today_stock_data(ticker): try: s = Share(ticker) data = { 'Open': float(s.get_open()), 'Close': float(s.get_prev_close()), 'High': float(s.get_days_high()), 'Low': float(s.get_days_low()), 'Volume': int(s.get_volume()), 'Date': datetime.date.today(), } return data except YQLQueryError: logger.error("Daily data not found for {}".format(ticker)) except Exception as e: logger.error("Unexpected error occurred: {}".format(e)) return {}
def RealTime(filepath,instrument): # Download CSV from yahoo if the CSV does not already exist. if not isfile(filepath): download_daily_bars(instrument,2016,filepath) # Get Live Results from Yahoo Database and append to CSV s = Share(instrument) date = datetime.today().strftime('%Y-%m-%d') O,H,L,C,V,AC = (s.get_open(), s.get_days_high(), s.get_days_low(), s.get_price(), s.get_volume(),s.get_price()) #Date,Open,High,Low,Close,Volume,Adj Close field = [date,O,H,L,C,V,AC] with open(filepath,'a') as csvfile: old = csv.writer(csvfile) add = old.writerow(field)
def getStockInfoNow(request): if request.method == 'GET': sid = request.GET['sid'] try: s = Share(sid + '.TW') p = float(s.get_price()) c = float(s.get_change()) v = float(s.get_volume()) / 1000 pc = float(s.get_prev_close()) except: return JsonResponse({"success": 'false', 'error': 'wrong sid'}) return JsonResponse({ 'price': p, 'change': c, 'volume': v, 'prev_close': pc }) else: return JsonResponse({"success": 'false', 'error': 'wrong method'})
def bot_action(c, symbol): stock = Share(symbol) # Link stock with yahoo_finance module print(stock) if stock.get_price() == None: main() #print(stock.get_price()) head = 'Up to date stock info for **${0}** ({1}):\n\n'.format(symbol.upper(), stock.get_name()) price = '**Price:** ${0:.2f}\n\n'.format(float(stock.get_price())) price_open = '**Open:** ${0:.2f}\n\n'.format(float(stock.get_open())) change = '**Change:** {0:.2f} ({1})\n\n'.format(float(stock.get_change()), stock.get_percent_change()) vol = '**Volume:** {0:,.2f}\n\n'.format(float(stock.get_volume())) market_cap = '**Mkt Cap:** {0}\n\n'.format(stock.get_market_cap()) average = '**Average (50 day):** {0:.2f}\n\n'.format(float(stock.get_50day_moving_avg())) exchange = '**Exchange:** {0}\n\n'.format(stock.get_stock_exchange()) divider = '-----------------------------------------------------------------------------------------\n\n' tail = "Don't abuse me, robots have feelings too! | [Source Code](https://github.com/Logicmn/Reddit-Stock-Bot) " \ "| [Report Bug](https://www.reddit.com/message/compose/?to=Pick-a-Stock) " \ "| [Suggest Feature](https://www.reddit.com/message/compose/?to=Pick-a-Stock)" c.reply(head + divider + price + price_open + change + vol + market_cap + average + exchange+ divider + tail)
def selectStock(stocks): ''' select the stock with today's trading volume at least 6 fold higher than average historical trading volume ''' start_time = time() resultStock = {} count = 0 num = 0 for symb in stocks.keys(): try: stock = Share(symb) vol = int(stock.get_volume()) daily_avg_vol = int(stock.get_avg_daily_volume()) price = float(stock.get_price()) prevPrice = float(stock.get_prev_close()) avg_50day = float(stock.get_50day_moving_avg()) avg_200day = float(stock.get_200day_moving_avg()) except (TypeError, AttributeError): continue num += 1 volRatio = vol / daily_avg_vol print num, stocks[symb][0], volRatio if volRatio > 6 and price > prevPrice and price > avg_50day: count += 1 stocks[symb].extend([ vol, daily_avg_vol, volRatio, price, prevPrice, avg_50day, avg_200day, stock.get_price_earnings_ratio(), stock.get_price_book(), stock.get_short_ratio(), stock.get_dividend_yield() ]) resultStock = { symb: stocks[symb] for symb in stocks.keys() if len(stocks[symb]) > 1 } print '{} stock(s) has marvelous volume'.format(count) print 'total time of running: {} seconds'.format(time() - start_time) return resultStock
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 main(): PATH_OF_DATA = 'data' error_log_file = open('error.log', 'a') index_lists = [ f[:-4] for f in listdir(PATH_OF_DATA) if f[-4:] == '.csv' ] skipFlag = True if len(sys.argv) > 1 else False tillFlag = True if len(sys.argv) > 2 else False for stock_index in index_lists: if skipFlag: if stock_index != sys.argv[1]: continue else: skipFlag = False if tillFlag: if stock_index == sys.argv[2]: break filename = join(PATH_OF_DATA, stock_index+'.csv') if isfile(filename):# 如果已經有檔案,就讀出最後一行然後插入在後面 lastline = get_last_row(filename) print 'lastline = ', lastline try: st = Share(stock_index+'.tw') if not time_after(lastline[0], st.get_trade_datetime()[:10]): print 'time : ', st.get_trade_datetime()[:10] fo = open(filename, 'ab') cw = csv.writer(fo, delimiter=',') # 更新當天資料 cw.writerow([st.get_trade_datetime()[:10], st.get_open(), st.get_days_high(), st.get_days_low(), st.get_price(), st.get_volume(), '0.0']) print "更新一筆!" else: print "不需要更新" except: print stock_index, "update error!" error_log_file.write('%s, Update Error\n' % (stock_index))
def fetch_current_data(self, sym): ts = get_latest_trading_date(get_cur_time()) if ts in self.datasets_daily[sym].keys() or ts > self.endDate: return try: sdata = Share(sym) gquote = gQuotes(sym) except: # live with the fact that data from the most recent day is missing return self.datasets_daily[sym][ts] = t = {} t['Date'] = '3000-01-01' #debugging purposes, so we know this is current. This won't be saved to file t['High'] = float(sdata.get_days_high()) t['Low'] = float(sdata.get_days_low()) t['Open'] = float(sdata.get_open()) # t['Close'] = sdata.get_price() t['Close'] = float(gquote[0]['LastTradePrice']) # use google data for latest 'Close', which is more accurate t['Volume'] = float(sdata.get_volume()) for k in t.keys(): if t[k] == None: raise Exception('missing most recent daily', sym)
def displayStockQuotes(symbols, seconds): """ Display each symbol's price, volume and percent change for seconds :param: symbols: List of stock symbols in Yahoo format. E.g. 'AMZN' or 'BRK-B' (NOTE: not 'BRK.B'). :param: seconds: Number of seconds to display each stock. # The following Adafruit_CharLCD library is found here: # https://github.com/adafruit/Adafruit_Python_CharLCD # See this tutorial for a hello world Python/Raspberry Pi/LCD tutorial: # https://learn.adafruit.com/drive-a-16x2-lcd-directly-with-a-raspberry-pi/python-code """ lcd = Adafruit_CharLCD() while True: for symbol in symbols: stock = Share(symbol) lcd.clear() lcd.message(symbol + ": $") lcd.message(stock.get_price() + "\n") volume = stock.get_volume() change = stock.get_change() lcd.message(change + "|") open = stock.get_open() if type(open) is str and type(change) is str: percentChange = float(change) / float(open) # format percent change in human-readable format: "1%" instead of ".01" lcd.message("{0:.00%}".format(percentChange)) else: lcd.message("?") if volume: lcd.message("|" + volume) else: lcd.message("|?") sleep(seconds) # Delay between each stock/symbol
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 moreInfo(from_number): #query to find the stock the user last looked up conn = mysql.connect() cur = conn.cursor() q = '''SELECT * FROM last_lookup WHERE phone = %s''' cur.execute(q, [from_number]) rv = cur.fetchone() #get stock information quote = Share(str(rv[1])) prevClose = quote.get_prev_close() openPrice = quote.get_open() volume = quote.get_volume() #if we get all the information back respond back with more info if prevClose and openPrice and volume: retStr = "PrevClose: " + prevClose + " OpenPrice: " + openPrice + " Volume: " + volume #else the user has not looked up a stock yet else: retStr = "ticker still not found" return retStr
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 moreInfo(from_number): #query to find the stock the user last looked up conn = mysql.connect() cur = conn.cursor() q = '''SELECT * FROM last_lookup WHERE phone = %s''' cur.execute(q,[from_number]) rv = cur.fetchone() #get stock information quote = Share(str(rv[1])) prevClose = quote.get_prev_close() openPrice = quote.get_open() volume = quote.get_volume() #if we get all the information back respond back with more info if prevClose and openPrice and volume: retStr = "PrevClose: "+prevClose+" OpenPrice: "+openPrice+" Volume: "+ volume #else the user has not looked up a stock yet else: retStr = "ticker still not found" return retStr
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 getHistoricalData(ticker, daysAgo): try: yahooHoo = Share(ticker) except ValueError: print "Couldn't 'Share("+str(ticker)+")'" data=yahooHoo.get_historical(dateDaysAgo(daysAgo), today()) try: empty={} element={'Volume': yahooHoo.get_volume(), 'Symbol': ticker, 'High': yahooHoo.get_days_high(), 'Low': yahooHoo.get_days_low(), 'Open': yahooHoo.get_open(), 'Close': yahooHoo.get_price(), 'Date': yahooHoo.get_trade_datetime()[0:10]} data.append(element) except ValueError: print "LALALA" if len(data) > 0: data = sorted(data, key=lambda k: k['Date'], reverse=False) #Sort by Date of json element #try: # if data[-1]['Date'] != element['Date']: # data.append(element) #except ValueError: # print "DOH" #data = sorted(data, key=lambda k: k['Date'], reverse=False) #Sort by Date of json element return data
# banks cba = Share("CBA.ax") nab = Share("NAB.ax") anz = Share("ANZ.ax") wbc = Share("WBC.ax") # westpac # retail wow = Share("WOW.ax") jbh = Share("JBH.ax") # jb hifi wes = Share("WES.ax") # west farmer # misc asx = Share("ASX.ax") bhp = Share("BHP.ax") ccl = Share("CCL.ax") # coca cola dmp = Share("DMP.ax") # domino pizza price = [cba.get_price(), nab.get_price(), anz.get_price(), wbc.get_price(), wow.get_price(), jbh.get_price(), wes.get_price(), asx.get_price(), bhp.get_price(), ccl.get_price(), dmp.get_price()] volume = [cba.get_volume(), nab.get_volume(), anz.get_volume(), wbc.get_volume(), wow.get_volume(), jbh.get_volume(), wes.get_volume(), asx.get_volume(), bhp.get_volume(), ccl.get_volume(), dmp.get_volume()] change = [cba.get_change(), nab.get_change(), anz.get_change(), wbc.get_change(), wow.get_change(), jbh.get_change(), wes.get_change(), asx.get_change(), bhp.get_change(), ccl.get_change(), dmp.get_change()] print "ASX overall price:", asx.get_price() # write to data.json counter = 0 def appendData(name): with open('data.json', 'r+') as f: global counter json_data = json.load(f) json_data["price"][name].append(float(price[counter])) f.seek(0) f.write(json.dumps(json_data)) f.truncate() json_data["volume"][name].append(float(volume[counter])) f.seek(0) f.write(json.dumps(json_data))
# find the date of yesterday now = datetime.datetime.now() # connect the sqlite conn = lite.connect('StockHistory.db') cursor = conn.cursor() # every time empty data cursor.execute('DELETE FROM TrueTimeValue') conn.commit() # get the records ranging from max date in DB to yesterday BeginTime = now.replace(hour=9, minute=0, second=0, microsecond=0) EndTime = now.replace(hour=16, minute=0, second=0, microsecond=0) # LocalTime = T.strftime('%Y-%m-%d ',T.localtime(T.time())) while now > BeginTime and now < EndTime: StockList = ['YHOO', 'GOOG', 'AAPL', 'TWTR', 'AMZN'] for stock in StockList: Symbol = stock Company = Share(stock) Price = Company.get_price() Time = Company.get_trade_datetime() Volume = Company.get_volume() purchases = (Symbol, Price, Time, Volume) cursor.execute('INSERT INTO TrueTimeValue VALUES (?,?,?,?)', purchases) conn.commit() Company.refresh() cursor.execute('SELECT * FROM TrueTimeValue') print(cursor.fetchall()) T.sleep(60) cursor.close() conn.close()
if myargs.ticker is not None: stock = Share(myargs.ticker) if myargs.realtime is True: selections.append('Realtime: ') subrealtimequote = realtime.scraper(myargs.ticker) output.append(subrealtimequote) if myargs.price is True: price = stock.get_price() print price if myargs.volume is True: volume = stock.get_volume() output.append(volume) selections.append('Volume: ') print output if myargs.change is True: change = stock.get_change() output.append(change) selections.append('Change: ') print change if myargs.avgvol is True: avgvolume = stock.get_avg_daily_volume() print avgvolume
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")
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(row_title) ticker = ticker.rstrip() if len(ticker) == 0: continue stock = Share(ticker) stock.refresh() change = (float(stock.get_price()) - float( stock.get_prev_close())) / float(stock.get_prev_close()) change = round(change * 100.0, 2) if change > 0.0: change = '+' + str(change) else: change = str(change) line = ticker.ljust(7) line += stock.get_price().ljust(9)+ change.ljust(8)+ stock.get_volume().ljust(11) + \ str(round(float(stock.get_volume())/float(stock.get_avg_daily_volume())*100.0)).ljust(8) +\ stock.get_open().ljust(10)+ \ stock.get_days_low().ljust(10)+ \ stock.get_days_high().ljust(10)+ \ stock.get_year_low().ljust(10)+ \ stock.get_year_high().ljust(10) line = line + str(stock.get_market_cap()).ljust(11) + \ str(stock.get_price_earnings_ratio()).ljust(8)+\ stock.get_50day_moving_avg().ljust(10) +\ stock.get_200day_moving_avg().ljust(10) print(line) except Exception as e: print("Exception error:", str(e)) traceback.print_exc() i += 1
def getStocksFromSource(indexes=data, sortBy=SORT_BY_TOP): ''' ''' stocks = [] index = ['AGTC'] # for stock in data["Ticker"][:100]: # for stock in index: for stock in data: try: print(stock) # print(type(stock)) yf_data = yqd.load_yahoo_quote(stock, '20170301', '20170830') # yf_data = yqd.load_yahoo_quote('ABEO', '20170712', '20170725') # print(yf_data) share = Share(stock) # history part history = [] for i, day in enumerate(yf_data[1:-1]): daily_data = day.split(',') history.append([ i, str(daily_data[0]), float(daily_data[1]), float(daily_data[2]), float(daily_data[3]), float(daily_data[4]), float(daily_data[6]) ]) # print(history) # comments part comments = [] new_StockTwits_comments = [] url = 'https://api.stocktwits.com/api/2/streams/symbol/{0}.json'.format( stock) print(url) try: r = requests.get(url).json() print(len(r['messages'])) for message in r['messages']: try: new_tweet = { 'id': message['id'], 'body': message['body'], 'created_at': message['created_at'], 'core_body': nltk_service.clean_tweet(message['body']), 'nltk_sentiment': nltk_service.get_tweet_sentiment(message['body']), # 'azure_sentiment': azure_sentiment_service.GetSentiment(message['body']) } try: new_tweet[ 'azure_sentiment'] = azure_sentiment_service.GetSentiment( message['body']) except Exception as e: new_tweet['azure_sentiment'] = 0.5 print(e) # print(new_tweet['azure_sentiment']) new_StockTwits_comments.append(new_tweet) except Exception as e: print(e) # pass except Exception as e: print('stock tweets part problem') print(e) # new_StockTwits_comments = [{'id': message['id'], 'body': message['body'], 'created_at': message['created_at']} for message in r['messages']] print(len(new_StockTwits_comments)) stock = { 'index': stock, 'open': share.get_open(), 'change': share.get_change(), 'percent_change': share.get_percent_change(), 'prev_close': share.get_prev_close(), 'price': share.get_price(), 'volume': share.get_volume(), 'history': history, 'new_StockTwits_comments': new_StockTwits_comments } # stock_json = json.dumps(stock) # print(type(stock_json)) print(len(history)) if len(history) != 0: # f.write(stock['index']+'/n') stocks.append(stock) except Exception as e: print(e) pass print(len(stocks)) return stocks # f.close() # get_price() # get_change() # get_percent_change() # get_volume() # get_prev_close() # get_open() # get_avg_daily_volume() # get_stock_exchange() # get_market_cap() # get_book_value() # get_ebitda() # get_dividend_share() # get_dividend_yield() # get_earnings_share() # get_days_high() # get_days_low() # get_year_high() # get_year_low() # get_50day_moving_avg() # get_200day_moving_avg() # get_price_earnings_ratio() # get_price_earnings_growth_ratio() # get_price_sales() # get_price_book() # get_short_ratio() # get_trade_datetime() # get_historical(start_date, end_date) # get_name() # refresh() # get_percent_change_from_year_high() # get_percent_change_from_year_low() # get_change_from_year_low() # get_change_from_year_high() # get_percent_change_from_200_day_moving_average() # get_change_from_200_day_moving_average() # get_percent_change_from_50_day_moving_average() # get_change_from_50_day_moving_average() # get_EPS_estimate_next_quarter() # get_EPS_estimate_next_year() # get_ex_dividend_date() # get_EPS_estimate_current_year() # get_price_EPS_estimate_next_year() # get_price_EPS_estimate_current_year() # get_one_yr_target_price() # get_change_percent_change() # get_dividend_pay_date() # get_currency() # get_last_trade_with_time() # get_days_range() # get_year_range()
def RealTime(stocks, count): cnx = mysql.connector.connect(user='******', password='******', host='127.0.0.1', database='DBtest') cursor = cnx.cursor() waitsec = 60 waitmessage = "Wait 1 minute for the next real-time values..." #The following lines avoid running the program multiple times if the market it's closed now = datetime.datetime.utcnow() # Check the current time today14 = now.replace(hour=14, minute=25, second=0, microsecond=0) today21 = now.replace(hour=21, minute=5, second=0, microsecond=0) if (now >= today21 or today14 >= now): count = 1 waitsec = 1 waitmessage = "" print( "The market opens at 14:30 UTC and closses at 21:00 UTC, \n***Data will be retrieved only once." ) print("Current datetime " + str(now)) while 0 < count: for i in stocks: print("Stock of " + i) stock = Share(i) stock.refresh() price = stock.get_price() stime = stock.get_trade_datetime() # Needed to fix the format the date is retrieved from Yahoo Finance timefixed = stime[:-8] volume = stock.get_volume() print("Price: " + price + " Time: " + timefixed + " Volume: " + volume) # Don't forget to use the config file to avoid writing the password on the source add_realtime = ("INSERT INTO realtime " "(tricker, price, time, volume) " "VALUES (%s, %s, %s, %s)") data_realtime = (i, price, timefixed, volume) try: cursor.execute(add_realtime, data_realtime) cnx.commit() # Make sure data is committed to the database except mysql.connector.IntegrityError as err: #print(err) # Ussually a Duplicate value error pass print("Database sucessfuly udpated. " + waitmessage) time.sleep(waitsec) count -= 1 cursor.close() cnx.close()
def print_menu(menuid, errorid): global selected_Ticker namesTicker = ["Stocks", "Exchanges", "Catagory", "Number Selection"] if selected_Ticker is not None: print("Selected:\t"+selected_Ticker.name) print("Type:\t\t"+namesTicker[selected_Ticker.typeof]) if selected_Ticker.typeof == 0: stock = Share(selected_Ticker.name) stock.refresh() print(stock.get_info()) print(stock.get_price()) print(stock.get_change()) print(stock.get_volume()) print("\n\n") if menuid == 0: print("------Menu------") print(" (e) exit") print(" (l) list") print(" (s) stats") error(errorid) elif menuid == 1: print("------Stats Menu------") print(" (a) all") print(" (u) uniques") print(" (b) back") if selected_Ticker is not None: print(" (r) run data collector") print(" (c) clear") error(errorid) elif menuid == 2: print("------All Data Menu------") print(" (e) exchanges") print(" (c) catagories") print(" (n) catagory Number") print(" (b) back") error(errorid) elif menuid == 3: print("------Unique Menu------") print(" (s) stock") print(" (e) exchange") print(" (c) catagories") print(" (n) catagory Number") print(" (b) back") error(errorid) elif menuid == 4: print("------Stock Tickers Selection------") exchanges_display(0) error(errorid) elif menuid == 5: print("------Exchanges Selection------") exchanges_display(1) error(errorid) elif menuid == 6: print("------Catagory Selection------") exchanges_display(2) error(errorid) elif menuid == 7: print("------Number Catagory Selection------") exchanges_display(3) error(errorid)
if (i%10) == 0: print(row_title) ticker = ticker.rstrip() if len(ticker) == 0: continue stock = Share(ticker) stock.refresh() change = (float(stock.get_price()) - float(stock.get_prev_close()))/float(stock.get_prev_close()) change = round(change *100.0, 2) if change > 0.0: change= '+' + str(change) else: change =str(change) line = ticker.ljust(7) line += stock.get_price().ljust(9)+ change.ljust(8)+ stock.get_volume().ljust(11) + \ str(round(float(stock.get_volume())/float(stock.get_avg_daily_volume())*100.0)).ljust(8) +\ stock.get_open().ljust(10)+ \ stock.get_days_low().ljust(10)+ \ stock.get_days_high().ljust(10)+ \ stock.get_year_low().ljust(10)+ \ stock.get_year_high().ljust(10) line = line + str(stock.get_market_cap()).ljust(11) + \ str(stock.get_price_earnings_ratio()).ljust(8)+\ stock.get_50day_moving_avg().ljust(10) +\ stock.get_200day_moving_avg().ljust(10) print(line) except Exception as e: print("Exception error:", str(e)) traceback.print_exc() i+=1
monthsAhead = input("How many months ahead would you to vacation? ") listStocksWithNoYahoo = [] for country in stocks: tempStock = "" value = 0 listStocksWithNoYahoo = [] ticks = stocks[country] sumOfCompany = 0 for tick in ticks: try: tempStock = Share(tick) tempstock.refresh() print tempStock.get_price() value = float(tempStock.get_price()) * int(tempStock.get_volume()) sumOfCompany = float(sumOfCompany) + value except: listStocksWithNoYahoo.append(tick) print "hit" for ls in listStocksWithNoYahoo: print ls ticks.remove(ls) weights = {} for tick in ticks: try: tempStock = Share(tick) weights[tick] = (float(tempStock.get_price()) * float( tempStock.get_volume())) / float(sumOfCompany)
def main(): # 1. get the time day = Time.get_utc_day() hours_mins = Time.get_utc_hours_minutes() # 1. Get all the list of stocks stocks = base.managers.stock_manager.get_many() # 2. go through stock and update the desired values for stock in stocks: ticker = stock.get('ticker') try: # 2.1 Get the info from the yahoo API updated_stock = Share(ticker) except: print "-->Failed to update: %s with Yahoo API" % ticker continue price = updated_stock.get_price() open = updated_stock.get_open() days_high = updated_stock.get_days_high() days_low = updated_stock.get_days_low() year_high = updated_stock.get_year_high() year_low = updated_stock.get_year_low() volume = updated_stock.get_volume() market_cap = updated_stock.get_market_cap() pe_ratio = updated_stock.get_price_earnings_ratio() div_yield = updated_stock.get_dividend_yield() change = updated_stock.get_change() change_percent = updated_stock.data_set.get('ChangeinPercent') # 2.2 Get the stock body stock_body = stock.get('body') stock_price = {hours_mins: price} if stock_body: # 1. Get the stock info for the day: stock_info = stock_body.get(day) if stock_info: stock_price = stock_info.get('price') stock_price.update({hours_mins: price}) else: stock_body = {} # 2.2.4 update the stock info dict stock_info = {'price': stock_price} stock_info.update({'open': open}) stock_info.update({'days_high': days_high}) stock_info.update({'days_low': days_low}) stock_info.update({'year_high': year_high}) stock_info.update({'year_low': year_low}) stock_info.update({'volume': volume}) stock_info.update({'market_cap': market_cap}) stock_info.update({'pe_ratio': pe_ratio}) stock_info.update({'div_yield': div_yield}) stock_info.update({'change': change}) stock_info.update({'change_percent': change_percent}) # update the stock body stock_body.update({day: stock_info}) stock.body = stock_body # 3. update the stock in the DB try: base.managers.stock_manager.update_one(stock) except: print "-->Failed to update: %s in DB" % ticker continue
sql = "insert into oneYearStock(name,time,open,close,high,low,volume) values('%s','%s','%s','%s','%s','%s','%s')" %(name,day,Open,Close,High,Low,Volume) sqlImplement(sql, cursor, db); #above is initialize print ("finish initialize oneYearStock table"); openTime = datetime.now().replace(hour = 9, minute = 30, second = 0); closeTime = datetime.now().replace(hour = 16, minute = 0, second = 0); print ("program start"); flag = True; stop = False; while True: if openTime < datetime.now() <= closeTime and datetime.now().minute != now.minute: now = datetime.now() for name in company_name: stock = Share(name) price = stock.get_price() volume = stock.get_volume() sql = "insert into oneDayStock(name,time,price,volume) values('%s','%s','%s','%s')" % (str(name),str(now),str(price),str(volume)) sqlImplement(sql, cursor, db); print ("click y to ouput tillnow csv in 10 sec, if not continue running program"); start_time = time.time(); while 1: while msvcrt.kbhit(): msvcrt.getch(); if msvcrt.kbhit(): key = ord(msvcrt.getche()); if key in map(ord, 'yY'): flag = False; print (''); print ("start generate onedaystock file"); for name in company_name:
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)
for s in russell3000.index.values: print "Retrieving data for", s try: shy=Share(s) except: continue try: russell3000.set_value(s,'Price',shy.get_price()) except: pass try: russell3000.set_value(s,'Change',shy.get_change()) except: pass try: russell3000.set_value(s,'Volume',shy.get_volume()) except: pass try: russell3000.set_value(s,'Open',shy.get_open()) 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: