Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Cancelling Conditional Orders
 Post subject: Cancelling Conditional Orders Post rating: 0   New post Posted: Wed 18 Jun, 2014, 11:49 
User avatar

User rating: 0
Joined: Wed 18 Jun, 2014, 11:34
Posts: 3
Location: United Kingdom,
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("-", "");
    }
}


Attachments:
EntryStrategy.java [2.58 KiB]
Downloaded 89 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: Cancelling Conditional Orders Post rating: 0   New post Posted: Thu 19 Jun, 2014, 12:29 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You have to manage order state, either by using IOrder.waitForUpdate or IStartegy.onMessage, see more:
https://www.dukascopy.com/wiki/#Manage_Order_State
Regarding cancelling orders see:
https://www.dukascopy.com/wiki/#Close_Orders/Order_close_and_cancel_workflow
Note that you have to make sure the instrument is subscribed, see the IContext.setSubscribedInstruments method with the parameter lock=true.


 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com