How do I check if a condition was met in the candles before?

 

Hi I am really new to coding,

and I am trying to code an alert system for a strategy.



double fastMA1 = iMA(NULL,0,lengthrsifast,0,MODE_SMA,r,0);

double slowMA1 = iMA(NULL,0,lenghtrsislow,0,MODE_SMA,r,0);


bool staticlong = fastMA1 < staticlonglvl;

bool ststicshort = fastMA1 < staticshortlvl;


so what I want to do is to check whethter  staticlong was true in one of the last 6 candles.

On Tradingview I solved it by just looking whether  staticlong or  staticlong[1]   or  staticlong[2]  or  staticlong[3]   or  staticlong[4] ... was true but I figured out that MQL4 is not placing everything into an array like Tradingview does.

Does anyone have a solution to this?

 
sasax7:

Hi I am really new to coding,

and I am trying to code an alert system for a strategy.



double fastMA1 = iMA(NULL,0,lengthrsifast,0,MODE_SMA,r,0);

double slowMA1 = iMA(NULL,0,lenghtrsislow,0,MODE_SMA,r,0);


bool staticlong = fastMA1 < staticlonglvl;

bool ststicshort = fastMA1 < staticshortlvl;


so what I want to do is to check whethter  staticlong was true in one of the last 6 candles.

On Tradingview I solved it by just looking whether  staticlong or  staticlong[1]   or  staticlong[2]  or  staticlong[3]   or  staticlong[4] ... was true but I figured out that MQL4 is not placing everything into an array like Tradingview does.

Does anyone have a solution to this?

You have to use the iMA() with the right candle number. Zero is the current candle, 1 is the previous candle and so on.

See an example:

   for(int i=1;i<7;i++)
     {
      if(iMA(NULL,0,lengthrsifast,0,MODE_SMA,r,i) < staticlonglvl) 
        {
         // Do something
        }
      ...
     }


 

Thats exactly what I was looking for thanks a lot @ Petr Nosek

 

one more question what if my ma si something like

double fastMA = (r+r)/2;

there I cannot the iMA function.


Reason: