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.

Label not unique
 Post subject: Label not unique Post rating: 0   New post Posted: Tue 10 May, 2011, 13:23 

User rating: -
Hello,

I noticed that, in certain conditions, I can introduce more than one order with the same Label.

I am running the strategy on a demo account through Java API (not JForex platform) in TEN_SECS period. I have two instances of the same strategy running, trying to introduce an order each new bar, with same Label. Almost every time both strategies are able to introduce the order, and I got a couple of orders with the same label.

Sometimes I get an "Order null rejected Order already being processed by the system." error, which I can't catch or treat in any way.

Thanks


 
 Post subject: Re: Label not unique Post rating: 0   New post Posted: Tue 24 May, 2011, 12:01 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Could you please provide an example code and a launch scenario for the case when when you manage to create two orders with the same label?
In a case you're only concerned about making your labels unique, you might consider the following method which ensures the label uniqueness between both of the strategies:
private String getLabel(String prefix){
        return prefix + UUID.randomUUID().toString().replace('-', '0');
}


 
 Post subject: Re: Label not unique Post rating: 0   New post Posted: Wed 25 May, 2011, 09:10 

User rating: -
Support wrote:
Could you please provide an example code and a launch scenario for the case when when you manage to create two orders with the same label?
In a case you're only concerned about making your labels unique, you might consider the following method which ensures the label uniqueness between both of the strategies:
private String getLabel(String prefix){
        return prefix + UUID.randomUUID().toString().replace('-', '0');
}


For instance, the attached code. It works when runned twice through the jforex platform: One of the instances introduces the order, and the other throw the following exception:

Quote:
07:48:38 com.dukascopy.api.JFException: Label not unique(code 2). (Order already exists) [DLUSDJPY20110525_094830]:label=DLUSDJPY20110525_094830;getId()=null;groupId=null;openingOrderId=null;pendingOrderId=null;parentOrderId=null;tpOrderId=null;slOrderId=null;state=CREATED;instrument=USD/JPY;openPrice=82.151;requestedAmount=0.01;amount=0.01;lastServerRequest=SUBMIT;awaitingResubmit=false;localCreationTime=1306309718116;


But when I run the strategy two times at the same time through Java API, I got no exception, and two orders are introduced with same label. See the screenshot:

https://postimage.org/image/1n0uysfb8/

Labels DLEURUSD20110525_095850 and DLEURUSD20110525_095910 are duplicated. Label DLEURUSD20110525_095900 is not, because that time it worked fine: one of the instances detected the duplicated label and throw the exception.

Source code:
package jforex;

import java.text.SimpleDateFormat;

import com.dukascopy.api.*;

public class DuplicatedLabel implements IStrategy {
    private IEngine engine;
    private IConsole console;
   
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    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 {
        if (period!=Period.TEN_SECS) return;
       
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String label = "DL"+instrument.toString().replaceAll("/", "")+sdf.format(bidBar.getTime());
        console.getOut().println("Introducing order BUY "+instrument.toString()+" with label "+label);
        try {
            engine.submitOrder(label, instrument, IEngine.OrderCommand.BUY, 0.01);
        } catch (Exception e) {
            console.getErr().println(e.toString());
        }
    }
}


 
 Post subject: Re: Label not unique Post rating: 0   New post Posted: Wed 25 May, 2011, 13:15 

User rating: -
I am not concerned about making my labels unique, but rather I do not want to duplicate orders if I launch more than one instance of my strategy in different computers (I may do that to ensure order execution if one of them is offline).


 
 Post subject: Re: Label not unique Post rating: 0   New post Posted: Wed 01 Jun, 2011, 08:28 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Current implementation doesn't check label uniqueness on server side, meaning that label uniqueness is ensured only within a process which runs the strategy.
You can ensure label uniqueness by putting as label's prefix the process name or pc name (in case if you never run your strategy in two different processes on the same pc). For example:
   String pcName = InetAddress.getLocalHost().getHostName();
   String processName = ManagementFactory.getRuntimeMXBean().getName();


 
 Post subject: Re: Label not unique Post rating: 0   New post Posted: Thu 02 Jun, 2011, 09:59 

User rating: -
Thank you for your answer, however my problem is exactly the opposite: I don't want to avoid duplicated label, but to avoid duplicated orders.

What I want to do is to have two servers running one strategy. So when I insert an order, I want just one order, not two of them with different labels. If labels were unique, it wouldn't be a problem: the fastest server inserts the order and the slower one gets an error.

Is there any way I could do this without forcing one server to wait some random time to ensure the other one is not running?

Thank you very much,


 
 Post subject: Re: Label not unique Post rating: 0   New post Posted: Wed 08 Jun, 2011, 11:50 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Guest wrote:
Is there any way I could do this without forcing one server to wait some random time to ensure the other one is not running?
No you can't.

Current JForex API implementation doesn't support communication between IClient or IStrategy instances that are located in separate processes. If you wish to check labels of orders from another process you might, for instance, consider using a web service. In this case before a submission of any order you would need to wait for the response from the service to arrive.


 

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