The API Javadocs (
IContext.executeTask) states:
Dukascopy wrote:
Every strategy executes in it's own thread. This will ensure single threaded model: Any handle method of IStrategy will be executed in order.
But the next code prove a different statement: the onMessage method is called from within the onTick method. A such behaviour is unexpected while API docs promise that the onMessage method will be executed AFTER the onTick method has exited. To see details run the code and look into it's output.
package jforex;
import com.dukascopy.api.*;
import java.io.PrintStream;
import java.util.EnumSet;
public final class TestThreading implements IStrategy {
private final Instrument instrument = Instrument.EURUSD;
private PrintStream out;
private IEngine engine;
private IOrder position;
private boolean start;
@Override
public void onStart(IContext context) throws JFException {
out = context.getConsole().getOut();
engine = context.getEngine();
context.setSubscribedInstruments(EnumSet.of(instrument), true);
start = true;
}
@Override
public void onTick(final Instrument instrument, ITick tick) throws JFException {
if (instrument != this.instrument) return;
if (start) {
for (StackTraceElement e : Thread.currentThread().getStackTrace()) out.println(e);
out.println("The 1st message");
position = engine.submitOrder("label", instrument, IEngine.OrderCommand.BUY, 0.001);
out.println("The 2nd message");
position.waitForUpdate(IOrder.State.FILLED, IOrder.State.CANCELED);
out.println("The 3rd message");
}
}
@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
@Override
public void onMessage(IMessage message) throws JFException {
if (message.getOrder() == null || message.getType() == IMessage.Type.NOTIFICATION) return;
if (message.getType() == IMessage.Type.ORDER_FILL_OK) {
start = false;
out.println("The 4th message");
for (StackTraceElement e : Thread.currentThread().getStackTrace()) out.println(e);
out.println("The 5th message");
}
}
@Override
public void onAccount(IAccount account) throws JFException {}
@Override
public void onStop() throws JFException { position.close(); }
}