-
Notifications
You must be signed in to change notification settings - Fork 195
/
challenge.py
52 lines (40 loc) · 1.48 KB
/
challenge.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
import tweepy
import csv
import numpy as np
from textblob import TextBlob
from keras.models import Sequential
from keras.layers import Dense
#Step 1 - Insert your API keys
consumer_key= 'CONSUMER_KEY_HERE'
consumer_secret= 'CONSUMER_SECRET_HERE'
access_token='ACCESS_TOKEN_HERE'
access_token_secret='ACCESS_TOKEN_SECRET_HERE'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
#Step 2 - Search for your company name on Twitter
public_tweets = api.search('company_name')
#Step 3 - Define a threshold for each sentiment to classify each
#as positive or negative. If the majority of tweets you've collected are positive
#then use your neural network to predict a future price
for tweet in public_tweets:
analysis = TextBlob(tweet.text)
print(analysis.sentiment)
#data collection
dates = []
prices = []
def get_data(filename):
with open(filename, 'r') as csvfile:
csvFileReader = csv.reader(csvfile)
next(csvFileReader)
for row in csvFileReader:
dates.append(int(row[0].split('-')[0]))
prices.append(float(row[1]))
return
#Step 5 reference your CSV file here
get_data('your_company_stock_data.csv')
#Step 6 In this function, build your neural network model using Keras, train it, then have it predict the price
#on a given day. We'll later print the price out to terminal.
def predict_prices(dates, prices, x):
predicted_price = predict_price(dates, prices, 29)
print(predicted_price)