class_docstr: A portfolio allocation environment for OpenAI gym.
  
  This environment simulates the interactions between an agent and the financial market
  based on data provided by a dataframe. The dataframe contains the time series of
  features defined by the user (such as closing, high and low prices) and must have
  a time and a tic column with a list of datetimes and ticker symbols respectively.
  An example of dataframe is shown below::
  
          date        high            low             close           tic
      0   2020-12-23  0.157414        0.127420        0.136394        ADA-USD
      1   2020-12-23  34.381519       30.074295       31.097898       BNB-USD
      2   2020-12-23  24024.490234    22802.646484    23241.345703    BTC-USD
      3   2020-12-23  0.004735        0.003640        0.003768        DOGE-USD
      4   2020-12-23  637.122803      560.364258      583.714600      ETH-USD
      ... ...         ...             ...             ...             ...
  
  Based on this dataframe, the environment will create an observation space that can
  be a Dict or a Box. The Box observation space is a three-dimensional array of shape
  (f, n, t), where f is the number of features, n is the number of stocks in the
  portfolio and t is the user-defined time window. If the environment is created with
  the parameter return_last_action set to True, the observation space is a Dict with
  the following keys::
  
      {
      "state": three-dimensional Box (f, n, t) representing the time series,
      "last_action": one-dimensional Box (n+1,) representing the portfolio weights
      }
  
  Note that the action space of this environment is an one-dimensional Box with size
  n + 1 because the portfolio weights must contains the weights related to all the
  stocks in the portfolio and to the remaining cash.
  
  Attributes:
      action_space: Action space.
      observation_space: Observation space.
      episode_length: Number of timesteps of an episode.
      portfolio_size: Number of stocks in the portfolio.
class_docstr_tok: ["A","portfolio","allocation","environment","for","OpenAI","gym",".","This","environment","simulates","the","interactions","between","an","agent","and","the","financial","market","based","on","data","provided","by","a","dataframe",".","The","dataframe","contains","the","time","series","of","features","defined","by","the","user","(","such","as","closing",",","high","and","low","prices",")","and","must","have","a","time","and","a","tic","column","with","a","list","of","datetimes","and","ticker","symbols","respectively",".","An","example","of","dataframe","is","shown","below",":",":","date","high","low","close","tic","0","2020-12-23","0.157414","0.127420","0.136394","ADA-USD","1","2020-12-23","34.381519","30.074295","31.097898","BNB-USD","2","2020-12-23","24024.490234","22802.646484","23241.345703","BTC-USD","3","2020-12-23","0.004735","0.003640","0.003768","DOGE-USD","4","2020-12-23","637.122803","560.364258","583.714600","ETH-USD","...","...","...","...","...","...","Based","on","this","dataframe",",","the","environment","will","create","an","observation","space","that","can","be","a","Dict","or","a","Box",".","The","Box","observation","space","is","a","three-dimensional","array","of","shape","(","f",",","n",",","t",")",",","where","f","is","the","number","of","features",",","n","is","the","number","of","stocks","in","the","portfolio","and","t","is","the","user-defined","time","window",".","If","the","environment","is","created","with","the","parameter","return_last_action","set","to","True",",","the","observation","space","is","a","Dict","with","the","following","keys",":",":","{","``","state","''",":","three-dimensional","Box","(","f",",","n",",","t",")","representing","the","time","series",",","``","last_action","''",":","one-dimensional","Box","(","n+1",",",")","representing","the","portfolio","weights","}","Note","that","the","action","space","of","this","environment","is","an","one-dimensional","Box","with","size","n","+","1","because","the","portfolio","weights","must","contains","the","weights","related","to","all","the","stocks","in","the","portfolio","and","to","the","remaining","cash",".","Attributes",":","action_space",":","Action","space",".","observation_space",":","Observation","space",".","episode_length",":","Number","of","timesteps","of","an","episode",".","portfolio_size",":","Number","of","stocks","in","the","portfolio","."]
docstr: Initializes environment's instance.
  
  Args:
      df: Dataframe with market information over a period of time.
      initial_amount: Initial amount of cash available to be invested.
      order_df: If True input dataframe is ordered by time.
      return_last_action: If True, observations also return the last performed
          action. Note that, in that case, the observation space is a Dict.
      normalize_df: Defines the normalization method applied to input dataframe.
          Possible values are "by_previous_time", "by_fist_time_window_value",
          "by_COLUMN_NAME" (where COLUMN_NAME must be changed to a real column
          name) and a custom function. If None no normalization is done.
      reward_scaling: A scaling factor to multiply the reward function. This
          factor can help training.
      comission_fee_model: Model used to simulate comission fee. Possible values
          are "trf" (for transaction remainder factor model) and "wvm" (for weights
          vector modifier model). If None, commission fees are not considered.
      comission_fee_pct: Percentage to be used in comission fee. It must be a value
          between 0 and 1.
      features: List of features to be considered in the observation space. The
          items of the list must be names of columns of the input dataframe.
      valuation_feature: Feature to be considered in the portfolio value calculation.
      time_column: Name of the dataframe's column that contain the datetimes that
          index the dataframe.
      time_format: Formatting string of time column.
      tic_name: Name of the dataframe's column that contain ticker symbols.
      time_window: Size of time window.
      cwd: Local repository in which resulting graphs will be saved.
      new_gym_api: If True, the environment will use the new gym api standard for
          step and reset methods.
