This repository has been archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
64 lines (55 loc) · 2.98 KB
/
test.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# %%
import requests
import json
class Secrets:
def __init__(self, json_secrets) -> None:
# I got a free token and secret key from the developer section in sentimentinvestor.com
with open(json_secrets) as f:
data = json.load(f)
self.token = data['sentiment_token']
self.secret_key = data['sentiment_key']
class Stock:
def __init__(self, ticker) -> None:
self.ticker = ticker
def get_yf_data(self):
all_data = requests.get(f"""https://query2.finance.yahoo.com/v10/finance/quoteSummary/
{self.ticker}?modules=assetProfile,balanceSheetHistory,
balanceSheetHistoryQuarterly,calendarEvents,cashflowStatementHistory,
cashflowStatementHistoryQuarterly,defaultKeyStatistics,earnings,
earningsHistory,earningsTrend,financialData,fundOwnership,
incomeStatementHistory,incomeStatementHistoryQuarterly,indexTrend,
industryTrend,insiderHolders,insiderTransactions,institutionOwnership,
majorDirectHolders,majorHoldersBreakdown,netSharePurchaseActivity,
price,quoteType,recommendationTrend,secFilings,sectorTrend,
summaryDetail,summaryProfile,symbol,upgradeDowngradeHistory,
fundProfile,topHoldings,fundPerformance""").json()
return all_data if all_data["quoteSummary"]["result"] != None else None
def get_analysis(self, recommendation):
strong_buy = recommendation["strongBuy"]
buy = recommendation["buy"]
hold = recommendation["hold"]
underperform = recommendation["sell"]
sell = recommendation["strongSell"]
recommendation_num = strong_buy + buy + hold + underperform + sell
if recommendation_num != 0:
return (strong_buy + buy * 2 + hold * 3 + underperform * 4 + sell * 4) / recommendation_num
else:
return 5
# I got a free token and secret key from the developer section in sentimentinvestor.com
secrets = Secrets('config.json')
RHI_rank = requests.get("https://sentimentinvestor.com/api/v3/sort?limit=100&metric=RHI&token={0}&key={1}".format(secrets.token, secrets.secret_key)).json()
stock_list = []
for stock in RHI_rank:
x = Stock(stock)
data = x.get_yf_data()
stock_cap = 0
if data != None:
try:
analysis = x.get_analysis(data["quoteSummary"]["result"][0]["recommendationTrend"]["trend"][0])
stock_cap = int(data["quoteSummary"]["result"][0]["defaultKeyStatistics"]["enterpriseValue"]["raw"])
price = data['quoteSummary']['result'][0]['price']['regularMarketPrice']['raw']
if stock_cap < 1000000000 and stock_cap > 1 and analysis != None and price < 2:
stock_list.append(stock["ticker"])
except:
pass
print(stock_list)