def find_utils(row):
    if row['Adj Close'] - row['Open'] < 0: return -1
    else: return 1

df = pd.read_csv('data/GOOGL.csv')
df['Utility'] = df.apply(find_utils, axis=1)

data = df.drop(['Utility', 'Date'], 1).values
target = df['Utility'].values

a = plt.plot(df['Open'])
b = plt.plot(df['Adj Close'])
plt.title('GOOGL')
plt.legend((a[0], b[0]), ('Open', 'Adj Close'), loc=1, bbox_to_anchor=(1.35, 1))
plt.show()
plt.close()
df.head()

"""
Explanation: Actions: Buy & Sell
Utility for each day determined by Open & Close.
Loading Data and getting utility values
End of explanation
"""


features=[]
for i in data:
    temp_list = []
    for j in i:
        temp_list.append([j])
    features.append(np.array(temp_list))

labels = []
for i in target:
    if i == -1: labels.append([1, -1])
    else: labels.append([-1, 1])

"""
Explanation: Formatting Data for LSTM Network
End of explanation
"""


from lstm import LSTM

epoch = 35000
num_cells=100

features_test = features[3000:]
labels_test = features[3000:]
features = features[:3000]
labels = labels[:3000]
len(features_test)

model = LSTM(num_hidden=num_cells)
predictions, train_error, test_error = model.run(features, labels, epoch=epoch, test_features=features_test,
                                                test_labels=labels_test)
plt.title('GOOGL - Mean Error Per Batch')
plt.plot(train_error)
plt.show()
plt.close()
print('Error on Testing Datset:', test_error)
model.reset()


"""
Explanation: LSTM
To keep the notebook short lstm network is implemented in ./lstm.py
End of explanation
"""


df = pd.read_csv('data/BRK-A.csv')
df['Utility'] = df.apply(find_utils, axis=1)

data = df.drop(['Utility', 'Date'], 1).values
target = df['Utility'].values

a = plt.plot(df['Open'])
b = plt.plot(df['Adj Close'])
plt.title('BRK-A')
plt.legend((a[0], b[0]), ('Open', 'Adj Close'), loc=1, bbox_to_anchor=(1.35, 1))
plt.show()
plt.close()

features=[]
for i in data:
    temp_list = []
    for j in i:
        temp_list.append([j])
    features.append(np.array(temp_list))

labels = []
for i in target:
    if i == -1: labels.append([1, 0])
    else: labels.append([0, 1])

df.head()

epoch = 35000
num_cells=100

features_test = features[8500:]
labels_test = features[8500:]
features = features[:8500]
labels = labels[:8500]
len(features_test)

model = LSTM(num_hidden=num_cells)
predictions, train_error, test_error = model.run(features, labels, epoch=epoch, test_features=features_test,
                                                test_labels=labels_test)
plt.title('BRK-A - Mean Error Per Batch')
plt.plot(train_error)
plt.show()
plt.close()
print('Error on Testing Datset:', test_error)
model.reset()

"""
Explanation: Berkshire Hathaway
End of explanation
"""


df = pd.read_csv('data/JNJ.csv')
df['Utility'] = df.apply(find_utils, axis=1)

data = df.drop(['Utility', 'Date'], 1).values
target = df['Utility'].values

a = plt.plot(df['Open'])
b = plt.plot(df['Adj Close'])
plt.title('JNJ')
plt.legend((a[0], b[0]), ('Open', 'Adj Close'), loc=1, bbox_to_anchor=(1.35, 1))
plt.show()
plt.close()

features=[]
for i in data:
    temp_list = []
    for j in i:
        temp_list.append([j])
    features.append(np.array(temp_list))

labels = []
for i in target:
    if i == -1: labels.append([1, 0])
    else: labels.append([0, 1])

df.head()

epoch = 35000
num_cells=25

features_test = features[10000:]
labels_test = features[10000:]
features = features[:10000]
labels = labels[:10000]
len(features_test)

model = LSTM(num_hidden=num_cells)
predictions, train_error, test_error = model.run(features, labels, epoch=epoch, test_features=features_test,
                                                test_labels=labels_test)
plt.title('JNJ - Mean Error Per Batch')
plt.plot(train_error)
plt.show()
plt.close()
print('Error on Testing Datset:', test_error)

model.reset()

"""
Explanation: Johnson and Johnson
End of explanation
"""


epoch = 40000
num_cells=50

model = LSTM(num_hidden=num_cells)
predictions, train_error, test_error = model.run(features, labels, epoch=epoch, test_features=features_test,
                                                test_labels=labels_test)
plt.title('JNJ - Mean Error Per Batch')
plt.plot(train_error)
plt.show()
plt.close()
print('Error on Testing Datset:', test_error)

"""
Explanation: Increase number of cells to see if that yeilds better results
End of explanation
"""


