Hi there,
I have implemented a custom fee model trying to charge a 2% spread while entering trade but when I run the backtest there is no fee accrued. What is wrong in this code?
from AlgorithmImports import *
class BTCUSDTrendFollowingAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2023, 1, 1) # Set Start Date
self.SetEndDate(2024, 12, 1) # Set End Date
self.SetCash(2_000) # Set Strategy Cash
self.symbol = self.AddCrypto("BTCUSD", Resolution.Minute).Symbol
self.highest_close = None
self.breakout_volume = None
self.trailing_stop = None
self.daily_closes = RollingWindow[float](5)
self.volume_sma = SimpleMovingAverage(20)
# Set the custom fee model
self.Securities[self.symbol].SetFeeModel(MyFeeModel())
def OnData(self, data):
if not data.Bars.ContainsKey(self.symbol):
return
bar = data.Bars[self.symbol]
# Update daily closes
if self.Time.hour == 0 and self.Time.minute == 0:
if self.daily_closes.IsReady:
self.daily_closes.Add(bar.Close)
else:
self.daily_closes.Add(bar.Close)
# Update volume SMA
self.volume_sma.Update(self.Time, bar.Volume)
# Check for breakout condition
if self.daily_closes.IsReady and self.volume_sma.IsReady:
highest_close = max(self.daily_closes)
breakout_volume = self.volume_sma.Current.Value * 5
if bar.Close > highest_close and bar.Volume > breakout_volume:
self.SetHoldings(self.symbol, 1)
self.trailing_stop = bar.Close * 0.98
# Implement trailing stop loss
if self.Portfolio[self.symbol].Invested:
if bar.Close < self.trailing_stop:
self.Liquidate(self.symbol)
else:
self.trailing_stop = max(self.trailing_stop, bar.Close * 0.95)
class MyFeeModel(FeeModel):
def GetOrderFee(self, parameters: OrderFeeParameters) -> OrderFee:
# Implementing a 2% spread fee
order = parameters.Order
fee = order.AbsoluteQuantity * order.Price * 0.02
return OrderFee(CashAmount(fee, 'USD'))
Mia Alissi
The code you provided seems correct for implementing a custom fee model in QuantConnect. The
MyFeeModel
class correctly overrides theGetOrderFee
method and calculates the fee as 2% of the order value. This fee is then returned as anOrderFee
object.However, please note that the fee is only applied when an order is filled. If no trades are executed during your backtest, no fees will be accrued. Make sure that your trading conditions are being met and trades are being executed.
If trades are being executed but no fees are being accrued, it might be a good idea to add some logging to your
GetOrderFee
method to ensure it's being called. You can do this by adding a line likeself.Debug(f'Fee: {fee}')
right before thereturn
statement.If you're still having issues, please provide more details about the trades being executed and the fees you're expecting.
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.
Sebastien M. Laignel
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!