Dukascopy
 
 
Wiki JStore Search Login

JFOREX-1214: submitOrder in onStart -> incorrect Thread
 Post subject: JFOREX-1214: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Mon 19 Jan, 2009, 23:04 

User rating: 0
Joined: Wed 17 Dec, 2008, 11:19
Posts: 29
Hi,

public void onStart(IContext context) throws JFException {
  this.context = context;
  engine = context.getEngine();
  engine.submitOrder ( "test1", Instrument.fromString( "EUR/USD" ), IEngine.OrderCommand.BUY, 0.25, 0 );
}

returns this error:
com.dukascopy.api.JFException: Incorrect thread @ hello.strategy.onStart(strategy.java:226)

How to submit/close,etc. orders out of onTick and onBar events ?

Thanks


 
 Post subject: Re: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Tue 20 Jan, 2009, 09:32 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
please use IContext.executeTask(Callable task). It will run task in strategy thread.
    Future<IOrder> future = context.executeTask(new Callable<IOrder>() {
        public IOrder call() {
               //your logic here, that will be executed in strategy thread
               return engine.submitOrder(...);
        }
    });
    //call the get method if you want to wait for the result, or skip it to run taks asynchronously
    IOrder result = future.get();


 
 Post subject: Re: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Wed 21 Jan, 2009, 05:31 

User rating: 0
Joined: Wed 17 Dec, 2008, 11:19
Posts: 29
thanks, but when I compile

    Future<IOrder> future = context.executeTask(new Callable<IOrder>() {
        public IOrder call() {
               return engine.submitOrder("test1", Instrument.fromString("EUR/CAD"), IEngine.OrderCommand. BUY, 0.1);
        }
    });

the error message is: Cannot instantiate target class

Thank for your help


 
 Post subject: Re: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Thu 22 Jan, 2009, 10:30 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
change the code to
       Future<IOrder> future = context.executeTask(new Callable<IOrder>() {
        public IOrder call() {
           try {
               return engine.submitOrder("test1", Instrument.fromString("EUR/CAD"), IEngine.OrderCommand. BUY, 0.1);
            } catch (JFException e) {
               console.getErr().println(e.getMessage());
               return null;
            }
        }
    });


it looks that "Cannot instantiate target class" is not related to this code, check that you didn't write some constructor with parameter in your strategy, also check that you don't have some static variable that could cause the problem when instantiating the class.


 
 Post subject: Re: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Wed 10 Jun, 2009, 14:46 
User avatar

User rating: 2
Joined: Mon 30 May, 2011, 15:41
Posts: 86
Location: Czech Republic, Prague
Hello,

your code is functional only when I don't access IOrder result
otherwise strategy freeze and I must restart JForex application.

I call method future.get()
There is probably some threads conflict, maybe I must first leave my thread.
How can I obtaint IOrder result from executeTask?

Thank you
Jiri


 
 Post subject: Re: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Fri 12 Jun, 2009, 08:40 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
This code should work well until you have initiate call from strategy thread and got deadlock.
We need to see all picture to provide any suggestion. How this function called, from which thread, which monitors locked at this moment ... etc.


 
 Post subject: Re: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Fri 12 Jun, 2009, 09:29 
User avatar

User rating: 2
Joined: Mon 30 May, 2011, 15:41
Posts: 86
Location: Czech Republic, Prague
Hello, thank you for reply.
Your example is not functionail as explained here - in onStart function.
Order is executed only when you don't need IOrder result and do not use future object.
Generally, after call any future function, I can't compile, stop or run strategy more.

public void onStart(IContext context) throws JFException {
        this.context = context;
        this.engine = context.getEngine();
        try {
            Future<IOrder> future = this.context.executeTask(new Callable<IOrder>() {
                public IOrder call() {
                    try {
                        return engine.submitOrder("test1", Instrument.fromString("EUR/CAD"), IEngine.OrderCommand. BUY, 0.1);
                    } catch(Exception e) {
                        console.getErr().println( "Order exception: " + e.toString() );
                        return null;
                    }
                }
            });
            IOrder result = future.get();
        } catch(Exception e) {
            console.getErr().println( "Future exception: " + e.toString() );
        }
}


 
 Post subject: Re: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Mon 15 Jun, 2009, 19:01 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Just understand we talking about onStart function :)

You can not send orders from this function in any conditions,
we will investigate hang , this must be exception without locks for sure.


 
 Post subject: Instantiating Visual Class in onStart Post rating: 0   New post Posted: Sat 03 Oct, 2009, 06:59 

User rating: 0
Joined: Sat 08 Mar, 2008, 03:10
Posts: 18
Firstly, fantastic work on the stand-alone API library!

I have a question about instantiating a visual class from onStart. The idea is to implement a nicer and more flexible dialogue rather than using 'Configurable', but I can't get this to work. Instead, the platform just freezes completely. In this example my GUI is named TestGui and contains a couple of combo boxes etc.

try {
            Future<TestGui> future = this.context.executeTask(new Callable<TestGui>() {
                public TestGui call() {
                    try {
                       TestGui testgui = new TestGui();
             testgui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             testgui.setVisible(true);
                        return testgui;
                    } catch(Exception e) {
                        print("TestGui Exception: " + e.toString() );
                        return null;
                    }
                }
            });
            TestGui testgui = future.get();
} catch(Exception e) {
            print( "Future exception: " + e.toString() );
}


No dice. It compiles OK - I've put the visual class in a jar file that is included by @Library(....) - but the future.get() seems to cause problems straight away - nothing within the try{ } clause is reached. The Eclipse console also doesn't tell me anything when I use the stand-alone library; it just never gets past a certain point. Can you tell me what's going on here? Or can you suggest another way of implementing the GUI?

Thanks


 
 Post subject: Some progress with invoking Visual Class Post rating: 0   New post Posted: Sat 03 Oct, 2009, 16:57 

User rating: 0
Joined: Sat 08 Mar, 2008, 03:10
Posts: 18
I've managed to get a bit further, but my solution is not a very good one: Just calling the visual class directly from onStart without using context.executeTask. I can't figure out how to force the strategy thread to wait for the user to be done with the GUI without tying up all the system resources, so instead onTick checks to see if the user is done with the GUI and then initializes the user-defined variables. This is not a good solution for two reasons: 1) Initialization should really be done in onStart, and 2) when using the historical tester, a lot of the historical testing period might have already passed by the time the user is done with the GUI and onTick is finally able to function as intended.

There must be a way to force the strategy thread in onStart to wait, though I haven't had much luck with synchronization and wait() + notify(). Or is it really better to work through the context.executeTask issue?

Thanks


 
 Post subject: Re: Future to close open trade hangs in onStop Post rating: 0   New post Posted: Sun 04 Oct, 2009, 12:45 
User avatar

User rating: 3
Joined: Wed 18 May, 2011, 16:25
Posts: 331
Location: SwitzerlandSwitzerland
I'm having a similiar problem with using Future calls in onStop() to close open trades.
After calling the first context.executeTask that basically only includes a order.close()
(with a prior check if order is in state created/opened/filled),
the strategy will hang, regardless of wether future.get() is called or not.

The only way then is to re-login to the platform.

Any ideas?
Thanks and best, RR.


 
 Post subject: Re: JFOREX-1214: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Mon 05 Oct, 2009, 11:08 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
1. Original issue of this thread about calling submitOrder from onStart method is fixed. Now onStart method is run in strategy thread. That was changed in version 1.8.1 and it seems it's breaking old functionality. We will change it so that when you call executeTask from strategy thread it will be executed in the call itself
2. What actually happens. When you call Future.get() it blocks current thread until Callable is executed. That means if you call Future.get from strategy thread it will wait for Callable execution that can be executed only in strategy thread. Strategy thread waiting for itself.
3. Same problem with onStop, but it was always called in strategy thread, you shouldn't use executeTask from that method.
4. About GUI dialog. Swing classes should be accessed only from event dispatching thread. If you use executeTask, you are breaking this rule. Same if you are calling swing from onStart without executeTask, you are breaking this rule. Correct use should be using SwingUtilities.invokeAndWait. Unfortunately it's also not possible now, because event dispatching thread is waiting for strategy to start. We will address this issue ASAP


 
 Post subject: Re: JFOREX-1214: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Mon 05 Oct, 2009, 11:18 
User avatar

User rating: 3
Joined: Wed 18 May, 2011, 16:25
Posts: 331
Location: SwitzerlandSwitzerland
That brings some light in here.
Thanks a lot for the clarifications!

Best, RR.


 
 Post subject: Re: JFOREX-1214: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Mon 05 Oct, 2009, 11:24 
User avatar

User rating: 3
Joined: Wed 18 May, 2011, 16:25
Posts: 331
Location: SwitzerlandSwitzerland
What is your recommended method to check for and close any FILLED, CREATED and OPENED orders in onStop()?

Thanks, RR.


 
 Post subject: Re: JFOREX-1214: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Mon 05 Oct, 2009, 13:45 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Get a list of orders with IEngine.getOrders method and close them all in for cycle. Maybe add waitforupdate to see if close was rejected, if you need it


 
 Post subject: Re: JFOREX-1214: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Mon 05 Oct, 2009, 14:46 
User avatar

User rating: 3
Joined: Wed 18 May, 2011, 16:25
Posts: 331
Location: SwitzerlandSwitzerland
I thought .submitorder() and order.close() were only valid in onTick() and onBar().
So oder.close() is (now?) fully supported in onStop()?

Thx, RR.


 
 Post subject: Re: JFOREX-1214: submitOrder in onStart -> incorrect Thread Post rating: 0   New post Posted: Tue 06 Oct, 2009, 15:26 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
submitOrder and order.close are only valid when called from strategy thread to avoid synchronization problems. onStop same as onTick onBar and onActionInfo was always called in strategy thread. Now also onStart is called in strategy thread, so it's true to say that all methods defined in IStrategy interface are called in strategy thread


 
 Post subject: Event Dispatching Thread Post rating: 0   New post Posted: Thu 22 Oct, 2009, 15:14 

User rating: 0
Joined: Sat 08 Mar, 2008, 03:10
Posts: 18
Thanks for the information regarding correct usage of threading. As soon as you have a workaround that will enable us to use the event dispatching thread from OnStart (or anywhere else), please advise.

Thanks again!


 

Jump to:  

  © 1998-2024 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