forked from yacoubb/stock-trading-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trading_algo.py
86 lines (65 loc) · 2.57 KB
/
trading_algo.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
import numpy as np
from keras.models import load_model
from util import csv_to_dataset, history_points
model = load_model('technical_model.h5')
ohlcv_histories, technical_indicators, next_day_open_values, unscaled_y, y_normaliser = csv_to_dataset('MSFT_daily.csv')
test_split = 0.9
n = int(ohlcv_histories.shape[0] * test_split)
ohlcv_train = ohlcv_histories[:n]
tech_ind_train = technical_indicators[:n]
y_train = next_day_open_values[:n]
ohlcv_test = ohlcv_histories[n:]
tech_ind_test = technical_indicators[n:]
y_test = next_day_open_values[n:]
unscaled_y_test = unscaled_y[n:]
y_test_predicted = model.predict([ohlcv_test, tech_ind_test])
y_test_predicted = y_normaliser.inverse_transform(y_test_predicted)
buys = []
sells = []
thresh = 0.1
start = 0
end = -1
x = -1
for ohlcv, ind in zip(ohlcv_test[start: end], tech_ind_test[start: end]):
normalised_price_today = ohlcv[-1][0]
normalised_price_today = np.array([[normalised_price_today]])
price_today = y_normaliser.inverse_transform(normalised_price_today)
predicted_price_tomorrow = np.squeeze(y_normaliser.inverse_transform(model.predict([[ohlcv], [ind]])))
delta = predicted_price_tomorrow - price_today
if delta > thresh:
buys.append((x, price_today[0][0]))
elif delta < -thresh:
sells.append((x, price_today[0][0]))
x += 1
print(f"buys: {len(buys)}")
print(f"sells: {len(sells)}")
def compute_earnings(buys_, sells_):
purchase_amt = 10
stock = 0
balance = 0
while len(buys_) > 0 and len(sells_) > 0:
if buys_[0][0] < sells_[0][0]:
# time to buy $10 worth of stock
balance -= purchase_amt
stock += purchase_amt / buys_[0][1]
buys_.pop(0)
else:
# time to sell all of our stock
balance += stock * sells_[0][1]
stock = 0
sells_.pop(0)
print(f"earnings: ${balance}")
# we create new lists so we dont modify the original
compute_earnings([b for b in buys], [s for s in sells])
import matplotlib.pyplot as plt
plt.gcf().set_size_inches(22, 15, forward=True)
real = plt.plot(unscaled_y_test[start:end], label='real')
pred = plt.plot(y_test_predicted[start:end], label='predicted')
if len(buys) > 0:
plt.scatter(list(list(zip(*buys))[0]), list(list(zip(*buys))[1]), c='#00ff00', s=50)
if len(sells) > 0:
plt.scatter(list(list(zip(*sells))[0]), list(list(zip(*sells))[1]), c='#ff0000', s=50)
# real = plt.plot(unscaled_y[start:end], label='real')
# pred = plt.plot(y_predicted[start:end], label='predicted')
plt.legend(['Real', 'Predicted', 'Buy', 'Sell'])
plt.show()