Flat Commission - IB



  • Does anyone know how to create a flat commission model?
    I´m trying to replicate the CFD´s model on Interactive Brokers, being $1 for each entry/exit of the market (independently of the volume until some level).
    If I just set the commision, this is created as a percentage, but if I use the futures like model, the problem is that the commision is fixed but gets multiplied by the number of stocks... so it doesn´t work.

    I´ve tried
    cerebro.broker.setcommission(commission=1, commtype=bt.CommInfoBase.COMM_FIXED, stocklike = True)
    but the commission remains being multiplied by the number of stocks... should be like this?

    Anyone knows how to set this model?



  • Subclassing can help. Something like this:

    class CommInfo_CFD(bt.CommInfoBase):
        params = (('stocklike', True), ('commtype', bt.CommInfoBase.COMM_FIXED),)
    
        def _getcommission(self, size, price, pseudoexec):
            return self.p.commission
    

    Then call as

    comminfo = CommInfo_CFD(commission=1) # 1$
    cerebro.addcommissioninfo(comminfo)
    

    I didn't check it, but it is based on
    https://www.backtrader.com/docu/user-defined-commissions/commission-schemes-subclassing.html


  • administrators

    Edit: Beaten to the pole position (deemed to happen sooner than later)


    After all FIXED isn't FLAT. But in any case the solution shouldn't be that difficult:

    See here: Docs - User Defined Commissions

    Instead of using setcommission (which can only tune the existing scheme with parameters) you have to use addcommissioninfo as in:

    import backtrader as bt
    ...
    cerebro = bt.Cerebro()
    ...
    cerebro.broker.addcommissioninfo(MyCommissionSchemeInstance())
    ...
    

    Although explained in the doc, your case would only need to override the following method from a CommInfoBase (or CommissionInfo if you prefer) class

    def _getcommission(self, size, price, pseudoexec):
       '''Calculates the commission of an operation at a given price
    
       pseudoexec: if True the operation has not yet been executed
       '''
    

    And you may return 1 for each entry/exit (disregarding the pseudoexec parameter in your case, which is there to indicate if the broker is simply calculating whether an operation would deplete the cash reserves before being accepted by the broker)



  • Thanks both.. it works! only a little thing:
    the method is addcommissioninfo, not addcommission

    but apart of that it works as expected.


  • administrators

    Note: with CFD products you should consider taking the interest into account for both long and short positions.

    Note2: that's the problem with snippet typing (as opposed to testing it), they usually contain typos. And the same applied partially to the documentation. Both posts have been corrected to avoid future confusion and the docs updated.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.