Consider the following example strategy
package jforex.strategies.oneorder;
import java.text.DecimalFormat;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
/**
* The example strategy creates an order on every bar if the
* bar height does not exceed barMaxPips
*
*/
@RequiresFullAccess
public class OrderDependOnPrevBarSize implements IStrategy {
@Configurable("Instrument")
public Instrument instrument = Instrument.EURUSD;
@Configurable("Period")
public Period period = Period.TEN_SECS;
@Configurable("Bar max height in pips")
public int barMaxPips = 1;
private IEngine engine;
private IConsole console;
private int counter;
private DecimalFormat df = new DecimalFormat("0.00000");
@Override
public void onStart(IContext context) throws JFException {
this.console = context.getConsole();
this.engine = context.getEngine();
}
@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {}
@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if(instrument != this.instrument || period != this.period){
return;
}
double barHeight = Math.abs(bidBar.getOpen() - bidBar.getClose());
//create order if the candle height is less than barMaxPips pips
boolean createOrder = barHeight < barMaxPips * instrument.getPipValue();
if(createOrder){
engine.submitOrder("order" + ++counter, instrument, OrderCommand.BUY, 0.001);
}
print("Candle height in pips: " + df.format(barHeight / instrument.getPipValue()) + " create order? " + (createOrder? "YES!":"NO!"));
}
private void print(Object o){
console.getOut().println(o);
}
@Override
public void onMessage(IMessage message) throws JFException {
//print order related messages
if(message.getOrder() != null){
print(message);
}
}
@Override
public void onAccount(IAccount account) throws JFException {}
@Override
public void onStop() throws JFException {
//close all orders on strategy stop
for(IOrder o: engine.getOrders()){
o.close();
}
}
}