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

Method _trade

pytorch/rl_trader.py:241–278  ·  view source on GitHub ↗
(self, action)

Source from the content-addressed store, hash-verified

239
240
241 def _trade(self, action):
242 # index the action we want to perform
243 # 0 = sell
244 # 1 = hold
245 # 2 = buy
246 # e.g. [2,1,0] means:
247 # buy first stock
248 # hold second stock
249 # sell third stock
250 action_vec = self.action_list[action]
251
252 # determine which stocks to buy or sell
253 sell_index = [] # stores index of stocks we want to sell
254 buy_index = [] # stores index of stocks we want to buy
255 for i, a in enumerate(action_vec):
256 if a == 0:
257 sell_index.append(i)
258 elif a == 2:
259 buy_index.append(i)
260
261 # sell any stocks we want to sell
262 # then buy any stocks we want to buy
263 if sell_index:
264 # NOTE: to simplify the problem, when we sell, we will sell ALL shares of that stock
265 for i in sell_index:
266 self.cash_in_hand += self.stock_price[i] * self.stock_owned[i]
267 self.stock_owned[i] = 0
268 if buy_index:
269 # NOTE: when buying, we will loop through each stock we want to buy,
270 # and buy one share at a time until we run out of cash
271 can_buy = True
272 while can_buy:
273 for i in buy_index:
274 if self.cash_in_hand > self.stock_price[i]:
275 self.stock_owned[i] += 1 # buy one share
276 self.cash_in_hand -= self.stock_price[i]
277 else:
278 can_buy = False
279
280
281

Callers 1

stepMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected