The trading environment
As we have a lot of code that is supposed to work with the Gym API, we will implement the trading functionality following Gym’s Env class, which should be already familiar to you. Our environment is implemented in the StocksEnv class in the Chapter10/lib/environ.py module. It uses several internal classes to keep its state and encode observations.
Let’s first look at the public API class:
import typing as tt
import gymnasium as gym
from gymnasium import spaces
from gymnasium.utils import seeding
from gymnasium.envs.registration import EnvSpec
import enum
import numpy as np
from . import data
DEFAULT_BARS_COUNT = 10
DEFAULT_COMMISSION_PERC = 0.1
class Actions(enum.Enum):
Skip = 0
Buy = 1
Close = 2
We encode all available actions as an enumerator’s fields and provide just three actions: do nothing, buy a single share, and close the existing position.
...