-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.py
135 lines (102 loc) · 4.43 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
import datetime as dt
import math
import fix_yahoo_finance as yf
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tweepy
from matplotlib import style
from sklearn import preprocessing
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from textblob import TextBlob
import constants as ct
from Tweet import Tweet
style.use('ggplot')
def check_stock_symbol(flag=False, companies_file='companylist.csv'):
df = pd.read_csv(companies_file, usecols=[0])
while flag is False:
symbol = raw_input('Enter a stock symbol to retrieve data from: ').upper()
for index in range(len(df)):
if df['Symbol'][index] == symbol:
flag = True
return flag, symbol
def get_stock_data(symbol, from_date, to_date):
data = yf.download(symbol, start=from_date, end=to_date)
df = pd.DataFrame(data=data)
df = df[['Open', 'High', 'Low', 'Close', 'Volume']]
df['HighLoad'] = (df['High'] - df['Close']) / df['Close'] * 100.0
df['Change'] = (df['Close'] - df['Open']) / df['Open'] * 100.0
df = df[['Close', 'HighLoad', 'Change', 'Volume']]
return df
def stock_forecasting(df):
forecast_col = 'Close'
forecast_out = int(math.ceil(0.1*len(df)))
df['Label'] = df[[forecast_col]].shift(-forecast_out)
X = np.array(df.drop(['Label'], axis=1))
X = preprocessing.scale(X)
X_forecast = X[-forecast_out:]
X = X[:-forecast_out]
df.dropna(inplace=True)
y = np.array(df['Label'])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)
clf = LinearRegression(n_jobs=-1)
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
forecast = clf.predict(X_forecast)
df['Prediction'] = np.nan
last_date = df.iloc[-1].name
last_date = dt.datetime.strptime(str(last_date), "%Y-%m-%d %H:%M:%S")
for pred in forecast:
last_date += dt.timedelta(days=1)
df.loc[last_date.strftime("%Y-%m-%d")] = [np.nan for _ in range(len(df.columns) - 1)] + [pred]
return df, forecast_out
def forecast_plot(df):
df['Close'].plot(color='black')
df['Prediction'].plot(color='green')
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()
def retrieving_tweets_polarity(symbol):
auth = tweepy.OAuthHandler(ct.consumer_key, ct.consumer_secret)
auth.set_access_token(ct.access_token, ct.access_token_secret)
user = tweepy.API(auth)
tweets = tweepy.Cursor(user.search, q=str(symbol), tweet_mode='extended', lang='en').items(ct.num_of_tweets)
tweet_list = []
global_polarity = 0
for tweet in tweets:
tw = tweet.full_text
blob = TextBlob(tw)
polarity = 0
for sentence in blob.sentences:
polarity += sentence.sentiment.polarity
global_polarity += sentence.sentiment.polarity
tweet_list.append(Tweet(tw, polarity))
global_polarity = global_polarity / len(tweet_list)
return global_polarity
def recommending(df, forecast_out, global_polarity):
if df.iloc[-forecast_out-1]['Close'] < df.iloc[-1]['Prediction']:
if global_polarity > 0:
print("According to the predictions and twitter sentiment analysis -> Investing in %s is a GREAT idea!" % str(symbol))
elif global_polarity < 0:
print("According to the predictions and twitter sentiment analysis -> Investing in %s is a BAD idea!" % str(symbol))
else:
print("According to the predictions and twitter sentiment analysis -> Investing in %s is a BAD idea!" % str(symbol))
if __name__ == "__main__":
(flag, symbol) = check_stock_symbol(False, 'companylist.csv')
if flag:
actual_date = dt.date.today()
past_date = actual_date - dt.timedelta(days=365 * 3)
actual_date = actual_date.strftime("%Y-%m-%d")
past_date = past_date.strftime("%Y-%m-%d")
print "Retrieving Stock Data from introduced symbol..."
dataframe = get_stock_data(symbol, past_date, actual_date)
print "Forecasting stock DataFrame..."
(dataframe, forecast_out) = stock_forecasting(dataframe)
print "Plotting existing and forecasted values..."
forecast_plot(dataframe)
print "Retrieving %s related tweets polarity..." % symbol
polarity = retrieving_tweets_polarity(symbol)
print "Generating recommendation based on prediction & polarity..."
recommending(dataframe, forecast_out, polarity)