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.

IStrategy callback thread detection
 Post subject: IStrategy callback thread detection Post rating: 0   New post Posted: Thu 04 Apr, 2013, 21:11 
User avatar

User rating: 98
Joined: Mon 23 Jul, 2012, 02:02
Posts: 656
Location: United States, Durham, NC
In an IStrategy interface, there are defined "callbacks", such as onTick, onBar, etc which are executed within a special thread context, so that Order operations are valid. This guarantees serial execution of the operations, and the integrity of the API.

For Swing, there is a call to identify whether code is on the event dispatch thread.
SwingUtilities.isEventDispatchThread(), identifies whether the current thread is the Swing Event Dispatch Thread. This allows code to determine whether it can perform Swing operations, or whether it must use SwingUtilities.invokeLater(<runnable>) to perform Swing operations.

JForex API :
In a similar fashion, when code is executing on an IStrategy callback, such as void onTick(...) then it is OK to perform Order Entry API operations.

But, if code is NOT executing within an IStrategy callback, then it MUST use IContext.executeTask(<callableTask>) mechanism in order to perform the operation, on the proper thread, with the correct synchronization.

Could you please add a call to determine whether the current thread is an IStrategy callback event thread, or not?

As a suggestion, it could be in the IContext interface.

boolean IContext.isDispatchThread() // the naming isn't important

So, if an IEngine operation is being attempted, code could check like this:

IContext context = <valid context>;
// checking for thread context
if (context.isDispatchThread()) { // execute IEngine operations directly
     // ok to perform order operations
}
else { // we are not in the callback context
    // must use IContext.executeTask(<callable>) to perform order operations
}


Thanks very much for considering this enhancement.

HyperScalper


 
 Post subject: Re: IStrategy callback thread detection Post rating: 0   New post Posted: Fri 05 Apr, 2013, 07:47 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Why not fetching the thread in the onStart and then comparing it to the current one when necessary?


 
 Post subject: Re: IStrategy callback thread detection Post rating: 0   New post Posted: Fri 05 Apr, 2013, 20:20 
User avatar

User rating: 98
Joined: Mon 23 Jul, 2012, 02:02
Posts: 656
Location: United States, Durham, NC
API Support wrote:
Why not fetching the thread in the onStart and then comparing it to the current one when necessary?


Well, it may be true that there is a fixed specific thread which is used in all callbacks, for a given IStrategy object instance. However, that is by no means guaranteed by the interface. If callbacks are done by a thread pool, then a number of threads could be "valid" for Order Entry operations, but they would not necessarily be the same thread instance every time.

If I have a generic routine as follows, then it is "thread safe" provided I can distinguish the thread I am using:
// thread safe version, valid on any thread
void cancelOrder(IContext context, IOrder order) {
    // validate params
    if (context.isDispatchThread()) {
               // directly operate on the order object, this is valid context
    }
    else { // must use context.executeTask(callable)
              context.executeTask(new OrderCancelCallableTask(order));
              // no need to wait
    }
}


That's why I would like to be able to identify the correct thread context.

HyperScalper


 
 Post subject: Re: IStrategy callback thread detection Post rating: 0   New post Posted: Fri 12 Apr, 2013, 17:58 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
hyperscalper wrote:
Well, it may be true that there is a fixed specific thread which is used in all callbacks, for a given IStrategy object instance. However, that is by no means guaranteed by the interface. If callbacks are done by a thread pool, then a number of threads could be "valid" for Order Entry operations, but they would not necessarily be the same thread instance every time.
There is a single thread per strategy instance. In other words - every strategy works in its own thread, thus there is no necessity for adding specific function for the sole purpose of checking:
boolean isStrategyThread = Thread.currentThread().getId() == ThreadFetchedFromOnStart.getId();


 

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