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

Method _trade

tf2.0/mlp_trader.py:204–241  ·  view source on GitHub ↗
(self, action)

Source from the content-addressed store, hash-verified

202
203
204 def _trade(self, action):
205 # index the action we want to perform
206 # 0 = sell
207 # 1 = hold
208 # 2 = buy
209 # e.g. [2,1,0] means:
210 # buy first stock
211 # hold second stock
212 # sell third stock
213 action_vec = self.action_list[action]
214
215 # determine which stocks to buy or sell
216 sell_index = [] # stores index of stocks we want to sell
217 buy_index = [] # stores index of stocks we want to buy
218 for i, a in enumerate(action_vec):
219 if a == 0:
220 sell_index.append(i)
221 elif a == 2:
222 buy_index.append(i)
223
224 # sell any stocks we want to sell
225 # then buy any stocks we want to buy
226 if sell_index:
227 # NOTE: to simplify the problem, when we sell, we will sell ALL shares of that stock
228 for i in sell_index:
229 self.cash_in_hand += self.stock_price[i] * self.stock_owned[i]
230 self.stock_owned[i] = 0
231 if buy_index:
232 # NOTE: when buying, we will loop through each stock we want to buy,
233 # and buy one share at a time until we run out of cash
234 can_buy = True
235 while can_buy:
236 for i in buy_index:
237 if self.cash_in_hand > self.stock_price[i]:
238 self.stock_owned[i] += 1 # buy one share
239 self.cash_in_hand -= self.stock_price[i]
240 else:
241 can_buy = False
242
243
244

Callers 1

stepMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected