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

Class MultiStockEnv

tf2.0/mlp_trader.py:104–241  ·  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

102
103
104class MultiStockEnv:
105 """
106 A 3-stock trading environment.
107 State: vector of size 7 (n_stock * 2 + 1)
108 - # shares of stock 1 owned
109 - # shares of stock 2 owned
110 - # shares of stock 3 owned
111 - price of stock 1 (using daily close price)
112 - price of stock 2
113 - price of stock 3
114 - cash owned (can be used to purchase more stocks)
115 Action: categorical variable with 27 (3^3) possibilities
116 - for each stock, you can:
117 - 0 = sell
118 - 1 = hold
119 - 2 = buy
120 """
121 def __init__(self, data, initial_investment=20000):
122 # data
123 self.stock_price_history = data
124 self.n_step, self.n_stock = self.stock_price_history.shape
125
126 # instance attributes
127 self.initial_investment = initial_investment
128 self.cur_step = None
129 self.stock_owned = None
130 self.stock_price = None
131 self.cash_in_hand = None
132
133 self.action_space = np.arange(3**self.n_stock)
134
135 # action permutations
136 # returns a nested list with elements like:
137 # [0,0,0]
138 # [0,0,1]
139 # [0,0,2]
140 # [0,1,0]
141 # [0,1,1]
142 # etc.
143 # 0 = sell
144 # 1 = hold
145 # 2 = buy
146 self.action_list = list(map(list, itertools.product([0, 1, 2], repeat=self.n_stock)))
147
148 # calculate size of state
149 self.state_dim = self.n_stock * 2 + 1
150
151 self.reset()
152
153
154 def reset(self):
155 self.cur_step = 0
156 self.stock_owned = np.zeros(self.n_stock)
157 self.stock_price = self.stock_price_history[self.cur_step]
158 self.cash_in_hand = self.initial_investment
159 return self._get_obs()
160
161

Callers 1

mlp_trader.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected