# 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.abstractimportAbstractRewardSchemefromtensortrade.feedimportDataFeed,Stream
[docs]classPBR(AbstractRewardScheme):"""A reward scheme for position-based returns. * Let :math:`p_t` denote the price at time t. * Let :math:`x_t` denote the position at time t. * Let :math:`R_t` denote the reward at time t. Then the reward is defined as, :math:`R_{t} = (p_{t} - p_{t-1}) \cdot x_{t}`. Parameters ---------- price : `Stream` The price stream to use for computing rewards. """registered_name="pbr"def__init__(self,price:Stream)->None:super().__init__()self.position=-1r=Stream.sensor(price,lambdap:p.value,dtype="float").diff()position=Stream.sensor(self,lambdars:rs.position,dtype="float")reward=(position*r).fillna(0).rename("reward")self.feed=DataFeed([reward])self.feed.compile()