Hi,
first of all I'm new to quantconnect, thus I apologize for my possible dumb questions. I switched from quantopian a few days ago primarily because they don't offer live trading. Currently I'm trying to implement my ideas but I'm kind of confused about the fee/slippage modelling with LEAN.
Long story short, I've created a strategy which has a pretty heavy turnover, thus I'm using Alpaca as brogerage model (otherwise the fees would eat up most of the profits). Nevertheless the backtest results seem way too good to be true. I'm currently modelling the fee/slippage like so:
class CustomSlippageModel:
def __init__(self, algorithm):
self.algorithm = algorithm
def GetSlippageApproximation(self, asset, order):
slippage = asset.Price * d.Decimal(0.0001 * np.log10(2*float(order.AbsoluteQuantity)))
return slippage
class MyAlgo(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1)
self.SetEndDate(2020, 4, 1)
self.SetCash(10000)
self.UniverseSettings.Resolution = Resolution.Minute
self.AddUniverse(self.Universe.Index.QC500)
self.AddEquity("SPY", Resolution.Minute)
self.SetBrokerageModel(BrokerageName.Alpaca , AccountType.Margin)
for sec in self.Securities.keys():
self.Securities[sec].SetSlippageModel(CustomSlippageModel(self))
self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.Every(timedelta(minutes=10)),
self.rebalance)
My questions now are:
Will the backtest produce realistic results?
Can I improve the code somehow to be more realistic?
Is the slippage model even assigned correcly?
Thanks in advance for any help :)
Rahul Chowdhury
Hey Georg,
The Alpaca brokerage model as its own slippage and fee models which you can find on AlpacaBrokerageModel.cs. If you want to implement your own custom slippage model, you should use
Security.SetSlippageModel(CustomSlippageModel(self))
to set your slippage model for a security. You can learn more about slippage, fills, and fees in the documentation.
In order to set a custom slippage model for every security that enters our universe, we must use SetSecurityInitializer. Let's define our custom security initializer as
def CustomSecurityInitializer(self, security): security.SetSlippageModel(CustomSlippageModel(self))
Then in Initialize we can set our custom security initializer.
self.SetSecurityInitializer(self.CustomSecurityInitializer)
Best
Rahul
Georg Keller
Hi Rahul,
thank you so much for your comprehensive answer to my questions. Especially, I wasn't aware about the "SetSecurityInitializer" method - now modelling slippage works well.
Anyways, I've got one additional question: You mentioned the AlpacaBrokerageModel already implements a slippage model so I checked the code you linked and recognized the slippage model is set to a constant model of 0 slippage. Is this value set to 0 because the slippage impact on alpaca is this low, or is it just set to 0 because you don't have enough data to make a good estimate on the slippage impact on this specific broker?
In other words: Can the default AlpacaBroker model be used to produce reliable and realistic backtests, or should I set a custom model?
Best regards,
Georg
Rahul Chowdhury
Hey Georg,
By default, Lean models slippage with a constant value of 0, which corresponds to no slippage. It's up to the users of Lean to create their own slippage models according to their needs. Using a constant slippage model will not affect backtest results dramatically, unless your strategy trades on small margins where slippage can make the difference between profit and loss.
Best
Rahul
Georg Keller
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!