class sentiment: host = "localhost" port = 8086 user = None password = None dbname = "mydb2" db = DBHandler(host, port, user, password, dbname) dl = DataLoader() def get_sentiment(self, ticker, company, startDate): db = self.db dl = self.dl startTime = datetime.datetime.strptime(startDate, "%Y-%m-%d") currentTime = datetime.datetime.today().strftime("%Y-%m-%d") currentTime = datetime.datetime.strptime(currentTime, "%Y-%m-%d") days = currentTime - startTime days = days.days output = db.QueryDataFromTable("select * from " + ticker + " where time > now() - " + str(days) + "d") df = dl.db_to_df(output) for i in range(len(df)): time = df.iloc[[i]].index[0] if time < datetime.datetime(2006, 6, 30): avg_polarity = 0.0 avg_subjectivity = 0.0 time = str(time) time = time.replace(" 00:00:00", "") else: time = str(time) time = time.replace(" 00:00:00", "") time2 = datetime.datetime.strptime(time, "%Y-%m-%d") next_day = str(time2 + datetime.timedelta(days=1)) next_day = next_day.replace(" 00:00:00", "") tweetCriteria = (got.manager.TweetCriteria().setQuerySearch( company).setSince(time).setUntil(next_day).setMaxTweets(0)) tweets = got.manager.TweetManager.getTweets(tweetCriteria) if len(tweets) == 0: avg_polarity = 0.0 avg_subjectivity = 0.0 else: data = dict() j = 0 for tweet in tweets: if "http" not in tweet.text: analysis = TextBlob(tweet.text) data[j] = { "polarity": analysis.sentiment.polarity, "subjectivity": analysis.sentiment.subjectivity, } j += 1 polarity_total = 0 subjectivity_total = 0 invalid_data_counter = 0 for index in data: if (data[index]["polarity"] == 0 or data[index]["subjectivity"] == 0): invalid_data_counter += 1 if invalid_data_counter == len(data): avg_polarity = 0.0 avg_subjectivity = 0.0 else: polarity_total = polarity_total + data[index][ "polarity"] subjectivity_total = (subjectivity_total + data[index]["subjectivity"]) if len(data) - invalid_data_counter == 0: avg_polarity = 0.0 avg_subjectivity = 0.0 else: avg_polarity = polarity_total / (len(data) - invalid_data_counter) avg_subjectivity = subjectivity_total / ( len(data) - invalid_data_counter) output = db.GetRowFromTable(ticker, time) close = float(output[0][1]) predict = float(output[0][2]) data = [[time, close, predict, avg_polarity, avg_subjectivity]] df2 = pd.DataFrame( data, columns=[ "Date", "Close", "Prediction", "Polarity", "Subjectivity" ], ) df2["Date"] = pd.to_datetime(df2.Date, format="%Y-%m-%d") df2.index = df2["Date"] df2.drop(["Date"], inplace=True, axis=1) print(df2) db.DataFrameToDB(df2, ticker)
from alpha_vantage.timeseries import TimeSeries from dbhelper.db_handler import DBHandler from time import mktime import GetOldTweets3 as got from textblob import TextBlob from pandas.io.json import json_normalize host = "localhost" port = 8086 user = None password = None dbname = "mydb" db = DBHandler(host, port, user, password, dbname) dl = DataLoader() status = False i = 1 ticker = "JPM" output = db.QueryDataFromTable("select * from " + ticker) df = dl.db_to_df(output) print(df) # time = df.tail(1).index[0]
#!/usr/bin/env python3 from core.get_data import DataLoader import pandas as pd import json import datetime from alpha_vantage.timeseries import TimeSeries from dbhelper.db_handler import DBHandler from time import mktime import GetOldTweets3 as got from textblob import TextBlob dl = DataLoader() company = "American Airlines" time = "2012-09-06" next_day = "2012-09-07" tweetCriteria = ( got.manager.TweetCriteria() .setQuerySearch(company) .setSince(time) .setUntil(next_day) .setMaxTweets(1) ) tweets = got.manager.TweetManager.getTweets(tweetCriteria) print(tweets) # data = dict()
#!/usr/bin/env python3 from core.get_data import DataLoader from core.data_processing import DataProcessing from alpha_vantage.timeseries import TimeSeries from core.model import Model from dbhelper.db_handler import DBHandler import pandas as pd import json dl = DataLoader() dp = DataProcessing() host = "localhost" port = 8086 user = None password = None dbname = "mydb2" db = DBHandler(host, port, user, password, dbname) company = "AAL" df = dl.csv_to_df(company + ".csv") print(df) db.DataFrameToDB(df, "testing")
#!/usr/bin/env python3 from core.get_data import DataLoader from core.model import Model import pandas as pd import json import datetime from alpha_vantage.timeseries import TimeSeries from dbhelper.db_handler import DBHandler from keras import backend as keras from time import mktime import GetOldTweets3 as got from textblob import TextBlob dl = DataLoader() md = Model(dl.scaler) host = "localhost" port = 8086 user = None password = None dbname = "mydb" db = DBHandler(host, port, user, password, dbname) df = dl.get_historical_data("AAL") x_train, y_train = dl.get_train_data(df) model = md.create_model(x_train, y_train)
#!/usr/bin/env python3 from core.get_data import DataLoader from core.data_processing import DataProcessing from alpha_vantage.timeseries import TimeSeries from core.model import Model from dbhelper.db_handler import DBHandler import pandas as pd import json dl = DataLoader() dp = DataProcessing() host = "localhost" port = 8086 user = None password = None dbname = "mydb2" db = DBHandler(host, port, user, password, dbname) output = db.QueryDataFromTable("select * from AAL") new_output = dl.db_to_df(output) print(new_output) dl.df_to_csv(new_output, "AAL.csv")