Is there a need to block OnTick while handling a OnChartEvent?

 

Hi,

I red somewhere an EA has a single thread running all of its code, making mutexes/semaphores not needed to handle the code inside it.

Unfortunately I have a situation which may require a mutex of some sort: I have an EA that handles both OnTick event as well as OnChartEvent, when the user sends a command to open or close a position with a market order for example. One of such commands is to invert an operation. In OnTick, there is a constant check for the state of current positions/orders in accordace to a structure. My idea is that, once a OnChartEvent was being handled, no calls to OnTick would be processed and vice-versa, but it doesn't seems to be the case: OnChartEvents are being processed at the same time as OnTick events as the log below points out:

JE      0       18:15:27.201    MW MTC (WINZ21,M1)      OnTick 1: glOpData[aaa].status.status: 2
IP      0       18:15:27.201    MW MTC (WINZ21,M1)      OnTick 2: updateStatusLabel no main glOpData[aaa].status.profit: -1.0
PQ      0       18:15:27.201    MW MTC (WINZ21,M1)      OnTick 3: profit: LOSS
RF      0       18:15:27.843    MW MTC (WINZ21,M1)      OnTick 1: glOpData[aaa].status.status: 2
MO      0       18:15:27.843    MW MTC (WINZ21,M1)      OnChartEvent 1: 999 ID 1: Trying to invert operation with market order
DQ      0       18:15:28.270    MW MTC (WINZ21,M1)      OnChartEvent 2: profit: 0
PI      0       18:15:28.270    MW MTC (WINZ21,M1)      OnTick 2: updateStatusLabel no main glOpData[aaa].status.profit: -1.0
IH      0       18:15:28.270    MW MTC (WINZ21,M1)      OnTick 3: profit: LOSS
JM      0       18:15:28.279    MW MTC (WINZ21,M1)      OnTick 1: glOpData[aaa].status.status: 0

As you can see, while a tick was being handled in OnTick, an OnChartEvent happened leading to an attempt to invert a position. After that, the current position is closed (hedge account) leading to the "profit: 0" result, but as soon as that finishes, OnTick is resumed leading to a re-update of the "profit" marker. 

Judging by the scence above, it would seem I need to implement some kind of semaphore for handling OnTick and OnChartEvents, namely: don't process your code until the other is 100% finished. Am I correct regarding that? And so, how could it be implemented? In Qt's QMutex class I would just call a ::lock or even ::tryLock to handle, but I don't know how to do that in here. Notice that any tick or button press needs to be evaluated, in particularly the second.

 
What?

No. They are sequencially processed.

Believe it or not. But you could try a faulty increment code and see if it is consistent.

Take a global variable and read it, add one to the temporary variable and write it back to the global variable.

Do both in OnTick and OnEvent. Maybe use a sleep statement to increase thread race condition creation.

Let it run and see if it skips an addition.

You will see, it doesn't.

It's your code that's giving you a false interpretation.
 
Dominik Egert #:
What?

No. They are sequencially processed.

Believe it or not. But you could try a faulty increment code and see if it is consistent.

Take a global variable and read it, add one to the temporary variable and write it back to the global variable.

Do both in OnTick and OnEvent. Maybe use a sleep statement to increase thread race condition creation.

Let it run and see if it skips an addition.

You will see, it doesn't.

It's your code that's giving you a false interpretation.

Gosh, I found the problem. Please forget I ever wrote this thread xD

Reason: