Hey, I'm trying to create a custom slippage model to to esitmate the slippage when all have is a TradeBar (Second or Minute resolution).
After looking around and looking at the code, it seems like all I have to do is create my model,
public class EstimatedSlippageModel : ISlippageModel
{
public decimal GetSlippageApproximation(Security asset, Order order)
{
....
}
}
And then add it to the security:
Securities[equity.Symbol].SlippageModel = new EstimatedSlippageModel();
But it doesn't look like this is working. When Buying, it's still calling into the SpreadSlippageModel which is being supplied through the default ISecurityTransactionModel.
So I create a custom ISecurityTransactionModel,
public class MySecurityTransactionModel : SecurityTransactionModel
{
private ISlippageModel SlippageModel { get; set; }
public MySecurityTransactionModel(ISlippageModel slippageModel)
{
this.SlippageModel = slippageModel;
}
public override decimal GetSlippageApproximation(Security security, Order order)
{
return this.SlippageModel.GetSlippageApproximation(security, order);
}
}
And then assigned it to the Security's model,
Securities[equity.Symbol].Model = new MySecurityTransactionModel(new EstimatedSlippageModel());
But this is still not working. The default SpreadSlippageModel is being ran through the default SecurityTransactionModel.
Any suggestions?
Levitikon
this.SetBrokerageModel(BrokerageName.TradierBrokerage, AccountType.Margin);
I suppose the two don't mix well. So, the final solution, create a custom BrokerageModel,public class MyBrokerageModel : TradierBrokerageModel { private ISecurityTransactionModel SecurityTransactionModel { get; set; } public MyBrokerageModel(ISecurityTransactionModel securityTransactionModel) { this.SecurityTransactionModel = securityTransactionModel; } public override ISecurityTransactionModel GetTransactionModel(Security security) { return this.SecurityTransactionModel; } }
And then set it. Works like a charm.this.SetBrokerageModel(new MyBrokerageModel(new MySecurityTransactionModel(new EstimatedSlippageModel())), AccountType.Margin);
Levitikon
public decimal GetSlippageApproximation(Security asset, Order order) { //var lastData = asset.GetLastData(); var tradeBar = asset.GetLastData() as TradeBar; var potentialSlippage = CalculatePotentialSlippage(tradeBar); this.Algorithm.Debug(string.Format("PotentialSlippage: {0:c6}", potentialSlippage)); return potentialSlippage; } ///
/// http://www.elitetrader.com/et/index.php?threads/how-do-you-emulate-slippage-in-a-backtest.247865/
///
private decimal CalculatePotentialSlippage(TradeBar tradeBar)
{
var potentialSlippage = 0m;
if (tradeBar.Close < tradeBar.Open)
{
potentialSlippage = tradeBar.Low - tradeBar.Open;
}
else
{
potentialSlippage = tradeBar.High - tradeBar.Open;
}
return potentialSlippage;
}
Michael Handschuh
Nik milev
Levitikon
Levitikon
Jared Broad
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.
Levitikon
Nik milev
Michael Handschuh
Levitikon
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!