Use onTick(...) to monitor the Bid / Ask spread and maintain the lastInsideBidPrice
and lastInsideAskPrice as member variables in the IStrategy object instance.
Then, check them in onBar(...) to make your "bar" type decisions, part of
which is calculating the spread. Remember the spread (in pips) is calculated
as
double lastInsideAskPrice = 0;
double lastInsideBidPrice = 0;
void onTick(Instrument instrument, ITick tick) {
//...
this.lastInsideAskPrice = tick.getAsk(); // member variable
this.lastInsideBidPrice = tick.getBid();
//.....
}
// and in onBar(...) do this
double absInstantaneousSpreadInPips = Math.abs( (this.lastInsideAskPrice-this.lastInsideBidPrice) / instrument.getPipValue() );
// 'this' is being used just to emphasize that it is a member variable
//
It you want to be more sophisticated, then your onTick(...) processing can maintain
some "average inter-bar bid/ask spread" which develops while your bar is being formed. You can do
just about anything you want, with some creativity
onBar(...) is called only at the
completion of a bar, but onTick(...) is called on every price change, so don't do
any lengthy calculations.......
If your goal is to trade only when the "average spread" is "narrow enough", then just
checking the momentary spread, at the instant when onBar completes might not
be "robust" enough for your needs. In that case, your spread monitoring needs
to be more stable and robust, but don't forget to normalize it to PIPs, as shown.
HyperScalper