Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Flat Commission - IB

    General Discussion
    4
    5
    2113
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ealvarpe
      ealvarpe last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by backtrader

        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

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators last edited by backtrader

          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)

          1 Reply Last reply Reply Quote 0
          • ealvarpe
            ealvarpe last edited by

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

            but apart of that it works as expected.

            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by backtrader

              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.

              1 Reply Last reply Reply Quote 1
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors