For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
2019-10-02: The community is currently in read-only mode

Changing Commissions in Next()?



  • Hello all!

    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!!


  • administrators

    @cwse said in Changing Commissions in Next()?:

    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.

    It does for sure work. You replace the existing scheme with the new one.



  • 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 bt.CommInfoBase objects?

    Cheers,
    cwse


  • administrators

    When you add a commission scheme you do it for a given name. The default name is None which applies when there is no given commission scheme with a matching name for an asset. So yes.


Log in to reply
 

});