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

Method _trade

tf2.0/keras_trader.py:223–260  ·  view source on GitHub ↗
(self, action)

Source from the content-addressed store, hash-verified

221
222
223 def _trade(self, action):
224 # index the action we want to perform
225 # 0 = sell
226 # 1 = hold
227 # 2 = buy
228 # e.g. [2,1,0] means:
229 # buy first stock
230 # hold second stock
231 # sell third stock
232 action_vec = self.action_list[action]
233
234 # determine which stocks to buy or sell
235 sell_index = [] # stores index of stocks we want to sell
236 buy_index = [] # stores index of stocks we want to buy
237 for i, a in enumerate(action_vec):
238 if a == 0:
239 sell_index.append(i)
240 elif a == 2:
241 buy_index.append(i)
242
243 # sell any stocks we want to sell
244 # then buy any stocks we want to buy
245 if sell_index:
246 # NOTE: to simplify the problem, when we sell, we will sell ALL shares of that stock
247 for i in sell_index:
248 self.cash_in_hand += self.stock_price[i] * self.stock_owned[i]
249 self.stock_owned[i] = 0
250 if buy_index:
251 # NOTE: when buying, we will loop through each stock we want to buy,
252 # and buy one share at a time until we run out of cash
253 can_buy = True
254 while can_buy:
255 for i in buy_index:
256 if self.cash_in_hand > self.stock_price[i]:
257 self.stock_owned[i] += 1 # buy one share
258 self.cash_in_hand -= self.stock_price[i]
259 else:
260 can_buy = False
261
262
263

Callers 1

stepMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected