MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / MultiStockEnv

Class MultiStockEnv

tf2.0/keras_trader.py:123–260  ·  view source on GitHub ↗

A 3-stock trading environment. State: vector of size 7 (n_stock * 2 + 1) - # shares of stock 1 owned - # shares of stock 2 owned - # shares of stock 3 owned - price of stock 1 (using daily close price) - price of stock 2 - price of stock 3 - cash owned (can be used t

Source from the content-addressed store, hash-verified

121
122
123class MultiStockEnv:
124 """
125 A 3-stock trading environment.
126 State: vector of size 7 (n_stock * 2 + 1)
127 - # shares of stock 1 owned
128 - # shares of stock 2 owned
129 - # shares of stock 3 owned
130 - price of stock 1 (using daily close price)
131 - price of stock 2
132 - price of stock 3
133 - cash owned (can be used to purchase more stocks)
134 Action: categorical variable with 27 (3^3) possibilities
135 - for each stock, you can:
136 - 0 = sell
137 - 1 = hold
138 - 2 = buy
139 """
140 def __init__(self, data, initial_investment=20000):
141 # data
142 self.stock_price_history = data
143 self.n_step, self.n_stock = self.stock_price_history.shape
144
145 # instance attributes
146 self.initial_investment = initial_investment
147 self.cur_step = None
148 self.stock_owned = None
149 self.stock_price = None
150 self.cash_in_hand = None
151
152 self.action_space = np.arange(3**self.n_stock)
153
154 # action permutations
155 # returns a nested list with elements like:
156 # [0,0,0]
157 # [0,0,1]
158 # [0,0,2]
159 # [0,1,0]
160 # [0,1,1]
161 # etc.
162 # 0 = sell
163 # 1 = hold
164 # 2 = buy
165 self.action_list = list(map(list, itertools.product([0, 1, 2], repeat=self.n_stock)))
166
167 # calculate size of state
168 self.state_dim = self.n_stock * 2 + 1
169
170 self.reset()
171
172
173 def reset(self):
174 self.cur_step = 0
175 self.stock_owned = np.zeros(self.n_stock)
176 self.stock_price = self.stock_price_history[self.cur_step]
177 self.cash_in_hand = self.initial_investment
178 return self._get_obs()
179
180

Callers 1

keras_trader.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected