Changing Commissions in Next()?
I am wondering if it is possible to change the commission during "next()" iterations?
I have cases where if certain conditions are met, I want to go "leveraged long with margin" (eg. 2x leverage)... but if these conditions are not met, I do not want any leverage, just normal buy and sell trades (without any margin requirements).
I tried using "self.broker.addcommissioninfo" at the start of each "next()" and adding a margin comminfo where appropriate, but I do not believe this is the correct way to do it.
Would love to know if this is possible at all? I am using my own custom comminfo if that matters (code below).
class CommInfo_Custom(bt.CommInfoBase): params = ( ('commtype', bt.CommInfoBase.COMM_PERC), ('min_fee', 0), ) def _getcommission(self, size, price, pseudoexec): commvalue = abs(size) * price * self.p.commission # %based commission value commvalue = max(self.p.min_fee,commvalue) # choose the greater of $min_fee and percentage calculated return commvalue
Desired commission info to "use" when in normal conditions:
comminfo = CommInfo_Custom(commission=v.comm_pct ,min_fee=0.0 ,interest=v.short_holding_fee ,interest_long=False ,percabs=True ,automargin=False ,leverage=1.0 ,mult=1.0)
Desired commission info to "use" when in "leverage" conditions:
comminfo_leveraged_long = CommInfo_Custom(commission=v.comm_pct ,min_fee=0.0 ,interest=v.short_holding_fee ,interest_long=True ,percabs=True ,automargin=0.5 #must hold 0.5*price to hold a margin position ,leverage=2.0 #Amount of leverage for the asset with regards to the needed cash ,mult=2.0) #multiplier applied to the asset for value/profit
Thanks so much!!
Thank you @backtrader,
When you say it is OK to replace it, would something like the following (psuedo code) be acceptable?
def next(self): if market_scenario_one: self.broker.addcommissioninfo(comminfo_one) else: self.broker.addcommissioninfo(comminfo_two)
...be OK, assuming comminfo_one and comminfo_two are defined
When you add a commission scheme you do it for a given
name. The default name is
Nonewhich applies when there is no given commission scheme with a matching name for an asset. So yes.