Is there a way to listen for a when a variable changes?

 

I am trying to do something along the lines of:


If( *variable changes* ){

Buy=true;

}


I am pretty sure this would involve some type of loop, but I cannot seem to get it to work without doing something more complicated that involves multiple variables.

Is there a simple way to do this?


   

 
Aidan Stcyr:

I am trying to do something along the lines of:


If( *variable changes* ){

Buy=true;

}


I am pretty sure this would involve some type of loop, but I cannot seem to get it to work without doing something more complicated that involves multiple variables.

Is there a simple way to do this?


   

What kind of data does the variable carry? 

 
Lorentzos Roussos:

What kind of data does the variable carry?


At the moment the variable is (string Indicator="buy"). I am using it for "buy" and "sell" only.

 
Aidan Stcyr:

At the moment the variable is (string Indicator="buy"). I am using it for "buy" and "sell" only.

who controls the variable 

 
Lorentzos Roussos:

who controls the variable 

It is inside of an EA I am making. 

More specifically however, the buy/sell signals are generated off of readings from the iSAR indicator (Parabolic indicator).

 
Aidan Stcyr:

It is inside of an EA I am making. 

More specifically however, the buy/sell signals are generated off of readings from the iSAR indicator (Parabolic indicator).

Great . So you are reading the iSAR indicator essentially . 
So you need to see when the values you read change .
Thats in every Tick (OnTick()) 
So in there you will place the controls to associate the value of your Indicator string with iSAR values.

 
Lorentzos Roussos:

Great . So you are reading the iSAR indicator essentially . 
So you need to see when the values you read change .
Thats in every Tick (OnTick()) 
So in there you will place the controls to associate the value of your Indicator string with iSAR values.

OK thanks!

One other thing. Although I am only using the iSAR indicator at the moment, my intention is to incorporate stop/start functions depending on the time of day, other indicators, etc.

The problem I am running in to is that my EA will buy right at the start of when it reaches a certain time of day or the chart is within acceptable perimeters of other functions. This often causes the EA to open a trade towards the middle or end of each Parabola, which usually ends up in a loss of money. I am trying to trigger the buy/sell functions only when the iSAR flips so it will eliminate that problem. 


I am new to mql4, so all of the help is greatly apreaciated!!!

Documentation on MQL5: Technical Indicators / iSAR
Documentation on MQL5: Technical Indicators / iSAR
  • www.mql5.com
//|                                                    Demo_iSAR.mq5 | //|                        Copyright 2011, MetaQuotes Software Corp. | //|                                              https://www.mql5.com | "The method of creation of the handle is set through the 'type' parameter (function type...
 
Aidan Stcyr:

OK thanks!

One other thing. Although I am only using the iSAR indicator at the moment, my intention is to incorporate stop/start functions depending on the time of day, other indicators, etc.

The problem I am running in to is that my EA will buy right at the start of when it reaches a certain time of day or the chart is within acceptable perimeters of other functions. This often causes the EA to open a trade towards the middle or end of each Parabola, which usually ends up in a loss of money. I am trying to trigger the buy/sell functions only when the iSAR flips so it will eliminate that problem. 

Yeah , you need to monitor for a change in conditions not just the conditions being valid .

//global
string isradirection="none";

//in the monitoring on tick after the time checks

string newdirection="none";
//do your calcs , assing signal to newdirection

if(newdirection=="buy"&&isradirection=="sell") //when none its blocked
if(newdirection=="sell&&isradirection=="buy")  //same


isradirection=newdirection;


 
Lorentzos Roussos:

Yeah , you need to monitor for a change in conditions not just the conditions being valid .

Thanks!
 
Aidan Stcyr:
Thanks!

You are welcome.

Dont forget to set the value to "none" if time -or when time- becomes invalid

 
// Global variables                              
string SAR_Old="none";

void OnTick()
  {
    
   string SAR_Ind="none";   // Requirment status for SAR: Buy/Sell
   
//--------------------------------------------------------------------     
  // calculate SAR value
  double SAR = iSAR(_Symbol,_Period,.02,.2,0);

//--------------------------------------------------------------------    
  // Defining buy/sell signals for SAR
  if(SAR<Bid)
    {
     SAR_Ind="buy";  
    }
    
  if(SAR>Ask)
    { 
     SAR_Ind="sell";
    }
  Comment("The signal is: ",SAR_Ind);

//--------------------------------------------------------------------    
    // Determining buy/sell signal switch *******
   if(SAR_Ind=="buy" && SAR_Old=="sell"){    // Signal switches from sell to buy; allows trade
      Alert("1 ");}
      
   if(SAR_Ind=="sell" && SAR_Old=="buy"){    // Signal switches from buy to sell; allows trade
      Alert("2 ");}  
    

   SAR_Old=SAR_Ind;
}

Lorentzos Roussos:

You are welcome.

Dont forget to set the value to "none" if time -or when time- becomes invalid

Hey Lorentzos,

I am having a bizarre problem with my code regarding this problem.

Determining when there is a switch within the iSAR works, but it is only triggered for the second condition, Alert("2").

After testing over a long period the first condition, Alert("1"), would only trigger once, maybe twice, while the second condition would trigger many many times.

Am I setting it up incorrectly?

Reason: