MCPcopy Create free account
hub / github.com/DVampire/FinAgent / step

Method step

finagent/environment/trading.py:235–289  ·  view source on GitHub ↗
(self, action: int = 0)

Source from the content-addressed store, hash-verified

233 self.value = self.current_value(price)
234
235 def step(self, action: int = 0):
236
237 pre_value = self.value
238
239 if action > 0:
240 self.buy(self.price, amount=action)
241 elif action < 0:
242 self.sell(self.price, amount=action)
243 else:
244 self.hold_on(self.price, amount=action)
245
246 post_value = self.value
247
248 reward = (post_value - pre_value) / pre_value
249
250 self.day = self.day + 1
251
252 if self.day < self.end_day:
253 done = False
254 truncted = False
255 else:
256 done = True
257 truncted = True
258
259 next_state = self.get_state()
260 self.state = next_state
261
262 self.value = post_value
263 self.cash = self.cash
264 self.position = self.position
265 self.ret = (post_value - pre_value) / pre_value
266 self.date = self.get_current_date()
267
268 self.price = self.get_current_price()
269 self.total_return += self.discount * reward
270 self.discount *= 0.99
271 self.total_profit = 100 * (self.value - self.initial_amount) / self.initial_amount
272
273 info = {
274 "symbol": str(self.symbol),
275 "asset_type": str(self.asset_type),
276 "day": int(self.day),
277 "value": float(self.value),
278 "cash": float(self.cash),
279 "position": int(self.position),
280 "ret": float(self.ret),
281 "date": self.date.strftime('%Y-%m-%d'),
282 "price": float(self.price),
283 "discount": float(self.discount),
284 "total_profit": float(self.total_profit),
285 "total_return": float(self.total_return),
286 "action": str(self.action)
287 }
288
289 return next_state, reward, done, truncted, info
290
291
292if __name__ == '__main__':

Callers 6

runFunction · 0.80
runFunction · 0.80
runFunction · 0.80
runFunction · 0.80
runFunction · 0.80
runFunction · 0.80

Calls 6

buyMethod · 0.95
sellMethod · 0.95
hold_onMethod · 0.95
get_stateMethod · 0.95
get_current_dateMethod · 0.95
get_current_priceMethod · 0.95

Tested by

no test coverage detected