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. Thecommission
is defined as one ofparams
(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__()
-
@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