MCPcopy Index your code
hub / github.com/lazyprogrammer/machine_learning_examples / MultiStockEnv

Class MultiStockEnv

tf2.0/rl_trader.py:116–253  ·  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

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

Callers 1

rl_trader.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected