I needed to emulate IB 's tiered structure of fees in python, where most examples I found were for C#. Anyways, this works and may help someone else:
for security in self.stocks:
self.Securities[security].SetSlippageModel(CustomSlippageModel())
self.Securities[security].SetDataNormalizationMode(DataNormalizationMode.Raw)
self.Securities[security].SetFeeModel(CustomFeeModel(self)) #ConstantFeeModel(10.00))
class CustomFeeModel(FeeModel):
def __init__(self, algorithm):
self.algorithm = algorithm
def GetOrderFee(self, parameters):
# custom fee math
# less than 300000 shares
#USD 0.0035 USD 0.35 1.0% of trade value
minfee = max(0.35, parameters.Security.Price * parameters.Order.AbsoluteQuantity * 0.0035 )
maxfee = parameters.Order.AbsoluteQuantity * 0.01
fee = min(minfee,maxfee)
self.algorithm.Log("CustomFeeModel: " + str(fee))
return OrderFee(CashAmount(fee, "USD"))
# Custom slippage implementation
class CustomSlippageModel:
def GetSlippageApproximation(self, asset, order):
try:
return decimal.Decimal(0.0002)
except:
self.Log('An unknown error occurred trying CustomSlippageModel.' + str(sys.exc_info()[0]) )
return 0.0002
Hope this helps someone else.
Pier Paolo
that's exactly what Iwas lloking for.. thanksfor sharing Serge! Why did you implement also a custom slippage model?
Serge d'Adesky
Hi Paolo, Glad it could be of use. I don't remember why I set up the custom sl.ippage any more. It would be useful if I were trading a stock or stocks that deviated widely from the "normal" slippage, e.g. penny stocks, crypto, etc.
Derek Melchin
Hi everyone,
Note that the fees above should be
# $0.0035 per share; Minimum of $0.35 per order minfee = max(0.35, parameters.Order.AbsoluteQuantity * 0.0035 ) # Maximum fee is 1.0% of trade value maxfee = parameters.Security.Price * parameters.Order.AbsoluteQuantity * 0.01
See this related thread for a full backtest.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Serge d'Adesky
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!