Navigation

    Backtrader Community

    • Register
    • 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/

    Commission dependent on direction (buy or sell)

    General Code/Help
    2
    4
    1268
    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.
    • M
      mirono last edited by

      My commissions for stocks are 1$ up to 100 units, or 0.01$ per unit if over 100 units.
      In addition exchange charges are 0.05$ on buy, 0.06$ on sell.
      I was able to code the 1st part:

      class CommInfo_My(bt.CommInfoBase):
          params = (
            ('stocklike', True),  # Stocks
            ('commtype', bt.CommInfoBase.COMM_FIXED),  # Apply fixed Commission
          # ('percabs', False),  # pass perc as xx% which is the default
            ('interest_long', True),
          )
      
          def _getcommission(self, size, price, pseudoexec):
              if size <= 100:
                  return 1.00
              else:
                  return size * self.p.commission
      

      (where I pass commission=0.01)

      But how do I code the 2nd part? Is there a direction indication in the CommInfoBase class?

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

        In the original bt code for commission calculations the size variable is used with abs function, so I can assume that short position gives size < 0 and long position gives size > 0. You can directly use it in your new _getcommission fucntion.

        But honestly, it is just 1 cent difference, why not to use 6 cents everywhere?

        • 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 0
        • M
          mirono last edited by

          Thanks. So size should be positive for buy and negative for sell (not short)?
          As for the 1c, I suspect it has to do with the time the position is held. I assume it increases with time (never had long lived positions in real life, to verify this). I'll take our advice and add 0.06 to the commissions.

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

            @mirono said in Commission dependent on direction (buy or sell):

            Thanks. So size should be positive for buy and negative for sell (not short)?

            From my understanding if you buy, then internally size is positive for trade and commission calcs; if you sell, then size is negative. short is equal to sell if you have nothing to sell, so I assume negative size also, but you know your trading conditions better. anyway, code it and let us know your results.

            As for the 1c, I suspect it has to do with the time the position is held. I assume it increases with time (never had long lived positions in real life, to verify this).

            I don't think so. My understanding that exchange fee is included into brokerage fee and doesn't apply separately. But I can be wrong.

            • 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
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors