Flat Commission - IB
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.
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?
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
Edit: Beaten to the pole position (deemed to happen sooner than later)
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
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
CommissionInfoif 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
1for each entry/exit (disregarding the
pseudoexecparameter 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)
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.
CFDproducts 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.