forked from Ankitkundu21/Hacktoberfest-python-code-bunch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstockpredict.py
More file actions
90 lines (71 loc) · 2.65 KB
/
stockpredict.py
File metadata and controls
90 lines (71 loc) · 2.65 KB
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
import sys
import numpy as np
import tweepy
import os
import requests
from keras.models import Sequential
from keras.layers import Dense
from textblob import TextBlob
consumer_key = "zYfepKVChCXVHyd6tYQEEcsmF"
comsumer_secret = "g7NhffplUKTV8pCnmRn5SdY9aD0cOkVTLzqZK7nCEp53kr12yV"
access_token = "1636245794-6lSimXGxl25ThnAPbObNcT795a6F8XK09uPkXMq"
access_secret = "jaguuzAsxJfsWwkYP4mgunwwReLI1XkGc0xp0FqbOXOD3"
login = tweepy.OAuthHandler(consumer_key, comsumer_secret)
login.set_access_token(access_token, access_secret)
user = tweepy.API(login)
file = 'historical.csv'
def get_name(symbol):
url = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={}®ion=1&lang=en".format(symbol)
result = requests.get(url).json()
for x in result['ResultSet']['Result']:
if x['symbol'] == symbol:
return x['name']
def sentiment(quote, num):
tweet_list = user.search(get_name(quote), count = num)
positive = 0
null = 0
for tweet in tweet_list:
check = TextBlob(tweet.text).sentiment
if check.subjectivity == 0:
null += 1
next
if check.polarity > 0:
positive += 1
if positive > ((num - null)/2):
return True
def get_data(quote):
url = "https://www.quandl.com/api/v3/datasets/NSE/{}.csv?api_key=-2RZqXCVrgC5VNizaqb8".format(quote)
# url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={}&interval=1min&apikey=STOPKETMIGTW8LT4&datatype=csv".format(quote)
r = requests.get(url)
if r.status_code != 400:
with open(file, 'wb') as fl:
for line in r:
fl.write(line)
return True
def predict():
data = []
with open(file) as f:
for num, line in enumerate(f):
if num != 0:
data.append(float(line.split(',')[1]))
data = np.array(data)
def create_set(data):
datax = [data[n+1] for n in range(len(data)-2)]
return np.array(datax), data[2:]
trainx, trainy = create_set(data)
classifier = Sequential()
classifier.add(Dense(8, input_dim = 1, activation = 'relu'))
classifier.add(Dense(1))
classifier.compile(loss = 'mean_squared_error', optimizer = 'adam')
classifier.fit(trainx, trainy, epochs= 200, batch_size = 2, verbose = 0)
prediction = classifier.predict(np.array([data[0]]))
return 'from %s to %s' % (data[0], prediction[0][0])
quote = raw_input("Enter stock quote: ").upper()
if not get_data(quote):
print ('ERROR, please re-run the script')
print(predict())
if not sentiment(quote, num = 100):
print ('This stock has good sentiment')
else:
print ('This stock has bad sentiment')
os.remove(file)