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/

    How to write __init__() in customized Commission schema?

    General Discussion
    2
    3
    44
    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.
    • S
      soulmachine last edited by

      I've written a customized commission schema that supports fractional size:

      class CryptoCommissionInfo(bt.CommissionInfo):
          '''Commission scheme for cryptocurrency spot market.'''
          params = (
              ('stocklike', True),
              ('commtype', bt.CommInfoBase.COMM_PERC), # apply % commission
          )
          
          def __init__(self, commission: float = None):
              assert abs(commission) < 1.0 # commission is a percentage, error: bad operand type for abs(): 'NoneType'
              super().__init__()
      
          def getsize(self, price, cash):
              '''Support fractional-size.
              
                  More details at https://www.backtrader.com/blog/posts/2019-08-29-fractional-sizes/fractional-sizes/.
              '''
              return self.p.leverage * (cash / price)
      

      Everything works fine except the code in __init__().

      I think this is related to meta programming in Python, which I'm not familiar with.

      Can someone fix my __init__() function? Thanks!

      vladisld 1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld @soulmachine last edited by

        @soulmachine said in How to write __init__() in customized Commission schema?:

        def init(self, commission: float = None):

        Please try to remove the commission argument from the __init__ method. The commission is defined as one of params (in CommInfoBase) and will be processed by MetaParams metaclass (as you've correctly mentioned):

        def __init__(self):
                assert abs(self.p.commission) < 1.0 # commission is a percentage, error: bad operand type for abs(): 'NoneType'
                super().__init__()
        
        1 Reply Last reply Reply Quote 2
        • S
          soulmachine last edited by

          @vladisld Thanks, it works! I shared my complete code in another post https://community.backtrader.com/topic/2806/share-my-customized-commission-scheme-for-cryptocurrencies

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }