def updateInfos(): print("Updating Infos!") with open('static/sp100.json', 'rb') as f: ls = json.load(f) for i in ls: timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") print (i['name']) symbol = Share(i['name']) item = { 'name': i['name'], 'price': symbol.get_price(), 'time': timestamp, 'prev_close': symbol.get_prev_close(), 'open': symbol.get_open(), 'volume': symbol.get_volume(), 'pe': symbol.get_price_earnings_ratio(), 'eps': symbol.get_earnings_share(), 'price_sales': symbol.get_price_sales(), 'ebitda': symbol.get_ebitda(), 'hotness': ms.hotness_function(i['name']), 'BS': ms.bs_function(i['name'])} db.infos.update( {"name": i['name']}, { "$push": {"data": item} } ) print('Collection Infos Updated.') return Response('Collection Infos Updated.')
def createInfos(): if db.infos.count() == 0: print("Creating Infos!!") with open('static/sp100.json', 'rb') as f: ls = json.load(f) for i in ls: timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") symbol = Share(i['name']) item = { 'name': i['name'], 'price': symbol.get_price(), 'time': timestamp, 'prev_close': symbol.get_prev_close(), 'open': symbol.get_open(), 'volume': symbol.get_volume(), 'pe': symbol.get_price_earnings_ratio(), 'eps': symbol.get_earnings_share(), 'price_sales': symbol.get_price_sales(), 'ebitda': symbol.get_ebitda(), 'hotness': ms.hotness_function(i['name']), 'BS': ms.bs_function(i['name'])} db.infos.insert_one({ "name": i['name'], "sector": i['sector'], "data": [item] }) print('Collection Infos Created.') return Response('Collection Infos Created.')
def stock_summary(request, symbol=None): if symbol == None: symbol = request.POST['symbol'] current_stock = Stock() stock = Share(symbol) current_stock.symbol = symbol.upper() current_stock.price = stock.get_price() current_stock.change = stock.get_change() current_stock.volume = stock.get_volume() current_stock.prev_close = stock.get_prev_close() current_stock.stock_open = stock.get_open() current_stock.avg_daily_volume = stock.get_avg_daily_volume() current_stock.stock_exchange = stock.get_stock_exchange() current_stock.market_cap = stock.get_market_cap() current_stock.book_value = stock.get_book_value() current_stock.ebitda = stock.get_ebitda() current_stock.dividend_share = stock.get_dividend_share() current_stock.dividend_yield = stock.get_dividend_yield() current_stock.earnings_share = stock.get_earnings_share() current_stock.days_high = stock.get_days_high() current_stock.days_low = stock.get_days_low() current_stock.year_high = stock.get_year_high() current_stock.year_low = stock.get_year_low() current_stock.fifty_day_moving_avg = stock.get_50day_moving_avg() current_stock.two_hundred_day_moving_avg = stock.get_200day_moving_avg() current_stock.price_earnings_ratio = stock.get_price_earnings_ratio() current_stock.price_earnings_growth_ratio = stock.get_price_earnings_growth_ratio() current_stock.price_sales = stock.get_price_sales() current_stock.price_book = stock.get_price_book() current_stock.short_ratio = stock.get_short_ratio() date_metrics = [] url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+symbol+'/chartdata;type=quote;range=1y/csv' page = urllib2.urlopen(url).read() pagebreaks = page.split('\n') for line in pagebreaks: items = line.split(',') if 'Company-Name:' in line: current_stock.company_name = line[13:len(line)] current_stock.save() if 'values' not in items: if len(items)==6: hd = HistoricalData( stock_id = Stock.objects.get(id=int(current_stock.id)).id, date = items[0][4:6]+'/'+items[0][6:9]+'/'+items[0][0:4], close = items[1][0:(len(items[1])-2)], high = items[2][0:(len(items[2])-2)], price_open = items[3][0:(len(items[3])-2)], low = items[4][0:(len(items[4])-2)], volume = items[5][0:-6]+","+items[5][-6:-3]+","+items[5][-3:len(items[5])]) hd.save() date_metrics.append(hd) del date_metrics[0] return render(request, "stock_summary.html", {'current_stock': current_stock, 'date_metrics': date_metrics})
def getAllStockData(ticker): '''Get a few random tickers.''' stock = Share(ticker) stock.refresh() data = { 'name': stock.get_name(), 'price': stock.get_price(), 'change': stock.get_change(), 'volume': stock.get_volume(), 'prev_close': stock.get_prev_close(), 'open': stock.get_open(), 'avg_daily_volume': stock.get_avg_daily_volume(), 'stock_exchange': stock.get_stock_exchange, 'market_cap': stock.get_market_cap(), 'book_value': stock.get_book_value(), 'ebitda': stock.get_ebitda(), 'dividend_share': stock.get_dividend_share(), 'dividend_yield': stock.get_dividend_yield(), 'earnings_share': stock.get_earnings_share(), 'days_high': stock.get_days_high(), 'days_low': stock.get_days_low(), 'year_high': stock.get_year_high(), 'year_low': stock.get_year_low(), '50day_moving_avg': stock.get_50day_moving_avg(), '200day_moving_avg': stock.get_200day_moving_avg(), 'price_earnings_ratio': stock.get_price_earnings_ratio(), 'price_earnings_growth_ratio': stock.get_price_earnings_growth_ratio(), 'get_price_sales': stock.get_price_sales(), 'get_price_book': stock.get_price_book(), 'get_short_ratio': stock.get_short_ratio(), 'trade_datetime': stock.get_trade_datetime(), 'percent_change_from_year_high': stock.get_percent_change_from_year_high(), 'percent_change_from_year_low': stock.get_percent_change_from_year_low(), 'change_from_year_low': stock.get_change_from_year_low(), 'change_from_year_high': stock.get_change_from_year_high(), 'percent_change_from_200_day_moving_average': stock.get_percent_change_from_200_day_moving_average(), 'change_from_200_day_moving_average': stock.get_change_from_200_day_moving_average(), 'percent_change_from_50_day_moving_average': stock.get_percent_change_from_50_day_moving_average(), 'change_from_50_day_moving_average': stock.get_change_from_50_day_moving_average(), 'EPS_estimate_next_quarter': stock.get_EPS_estimate_next_quarter(), 'EPS_estimate_next_year': stock.get_EPS_estimate_next_year(), 'ex_dividend_date': stock.get_ex_dividend_date(), 'EPS_estimate_current_year': stock.get_EPS_estimate_current_year(), 'price_EPS_estimate_next_year': stock.get_price_EPS_estimate_next_year(), 'price_EPS_estimate_current_year': stock.get_price_EPS_estimate_current_year(), 'one_yr_target_price': stock.get_one_yr_target_price(), 'change_percent_change': stock.get_change_percent_change(), 'divended_pay_date': stock.get_dividend_pay_date(), 'currency': stock.get_currency(), 'last_trade_with_time': stock.get_last_trade_with_time(), 'days_range': stock.get_days_range(), 'years_range': stock.get_year_range() } return data
def 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
print daylow if myargs.yearhigh is True: yearhigh = stock.get_year_high() print yearhigh if myargs.yearlow is True: yearlow = stock.get_year_low() print yearlow if myargs.ebitda is True: ebitda = stock.get_ebitda() print ebitda if myargs.ps is True: ps = stock.get_price_sales() print ps if myargs.peg is True: peg = stock.get_price_earnings_growth_ratio() print peg if myargs.percentchange is True: change = stock.get_change() getopen = stock.get_open() percentchange = yahoofinancecalc.getPercentChange(stock, change, getopen) print str(percentchange) + "%" if myargs.percentoffhigh is True: realtimequote = realtime.scraper(myargs.ticker) poh = yahoofinancecalc.offHigh(stock, realtimequote)
russell3000.set_value(s,'50 days MA',shy.get_50day_moving_avg()) except: pass try: russell3000.set_value(s,'200 days MA',shy.get_200day_moving_avg()) except: pass try: russell3000.set_value(s,'Price earnings ratio',shy.get_price_earnings_ratio()) except: pass try: russell3000.set_value(s,'Price earnings growth ratio',shy.get_price_earnings_growth_ratio()) except: pass try: russell3000.set_value(s,'Price sales',shy.get_price_sales()) except: pass try: russell3000.set_value(s,'Price book',shy.get_price_book()) except: pass try: russell3000.set_value(s,'Short ratio',shy.get_short_ratio()) except: pass u=datetime.now() ofn='r3alldata'+str(u)[0:4]+str(u)[5:7]+str(u)[8:10]+'.xls' russell3000.to_excel(ofn)
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 loadKeyStatistics (cls, companyID = 'A'): ''' dataset df= pd.DataFrame(columns=['marketCapital','bookValue','ebitda','dividentShare','DividentYield','earningShare', 'BookPrice','SalesPrice','earningsGrowth','earningsRatio', 'symbol', 'date']) ''' yahoo = Share(companyID) yahoo.refresh() try: a = re.search('[a-zA-Z]+', yahoo.get_market_cap()) b = re.search('[a-zA-Z]+', yahoo.get_ebitda()) if a.group(0) is not None: p = re.split('[a-zA-Z]+', yahoo.get_market_cap()) if a.group(0) in 'B': marketCap = float(p[0]) * 10 ** 9 elif a.group(0) in 'M': marketCap = float(p[0]) * 10 ** 6 else: marketCap = -1 print ('Market cap: ' + yahoo.get_market_cap()) else: marketCap = yahoo.get_market_cap() if b.group(0) is not None: p = re.split('[a-zA-Z]+', yahoo.get_ebitda()) if b.group(0) in 'B': ebitda = float(p[0]) * 10 ** 9 elif b.group(0) in 'M': ebitda = float(p[0]) * 10 ** 6 else: ebitda = -1 print ('Ebitda: ' +yahoo.get_ebitda()) else: ebitda = yahoo.get_ebitda() except (TypeError, AttributeError): print ('Missing :' + companyID) e = sys.exc_info()[0] print( "<p>Error: %s</p>" % e ) ebitda = -1.0 marketCap = -1.0 try: company = LoadYahooFinance(symbol = companyID, marketCap = marketCap, bookValue = float(yahoo.get_book_value()), ebitda = ebitda, dividentShare = float(yahoo.get_dividend_share()), dividentYield = float(yahoo.get_dividend_yield()), earningShare = float(yahoo.get_earnings_share()), bookPrice = float(yahoo.get_price_book()), salesPrice = float(yahoo.get_price_sales()), earningsGrowth = float(yahoo.get_price_earnings_growth_ratio()), earningRatio = float(yahoo.get_price_earnings_ratio())) return company except TypeError: print ('Missing :' + companyID) e = sys.exc_info()[0] print( "<p>Error: %s</p>" % e )
def YaOrg(gather=[ 'debtToEquity', 'Trailing P/E', 'Price/Sales', 'priceToBook', 'profitMargins', 'operatingMargins', 'returnOnAssets', 'returnOnEquity', 'revenuePerShare', 'Market Cap', 'enterpriseValue', 'forwardPE', 'pegRatio', 'enterpriseToRevenue', 'enterpriseToEbitda', 'totalRevenue', 'grossProfit', 'ebitda', 'netIncomeToCommon', 'trailingEps', 'earningsGrowth', 'revenueGrowth', 'totalCash', 'totalCashPerShare', 'totalDebt', 'currentRatio', 'bookValue', 'operatingCashflow', 'beta', 'heldPercentInsiders', 'heldPercentInstitutions', 'sharesShort', 'shortRatio', 'shortPercentOfFloat', 'sharesShortPriorMonth', 'currentPrice', 'sharesOutstanding' ]): df = pd.DataFrame(columns=[ 'Date', 'Unix', 'Ticker', 'Price', 'stock_p_change', 'SP500', 'sp500_p_change', 'Difference', 'DE Ratio', 'Trailing P/E', 'Price/Sales', 'Price/Book', 'Profit Margin', 'Operating Margin', 'Return on Assets', 'Return on Equity', 'Revenue Per Share', 'Market Cap', 'Enterprise Value', 'Forward P/E', 'PEG Ratio', 'Enterprise Value/Revenue', 'Enterprise Value/EBITDA', 'Revenue', 'Gross Profit', 'EBITDA', 'Net Income Avl to Common ', 'Diluted EPS', 'Earnings Growth', 'Revenue Growth', 'Total Cash', 'Total Cash Per Share', 'Total Debt', 'Current Ratio', 'Book Value Per Share', 'Cash Flow', 'Beta', 'Held by Insiders', 'Held by Institutions', 'Shares Short (as of', 'Short Ratio', 'Short % of Float', 'Shares Short (prior ', 'Status' ]) #time.sleep(2) file_list = os.listdir("YaParse") counter = 0 passfiles = 0 for each_file in file_list: ticker = each_file.split(".json")[0] full_file_path = "YaParse/" + each_file source = open(full_file_path, "r").read() data = Share(ticker.upper()) counter += 1 print("File No: " + str(counter)) try: value_list = [] for each_data in gather: try: #value = float(source.split(gather + ':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0]) regex = re.escape( each_data) + r'.*?(\d{1,8}\.\d{1,8}M?B?K?|N/A)%?' value = re.search(regex, source) value = (value.group(1)) if "B" in value: value = float(value.replace("B", '')) * 1000000000 elif "M" in value: value = float(value.replace("M", '')) * 1000000 value_list.append(value) except Exception as e: if not (each_data == 'Trailing P/E' or each_data == 'Price/Sales' or each_data == 'Market Cap'): print("Exception1: " + str(e)) print("Data: " + str(each_data) + " File: " + str(each_file)) #time.sleep(5) value = "N/A" if each_data == 'Price/Sales': value = data.get_price_sales() if each_data == 'Market Cap': value = data.get_market_cap() if each_data == 'Trailing P/E': value = data.get_price_earnings_ratio() try: if "B" in value: value = float(value.replace("B", '')) * 1000000000 elif "M" in value: value = float(value.replace("M", '')) * 1000000 except Exception as e: value = "N/A" value_list.append(value) #print(value_list) #time.sleep(5) if value_list.count("N/A") > 0: print("Passing on this file: " + str(each_file) + " ::::Count of N/A: " + str(value_list.count("N/A"))) passfiles += 1 pass else: try: df = df.append( { 'Date': "N/A", 'Unix': "N/A", 'Ticker': ticker, 'Price': "N/A", 'stock_p_change': "N/A", 'SP500': "N/A", 'sp500_p_change': "N/A", 'Difference': "N/A", 'DE Ratio': value_list[0], 'Trailing P/E': value_list[1], 'Price/Sales': value_list[2], 'Price/Book': value_list[3], 'Profit Margin': value_list[4], 'Operating Margin': value_list[5], 'Return on Assets': value_list[6], 'Return on Equity': value_list[7], 'Revenue Per Share': value_list[8], 'Market Cap': value_list[9], 'Enterprise Value': value_list[10], 'Forward P/E': value_list[11], 'PEG Ratio': value_list[12], 'Enterprise Value/Revenue': value_list[13], 'Enterprise Value/EBITDA': value_list[14], 'Revenue': value_list[15], 'Gross Profit': value_list[16], 'EBITDA': value_list[17], 'Net Income Avl to Common ': value_list[18], 'Diluted EPS': value_list[19], 'Earnings Growth': value_list[20], 'Revenue Growth': value_list[21], 'Total Cash': value_list[22], 'Total Cash Per Share': value_list[23], 'Total Debt': value_list[24], 'Current Ratio': value_list[25], 'Book Value Per Share': value_list[26], 'Cash Flow': value_list[27], 'Beta': value_list[28], 'Held by Insiders': value_list[29], 'Held by Institutions': value_list[30], 'Shares Short (as of': value_list[31], 'Short Ratio': value_list[32], 'Short % of Float': value_list[33], 'Shares Short (prior ': value_list[34], 'Current Price': value_list[35], 'Shares Outstanding': value_list[36], 'Status': "N/A" }, ignore_index=True) except Exception as e: print("Problem in DF Append: " + str(e)) print("Data: " + str(each_data) + "File: " + str(each_file)) print( 'Error on line {}'.format( sys.exc_info()[-1].tb_lineno), type(e).__name__, e) time.sleep(5) #print("DataFrame: ", df) except Exception as e: print("Problem as a whole: " + str(e)) print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) pass df.to_csv('YaParse_sample_NO_NA.csv') print("Done making csv") print("No. of files passed: " + str(passfiles))
def view_stock(request, ticker): if request.user.__class__.__name__ is 'CustomUser': c_user = get_object_or_404(CustomUser, pk=request.user.pk) account = Account.objects.get(user=c_user) else: account = False stock = get_object_or_404(Stock, ticker=ticker) companyName = stock.ticker companyName = companyName.upper() stock = Stock.objects.get(ticker=companyName) namer = "'" + companyName + "'" ystock = Share(companyName) the_price = ystock.get_price() regex = 'Business Summary</span></th><th align="right"> </th></tr></table><p>(.+?)</p>' pattern = re.compile(regex) root_url = urllib.urlopen("http://finance.yahoo.com/q/pr?s=" + companyName + "+Profile") htmltext = root_url.read() decoded_str = str(re.findall(pattern, htmltext)).decode("utf8") encoded_str = decoded_str.encode('ascii', 'ignore') stock.description = encoded_str stock.description = stock.description[:-2] stock.description = stock.description[2:] stock.book_value = ystockquote.get_book_value(companyName) stock.change = ystockquote.get_change(companyName) #stock.dividend_per_share = ystockquote.get_dividend_per_share(companyName) #stock.dividend_yield = ystockquote.get_dividend_yield(companyName) stock.ebitda = ystockquote.get_ebitda(companyName) stock.fifty_two_week_high = ystockquote.get_52_week_high(companyName) stock.fifty_two_week_low = ystockquote.get_52_week_low(companyName) stock.market_cap = ystockquote.get_market_cap(companyName) stock.short_ratio = ystockquote.get_short_ratio(companyName) stock.stock_exchange = ystockquote.get_stock_exchange(companyName) stock.volume = ystockquote.get_volume(companyName) stock.price = ystock.get_price() #yahoo_finance stock.average_daily_volume = ystock.get_avg_daily_volume() stock.earnings_per_share = ystock.get_price_earnings_ratio() stock.fifty_day_moving_avg = ystock.get_50day_moving_avg() stock.two_hundred_day_moving_avg = ystock.get_200day_moving_avg() stock.price_book_ratio = ystock.get_price_book() stock.last_sale = ystock.get_price() stock.price_earnings_growth_ratio = ystock.get_price_earnings_growth_ratio( ) stock.price_earnings_ratio = ystock.get_price_earnings_ratio() stock.price_sales_ratio = ystock.get_price_sales() stock.save() vl = [] acl = [] hl = [] ll = [] cl = [] ol = [] days_list = [] d = 0 seven_days_ago = datetime.datetime.now() + datetime.timedelta(-30) today = datetime.datetime.now() days = ystockquote.get_historical_prices( 'GOOGL', seven_days_ago.strftime("%Y-%m-%d"), today.strftime("%Y-%m-%d")) for day in days.keys(): d += 1 date_label = datetime.datetime.now() + datetime.timedelta(-d) days_list.append(date_label.strftime("%b-%d")) day_info = days.get(day) vol = int(day_info.get('Volume')) vl.append(vol) adjcl = float(day_info.get('Adj Close')) acl.append(adjcl) highs = float(day_info.get('High')) hl.append(highs) lows = float(day_info.get('Low')) ll.append(lows) closes = float(day_info.get('Close')) cl.append(closes) opens = float(day_info.get('Open')) ol.append(opens) volume = vl lows = ll opens = ol highs = hl averages = acl closes = cl days_l = days_list[::-1] context = RequestContext( request, dict(account=account, request=request, stock=stock, volume=volume, lows=lows, highs=highs, opens=opens, closes=closes, averages=averages, days_l=days_l)) return render_to_response('scrapyr_app/stock.html', context=context)
def view_stock(request, ticker): if request.user.__class__.__name__ is "CustomUser": c_user = get_object_or_404(CustomUser, pk=request.user.pk) account = Account.objects.get(user=c_user) else: account = False stock = get_object_or_404(Stock, ticker=ticker) companyName = stock.ticker companyName = companyName.upper() stock = Stock.objects.get(ticker=companyName) namer = "'" + companyName + "'" ystock = Share(companyName) the_price = ystock.get_price() regex = 'Business Summary</span></th><th align="right"> </th></tr></table><p>(.+?)</p>' pattern = re.compile(regex) root_url = urllib.urlopen("http://finance.yahoo.com/q/pr?s=" + companyName + "+Profile") htmltext = root_url.read() decoded_str = str(re.findall(pattern, htmltext)).decode("utf8") encoded_str = decoded_str.encode("ascii", "ignore") stock.description = encoded_str stock.description = stock.description[:-2] stock.description = stock.description[2:] stock.book_value = ystockquote.get_book_value(companyName) stock.change = ystockquote.get_change(companyName) # stock.dividend_per_share = ystockquote.get_dividend_per_share(companyName) # stock.dividend_yield = ystockquote.get_dividend_yield(companyName) stock.ebitda = ystockquote.get_ebitda(companyName) stock.fifty_two_week_high = ystockquote.get_52_week_high(companyName) stock.fifty_two_week_low = ystockquote.get_52_week_low(companyName) stock.market_cap = ystockquote.get_market_cap(companyName) stock.short_ratio = ystockquote.get_short_ratio(companyName) stock.stock_exchange = ystockquote.get_stock_exchange(companyName) stock.volume = ystockquote.get_volume(companyName) stock.price = ystock.get_price() # yahoo_finance stock.average_daily_volume = ystock.get_avg_daily_volume() stock.earnings_per_share = ystock.get_price_earnings_ratio() stock.fifty_day_moving_avg = ystock.get_50day_moving_avg() stock.two_hundred_day_moving_avg = ystock.get_200day_moving_avg() stock.price_book_ratio = ystock.get_price_book() stock.last_sale = ystock.get_price() stock.price_earnings_growth_ratio = ystock.get_price_earnings_growth_ratio() stock.price_earnings_ratio = ystock.get_price_earnings_ratio() stock.price_sales_ratio = ystock.get_price_sales() stock.save() vl = [] acl = [] hl = [] ll = [] cl = [] ol = [] days_list = [] d = 0 seven_days_ago = datetime.datetime.now() + datetime.timedelta(-30) today = datetime.datetime.now() days = ystockquote.get_historical_prices("GOOGL", seven_days_ago.strftime("%Y-%m-%d"), today.strftime("%Y-%m-%d")) for day in days.keys(): d += 1 date_label = datetime.datetime.now() + datetime.timedelta(-d) days_list.append(date_label.strftime("%b-%d")) day_info = days.get(day) vol = int(day_info.get("Volume")) vl.append(vol) adjcl = float(day_info.get("Adj Close")) acl.append(adjcl) highs = float(day_info.get("High")) hl.append(highs) lows = float(day_info.get("Low")) ll.append(lows) closes = float(day_info.get("Close")) cl.append(closes) opens = float(day_info.get("Open")) ol.append(opens) volume = vl lows = ll opens = ol highs = hl averages = acl closes = cl days_l = days_list[::-1] context = RequestContext( request, dict( account=account, request=request, stock=stock, volume=volume, lows=lows, highs=highs, opens=opens, closes=closes, averages=averages, days_l=days_l, ), ) return render_to_response("scrapyr_app/stock.html", context=context)
# -*- coding: utf-8 -*- """ Created on Fri Jun 10 17:01:18 2016 @author: SMALLON """ IBB = ['AMGN','GILD','CELG','BIIB','REGN','MYL','ILMN','VRTX','ALXN','INCY','BMRN','MDVN','JAZZ', 'ALKS','SHPG','SGEN','ALNY','QGEN','UTHR','TSRO','NBIX','TECH','ENDP','ACAD','AKRX','ICPT','IONS','HZNP','JUNO','CBPO','LGND','PRAH','GRFS','MDCO','KITE','INCR','IPXL','MYGN','OPHT','NKTR','RARE','NVAX','EXEL','PRTA','IRWD','RDUS','BLUE','ONCE','AGIO','LXRX','ARIA','GWPH','PCRX','SAGE','PTLA','MCRB','ALDR','FPRX','INVA','XLRN','HALO','ACOR','ACHN','DEPO','FGEN','DERM','INSY','TBPH','CXRX','SRPT','CHRS','SUPN','MGNX','LMNX','AMAG','GHDX','RGEN','RLYP','CEMP','ADRO','MNTA','DBVT','FMI','AMPH','FOLD','XNCR','EGRX','SGNT','MACK','PACB','INSM','SGYP','ANIP','INO','CERS','RTRX','ATRA','AMRI','EPZM','PDLI','BPMC','CLVS','SCMP','NK','ARRY','XBIT','SCLN','FLML','RPTP','VNDA','LBIO','AERI','MNKD','CLDX','SPPI','OTIC','TLGT','FLXN','ARNA','OMER','GERN','PGNX','ENTA','AMRN','SGMO','RVNC','NDRM', 'BLCM','OMED','TRVN','ARDX','ARWR','CGEN','VSAR','AKBA','ADMS','NLNK','VTAE','COLL','AGTC','NSTG','CNCE','ESPR','IMGN','KPTI','PTCT','IMMU','PETX','ITEK','CCXI','BCRX','DRRX','FOMX','FLKS','ZGNX','RIGL','CRIS','VTL','CMRX','OVAS','RGLS','OSIR','QURE','EGLT','TTPH','NEOS','CASC','IMDZ','CARA','ECYT','AQXP','ANTH','OCUL','ADVM','ADHD','TKAI','SQNM','AFMD','ZFGN','INFI','LIFE','DRNA','CHMA','DNAI','KMPH','OREX','AEGR','USD'] SMBIx = IBB = ['AMGN','GILD','CELG','BIIB','REGN','MYL','ILMN','VRTX','ALXN','INCY','BMRN','MDVN','JAZZ', 'ALKS','SHPG','SGEN','ALNY','QGEN','UTHR','TSRO','NBIX','TECH','ENDP','ACAD','AKRX','ICPT','IONS','HZNP','JUNO','CBPO','LGND','PRAH','GRFS','MDCO','KITE','INCR','IPXL','MYGN','OPHT','NKTR','RARE','NVAX','EXEL','PRTA','IRWD','RDUS','BLUE','ONCE','AGIO','LXRX','ARIA','GWPH','PCRX','SAGE','PTLA','MCRB','ALDR','FPRX','INVA','XLRN','HALO','ACOR','ACHN','DEPO','FGEN','DERM','INSY','TBPH','CXRX','SRPT','CHRS','SUPN','MGNX','LMNX','AMAG','GHDX','RGEN','RLYP','CEMP','ADRO','MNTA','DBVT','FMI','AMPH','FOLD','XNCR','EGRX','SGNT','MACK','PACB','INSM','SGYP','ANIP','INO','CERS','RTRX','ATRA','AMRI','EPZM','PDLI','BPMC','CLVS','SCMP','NK','ARRY','XBIT','SCLN','FLML','RPTP','VNDA','LBIO','AERI','MNKD','CLDX','SPPI','OTIC','TLGT','FLXN','ARNA','OMER','GERN','PGNX','ENTA','AMRN','SGMO','RVNC','NDRM', 'BLCM','OMED','TRVN','ARDX','ARWR','CGEN','VSAR','AKBA','ADMS','NLNK','VTAE','COLL','AGTC','NSTG','CNCE','ESPR','IMGN','KPTI','PTCT','IMMU','PETX','ITEK','CCXI','BCRX','DRRX','FOMX','FLKS','ZGNX','RIGL','CRIS','VTL','CMRX','OVAS','RGLS','OSIR','QURE','EGLT','TTPH','NEOS','CASC','IMDZ','CARA','ECYT','AQXP','ANTH','OCUL','ADVM','ADHD','TKAI','SQNM','AFMD','ZFGN','INFI','LIFE','DRNA','CHMA','DNAI','KMPH','OREX','AEGR','USD'] from yahoo_finance import Share for i in IBB: t = Share(i) book = float(t.get_book_value()) if (book > 5.00): print(i,book,"Book value more than 5b") from yahoo_finance import Share for i in IBB: t = Share(i) P2S = (t.get_price_sales()) print(P2S)
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 checklist(symbol, ibd50_list, ibd_session): """ Looks up information on a given stock market symbol. The returned dictionary contains all information from Dr. Wish's Stock Checklist for HONR348M. """ stock = {} # Load price data from yahoo. share = Share(symbol) ks = yahoo_ks(symbol) # Basics basics = stock["basics"] = {} basics["date"] = datetime.now().strftime("%m/%d/%Y %I:%M:%S%z") basics["symbol"] = symbol basics["equity_name"] = share.get_name() basics["price"] = float(share.get_price()) basics["52w_low"] = float(share.get_year_low()) basics["52w_high"] = float(share.get_year_high()) basics["percent_from_52w_low"] = share.get_percent_change_from_year_low() basics["percent_from_52w_high"] = share.get_percent_change_from_year_high() # IBD (Stocks only) ibd = stock["ibd"] = ibd_stock_checkup(symbol, ibd_session) # ibd["industry"] ibd["industry_rank"] = float(ibd["industry_rank"]) # ibd["industry_top5"] # ibd["3y_eps_growth"] # ibd["3y_sales_growth"] # ibd["eps_change"] ibd["eps_rating"] = float(ibd["eps_rating"]) ibd["rs_rating"] = float(ibd["rs_rating"]) # ibd["acc_distr_rating"] ibd["ibd_rating"] = float(ibd["ibd_rating"]) ibd["in_ibd50"] = symbol in ibd50_list # ibd["fundamental_greens"] # ibd["technical_greens"] ibd["next_earning"] = datetime.strptime(ibd["next_earning"], '%m/%d/%Y') # Yahoo Finance (Stocks only) yahoo = stock["yahoo"] = {} yahoo["pe"] = float(share.get_price_earnings_ratio()) yahoo["peg"] = float(share.get_price_earnings_growth_ratio()) yahoo["ps"] = float(share.get_price_sales()) yahoo["market_cap"] = share.get_market_cap() yahoo["float"] = ks["Float"] yahoo["annual_roe"] = ks["Return on Equity"] yahoo["percent_inst"] = ks["% Held by Institutions"] yahoo["percent_float_short"] = ks["Short % of Float"] yahoo["short_ratio"] = float(share.get_short_ratio()) # Evidence of an uptrend/downtrend uptrend = stock["uptrend"] = {} downtrend = stock["downtrend"] = {} pdstockdata = data.DataReader(symbol, 'yahoo', '1900-01-01') sd = StockDataFrame.retype(pdstockdata) sd.BOLL_PERIOD = 15 close1 = sd['close'][-1] close2 = sd['close'][-2] low1 = sd['low'][-1] low2 = sd['low'][-2] high1 = sd['high'][-1] high2 = sd['high'][-2] avg_30d = sd['close_30_sma'][-1] avg_4w = sd['close_20_sma'][-1] avg_10w = sd['close_50_sma'][-1] avg_30w = sd['close_150_sma'][-1] high_52w = sd['high'].tail(250).max() lbb1 = sd['boll_lb'][-1] lbb2 = sd['boll_lb'][-2] ubb1 = sd['boll_ub'][-1] ubb2 = sd['boll_ub'][-2] # Find all GLTs (ATH not broken for at least another 90 days) last_ath = 0.0 ath = Series() for day, day_high in sd['high'].iteritems(): last_ath = max(last_ath, day_high) ath.set_value(day, last_ath) ath_days = sd[sd['high'] == ath]['high'] glt = Series() for i, (day, high) in enumerate(ath_days.iteritems()): next_day = ath_days.keys()[i + 1] if i < len(ath_days) - 1 else Timestamp( str(date.today())) if next_day - day >= Timedelta('90 days'): glt.set_value(day, high) uptrend["c>30d_avg"] = close1 > avg_30d uptrend["c>10w_avg"] = close1 > avg_10w uptrend["c>30w_avg"] = close1 > avg_30w uptrend["4w>10w>30w"] = avg_4w > avg_10w > avg_30w # uptrend["w_rwb"] = uptrend["last_glt_date"] = glt.keys()[-1].to_datetime().date( ) if len(glt) > 0 else None uptrend["last_glt_high"] = glt[-1] if len(glt) > 0 else None uptrend["above_last_glt"] = len(glt) > 0 and close1 > glt[-1] uptrend["macd_hist_rising"] = sd['macdh'][-1] > sd['macdh_4_sma'][-1] uptrend["stoch_fast>slow"] = sd['rsv_10_4_sma'][-1] > sd[ 'rsv_10_4_sma_4_sma'][-1] # uptrend["bb_up_expansion_l2"] # uptrend["rs>30d_avg"] = (Need Investors.com data) # uptrend["rs_rising"] = (Need Investors.com data) uptrend["52w_high_l2"] = high_52w == high1 or high_52w == high2 uptrend["ath_l2"] = ath[-1] == high1 or ath[-2] == high2 uptrend["1y_doubled"] = close1 >= 2 * sd['close'][-255:-245].mean() # uptrend["bounce_30d_l2"] = # uptrend["bounce_10w_l2"] = # uptrend["bounce_30w_l2"] = uptrend["stoch<50"] = sd['rsv_10_4_sma'][-1] < 50 uptrend["<bb_lower_l2"] = low1 < lbb1 or low2 < lbb2 uptrend[ "above_avg_volume"] = sd['volume'][-1] > 1.5 * sd['volume_50_sma'][-1] downtrend["c<30d_avg"] = close1 < avg_30d downtrend["c<10w_avg"] = close1 < avg_10w downtrend["c<30w_avg"] = close1 < avg_30w downtrend["4w<10w<30w"] = avg_4w < avg_10w < avg_30w # downtrend["w_bwr"] = downtrend["macd_hist_falling"] = sd['macdh'][-1] < sd['macdh_4_sma'][-1] downtrend["stoch_fast<slow"] = sd['rsv_10_4_sma'][-1] < sd[ 'rsv_10_4_sma_4_sma'][-1] # downtrend["bb_down_expansion_l2"] # downtrend["bounce_30d_l2"] = # downtrend["bounce_10w_l2"] = # downtrend["bounce_30w_l2"] = downtrend["stoch>50"] = sd['rsv_10_4_sma'][-1] > 50 downtrend[">bb_upper_l2"] = high1 > ubb1 or high2 > ubb2 return stock
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)
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( ) print "get_percent_change_from_year_low:", tesla.get_percent_change_from_year_low( ) print "get_change_from_year_low:", tesla.get_change_from_year_low() print "get_change_from_year_high:", tesla.get_change_from_year_high() print "get_percent_change_from_200_day_moving_average:", tesla.get_percent_change_from_200_day_moving_average( )