-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment.py
93 lines (74 loc) · 3.21 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
82
83
84
85
86
87
88
89
90
91
92
93
#IMPORTING DEPENDENCIES
print('Importing Flair/Torch...')
import flair
import torch
from flair.data import Sentence
print('Importing WSS(s)...')
from newsapi import NewsApiClient
import alpaca_trade_api as tradeapi
#DONE IMPORTING DEPENDENCIES
#SET RUN DEVICE AS CPU
flair.device = torch.device('cpu')
#STATING POLYGON.IO API
api = tradeapi.REST('YOURPOLYGONAPIKEY','https://api.polygon.io' )
#DEFINING FUNCTION
def sentiment(stock, api):
#LOADING TRADERVIEW
url = 'https://www.tradingview.com/screener/'
#LOADING FLAIR
flair_sentiment = flair.models.TextClassifier.load('en-sentiment')
#NEWSAPI API call
newsapi = NewsApiClient(api_key='YOURNEWSAPIKEY')
#GET THE ARTICLES
response = newsapi.get_everything(qintitle=stock)
#SPECIFY API CALL INSIDE THE FUNCTION
news = api.polygon.news(stock)
#OPEN NEWS.TXT TO WRITE NEWS
file = open('news.txt', 'w')
#VERIFY SENTIMENT VARIABLE IS 0
sentiment = 0
print(response)
#ITERATES THROUGH EVERY NEWS ARTICLE FROM NEWS API
for line in response['articles']:
words = str(line['title'])
file.write(words)
#RUNS FLAIR SENTIMENT ANALYSIS
sentence = Sentence(str(words))
flair_sentiment.predict(sentence)
total_sentiment = sentence.labels
print(str(words))
# Checks to see if the sentiment is negative and subtracts by how negative flair thinks it is
if total_sentiment[0].value == 'NEGATIVE':
print(str(total_sentiment[0].value) + " : " + str(total_sentiment[0].to_dict()['confidence']))
sentiment -= total_sentiment[0].to_dict()['confidence'] / 2 # Flair favors negative outcomes
# Checks to see if the sentiment is positive and adds how positive flair thinks it is
elif total_sentiment[0].value == 'POSITIVE':
print(str(total_sentiment[0].value) + " : " + str(total_sentiment[0].to_dict()['confidence']))
sentiment += total_sentiment[0].to_dict()['confidence']
#ITERATES THROUGH EVERY NEWS ARTICLE FROM POLYGON.IO
for source in news:
words = source.summary
try:
file.write(words)
except:
print('FAILSAFE ACTIVATED')
file.write('\n')
# Runs Flair sentiment analysis
sentence = Sentence(str(words))
try:
flair_sentiment.predict(sentence)
except:
print("\n")
total_sentiment = sentence.labels
print(str(words))
# Checks to see if the sentiment is negative and subtracts by how negative flair thinks it is
if total_sentiment[0].value == 'NEGATIVE':
print(str(total_sentiment[0].value) + " : " + str(total_sentiment[0].to_dict()['confidence']))
sentiment -= total_sentiment[0].to_dict()['confidence'] / 2 # Flair favors negative outcomes
# Checks to see if the sentiment is positive and adds how positive flair thinks it is
if total_sentiment[0].value == 'POSITIVE':
print(str(total_sentiment[0].value) + " : " + str(total_sentiment[0].to_dict()['confidence']))
sentiment += total_sentiment[0].to_dict()['confidence']
file.close()
print('Total sentiment', sentiment) #News Sentiment
sentiment('AAPL', api)