Last active
August 16, 2021 01:39
-
-
Save shashankvemuri/26e518c74305456a1fab1fadf6b3cee8 to your computer and use it in GitHub Desktop.
This gist contains all the code needed for the algorithm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import pandas as pd | |
from yahoo_fin import stock_info as si | |
from pandas_datareader import DataReader | |
import numpy as np | |
tickers = si.tickers_sp500() | |
recommendations = [] | |
for ticker in tickers: | |
lhs_url = 'https://query2.finance.yahoo.com/v10/finance/quoteSummary/' | |
rhs_url = '?formatted=true&crumb=swg7qs5y9UP&lang=en-US®ion=US&' \ | |
'modules=upgradeDowngradeHistory,recommendationTrend,' \ | |
'financialData,earningsHistory,earningsTrend,industryTrend&' \ | |
'corsDomain=finance.yahoo.com' | |
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'} | |
url = lhs_url + ticker + rhs_url | |
r = requests.get(url, headers=headers) | |
if not r.ok: | |
recommendation = 6 | |
try: | |
result = r.json()['quoteSummary']['result'][0] | |
recommendation =result['financialData']['recommendationMean']['fmt'] | |
except: | |
recommendation = 6 | |
recommendations.append(recommendation) | |
print("--------------------------------------------") | |
print ("{} has an average recommendation of: ".format(ticker), recommendation) | |
#time.sleep(0.5) | |
dataframe = pd.DataFrame(list(zip(tickers, recommendations)), columns =['Company', 'Recommendations']) | |
dataframe = dataframe.set_index('Company') | |
dataframe.to_csv('recommendations.csv') | |
print (dataframe) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment