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.

Threading article example
 Post subject: Threading article example Post rating: 0   New post Posted: Thu 07 Nov, 2013, 12:44 

User rating: 2
Joined: Thu 07 Nov, 2013, 12:15
Posts: 121
Hi

Some of the code on the page https://www.dukascopy.com/wiki/#Threading is unhelpful.

As written, this code doesn't work if you pass in variable values. In the example you pass in hard-coded values, but this is hardly useful in real-life.

My understanding is that to work with parameters passed to the constructor you need to declare the class variables as final:

private final Instrument instrument;


You could have saved at least one Java newbie an hour of head-scratching if the example actually worked in real-life. Java threading is hard enough to get your head around without misleading examples!

Hope you find this useful.

private class BuyTask implements Callable<Object>{
        private Instrument instrument;
        private double stopLossPips;           
       
        public BuyTask(Instrument instrument, double stopLossPips){
                this.instrument = instrument;
                this.stopLossPips = stopLossPips;
}
                       
        public Object call() throws Exception {
                double stopLossPrice = history.getLastTick(this.instrument).getBid() - this.stopLossPips * this.instrument.getPipValue();
                return engine.submitOrder("Buy_order", this.instrument, OrderCommand.BUY, 0.001, 0, 20, stopLossPrice, 0);
        }
    }


 
 Post subject: Re: Docs bug - misleading code in Threading article Post rating: 0   New post Posted: Thu 07 Nov, 2013, 13:21 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
scotpip wrote:
As written, this code doesn't work if you pass in variable values.
Do you mean configurable parameters? The example does not contain any configurable parameters. If you modify the strategy code of course it may not work as described or as you expect.
scotpip wrote:
In the example you pass in hard-coded values, but this is hardly useful in real-life.
The examples are not written to be universal, rather the opposite - they are there to show the idea - the simpler the implementation the better.
scotpip wrote:
My understanding is that to work with parameters passed to the constructor you need to declare the class variables as final:
This is the case with anonymous classes and when the variable is defined in the respective method. The particular example has its Callable implemented in a separate class, i.e., BuyTask. Thus, this is not necessary in this case.

For further inquiries please provide full example strategy that you have come up to so far, describe what you expect the strategy to do and what it does instead (i.e., what you find being wrong). Also please provide specific launch conditions (if any).


 
 Post subject: Re: Docs bug - misleading code in Threading article Post rating: 0   New post Posted: Thu 07 Nov, 2013, 13:30 
User avatar

User rating: 96
Joined: Mon 09 Sep, 2013, 07:09
Posts: 287
Location: Ukraine, SHostka
scotpip wrote:
... you need to declare the class variables as final:
private final Instrument instrument;
A note: this is an instance variable, not a class variable.


 
 Post subject: Re: Docs bug - misleading code in Threading article Post rating: 0   New post Posted: Tue 12 Nov, 2013, 10:37 

User rating: 2
Joined: Thu 07 Nov, 2013, 12:15
Posts: 121
With the class as written in the docs:

private class BuyTask implements Callable<Object>{
        private Instrument instrument;
        private double stopLossPips;           
         
        public BuyTask(Instrument instrument, double stopLossPips){
                this.instrument = instrument;
                this.stopLossPips = stopLossPips;
}
                         
        public Object call() throws Exception {
                double stopLossPrice = history.getLastTick(this.instrument).getBid() - this.stopLossPips * this.instrument.getPipValue();
                return engine.submitOrder("Buy_order", this.instrument, OrderCommand.BUY, 0.001, 0, 20, stopLossPrice, 0);
        }
    }


This works:

BuyTask task = new BuyTask(Instrument.EURUSD, 40);
context.executeTask(task); 


But this doesn't:

Double pips = 40;
BuyTask task = new BuyTask(Instrument.EURUSD, pips);
context.executeTask(task); 


In other words, it only works if the parameter is hard coded, as I said.

To make the second call work, you need to rewrite the class as:

private class BuyTask implements Callable<Object>{
        private Instrument instrument;
        private final double stopLossPips;           
         
        public BuyTask(Instrument instrument, double stopLossPips){
                this.instrument = instrument;
                this.stopLossPips = stopLossPips;
}
                         
        public Object call() throws Exception {
                double stopLossPrice = history.getLastTick(this.instrument).getBid() - this.stopLossPips * this.instrument.getPipValue();
                return engine.submitOrder("Buy_order", this.instrument, OrderCommand.BUY, 0.001, 0, 20, stopLossPrice, 0);
        }
    }


I agree that you should be keeping your examples simple, but when it's too simple to work in any real-life situation, and is difficult to convert to real-life use without quite deep knowledge of Java idioms, I would still be suggesting that you have got the balance wrong here.


 
 Post subject: Re: Docs bug - misleading code in Threading article Post rating: 0   New post Posted: Tue 12 Nov, 2013, 11:26 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Quote:
This works:

BuyTask task = new BuyTask(Instrument.EURUSD, 40);
context.executeTask(task); 


But this doesn't:

Double pips = 40;
BuyTask task = new BuyTask(Instrument.EURUSD, pips);
context.executeTask(task); 

This does not even compile - it yields: "Type mismatch: cannot convert from int to Double". You can't assign a java.lang.Double a numeric value. Probably you wanted to use double instead of Double. For automatic assistance on compile-time errors, consider using some IDE, e.g., Eclipse or NetBeans.

Quote:
To make the second call work, you need to rewrite the class as:
private class BuyTask implements Callable<Object>{
        private Instrument instrument;
        private final double stopLossPips;           
         
        public BuyTask(Instrument instrument, double stopLossPips){
                this.instrument = instrument;
                this.stopLossPips = stopLossPips;
}
                         
        public Object call() throws Exception {
                double stopLossPrice = history.getLastTick(this.instrument).getBid() - this.stopLossPips * this.instrument.getPipValue();
                return engine.submitOrder("Buy_order", this.instrument, OrderCommand.BUY, 0.001, 0, 20, stopLossPrice, 0);
        }
    }
The variable being final or not does not make any difference in the behaviour of the program, see our attached program.

P.S. For further inquiries please always provide full example strategies. Otherwise we need to speculate if there are any parts of the strategy that cause the unexpected behaviour that have not been included in the snippets.


Attachments:
TestBuyTask.java [3.22 KiB]
Downloaded 100 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: Threading article example Post rating: 0   New post Posted: Tue 12 Nov, 2013, 12:37 

User rating: 2
Joined: Thu 07 Nov, 2013, 12:15
Posts: 121
Well, I'm a total beginner with Java so I could very well be misunderstanding something.

All I can say is that whenever I passed in a variable to the Callable constructor and placed it into an instance variable, call() couldn't see it unless the variable was final.

I fixed the problem with the advice on this thread in StackOverflow about how to pass values into a Callable class. Please note how all the replies use final.

https://stackoverflow.com/questions/9992992/is-there-a-way-to-take-an-argument-in-a-callable-method


 
 Post subject: Re: Threading article example Post rating: 0   New post Posted: Tue 12 Nov, 2013, 12:52 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
scotpip wrote:
All I can say is that whenever I passed in a variable to the Callable constructor and placed it into an instance variable, call() couldn't see it unless the variable was final.
Then how would you explain that our example given in the previous post does work without the field variable being final? Until you provide a full example strategy for the case that you report, we can't check where the actual problem was.


 

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