Back

A custom ISlippageModel

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?
Update Backtest








Actually, I have it figured out.

Turns out I was setting my SetBrokerageModel earlier in Initialization:

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);
0

Can anyone tell me if this looks about right? I picked up the formula from the url below. Also, I'm not sure I'm returning the slippage right in it's positive or negative context to Buy or Sell. The original SlippageModel was ABS(Price - ask or bid), but in all cases for me Price is 0 so I can assume slippage should always be positive regardless of buying or selling.

Also, according to the URL, the first part of the formula is (slippage in seconds / bar width in seconds), but I'm not quite sure how to translate this.

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;
}
0

A positive slip indicates a slip that will negatively affect the trade. For example, a slip of a penny on a buy will give you a higher buy price, but a penny slip on a sell will give you a lower sell price. A negative slip would indicate a slip in your favor.
0

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.


Adding to Michael's negative/positive slippage explanation: The potential slippage as you calculate in the case high-open in my opinion can lead to wrong result: e.g. if SPY goes up 2% for the day, when using daily bar, the above calculation will return huge slippage not typical for SPY.

In my tests, I assume that slippage is a percent of the close (current Price) of the bar and is different for each security.

From internet I get the slippage percent of each security I want to test (for etf go to etf.com)
Then pass it to the algorithm as you do in your example.


public class MyTransactionModel : SecurityTransactionModel
{
//private QCAlgorithm algo = null;
private decimal SlippagePercent {get;set;}

public MyTransactionModel( decimal slippagePercent, QCAlgorithm a)
{
SlippagePercent = slippagePercent;
//algo = a;
}

public override decimal GetSlippageApproximation(Security asset, Order order)
{
decimal price = order.GetValue(order.Price)/order.Quantity;
decimal slippage = price * SlippagePercent/100m;
//algo.Debug(algo.Time.ToString() + " Slippage =" + SlippagePercent.ToString() + " no slippage price = " + price + ", slippage = " + slippage);
return slippage;
}
} // class
1

@nik, this was probably the best thing I've heard all year!! One problem though. ETF.com only has a handful of stocks. No AAPL, MSFT, and many of the stocks I'm tracking. Do i need to subscribe or something to see more? I've exhausted my google-fu trying to find other screeners that provide this info. Any suggestions?
0

Found something that works in the interim. Still very very interested in what Nik has to say, but this is what I found after an epic search-fest.

TDAmeritrade offers spread info for SPY, but not MSFT (like eft.com) so I'm assuming spread info only pertains to EFTs? How frustrating.

The ONLY way to derive spread info I've found is the following. Query Yahoo's API with the following URL & paramters:

1. Url: http://finance.yahoo.com/d/quotes.csv?s=MSFT+AAPL+BAC&f=sd1pab
s = symbol, d1 = date, p = previous close, a = ask, b = bid
2. Open the download csv in excel.
3. Insert the following formula into cell F1: =(D1-E1)/C1

MSFT 1/12/2016 52.3 52.29 52.28 0.00019120
AAPL 1/12/2016 98.53 99.34 99.32 0.00020298
BAC 1/12/2016 15.31 15.16 15.15 0.00065317


Low and behold, the spread for .. what exactly I'm not sure. The last quote, yesterday's last quote, an average of the yesterday's quote. I don't know, but I do know it's something and that's so much better than nothing. A 6 month average would still be phenomenal, like what eft.com and tdameritrade offer.

If you're interested in trying to figure it out the api params, here's some info: https://greenido.wordpress.com/2009/12/22/yahoo-finance-hidden-api/
0

@Levi, searched google for "apple current bid price" and stumbled on this site. For liquid stocks the spread is pretty small. They claim its delayed but looks up to date to me.

Share Price 99.50 Bid Price 99.50 Ask Price 99.51
0

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.


@Jared, That's cool and all. But what I really need to know is the average ask/bid spread, over a period of time. I know its simple enough to plug the current ask/bid into a calculator and get the spread: (ask-bid)/close. But the current spread could be smaller or larger than normal. ETF.com was showing me an average over 45 days which is far more reliable. Plus I'm monitoring 40 some odd securities updated daily it's just painful to manually calculate spot ask/bid prices to build up a history of averages for each security. Something like etf.com's Average Spread(right column towards the bottom: Average Spread (%)) is freaking money. A real shame it doesn't track all stocks.
0

Some months ago, I read an article how to calculate bid/ask from the price. I need to find it out. As far as I recall it claimed it achieved good results.
Not sure if you are interested in this though...
0

Here's an example setting the various models (Fill/Fee/Slippage).

I've also modified that code that updates the models automatically based on brokerage model, this should work in live, where if you do set a brokerage model, but also set custom Fill/Fee/Slippage models then they should be respected. Let me know if you have further issues on master!
0

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.


Update Backtest





0

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.


Loading...

This discussion is closed