Hi,
I have found the Dukascopy APIs really good to work with. However I have encountered a brick wall where I can figure out how to cancel a conditional order. I open the conditional order, it goes into the state "CREATED" and then I gen an "Order ACCEPTED" come through. However the state of the order doesnt change. Then after 10s delay I try to close the order and get an error as below:
10:43:42 com.dukascopy.api.JFException: state is CREATED @ com.dukascopy.api.impl.connect.PlatformOrderImpl.close(Unknown Source)
There is no issue when cancelling the order through the JForex console.
Here is my test code:
package jforex;
import com.dukascopy.api.*;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.IUserInterface;
import java.util.*;
import com.dukascopy.api.IEngine.OrderCommand;
public class EntryStrategy implements IStrategy {
private IEngine engine;
private IConsole console;
private IHistory history;
private IContext context;
private IIndicators indicators;
private IUserInterface userInterface;
List<IOrder> orderList = new ArrayList<>();
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
this.context = context;
this.indicators = context.getIndicators();
this.userInterface = context.getUserInterface();
Instrument instrument = Instrument.GBPJPY;
ITick lastTick = history.getLastTick(instrument);
double price = lastTick.getAsk() + instrument.getPipValue() * 5;
IOrder orderBuy = engine.submitOrder(getUniqueId(), instrument, OrderCommand.BUYSTOP, 0.1, price);
console.getOut().println("Order: "+orderBuy);
orderList.add(orderBuy);
price = lastTick.getAsk() - instrument.getPipValue() * 5;
IOrder orderSell = engine.submitOrder(getUniqueId(), instrument, OrderCommand.SELLSTOP, 0.1, price);
orderList.add(orderSell);
try {Thread.sleep(10000);} catch (InterruptedException e) {}
IOrder order = engine.getOrder(orderSell.getLabel());
console.getOut().println(order);
order.close();
}
public void onMessage(IMessage message) throws JFException {
IOrder order = message.getOrder();
if(order!=null) {
console.getOut().println("Order received: "+order);
}
}
public void onStop() throws JFException {}
public void onTick(Instrument instrument, ITick tick) throws JFException {}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
public void onAccount(IAccount account) throws JFException {}
private String getUniqueId() {
return "E"+UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
}
}