Rounding prices
There are certain values in JForex-API which have minimum increments, like stop loss and take profit prices which have 0.1 pip minimum increments. Consider the following price rounding approach:
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.history = context.getHistory();
ITick tick = history.getLastTick(instrument);
double slPrice = tick.getBid() - 0.0010001;
engine.submitOrder("marketOrder", instrument, OrderCommand.BUY, 1, 0, 10, roundToPippette(slPrice, instrument), 0);
}
private static double roundToPippette(double amount, Instrument instrument) {
return round(amount, instrument.getPipScale() + 1);
}
private static double round(double amount, int decimalPlaces) {
return (new BigDecimal(amount)).setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP).doubleValue();
}
Replace the roundToPippette(slPrice, instrument) call with slPrice to see the necessity of the rounding in this case.
The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.