Calculating bid/ask spread accurately & efficiently
Posted: Nov 21 2013
How does one calculate the bid/ask spread in an efficient and accurate manner?
Bars.StatusLine.Ask - Bars.StatusLine.Bid gives the bid/ask spread, but:
Can MultiCharts .NET do this?
Bars.StatusLine.Ask - Bars.StatusLine.Bid gives the bid/ask spread, but:
- CalcBar() is only evaluated on a incoming tick. Even with a chart set to Bid prices, the bid/ask spread calculation will not be accurate (e.g., when the ask changes but bid remains the same).
- ExecControl.RecalcLastBarAfter() provides the option to recalculate CalcBar() on a fixed time interval, but
(a) this is not efficient to do this a couple of times per second when the instrument is not that actively trading (e.g., outside RTH), and
(b) there is still the chance that bid/ask updates are missed if the TimeSpan is not low enough.
Code: Select all
protected override void OnMarketData(MarketDataEventArgs e)
{
// Print some data to the Output window
if (e.MarketDataType == MarketDataType.Last)
Print("Last = " + e.Price + " " + e.Volume);
else if (e.MarketDataType == MarketDataType.Ask)
Print("Ask = " + e.Price + " " + e.Volume);
else if (e.MarketDataType == MarketDataType.Bid)
Print("Bid = " + e.Price + " " + e.Volume);
}