# 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__importannotationsimporttypingfromgymnasium.spacesimportDiscretefromtensortrade.env.actions.abstractimportAbstractActionSchemefromtensortrade.oms.ordersimportproportion_orderiftyping.TYPE_CHECKING:fromtypingimportListfromgymnasium.spacesimportSpacefromtensortrade.oms.ordersimportOrderfromtensortrade.oms.walletsimportWallet
[docs]classBSH(AbstractActionScheme):"""A simple discrete action scheme where the only options are to buy, sell, or hold. Parameters ---------- cash : `Wallet` The wallet to hold funds in the base intrument. asset : `Wallet` The wallet to hold funds in the quote instrument. """registered_name="bsh"def__init__(self,cash:Wallet,asset:Wallet):super().__init__()self.cash=cashself.asset=assetself.listeners=[]self.action=0@propertydefaction_space(self)->Space:returnDiscrete(2)
[docs]defget_orders(self,action:int)->List[Order]:order=Noneifabs(action-self.action)>0:src=self.cashifself.action==0elseself.assettgt=self.assetifself.action==0elseself.cashifsrc.balance==0:# We need to check, regardless of the proposed order, if we have balance in 'src'return[]# Otherwise just return an empty order listorder=proportion_order(self.trading_env.portfolio,src,tgt,1.0)self.action=actionforlistenerinself.listeners:listener.on_action(action)return[order]