-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentiment.py
82 lines (61 loc) · 2.12 KB
/
Sentiment.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import json
import certifi
import csv
import requests
def get_jsonparsed_data(url):
response = requests.get(url)
return response.json()
def getObj(url):
url = get_jsonparsed_data(url)
string = json.dumps(url)
obj = json.loads(string)
return obj
objAAPL = getObj("https://www.alphavantage.co/query?function=NEWS_SENTIMENT&tickers=ORIS&limit=1000&time_from=20241204T0130&apikey=0K6Z1IRED41VX9RS")
i = 0
tickerList = []
for item in objAAPL["feed"]:
for ticker_sentiment in item["ticker_sentiment"]:
newTicker = ticker_sentiment["ticker"]
found = False
for ticker in tickerList:
if newTicker == ticker:
found = True
if found == False:
tickerList.append(newTicker)
tickerList.sort()
print(tickerList)
def findSentAvg(tickerSymbol):
j = 0
sentScoreAvg = 0.0
for item in objAAPL["feed"]:
for ticker in item["ticker_sentiment"]:
if (ticker["ticker"] == tickerSymbol):
sentScoreAvg += float(ticker["ticker_sentiment_score"])
j += 1
sentScoreAvg /= j
return sentScoreAvg,j
sentScoreList = []
ratingsFound = []
verbalRating = []
for ticker in tickerList:
print(ticker)
sentScoreAvg, j = findSentAvg(ticker)
print(sentScoreAvg)
sentScoreList.append(sentScoreAvg)
ratingsFound.append(j)
if (sentScoreAvg <= -0.35):
verbalRating.append("Bearish")
elif (sentScoreAvg > -0.35) and (sentScoreAvg <= -0.15):
verbalRating.append("Somewhat Bearish")
elif (sentScoreAvg > -0.15) and (sentScoreAvg < 0.15):
verbalRating.append("Neutral")
elif (sentScoreAvg >= 0.15) and (sentScoreAvg < 0.35):
verbalRating.append("Somewhat Bullish")
elif (sentScoreAvg >= 0.35):
verbalRating.append("Bullish")
# Iterate through the JSON array
print(sentScoreList)
with open('SentimentScore.csv', 'w', newline="") as f:
csvWriter = csv.writer(f)
for i in range(len(tickerList)):
csvWriter.writerow([tickerList[i]] + [sentScoreList[i]] + [ratingsFound[i]] + [verbalRating[i]])