def Get_Earnings_Surprise(stock): '''Go to Yahoo Finance and scrape the Analyst Estimates. Determine if the Earning Suprises were positive. Return the results in a JSON object.''' analyst_estimates = 'http://ca.finance.yahoo.com/q/ae?s=' + stock + '+Analyst+Estimates' #Get Last 4 EPS Surprises analyst_estimates_web = requests.get(analyst_estimates) analyst_estimates_text = analyst_estimates_web.content analyst_estimates_soup = bs(analyst_estimates_text, "lxml") analyst_estimates_rows = analyst_estimates_soup.findAll('tr') #i = 0 try: for n in analyst_estimates_rows: n = str(n) if mods.Match('<tr><td class="yfnc_tablehead1"(.*?)EPS Est', n): n = n.split('<td class="yfnc_tabledata1">') est_list = [n[1], n[2], n[3], n[4]] for lst, item in enumerate(est_list): est_list[lst] = float(item.replace('</tr>', '').replace('</td>', '')) elif mods.Match('<tr><td class="yfnc_tablehead1"(.*?)EPS Actual', n): n = n.split('<td class="yfnc_tabledata1">') actual_list = [n[1], n[2], n[3], n[4]] for lst, item in enumerate(actual_list): actual_list[lst] = float(item.replace('</tr>', '').replace('</td>', '')) elif mods.Match('<tr><th class="yfnc_tablehead1"(.*?)Revenue Est', n): n = n.split('<th class="yfnc_tablehead1" scope="col" style="font-size:11px;text-align:right;" width="18%">') dates = [n[1], n[2], n[3], n[4]] for lst, item in enumerate(dates): dates[lst] = item.replace('</tr>', '').replace('</td>', '').replace('</th>', '').replace('<br/>', ' ') surprise1, surprise2, surprise3, surprise4 = 1 - est_list[0]/actual_list[0], 1 - est_list[1]/actual_list[1], 1 - est_list[2]/actual_list[2], 1 - est_list[3]/actual_list[3] #Test if Earnings Surprise was Positive is_postive_pes_surprise = False if surprise1 > 0 and surprise2 > 0 and surprise3 > 0 and surprise4 > 0: is_postive_pes_surprise = True #Create Ditionary of Results results = collections.OrderedDict() results['Stock'] = stock results['DateRun'] = mods.Today() results['TestRun'] = 'Earnings_Surprise' results[dates[0].replace('Qtr.', '').replace('Current', '',).replace('Next', '').replace('Year', '').strip()] = surprise1 results[dates[1].replace('Qtr.', '').replace('Current', '',).replace('Next', '').replace('Year', '').strip()] = surprise2 results[dates[2].replace('Qtr.', '').replace('Current', '',).replace('Next', '').replace('Year', '').strip()] = surprise3 results[dates[3].replace('Qtr.', '').replace('Current', '',).replace('Next', '').replace('Year', '').strip()] = surprise4 results['Actuals'] = actual_list results['EST'] = est_list results['Result'] = is_postive_pes_surprise return results except: results = collections.OrderedDict() results['stock'] = stock results['TestRun'] = 'Earnings_Surprise' results['DateRun'] = mods.Today() results['Result'] = 'Test Failed' return results
def Get_Revenue(stock): '''Go to Yahoo Finance and Pull the Last Three Years of Revenue Results and Determine if Revenue Passes or Fails Test. Return the results as a JSON object.''' revenue = 'http://ca.finance.yahoo.com/q/is?s=' + stock + '&annual' #Get Revenue Data from Last Income Statement revenue_web = requests.get(revenue) revenue_text = revenue_web.content revenue_soup = bs(revenue_text, "lxml") try: # Get Last Three Years of Revenue revenue = [] for n in revenue_soup.find_all('tr'): n = str(n) if mods.Match('<tr><td colspan="2">\n<strong>\n\s+Total Revenue', n): n = n.replace('\n', '').replace('</strong>', '').replace('</td>', '').replace('<td align="right">', '').replace('</tr', '').strip() n = n.split('<strong>') for item in n: item = item.strip() if mods.Match('\d+', item): item = item.replace(' ', '').replace('>', '').replace(',', '').replace('\xc2\xa0\xc2\xa0', '').strip() revenue.append(item) last_reported_rev, second_reported_rev, third_reported_rev = float(revenue[0]), float(revenue[1]), float(revenue[2]) # # #Get Dates for Last Three Reported Years dates = mods.get_reported_dates(stock) last_reported_dt, year_before_reported_dt, two_yr_before_reported_dt = dates[0], dates[1], dates[2] # #Test if Revenue Results are Positive is_positive_rev = False if last_reported_rev > second_reported_rev and second_reported_rev > third_reported_rev: is_positive_rev = True # # #Create Ditionary of Results results = collections.OrderedDict() results['stock'] = stock results['TestRun'] = 'Revenue_Test' results['DateRun'] = mods.Today() results['Result'] = is_positive_rev results[last_reported_dt] = last_reported_rev results[year_before_reported_dt] = second_reported_rev results[two_yr_before_reported_dt] = third_reported_rev return results except: results = collections.OrderedDict() results['stock'] = stock results['TestRun'] = 'Revenue_Test' results['DateRun'] = mods.Today() results['Result'] = 'Test Failed' return results
def Get_Earnings_V_Industry(stock): '''Go to Yahoo finance and pull information from Analyst Estimates. Determine if the earnings are favorable compared to the industry. Return the results as a JSON object.''' analyst_estimates = 'http://ca.finance.yahoo.com/q/ae?s=' + stock + '+Analyst+Estimates' #Get Last 4 EPS Surprises analyst_estimates_web = requests.get(analyst_estimates) analyst_estimates_text = analyst_estimates_web.content analyst_estimates_soup = bs(analyst_estimates_text) analyst_estimates_rows = analyst_estimates_soup.findAll('tr') #i = 0 try: for n in analyst_estimates_rows: n = str(n) if mods.Match('<tr><td class="yfnc_tablehead1"(.*?)Price/Earnings', n): n = n.split('<td class="yfnc_tabledata1">') pe_list = [n[1], n[2], n[3], n[4]] for lst, item in enumerate(pe_list): pe_list[lst] = float( item.replace('</tr>', '').replace('</td>', '').replace('%', '')) #Test if Earnings Surprise was Positive is_postive_pe_v_industry = False if pe_list[0] > pe_list[1]: is_postive_pe_v_industry = True #Create Ditionary of Results results = collections.OrderedDict() results['Stock'] = stock results['DateRun'] = mods.Today() results['TestRun'] = 'Earnings Versus Industry' results['PE Estimate: ' + stock] = pe_list[0] results['Industry Estimate:'] = pe_list[1] results['Result'] = is_postive_pe_v_industry return results except: results = collections.OrderedDict() results['stock'] = stock results['TestRun'] = 'Earnings Versus Industry' results['DateRun'] = mods.Today() results['Result'] = 'Test Failed' return results
def Get_Growth_Forecast(stock): '''Go to Yahoo Finance and get the analyst estimates. Determine if the last 4 EPS surprise results were positive. Return the results as a JSON object.''' analyst_estimates = 'http://ca.finance.yahoo.com/q/ae?s=' + stock + '+Analyst+Estimates' #Get Last 4 EPS Surprises analyst_estimates_web = requests.get(analyst_estimates) analyst_estimates_text = analyst_estimates_web.content analyst_estimates_soup = bs(analyst_estimates_text, 'lxml') analyst_estimates_rows = analyst_estimates_soup.findAll('tr') #i = 0 try: for n in analyst_estimates_rows: n = str(n) if mods.Match('<tr><td class="yfnc_tablehead1"(.*?)Next 5 Years', n): n = n.split('<td class="yfnc_tabledata1">') est_list = [n[1], n[2], n[3], n[4]] for lst, item in enumerate(est_list): est_list[lst] = float( item.replace('</tr>', '').replace('</td>', '').replace('%', '')) #Test if Earnings Surprise was Positive is_postive_growth_forecast = False if est_list[0] > 8: is_postive_growth_forecast = True #Create Ditionary of Results results = collections.OrderedDict() results['Stock'] = stock results['DateRun'] = mods.Today() results['TestRun'] = 'Growth_Forecast' results['Growth Forecast: ' + stock] = est_list[0] results['Result'] = is_postive_growth_forecast return results except: results = collections.OrderedDict() results['stock'] = stock results['TestRun'] = 'Growth_Forecast' results['DateRun'] = mods.Today() results['Result'] = 'Test Failed' return results
def Get_Shares_Short(stock): '''Got to Yahoo Finance and determine the Insider Tracking of the stock. Return the results as a JSON object.''' insider_transactions = 'http://ca.finance.yahoo.com/q/it?s=' + stock + '+Insider+Transactions' #Get Insider Transaction Data (Last 6 Months and Last 3 Months) insider_transactions_web = requests.get(insider_transactions) insider_transactions_text = insider_transactions_web.content insider_transactions_soup = bs(insider_transactions_text) insider_transactions_rows = insider_transactions_soup.findAll('tr') i = 0 insider_list = [] for n in insider_transactions_rows: if n.text[0:10] == 'Net Shares': n = str(n) n = n.split('<td align="right" class="yfnc_tabledata1"') insider_list.append(n[1].replace('</td>', '').replace( '</tr>', '').replace(' width="20%">', '').replace('>', '').replace(',', '')) try: #Test if Positive Insider Tracking (Positive Purchase of Stock in Last 3 Months) is_postive_insider = False if mods.Match('\(\d+\)', insider_list[1]): is_postive_insider = True #Create Ditionary of Results results = collections.OrderedDict() results['Stock'] = stock results['DateRun'] = mods.Today() results['TestRun'] = 'Insider_Trading' results['Insider Purchasing 6 Month: ' + stock] = insider_list[0] results['Insider Purchasing 3 Month: ' + stock] = insider_list[1] results['Result'] = is_postive_insider return results except: results = collections.OrderedDict() results['stock'] = stock results['TestRun'] = 'Insider_Trading' results['DateRun'] = mods.Today() results['Result'] = 'Test Failed' return results
def Get_Earnings_Forecast(stock): '''Go to Yahoo Finance and scrape the Analyst Estimates. Determine if the Earnings Forecast is postive. Return the results in a JSON object.''' forecast = 'http://ca.finance.yahoo.com/q/ae?s=' + stock + '+Analyst+Estimates' #Get Last 4 Quarters of Earnings Forecasts forecast_web = requests.get(forecast) forecast_text = forecast_web.content forecast_soup = bs(forecast_text) forecast_rows = forecast_soup.findAll('tr') #i = 0 try: lists = [] for n in forecast_rows: n = str(n) if mods.Match('<tr><th class="yfnc_tablehead1"(.*?)Earnings Est', n): n = n.split( '<th class="yfnc_tablehead1" scope="col" style="font-size:11px; font-weight:normal;text-align:right;" width="18%">' ) dates_list = [n[1], n[2], n[3], n[4]] for lst, item in enumerate(dates_list): dates_list[lst] = item.replace('</tr>', '').replace( '</td>', '').replace('</th>', '').replace('<br/>', ' ') elif mods.Match( '<tr><td class="yfnc_tablehead1"(.*?)Avg. Estimate', n): n = n.split('<td class="yfnc_tabledata1">') forecast_list = [n[1], n[2], n[3], n[4]] try: for lst, item in enumerate(forecast_list): forecast_list[lst] = float( item.replace('</tr>', '').replace('</td>', '')) lists.append(forecast_list) except: dummy = 0 #Test for Positive Forecasts is_positive_earnings_forecast = False if lists[0][0] < lists[0][1] and lists[0][1] < lists[0][2] and lists[ 0][2] < lists[0][3]: is_positive_earnings_forecast = True #Create Ditionary of Results results = collections.OrderedDict() results['Stock'] = stock results['TestRun'] = 'Earnings_Forecast' results['DateRun'] = mods.Today() results['Result'] = is_positive_earnings_forecast results['Current Qtr'] = lists[0][0] results['Next Qtr'] = lists[0][1] results['Current Year'] = lists[0][2] results['Next Year'] = lists[0][3] return results except: results = collections.OrderedDict() results['stock'] = stock results['TestRun'] = 'Earnings_Forecast' results['DateRun'] = mods.Today() results['Result'] = 'Test Failed' return results ##Testing: #stock = 'AAPL' #print Get_Earnings_Forecast(stock)
def Get_ROE(stock): '''Go to Yahoo Finance. Get the results from the Key Statistics page, Income Statement, and Balance Sheet. Determine if the results are positive for a good return on equity. Return the results as a JSON object.''' key_stats = 'http://ca.finance.yahoo.com/q/ks?s=' + stock + '+Key+Statistics' income_statement = 'http://ca.finance.yahoo.com/q/is?s=' + stock + '+Income+Statement&annual' balance_sheet = 'http://ca.finance.yahoo.com/q/bs?s=' + stock + '+Balance+Sheet&annual' #Get Last 12 Months ROE key_stats_web = requests.get(key_stats) key_stats_text = key_stats_web.content key_stats_soup = bs(key_stats_text, "lxml") for n in key_stats_soup.find_all('tr'): if n.text[0:16] == 'Return on Equity': amount = n.text.split(':') amount = str(amount[1]) roe_12month = float(amount[0:-1]) # Get Last Three Years of Return on Equity (Net Income/Stock Holder's Equity) # First get the Last Three Years of Net Income from Income Statement income_statement_web = requests.get(income_statement) income_statement_text = income_statement_web.content income_statement_soup = bs(income_statement_text) income_statement_rows = income_statement_soup.findAll('tr') #i = 0 try: for row in income_statement_rows: row = str(row) if mods.Match('<tr><td colspan="2">\n<strong>\n\s+Net Income', row): row = row.split('\n') last_reported_ni, second_reported_ni, third_reported_ni = row[6].strip(), row[10].strip(), row[14].strip() last_reported_ni, second_reported_ni, third_reported_ni = float(last_reported_ni[0:-4].replace(',', '')),float(second_reported_ni[0:-4].replace(',', '')), float(third_reported_ni[0:-4].replace(',', '')) # Second get the Last Three Years of StockHolder's Equity from Balance Sheet balance_sheet_web = requests.get(balance_sheet) balance_sheet_text = balance_sheet_web.content balance_sheet_soup = bs(balance_sheet_text, 'lxml') balance_sheet_rows = balance_sheet_soup.findAll('tr') for row in balance_sheet_rows: row = str(row) if mods.Match('<tr><td colspan="2">\n<strong>\n\s+Total Stockholder Equity', row): row = row.split('\n') last_reported_se, second_reported_se, third_reported_se = row[6].strip(), row[10].strip(), row[14].strip() last_reported_se, second_reported_se, third_reported_se = float(last_reported_se[0:-4].replace(',', '')),float(second_reported_se[0:-4].replace(',', '')), float(third_reported_se[0:-4].replace(',', '')) #Third Calculate ROE (Net Income/Stock Holders Equity) last_reported_roe, second_reported_roe, third_reported_roe = last_reported_ni/last_reported_se, second_reported_ni/second_reported_se, third_reported_ni/third_reported_se #Get Dates for Last Three Reported Years last_reported_dt, year_before_reported_dt, two_yr_before_reported_dt = mods.get_reported_dates(stock) is_positive_rev = False now = datetime.datetime.now() if int(last_reported_dt[0:2]) - now.month < 6: if roe_12month > second_reported_roe and second_reported_roe > third_reported_roe: is_positive_rev = True # Time since last annual reported revenue was less than 6 months else: if last_reported_roe > second_reported_roe and second_reported_roe > third_reported_roe: is_positive_rev = True #Create Ditionary of Results results = collections.OrderedDict() results['Stock'] = stock results['DateRun'] = mods.Today() results['TestRun'] = 'Positive_Return_on_Equity' results['Result'] = is_positive_rev results[last_reported_dt] = last_reported_roe results[year_before_reported_dt] = second_reported_roe results[two_yr_before_reported_dt] = third_reported_roe results['12 Month Return On Equity'] = roe_12month return results except: results = collections.OrderedDict() results['stock'] = stock results['TestRun'] = 'Positive_Return_on_Equity' results['DateRun'] = mods.Today() results['Result'] = 'Test Failed' return results
def Get_EPS(stock): #Go to Yahoo Finance and Pull the Last Three Years of Revenue Results and Determine if Revenue Passes or Fails Test #Current Year: http://finance.yahoo.com/q/ks?s=AAPL+Key+Statistics #Last Years Income Statement: http://finance.yahoo.com/q/is?s=AAPL+Income+Statement&annual key_stats = 'http://ca.finance.yahoo.com/q/ks?s=' + stock + '+Key+Statistics' market_watch = 'http://www.marketwatch.com/investing/stock/' + stock + '/financials' #Get Last 12 Months Earnings Per Share key_stats_web = requests.get(key_stats) key_stats_text = key_stats_web.content key_stats_soup = bs(key_stats_text) for n in key_stats_soup.find_all('tr'): if n.text[0:11] == 'Diluted EPS': amount = n.text.split(':') eps_12month = float(amount[1]) # Get Last Three Years of Earnings Per Share market_watch_web = requests.get(market_watch) market_watch_text = market_watch_web.content market_watch_soup = bs(market_watch_text) market_watch_rows = market_watch_soup.findAll('tr') #tag = '<tr class="mainRow"> \ #<td class="rowTitle"><a data-ref="ratio_Eps1YrAnnualGrowth" href="#"><span class="expand"></span></a> EPS (Basic)</td>' #i = 0 try: for row in market_watch_rows: row = str(row) if mods.Match('<tr class="mainRow">\n(.*?)EPS \(Diluted\)', row): row = row.split('<td class="valueCell">') last_reported_eps, second_reported_eps, third_reported_eps = float( row[5][0:4].strip()), float(row[4][0:4].strip()), float( row[3][0:4].strip()) #Get Dates for Last Three Reported Years last_reported_dt, year_before_reported_dt, two_yr_before_reported_dt = mods.get_reported_dates( stock) #print mods.get_reported_dates(stock) is_positive_eps = False now = datetime.datetime.now() if int(last_reported_dt[0:2]) - now.month < 6: if eps_12month > second_reported_eps and second_reported_eps > third_reported_eps: is_positive_eps = True # Time since last annual reported revenue was less than 6 months else: if last_reported_eps > second_reported_eps and second_reported_eps > third_reported_eps: is_positive_eps = True ''' ''' #Create Ditionary of Results results = collections.OrderedDict() results['stock'] = stock results['TestRun'] = 'EPS' results['DateRun'] = mods.Today() results['Result'] = is_positive_eps results[last_reported_dt] = last_reported_eps results[year_before_reported_dt] = second_reported_eps results[two_yr_before_reported_dt] = third_reported_eps results['12 Month Reported EPS'] = eps_12month return results except: results = collections.OrderedDict() results['stock'] = stock results['TestRun'] = 'EPS' results['DateRun'] = mods.Today() results['Result'] = 'Test Failed' return results #Testing: #stock = 'AAPL' #print Get_EPS(stock)