# Copyright 2024 The TensorTrade and TensorTrade-NG Authors.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the Licensefrom__future__importannotationsimporttypingimportnumpyasnpfromgymnasium.spacesimportBoxfromtensortrade.env.observers.abstractimportAbstractObserveriftyping.TYPE_CHECKING:fromtypingimportTypeAliasfromgymnasium.spacesimportSpacefromgymnasium.coreimportObsType
[docs]classSimpleObserver(AbstractObserver):"""A simple observer that allows to observe the data. This observer just returns the feature data. It is the simplest observer possible. :param observation_dtype: The data type of the observation. Defaults to ``np.float32``. :type observation_dtype: TypeAlias :param observation_lows: The lowest value of the observation. Defaults to ``-np.inf``. :type observation_lows: float :param observation_highs: The highest value of the observation. Defaults to ``np.inf``. :type observation_highs: float """registered_name="simple_observer"def__init__(self,observation_dtype:TypeAlias=np.float32,observation_lows:float=-np.inf,observation_highs:float=np.inf,)->None:super().__init__(observation_dtype=observation_dtype,observation_lows=observation_lows,observation_highs=observation_highs)@propertydefobservation_space(self)->Space:returnBox(low=self._observation_lows,high=self._observation_highs,shape=(1,self.trading_env.feed.features_size),dtype=self._observation_dtype)
[docs]defobserve(self)->ObsType:"""Observes the environment. This will return the actual state of the features. :returns: The current observation window. :rtype: ObsType """state=list(self.trading_env.feed.state.features.values())obs=np.array([state])obs=obs.astype(self._observation_dtype)returnobs
[docs]defhas_next(self)->bool:"""Checks if another observation can be generated. :returns: True if another observation can be generated. :rtype: bool """returnself.trading_env.feed.has_next()