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.

ExecuteTask() and manage orders in a different thread
 Post subject: ExecuteTask() and manage orders in a different thread Post rating: 0   New post Posted: Fri 10 Apr, 2009, 10:07 

User rating: 0
Joined: Wed 06 Feb, 2008, 11:14
Posts: 7
Hello,

Each time I launch an order ( with IEngine.submitOrder() )I want to limit the time the resulted position will be in place.

That means that if neither the TP or SL is reached after a certain amount of time (let's call it WaitingTimeBeforeClosing ) the position should be closed.

To achieve that, I use a timer which is created just after the order is created.

Because the timer is running in another thread, I've been told to use IContext.executeTask() to achieve that.

My problem is that I don't know how to use this method.

You can see the very simple strategy's code trying to do that :
Attachment:
testCloseOrderWithTimer.java [2.1 KiB]
Downloaded 548 times

Thank you very much.

Karim


 
 Post subject: Re: ExecuteTask() and manage orders in a different thread Post rating: 0   New post Posted: Fri 10 Apr, 2009, 17:38 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
"It's better to use timer as a strategy class level variable, so that it would be possible to reuse it and cancel when strategy stops. See TestTimerTask example."


Attachments:
TestTimerTask.java [3.02 KiB]
Downloaded 588 times
testCloseOrderWithTimerFIXED.java [3.19 KiB]
Downloaded 577 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: ExecuteTask() and manage orders in a different thread Post rating: 0   New post Posted: Fri 10 Apr, 2009, 19:17 

User rating: 0
Joined: Wed 17 Dec, 2008, 11:19
Posts: 29
Beware using ExecuteTask and Timer: I had a lot of "Thread Queue Overflow" errors. I removed all that from my code, which works fine now.


 
 Post subject: Re: ExecuteTask() and manage orders in a different thread Post rating: 0   New post Posted: Mon 13 Apr, 2009, 18:32 

User rating: 0
Joined: Wed 06 Feb, 2008, 11:14
Posts: 7
Thank you Pavel, your code works fine I tested it only on few trades though.

Bilbao thank you for your warning, but it seems I've read that these "Thread Queue Overflow" errors didn't exist anymore...

Anyway, these ways to manage positions (same thing for placing orders whithout SL or TP and seting them after the order is created) cannot be used as long as the platform is not 100 % stable (last crash I got was a "Network Failure" crash).

Pavel I understand that it's better using a class level timer, but if the strategy crashes (in that case the strategy doesn't execute onStop()) the timer will keep in place (and I don't know how to delete it, if so). But is it much more costy to create and delete timers when needed instead of using the same several times and deleting it at the end ?

Thank you


 
 Post subject: Re: ExecuteTask() and manage orders in a different thread Post rating: 0   New post Posted: Thu 23 Apr, 2009, 15:36 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Using timers in strategy is not a best practice. This complicate the code, make more potential problems , and you can't test your strategy in historical tester with this.
Best way to solve this would be using onBar method which is kind of sequence with guaranteed calling even if there were no ticks.
You can check open time of order with incoming bar time (10 second bars to make precise result), and if it reach required time , make a close.


 
 Post subject: Re: ExecuteTask() and manage orders in a different thread Post rating: 0   New post Posted: Fri 24 Apr, 2009, 10:19 

User rating: 0
Joined: Wed 06 Feb, 2008, 11:14
Posts: 7
Let me give you an example : I want to close my order after n hours if it still exist (n could vary according to the context).

In practice, if I use the 10s period in onBar(), that would result in n*360 requests about the presence of this order in my list of order. And among all these requests only one will lead to an action (closing the order).

If I do so for all my orders I'm not sure but I think that would consume too much ressources for a single action I know already when it should occur.

Now, with the current API, it's possible to specify the goodTillTime parameter for PlaceBid and PlaceOffer orders.

That would be very useful If that was possible to do so also for the other types of orders, and that would avoid timers and "most-of-the-time-Requesting-for-doing-nothing".

Thank you.


 
 Post subject: Re: ExecuteTask() and manage orders in a different thread Post rating: 0   New post Posted: Mon 27 Apr, 2009, 13:24 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Getting time of the order doesn't result in request to the server, actually it's just a "getter" method that returns variable. Comparing two longs and maybe some simple math operations is nothing for today processors, especially when it's done with 10 seconds intervals.


 
 Post subject: Re: ExecuteTask() and manage orders in a different thread Post rating: 0   New post Posted: Tue 28 Apr, 2009, 00:31 

User rating: -
you mean IOrder.getCreationTime()?

but a question about the "different threads" comment
is each ontick method opening it's own thread ?
as far as i can tell (tested by sleeping the methods). all ticks are processed by onTick() in the same thread. e.g. if at the beginning of the
onTick i have a 5 second sleep cycle (notifying me of sleeping and the waking) all of the onTick calls are made in series, and if i put one of them to sleep , it stops the pipeline.

is that not so ?

why then did the OP refer to the execute task ?


 
 Post subject: Re: ExecuteTask() and manage orders in a different thread Post rating: 0   New post Posted: Tue 28 Apr, 2009, 07:56 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
spider wrote:
you mean IOrder.getCreationTime()?

Yes, getCreationTime or IOrder.getFillTime()
spider wrote:
is each ontick method opening it's own thread ?
as far as i can tell (tested by sleeping the methods). all ticks are processed by onTick() in the same thread. e.g. if at the beginning of the
onTick i have a 5 second sleep cycle (notifying me of sleeping and the waking) all of the onTick calls are made in series, and if i put one of them to sleep , it stops the pipeline.

No, all ticks are processed in one thread, and yes, if you put sleep, then this thread will be blocked.
spider wrote:
execute task

Timer runs in it's own thread, but our API requires you to do all operations with orders in main strategy thread. That's why you have to use executeTask method to tell strategy thread to execute the task passed in the parameter. If you don't use timer, but add a check in onBar method, then you don't need to use executeTask, because onBar method already is called in main strategy thread. For example, to close order after one hour you can use the following code:
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) {
    if (instrument == Instrument.EURUSD && period == Period.TEN_SECS) { //should be subscribed to EURUSD
        IOrder order = engine.getOrder("some label"); //or some other way to find you order
        if (order.getFillTime() + 60 * 60 * 1000 < askBar.getTime()) {
            order.close();
        }
    }
}


 

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