docstr_tok: ["Initializes","environment","'s","instance",".","Args",":","df",":","Dataframe","with","market","information","over","a","period","of","time",".","initial_amount",":","Initial","amount","of","cash","available","to","be","invested",".","order_df",":","If","True","input","dataframe","is","ordered","by","time",".","return_last_action",":","If","True",",","observations","also","return","the","last","performed","action",".","Note","that",",","in","that","case",",","the","observation","space","is","a","Dict",".","normalize_df",":","Defines","the","normalization","method","applied","to","input","dataframe",".","Possible","values","are","``","by_previous_time","''",",","``","by_fist_time_window_value","''",",","``","by_COLUMN_NAME","''","(","where","COLUMN_NAME","must","be","changed","to","a","real","column","name",")","and","a","custom","function",".","If","None","no","normalization","is","done",".","reward_scaling",":","A","scaling","factor","to","multiply","the","reward","function",".","This","factor","can","help","training",".","comission_fee_model",":","Model","used","to","simulate","comission","fee",".","Possible","values","are","``","trf","''","(","for","transaction","remainder","factor","model",")","and","``","wvm","''","(","for","weights","vector","modifier","model",")",".","If","None",",","commission","fees","are","not","considered",".","comission_fee_pct",":","Percentage","to","be","used","in","comission","fee",".","It","must","be","a","value","between","0","and","1.","features",":","List","of","features","to","be","considered","in","the","observation","space",".","The","items","of","the","list","must","be","names","of","columns","of","the","input","dataframe",".","valuation_feature",":","Feature","to","be","considered","in","the","portfolio","value","calculation",".","time_column",":","Name","of","the","dataframe","'s","column","that","contain","the","datetimes","that","index","the","dataframe",".","time_format",":","Formatting","string","of","time","column",".","tic_name",":","Name","of","the","dataframe","'s","column","that","contain","ticker","symbols",".","time_window",":","Size","of","time","window",".","cwd",":","Local","repository","in","which","resulting","graphs","will","be","saved",".","new_gym_api",":","If","True",",","the","environment","will","use","the","new","gym","api","standard","for","step","and","reset","methods","."]
code: def __init__(
      self,
      df,
      initial_amount,
      order_df=True,
      return_last_action=False,
      normalize_df="by_previous_time",
      reward_scaling=1,
      comission_fee_model="trf",
      comission_fee_pct=0,
      features=["close", "high", "low"],
      valuation_feature="close",
      time_column="date",
      time_format="%Y-%m-%d",
      tic_column="tic",
      time_window=1,
      cwd="./",
      new_gym_api=False,
  ):
      """Initializes environment's instance.
  
      Args:
          df: Dataframe with market information over a period of time.
          initial_amount: Initial amount of cash available to be invested.
          order_df: If True input dataframe is ordered by time.
          return_last_action: If True, observations also return the last performed
              action. Note that, in that case, the observation space is a Dict.
          normalize_df: Defines the normalization method applied to input dataframe.
              Possible values are "by_previous_time", "by_fist_time_window_value",
              "by_COLUMN_NAME" (where COLUMN_NAME must be changed to a real column
              name) and a custom function. If None no normalization is done.
          reward_scaling: A scaling factor to multiply the reward function. This
              factor can help training.
          comission_fee_model: Model used to simulate comission fee. Possible values
              are "trf" (for transaction remainder factor model) and "wvm" (for weights
              vector modifier model). If None, commission fees are not considered.
          comission_fee_pct: Percentage to be used in comission fee. It must be a value
              between 0 and 1.
          features: List of features to be considered in the observation space. The
              items of the list must be names of columns of the input dataframe.
          valuation_feature: Feature to be considered in the portfolio value calculation.
          time_column: Name of the dataframe's column that contain the datetimes that
              index the dataframe.
          time_format: Formatting string of time column.
          tic_name: Name of the dataframe's column that contain ticker symbols.
          time_window: Size of time window.
          cwd: Local repository in which resulting graphs will be saved.
          new_gym_api: If True, the environment will use the new gym api standard for
              step and reset methods.
      """
      # super(StockEnv, self).__init__()
      # money = 10 , scope = 1
      self._time_window = time_window
      self._time_index = time_window - 1
      self._time_column = time_column
      self._time_format = time_format
      self._tic_column = tic_column
      self._df = df
      self._initial_amount = initial_amount
      self._return_last_action = return_last_action
      self._reward_scaling = reward_scaling
      self._comission_fee_pct = comission_fee_pct
      self._comission_fee_model = comission_fee_model
      self._features = features
      self._valuation_feature = valuation_feature
      self._cwd = Path(cwd)
      self._new_gym_api = new_gym_api
  
      # results file
      self._results_file = self._cwd / "results" / "rl"
      self._results_file.mkdir(parents=True, exist_ok=True)
  
      # price variation
      self._df_price_variation = None
  
      # preprocess data
      self._preprocess_data(order_df, normalize_df)
  
      # dims and spaces
      self._tic_list = self._df[self._tic_column].unique()
      self.portfolio_size = len(self._tic_list)
      action_space = 1 + self.portfolio_size
  
      # sort datetimes and define episode length
      self._sorted_times = sorted(set(self._df[time_column]))
      self.episode_length = len(self._sorted_times) - time_window + 1
  
      # define action space
      self.action_space = spaces.Box(
          low=0, high=1, shape=(action_space,)
      )
  
      # define observation state
      if self._return_last_action:
          # if  last action must be returned, a dict observation
          # is defined
          self.observation_space = spaces.Dict(
              {
                  "state": spaces.Box(
                      low=-np.inf,
                      high=np.inf,
                      shape=(
                          len(self._features),
                          self.portfolio_size,
                          self._time_window,
                      ),
                  ),
                  "last_action": spaces.Box(
                      low=0, high=1, shape=(action_space,)
                  ),
              }
          )
      else:
          # if information about last action is not relevant,
          # a 3D observation space is defined
          self.observation_space = spaces.Box(
              low=-np.inf,
              high=np.inf,
              shape=(
                  len(self._features),
                  self.portfolio_size,
                  self._time_window,
              ),
          )
  
      self._reset_memory()
  
      self._portfolio_value = self._initial_amount
      self._terminal = False
  
imports: from __future__ import annotations
  import math
  import gym
  import matplotlib
  import numpy
  import pandas
  from gym import spaces
  from gym.utils import seeding
  import matplotlib.pyplot
  from stable_baselines3.common.vec_env import DummyVecEnv
  from pathlib import Path
label_desc: Use image node_id 1 to create a new PortfolioOptimizationEnv object from inherited base classes: gym with example: obj = PortfolioOptimizationEnv(df, initial_amount, order_df, return_last_action, normalize_df, reward_scaling, comission_fee_model, comission_fee_pct, features, valuation_feature, time_column, time_format, tic_column, time_window, cwd, new_gym_api)