forked from tstewart161/Reddit_Sentiment_Trader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
163 lines (108 loc) · 3.47 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer as SIA
import praw
import matplotlib.pyplot as plt
import math
import datetime as dt
import pandas as pd
import numpy as np
# In[2]:
nltk.download('vader_lexicon')
nltk.download('stopwords')
# In[3]:
reddit = praw.Reddit(client_id='*********',
client_secret='******************',
user_agent='Quick-Sherbet-1373')
# In[4]:
sub_reddits = reddit.subreddit('wallstreetbets')
stocks = ["SPCE", "LULU", "CCL", "SDC"]
# In[5]:
def commentSentiment(ticker, urlT):
subComments = []
bodyComment = []
try:
check = reddit.submission(url=urlT)
subComments = check.comments
except:
return 0
for comment in subComments:
try:
bodyComment.append(comment.body)
except:
return 0
sia = SIA()
results = []
for line in bodyComment:
scores = sia.polarity_scores(line)
scores['headline'] = line
results.append(scores)
df =pd.DataFrame.from_records(results)
df.head()
df['label'] = 0
try:
df.loc[df['compound'] > 0.1, 'label'] = 1
df.loc[df['compound'] < -0.1, 'label'] = -1
except:
return 0
averageScore = 0
position = 0
while position < len(df.label)-1:
averageScore = averageScore + df.label[position]
position += 1
averageScore = averageScore/len(df.label)
return(averageScore)
# In[6]:
def latestComment(ticker, urlT):
subComments = []
updateDates = []
try:
check = reddit.submission(url=urlT)
subComments = check.comments
except:
return 0
for comment in subComments:
try:
updateDates.append(comment.created_utc)
except:
return 0
updateDates.sort()
return(updateDates[-1])
# In[7]:
def get_date(date):
return dt.datetime.fromtimestamp(date)
# In[8]:
submission_statistics = []
d = {}
for ticker in stocks:
for submission in reddit.subreddit('wallstreetbets').search(ticker, limit=130):
if submission.domain != "self.wallstreetbets":
continue
d = {}
d['ticker'] = ticker
d['num_comments'] = submission.num_comments
d['comment_sentiment_average'] = commentSentiment(ticker, submission.url)
if d['comment_sentiment_average'] == 0.000000:
continue
d['latest_comment_date'] = latestComment(ticker, submission.url)
d['score'] = submission.score
d['upvote_ratio'] = submission.upvote_ratio
d['date'] = submission.created_utc
d['domain'] = submission.domain
d['num_crossposts'] = submission.num_crossposts
d['author'] = submission.author
submission_statistics.append(d)
dfSentimentStocks = pd.DataFrame(submission_statistics)
_timestampcreated = dfSentimentStocks["date"].apply(get_date)
dfSentimentStocks = dfSentimentStocks.assign(timestamp = _timestampcreated)
_timestampcomment = dfSentimentStocks["latest_comment_date"].apply(get_date)
dfSentimentStocks = dfSentimentStocks.assign(commentdate = _timestampcomment)
dfSentimentStocks.sort_values("latest_comment_date", axis = 0, ascending = True,inplace = True, na_position ='last')
dfSentimentStocks
# In[9]:
dfSentimentStocks.author.value_counts()
# In[10]:
dfSentimentStocks.to_csv('Reddit_Sentiment_Equity.csv', index=False)
# In[ ]: