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?



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



  • @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__()
    



Log in to reply
 

});