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_Options(stock, strike_price): # # Use Pandas Data Reader to pull data from Yahoo Finance all_data = {} #for ticker in symbol: all_data = web.get_data_yahoo(stock, '1/1/2015', mods.Today()) lst = [] last = 0 for current in all_data['Adj Close']: if len(lst) == 0: lst.append(np.nan) last = current else: lst.append(current / last) last = current all_data['Percent Changed'] = lst # # Run Monte Carlo simulations. 1000 iterations for 60 weeks to determine number of times the value of the stock is greater than the strike price. simulation_end = [] num_iters = 100 num_weeks = 10 changed_avg = all_data['Percent Changed'].mean() changed_stdev = all_data['Percent Changed'].std() for i in range(0, num_iters): for i in range(0, num_weeks): if i == 0: wk_end = all_data['Adj Close'][0] else: wk_end = norminv(np.random.random_sample(), changed_avg, changed_stdev) * wk_end simulation_end.append(max(0, wk_end - strike_price)) return all_data
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 main(): for stock in stocks: data = collections.OrderedDict() data['DateRun'] = mods.Today() data['Stock'] = stock stock = stock.lower() if mods.get_balance_sheet(stock, 'Common Stocks') != None: #Revenue Test revenue_result = revenue.Get_Revenue(stock) Dict_Aggregator(data, revenue_result) #4 Key Result Tests key_result = key.Get_Key_Indicators(stock) for item in key_result: Dict_Aggregator(data, item) #Positive Return on Equity roe_result = roe.Get_ROE(stock) Dict_Aggregator(data, roe_result) #Analyst Opinion opinion_result = opinion.Get_Analyst_Opinion(stock) Dict_Aggregator(data, opinion_result) #Earnings Forecast forecast_result = forecast.Get_Earnings_Forecast(stock) Dict_Aggregator(data, forecast_result) #Earnings Per Share eps_result = eps.Get_EPS(stock) Dict_Aggregator(data, eps_result) #Earnings Surprise eps_surprise = surprise.Get_Earnings_Surprise(stock) Dict_Aggregator(data, eps_surprise) #Growth Forecast growth = growth_forecast.Get_Growth_Forecast(stock) Dict_Aggregator(data, growth) #PEG Ratio peg_ratio = peg.Get_PEG_Ratio(stock) Dict_Aggregator(data, peg_ratio) #Earnings Versus Industry pevi = industry.Get_Earnings_V_Industry(stock) Dict_Aggregator(data, pevi) #Insider Trading short = shares_short.Get_Shares_Short(stock) Dict_Aggregator(data, short) mods.Insert_Data(export, 'Results', Evaluator(data))
def Get_Key_Indicators(stock): '''Use the Common Modules to scrape data from different websites for the stock. Determine if the results are positive. Return the results in JSON objects.''' try: # Get Last Three Years of Net Income net_income = mods.get_net_income(stock, 'Net Income') net_income = mods.Is_Negative(net_income) last_reported_ni, second_reported_ni, third_reported_ni = float( net_income[0]), float(net_income[1]), float(net_income[2]) #Get Last Three Years of Common Stock common_stock = mods.get_balance_sheet(stock, 'Common Stocks') common_stock = mods.Is_Negative(common_stock) last_reported_cs, second_reported_cs, third_reported_cs = float( common_stock[0]), float(common_stock[1]), float(common_stock[2]) treasury_stock = mods.get_balance_sheet(stock, 'Treasury Stock') treasury_stock = mods.Is_Negative(treasury_stock) last_reported_ts, second_reported_ts, third_reported_ts = float( treasury_stock[0]), float(treasury_stock[1]), float( treasury_stock[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 EPS is postivie for last three years last_reported_eps, second_reported_eps, third_reported_eps = last_reported_ni / ( last_reported_cs - last_reported_ts), second_reported_ni / ( second_reported_cs - second_reported_ts), third_reported_ni / ( third_reported_cs - third_reported_ts) is_positive_eps = False if last_reported_eps > second_reported_eps and second_reported_eps > third_reported_eps: is_positive_eps = True #Get Cash On Hand Last Three Years cash = mods.get_balance_sheet(stock, 'Cash') cash = mods.Is_Negative(cash) last_reported_cash, second_reported_cash, third_reported_cash = float( cash[0]), float(cash[1]), float(cash[2]) #Get Long Term Debt debt = mods.get_balance_sheet(stock, '>Long-Term Debt') debt = mods.Is_Negative(debt) last_reported_debt, second_reported_debt, third_reported_debt = float( debt[0]), float(debt[1]), float(debt[2]) #Determine Long Term Investments investments = mods.get_balance_sheet(stock, 'Long-Term Investments') investments = mods.Is_Negative(investments) last_reported_investments, second_reported_investments, third_reported_investments = float( investments[0]), float(investments[1]), float(investments[2]) #Test if Net Income has been positive the last three years is_positive_ni = False if last_reported_ni > second_reported_ni and second_reported_ni > third_reported_ni: is_positive_ni = True #Test if Cash on Hand is positive is_positive_cash = False if last_reported_cash > 5000000: is_positive_cash = True #Create Ditionarys of Results results_cash = collections.OrderedDict() results_cash['Stock'] = stock results_cash['TestRun'] = 'Cash' results_cash['DateRun'] = mods.Today() results_cash['Result'] = is_positive_cash results_cash[last_reported_dt] = last_reported_cash results_cash[year_before_reported_dt] = second_reported_cash results_cash[two_yr_before_reported_dt] = third_reported_cash results_ni = collections.OrderedDict() results_ni['Stock'] = stock results_ni['TestRun'] = 'Net_Income' results_ni['DateRun'] = mods.Today() results_ni['Result'] = is_positive_ni results_ni[last_reported_dt] = last_reported_ni results_ni[year_before_reported_dt] = second_reported_ni results_ni[two_yr_before_reported_dt] = third_reported_ni debt_data = collections.OrderedDict() debt_data['Stock'] = stock debt_data['TestRun'] = 'Long_Term_Debt' debt_data['DateRun'] = mods.Today() debt_data['Result'] = 'Data_Only' debt_data[last_reported_dt] = last_reported_debt debt_data[year_before_reported_dt] = second_reported_debt debt_data[two_yr_before_reported_dt] = third_reported_debt invest_data = collections.OrderedDict() invest_data['Stock'] = stock invest_data['TestRun'] = 'Long_Term_Investment' invest_data['DateRun'] = mods.Today() invest_data['Result'] = 'Data_Only' invest_data[last_reported_dt] = last_reported_investments invest_data[year_before_reported_dt] = second_reported_investments invest_data[two_yr_before_reported_dt] = third_reported_investments return [results_cash, results_ni, invest_data, debt_data] except: results_cash = collections.OrderedDict() results_cash['stock'] = stock results_cash['TestRun'] = 'Cash' results_cash['DateRun'] = mods.Today() results_cash['Result'] = 'Test Failed' results_ni = collections.OrderedDict() results_ni['stock'] = stock results_ni['TestRun'] = 'Net_Income' results_ni['DateRun'] = mods.Today() results_ni['Result'] = 'Test Failed' return [results_cash, results_ni] #Testing: #stock = 'aapl' #print Get_Key_Indicators(stock) #print mods.get_balance_sheet(stock, 'Treasury Stock') #print get_balance_sh(stock, 'Common Stocks')
last = current else: lst.append(current / last) last = current all_data['Percent Changed'] = lst # # Run Monte Carlo simulations. 1000 iterations for 60 weeks to determine number of times the value of the stock is greater than the strike price. simulation_end = [] num_iters = 100 num_weeks = 10 changed_avg = all_data['Percent Changed'].mean() changed_stdev = all_data['Percent Changed'].std() for i in range(0, num_iters): for i in range(0, num_weeks): if i == 0: wk_end = all_data['Adj Close'][0] else: wk_end = norminv(np.random.random_sample(), changed_avg, changed_stdev) * wk_end simulation_end.append(max(0, wk_end - strike_price)) return all_data end = mods.Today() start = '1/1/2015' #print web.DataReader('AAPL', 'yahoo-actions', start, end) import datetime datetime.datetime.strptime('1/1/15') print len(web.get_data_yahoo(stock, start, end))
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_Analyst_Opinion(stock): '''Go to Yahoo Finance and get the last three monts of Analyst Opinions, determine if the analyst optinion is postive, and return the results as a JSON object.''' analyst_opinion = 'http://ca.finance.yahoo.com/q/ao?s=' + stock + '+Analyst+Opinion' analyst_opinion_web = requests.get(analyst_opinion) analyst_opinion_text = analyst_opinion_web.content analyst_opinion_soup = bs(analyst_opinion_text) #i = 0 try: for n in analyst_opinion_soup.find_all('tr'): if n.text[0:6] == 'Strong': strong_buy = str(n) strong_buy = strong_buy.split( '<td align="center" class="yfnc_tabledata1">') for lst, item in enumerate(strong_buy): strong_buy[lst] = item.replace('</td>', '') strong_buy[-1] = strong_buy[-1].replace('</tr>', '') strong_buy = [ float(strong_buy[1]), float(strong_buy[2]), float(strong_buy[3]), float(strong_buy[4]) ] elif n.text[0:3] == 'Buy': buy = str(n) buy = buy.split('<td align="center" class="yfnc_tabledata1">') for lst, item in enumerate(buy): buy[lst] = item.replace('</td>', '') buy[-1] = buy[-1].replace('</tr>', '') buy = [ float(buy[1]), float(buy[2]), float(buy[3]), float(buy[4]) ] elif n.text[0:4] == 'Hold': hold = str(n) hold = hold.split( '<td align="center" class="yfnc_tabledata1">') for lst, item in enumerate(hold): hold[lst] = item.replace('</td>', '') hold[-1] = hold[-1].replace('</tr>', '') hold = [ float(hold[1]), float(hold[2]), float(hold[3]), float(hold[4]) ] elif n.text[0:12] == 'Underperform': underperform = str(n) underperform = underperform.split( '<td align="center" class="yfnc_tabledata1">') for lst, item in enumerate(underperform): underperform[lst] = item.replace('</td>', '') underperform[-1] = underperform[-1].replace('</tr>', '') underperform = [ float(underperform[1]), float(underperform[2]), float(underperform[3]), float(underperform[4]) ] elif n.text[0:4] == 'Sell': sell = str(n) sell = sell.split( '<td align="center" class="yfnc_tabledata1">') for lst, item in enumerate(sell): sell[lst] = item.replace('</td>', '') sell[-1] = sell[-1].replace('</tr>', '') sell = [ float(sell[1]), float(sell[2]), float(sell[3]), float(sell[4]) ] #Create Pandas Dataframe table = [strong_buy, buy, hold, underperform, sell] columns = ['Strong Buy', 'Buy', 'Hold', 'Underperform', 'Sell'] index = [ 'This Month', 'Last Month', 'Two Months Ago', 'Three Months Ago' ] df = pd.DataFrame(table, columns, index) #Determine if the analyst opinion suggests to buy the stock is_positive_analyst_opinion = False if df['This Month']['Strong Buy'] + df['This Month']['Buy'] > df[ 'This Month']['Hold'] + df['This Month']['Underperform'] + df[ 'This Month']['Sell']: is_positive_analyst_opinion = True #Create Ditionary of Results results = collections.OrderedDict() results['Stock'] = stock results['TestRun'] = 'Analyst_Opinion' results['DateRun'] = mods.Today() results['DataFrame'] = df.reset_index().to_json(orient='index') results['Result'] = is_positive_analyst_opinion return results except: results = collections.OrderedDict() results['stock'] = stock results['TestRun'] = 'Analyst_Opinion' results['DateRun'] = mods.Today() results['Result'] = 'Test Failed' return results ##Testing: #stock = 'AAPL' #print Get_Analyst_Opinion(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)