Hi community,
I tried to apply slippage as presented in example in this algo.ref :
https://github.com/QuantConnect/Lean/blob/master/Algorithm.CSharp/CustomModelsAlgorithm.csI connect my contract to slippage via :
contract.SetSlippageModel(new CustomSlippageModel(this));
But it doesn't work, when i change "asset.Price * 0.0001" in the variable slippage in the public class, nothing change.
Backtest below with "asset.Price*0.01".
Thank you in advance for any help.
Alexandre Catarino
Hi Gmamuze Cht ,
When we subscribe to futures data with AddFuture, we create a universe of futures' contracts that are filtered by the function set by the SetFilter method. Consequently, we need to use SetSecurityInitializer in order to set models and properties of the Security object that represents each futures' contract.
If we want to set the same slippage model across all the futures' contracts, we need to use:
SetSecurityInitializer(x => x.SetSlippageModel(new CustomSlippageModel(this)));
Gmamuze Cht
Hi Alexander,
Thanks for your response ! Is there a same simple way to apply a model based on the % of volume traded ?
Gmamuze Cht
And i have a general question about slippage :
The minimum deviation is equal to the minimum price fluctuation, right ? Or is it normal that the model slippage above make a filled price value with lot of decimal than the original minimum price fluctuation ? And is it real to fill a price like this ?
Alexandre Catarino
Hi Gmamuze Cht ,
If you mean what the changes should be made in the custom model in the attached backtest, we just need to use a different formula:
var orderVolume = order.AbsoluteQuantity * asset.Price; var slippage = orderVolume * 0.01m;
The slippage model is responsible for the rounding to the minimum price fluctuation. We can find the value to make the adjustment in asset.SymbolProperties.MinimumPriceVariation.
Gmamuze Cht
I Follow your advice and I update in function of my needs.
The result take into account :
- Size of the last candle (High - Low) divide by a %. (enable the correlation with volatility)
- The ratio of OrderVolume divide by the last AssetVolume. (enable the correlation with liquidity)
Final Formula : var slippage = (High[0]-Low[0]) * 0.01m / (orderVolume/Volume[0]);
public class CustomSlippageModel : ISlippageModel { private readonly QCAlgorithm _algorithm; public RollingWindow<decimal> High = new RollingWindow<decimal>(2); public RollingWindow<decimal> Low = new RollingWindow<decimal>(2); public RollingWindow<decimal> Volume = new RollingWindow<decimal>(2); public CustomSlippageModel(QCAlgorithm algorithm) { _algorithm = algorithm; } public decimal GetSlippageApproximation(Security asset, Order order) { High.Add(asset.High); Low.Add(asset.Low); Volume.Add(asset.Volume); var orderVolume = order.AbsoluteQuantity; var slippage = (High[0]-Low[0]) * 0.01m / (orderVolume/Volume[0]); return slippage; } }
Gmamuze Cht
A Question, volumes given from the market data is the same unit than the quantity emit by the algorithm ?
Alexandre Catarino
Hi Gmamuze Cht
Volume is the sum of quantity of all trades within the data resolution period.
Gmamuze Cht
Hi Alexander, it was not the goal of my question :) I am talking about unit applyed to the data in thousand, million etc.. I pose this question because for example on Dukascopy when you download data they propose this option to choose your volume data unit. In case which 1 lot of EUR/USD was not equal to 1 lot = 100.000 unit, it would preferable to rethink the formula below.
Ps : In My code upper, I make a mistake the final formula is not :
var slippage = (High[0]-Low[0]) * 0.01m / (orderVolume/Volume[0]);
But :
var slippage = (High[0]-Low[0]) * 0.01m / (Volume[0]/orderVolume);
Alexandre Catarino
Hi Gmamuze Cht ,
Ok. For Futures (and Options), the Quantity refers to the number of contracts. 1 = 1 contract.
Gmamuze Cht
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!