Source code for tensortrade.env.rewards.simple_profit
# 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__importannotationsfromtensortrade.env.rewards.abstractimportAbstractRewardScheme
[docs]classSimpleProfit(AbstractRewardScheme):"""A simple reward scheme that rewards the agent for incremental increases in net worth. Parameters ---------- window_size : int The size of the look back window for computing the reward. Attributes ---------- window_size : int The size of the look back window for computing the reward. """registered_name="simple"def__init__(self,window_size:int=1):super().__init__()self._window_size=self.default('window_size',window_size)
[docs]defreward(self)->float:"""Rewards the agent for incremental increases in net worth over a sliding window. Returns ------- float The cumulative percentage change in net worth over the previous `window_size` time steps. """net_worths=[nw['net_worth']fornwinself.trading_env.portfolio.performance.values()]iflen(net_worths)>1:returnnet_worths[-1]/net_worths[-min(len(net_worths),self._window_size+1)]-1.0else:return0.0