Not updating global data?

 

Hi all,

I am still working on an EA that I am translating from MQL4 to MQL5. But I keep getting the same issue.

So the goal is when an order gets executed (is a position) I play a different sound. I use text to speech instead of the standard alerts MT5 brings.

The code itself runs well. The trading part. But not for the Alerts/Playsounds... When I am in a position i reset playSound to false after I played it. But the if statement still gets executed every tick!

Please help!


//Global data
bool playSound;
int magincNB = 123;

void OnTick()
{

//Here I check if there is any position or order open by the EA. If not I will find an entry
if(CheckIfOpenOrdersByMagicNumber(magicNB) && CheckIfOpenPositionsByMagicNumber(magicNB)) 
{
        //Entry codeblock. After executing an ordeplacement successfully:
        PlaySound("Order_Placed.wav");

        //Then I want to ''toggle'' the order executed/triggered. So:
        playSound = true;
}


//Now for the exit I split it to orders and positions. They seem very different in MT5.

if(!CheckIfOpenOrdersByMagicNumber(magicNB))
{
        //When certain time passes, I delete the order if not triggered.
        PlaySound("Order_expired.wav"); 
}

if(!CheckIfOpenPositionsByMagicNumber(magicNB))
{
        //So here I handle my exit of the open position. But when the EA arrives in this code. It means it is triggered. Because I used PositionSelectTicketinstead of Orders.

        if(playSound==true)
        {
                PlaySound("Order_triggered.wav");
                playSound = false;       <- So here I set it back to false. So when the EA keeps refreshing the code. it should only run once.... Untill the entry code block made a new trade again. Somehow this part doesn't work.
				            And the this part keeps getting executed every tick. 
                                            
        }

}
}
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 

So I tried reloading EA's and shutting down softwares and even rebooting my PC. The error still happened.

I created a new template and start adding code block by codeblock to see where it goes wrong. Also added a lot of Print() functions to see if I am in the correct loop.

Basicly I copied the whole old EA into a new file and now the code runs smoothly....

Reason: