-
Notifications
You must be signed in to change notification settings - Fork 390
/
recommendations.py
37 lines (29 loc) · 1.24 KB
/
recommendations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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'
url = lhs_url + ticker + rhs_url
r = requests.get(url)
if not r.ok:
recommendation = 0
try:
result = r.json()['quoteSummary']['result'][0]
recommendation =result['financialData']['recommendationMean']['fmt']
except:
recommendation = 0
recommendations.append(recommendation)
print("--------------------------------------------")
print ("{} has an average recommendation of: ".format(ticker), recommendation)
dataframe = pd.DataFrame(list(zip(tickers, recommendations)), columns =['Company', 'Recommendation'])
df = dataframe.sort_values('Recommendation', ascending = True)
df.to_csv('recommendations.csv')
print (df)