Can timer events and other functions run simultaneously on seperate threads/cores and create problems with your EA's execution?

 

I'm asking this question because when I trigger a function, it prints a variable's value twice in my logs, and there is nothing in that function that can change the variable's value, it can only read it. But sometime today, the function printed two different values for that variable even though that would seem impossible.

Since this value is calculated both by a chart event and a timer event, I wonder if it's possible that the timer event could have been running in parallel to the function that reads the variable's value and have changed its value between the two times the value was printed in the logs.

I can't think of any other reason this could happen, and it's really frustrating because I am unable to replicate this issue again. So if anyone has any information that could help, I would really appreciate it.

Thanks

*edit: I know this is easy to prevent by copying the value to a different variable when the function starts and only using the new variable as a source for that function but before I do it, I was hoping to know if it makes sense to think that this might have been what caused the error in my logs and that making this change could prevent the issue from happening again. I'd like to know what you think because I tried to replicate the issue but so far it only happened once so it's going to be difficult to know if this will solve the problem.

*edit: If the theory I have about this ends up true, then I suppose it means that passing variables to functions as parameters is a better coding method than letting functions modify global variables, because it prevents other programs from altering the value the function is working with, as long as it's not passed as a reference?

Documentation on MQL5: Common Functions / Print
Documentation on MQL5: Common Functions / Print
  • www.mql5.com
Print - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Jeepack:

I'm asking this question because when I trigger a function, it prints a variable's value twice in my logs, and there is nothing in that function that can change the variable's value, it can only read it. But sometime today, the function printed two different values for that variable even though that would seem impossible.

Since this value is calculated both by a chart event and a timer event, I wonder if it's possible that the timer event could have been running in parallel to the function that reads the variable's value and have changed its value between the two times the value was printed in the logs.

I can't think of any other reason this could happen, and it's really frustrating because I am unable to replicate this issue again. So if anyone has any information that could help, I would really appreciate it.

Thanks

*edits: I know this is easy to prevent by copying the value to a different variable when the function starts and only using the new variable as a source for that function but before I do it, I was hoping to know if it makes sense to think that this might have been what caused the error in my logs and that making this change could prevent the issue from happening again. I'd like to know what you think because I tried to replicate the issue but so far it only happened once so it's going to be difficult to know if this will solve the problem.

As far as I know they all run on the same thread.

 
Jeepack:

I'm asking this question because when I trigger a function, it prints a variable's value twice in my logs, and there is nothing in that function that can change the variable's value, it can only read it. But sometime today, the function printed two different values for that variable even though that would seem impossible.

Since this value is calculated both by a chart event and a timer event, I wonder if it's possible that the timer event could have been running in parallel to the function that reads the variable's value and have changed its value between the two times the value was printed in the logs.

I can't think of any other reason this could happen, and it's really frustrating because I am unable to replicate this issue again. So if anyone has any information that could help, I would really appreciate it.

Thanks

*edit: I know this is easy to prevent by copying the value to a different variable when the function starts and only using the new variable as a source for that function but before I do it, I was hoping to know if it makes sense to think that this might have been what caused the error in my logs and that making this change could prevent the issue from happening again. I'd like to know what you think because I tried to replicate the issue but so far it only happened once so it's going to be difficult to know if this will solve the problem.

*edit: If the theory I have about this ends up true, then I suppose it means that passing variables to functions as parameters is a better coding method than letting functions modify global variables, because it prevents other programs from altering the value the function is working with, as long as it's not passed as a reference?

Not it's not possible. An EA is single threaded.
 
Alain Verleyen #:
Not it's not possible. An EA is single threaded.

Thanks for your input!

If the issue isn't caused by parallelism, does anyone know if it could instead be a bug that happened because I'm doing what is considered bad coding practice by modifying global variables within functions? Can this sometimes cause bugs that creates delays or lag with how global variables are updated that may explain how one of my variables changed its value in the middle of a function that doesn't contain any code that can modify the variable's value?

 
Jeepack #:

Thanks for your input!

If the issue isn't caused by parallelism, does anyone know if it could instead be a bug that happened because I'm doing what is considered bad coding practice by modifying global variables within functions? Can this sometimes cause bugs that creates delays or lag with how global variables are updated that may explain how one of my variables changed its value in the middle of a function that doesn't contain any code that can modify the variable's value?

Not sure what kind of answer you are expecting without showing any code ?

All is possible, but from my experience, 99% of the time it's a bug in the user code.

 
Alain Verleyen #:

Not sure what kind of answer you are expecting without showing any code ?

All is possible, but from my experience, 99% of the time it's a bug in the user code.

Not sure either to be honest. I agree with you, for 99% of the bugs, but this feels like the 1%, I can't even replicate what happened right now.

The variable that changed is set on the global scope and being defined by functions that can be called multiple times per second from chart events and timers all in the same EA. A program will sometimes act in unpredictable ways when pushed to its limit so maybe this explains why the issue only happened once and I still can't replicate it after multiple days of testing.

My best guess right now is that relying on a global variable is just asking for this type of trouble. If I worked with the variable on the local scope of my function instead, that might protect my code from any terminal malfunction that may have caused the glitch I saw in my logs last week. I can't prove it right now, but I'll make the change and keep an eye on my logs to see if it ever happens again in the future.

If you have any insight into what kind of problems an EA could experience because of an overreliance on variables from the global scope to perform calculations, it might help me to know if I'm on the right track.

 
I've ruled out pretty much everything and it only leads to one conclusion: Between the moment function "A" started and finished, function "B" was executed without any code from function "A" requesting it. So something about the thread wasn't behaving as we would expect it at that very moment, and it left a foot print in my logs that can't be denied. While I can't prove it, I'm 100% sure of this right now from looking at the code's basic logic for more than 20 hours. This leads me to think I'll avoid this happening again by passing all the vars I need in function "A" as function parameters, and use only those copies during function "A"s calculations. This way, if that happens again, it won't corrupt any of the results from function "A"s calculations. I don't know why this is happening in my terminal, but I can't come up with any other logical solutions...
 
Jeepack #:

If you have any insight into what kind of problems an EA could experience because of an overreliance on variables from the global scope to perform calculations, it might help me to know if I'm on the right track.

I never do that, it's like openning the window and call the thief in.
How can you know who changed it? you never know because every one is a suspect.

And how can you test like that? Your classes become open to the world, so why do you need them at all?

From secluded units of logic and action, it becomes a general all intervention allowed, without leaving a trace (well, the log..)

You can never be sure that the class, or the method that's using it, is a closed box and test all the use cases of input it needs, because someone is playing with it's data outside.

EDIT: Second read I realized not everyone uses oo, you might use functions as you say. Still, the same should apply, you should strive for each function to be a closed box as much as possible, because this is the only way to predictability.
 
Amir Yacoby #:

I never do that, it's like openning the window and call the thief in.
How can you know who changed it? you never know because every one is a suspect.

And how can you test like that? Your classes become open to the world, so why do you need them at all?

From secluded units of logic and action, it becomes a general all intervention allowed, without leaving a trace (well, the log..)

You can never be sure that the class, or the method that's using it, is a closed box and test all the use cases of input it needs, because someone is playing with it's data outside.

EDIT: Second read I realized not everyone uses oo, you might use functions as you say. Still, the same should apply, you should strive for each function to be a closed box as much as possible, because this is the only way to predictability.

Thanks for the input Amir, I'll make those changes and hope it doesn't happen again, but even if it does, at least it'll be easier to know what caused the change instead of having every outside function being a suspect! I appreciate the info, cheers!