-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathAI_Stock_Trading.py
175 lines (140 loc) · 5.18 KB
/
AI_Stock_Trading.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import abc
import threading
import time
import pandas as pd
import numpy as np
from keras.layers import Dense
from keras.models import Sequential, model_from_json
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from alpaca_trade_api import REST
class AlpacaPaperSocket(REST):
def __init__(self):
super().__init__(
key_id='PKPO0ZH3XTVB336B7TEO',
secret_key='gcs4U2Hp/ACI4A5UwYjYugrPqB2odD/m40Zuz5qw',
base_url='https://paper-api.alpaca.markets'
)
class TradingSystem(abc.ABC):
def __init__(self, api, symbol, time_frame, system_id, system_label):
# Connect to api
# Connect to BrokenPipeError
# Save fields to class
self.api = api
self.symbol = symbol
self.time_frame = time_frame
self.system_id = system_id
self.system_label = system_label
thread = threading.Thread(target=self.system_loop)
thread.start()
@abc.abstractmethod
def place_buy_order(self):
pass
@abc.abstractmethod
def place_sell_order(self):
pass
@abc.abstractmethod
def system_loop(self):
pass
# Class to develop your AI portfolio manager
class PMModelDevelopment:
def __init__(self):
# Read your data in and split the dependent and independent
data = pd.read_csv('IBM.csv')
X = data['Delta Close']
y = data.drop(['Delta Close'], axis=1)
# Train test spit
X_train, X_test, y_train, y_test = train_test_split(X, y)
# Create the sequential
network = Sequential()
# Create the structure of the neural network
network.add(Dense(1, input_shape=(1,), activation='tanh'))
network.add(Dense(3, activation='tanh'))
network.add(Dense(3, activation='tanh'))
network.add(Dense(3, activation='tanh'))
network.add(Dense(1, activation='tanh'))
# Compile the model
network.compile(
optimizer='rmsprop',
loss='hinge',
metrics=['accuracy']
)
# Train the model
network.fit(X_train.values, y_train.values, epochs=100)
# Evaluate the predictions of the model
y_pred = network.predict(X_test.values)
y_pred = np.around(y_pred, 0)
print(classification_report(y_test, y_pred))
# Save structure to json
model = network.to_json()
with open("model.json", "w") as json_file:
json_file.write(model)
# Save weights to HDF5
network.save_weights("weights.h5")
# AI Portfolio Manager
class PortfolioManagementModel:
def __init__(self):
# Data in to test that the saving of weights worked
data = pd.read_csv('IBM.csv')
X = data['Delta Close']
y = data.drop(['Delta Close'], axis=1)
# Read structure from json
json_file = open('model.json', 'r')
json = json_file.read()
json_file.close()
self.network = model_from_json(json)
# Read weights from HDF5
self.network.load_weights("weights.h5")
# Verify weights and structure are loaded
y_pred = self.network.predict(X.values)
y_pred = np.around(y_pred, 0)
print(classification_report(y, y_pred))
PortfolioManagementModel()
# in implemenation create a vector to store data...
class PortfolioManagementSystem(TradingSystem):
def __init__(self):
super().__init__(AlpacaPaperSocket(), 'IBM', 86400, 1, 'AI_PM')
self.AI = PortfolioManagementModel()
def place_buy_order(self):
self.api.submit_order(
symbol='IBM',
qty=1,
side='buy',
type='market',
time_in_force='day',
)
def place_sell_order(self):
self.api.submit_order(
symbol='IBM',
qty=1,
side='sell',
type='market',
time_in_force='day',
)
def system_loop(self):
# Variables for weekly close
this_weeks_close = 0
last_weeks_close = 0
delta = 0
day_count = 0
while(True):
# Wait a day to request more data
time.sleep(1440)
# Request EoD data for IBM
data_req = self.api.get_barset('IBM', timeframe='1D', limit=1).df
# Construct dataframe to predict
x = pd.DataFrame(
data=[[
data_req['IBM']['close'][0]]], columns='Close'.split()
)
if(day_count == 7):
day_count = 0
last_weeks_close = this_weeks_close
this_weeks_close = x['Close']
delta = this_weeks_close - last_weeks_close
# AI choosing to buy, sell, or hold
if np.around(self.AI.network.predict([delta])) <= -.5:
self.place_sell_order()
elif np.around(self.AI.network.predict([delta]) >= .5):
self.place_buy_order()
PortfolioManagementSystem()