Global variable resets over and over again.

 

Hi.  :) I am a beginner so please be patient. I have a simple code and I want it to run once so I add two global variables. SELL and BUY.  I don't know SELL and BUY it resets and code executes over and over again, but I need the code to execute once.


//+------------------------------------------------------------------+
//|                                          EMA Mario Indicator.mq4 |
//|                                                  Copyright 2021  |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021"
#property link      "https://www.mql5.com"
#property version   "1.00"      
#property strict
#property indicator_chart_window
 
  int EMA_100 = 100;
  int EMA_50 = 50;
  int EMA_25 = 25;
  int SELL;
  int BUY;
  string time;
  int time2;
  int time3;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+


void start()
{
 EMA();

}


void EMA()
   {
  
      double PreviousEMA_25=iMA(NULL,0,EMA_25,0,MODE_SMA,PRICE_CLOSE,2);
      double CurrentEMA25=iMA(NULL,0,EMA_25,0,MODE_SMA,PRICE_CLOSE,1);

      double PreviousEMA50=iMA(NULL,0,EMA_50,0,MODE_SMA,PRICE_CLOSE,2);
      double CurrentEMA50=iMA(NULL,0,EMA_50,0,MODE_SMA,PRICE_CLOSE,1);

      double PreviousEMA100=iMA(NULL,0,EMA_100,0,MODE_EMA,PRICE_CLOSE,2);
      double CurrentEMA100=iMA(NULL,0,EMA_100,0,MODE_EMA,PRICE_CLOSE,1);
        
        if (Period() == "1" || Period ()== "5" || Period ()== "15" || Period()== "30" )
        {
           time = " M";
           time3 =Period();
        }
        else if (Period () =="60")
        {
            time = " H"; 
            time3= "1";       
        }
        else if (Period () == "240")
        {
             time = " H";
             time3 = "4";
        }
        
      if (Close[1] < PreviousEMA50||PreviousEMA100)
         
         {
            
            if (SELL == 0)
            {
             
             SendNotification(" SELL !!! :)))) "+Symbol()+time+time3);
             SELL =1;
             BUY =0;
		Comment(SELL);	
            }
         }
         if (Close[1] > PreviousEMA50||PreviousEMA100)
         
         {
            if (BUY==0)
            {
            SendNotification("BUY !!! :))) "+Symbol()+time+time3);
            BUY=1;
            SELL=0;
            }
         }
         
        }
 
maniun:

Hi.  :) I am a beginner so please be patient. I have a simple code and I want it to run once so I add two global variables. SELL and BUY.  I don't know SELL and BUY it resets and code executes over and over again, but I need the code to execute once.


Like… every incoming tick? Every incoming tick Start() will run… if you want to run only once, put the code in a Script
 
Your code
if (Close[1] < PreviousEMA50||PreviousEMA100)
True is non-zero
if (Close[1] < PreviousEMA50||true)
Anything or true is always true.
if (…||true)
 
William Roeder #:
Your code
True is non-zero
Anything or true is always true.
Thanks.

I corrected my code and now it executes once :)

if (Close[1] < PreviousEMA50 && SELL ==0)
         
         {
            
          
             Comment (SELL);
             SendNotification(" SELL !!! :)))) "+Symbol()+time+time3);
             SELL =1;
             BUY =0;
           
         }
Reason: