Download MetaTrader 5

(MQL4) How to code previous or first indicator data?

To add comments, please log in or register
Piotr
1254
Piotr  

Hi,

I have opened chart and under the bars there is RSI indicator and Moving Average indicator on RSI (not on the bars) as first or previous indicator data.

Question: Can I code the moment when the line of Moving Average cut RSI line or vice versa?

Rgds,

whroeder1
15556
whroeder1  
Get the RSI values into an array, use iMAonArray to get the MA of the RSI, find the cross.
Piotr
1254
Piotr  

Can I please any MQL SYNTAX example according to following image?



Tjipke de Vries
6370
Tjipke de Vries  
WHRoeder:
Get the RSI values into an array, use iMAonArray to get the MA of the RSI, find the cross.
   int i;
   int limit;
   int counted_bars=IndicatorCounted();
   
   //---- check for possible errors
   if(counted_bars<0) return(-1);
   
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
 
   //--- main loops 1 and 2
   for(i=0; i < limit; i++)
      {
        RSIBuffer[i]=iRSI(Symbol(),0,RSIPeriod,PRICE_CLOSE,i);
      }
  
   for(i=0; i < limit; i++)
      {
        MAofRSIBuffer[i]=iMAOnArray(RSIBuffer,0,MAofRSI,0,MA_method,i);
      }
 

search for cross means RSIBuffer[i+1]>MAofRSIBuffer[i+1] and RSIBuffer[i]<MAofRSIBuffer[i]

or    RSIBuffer[i+1]<MAofRSIBuffer[i+1] and RSIBuffer[i]>MAofRSIBuffer[i]

whroeder1
15556
whroeder1  
deVries:

search for cross means RSIBuffer[i+1]>MAofRSIBuffer[i+1] and RSIBuffer[i]<MAofRSIBuffer[i]

or    RSIBuffer[i+1]<MAofRSIBuffer[i+1] and RSIBuffer[i]>MAofRSIBuffer[i]

or

(RSIBuffer[i+1] - MAofRSIBuffer[i+1]) * (RSIBuffer[i] - MAofRSIBuffer[i]) < 0

or

bool wasAbove = RSIBuffer[i+1] > MAofRSIBuffer[i+1],
      isAbove = RSIBuffer[i]   > MAofRSIBuffer[i],
     isCross  = isAbove != wasAbove;
Piotr
1254
Piotr  
deVries:

search for cross means RSIBuffer[i+1]>MAofRSIBuffer[i+1] and RSIBuffer[i]<MAofRSIBuffer[i]

or    RSIBuffer[i+1]<MAofRSIBuffer[i+1] and RSIBuffer[i]>MAofRSIBuffer[i]


It doesn't work ! In my EA I have created the following function to check MA on RSI ARRAY. But I have only SELL signal.

Following there is my code. What I made wrong?


int CheckMaOnRsi()
{
   int i;
   int limit;
   int counted_bars=IndicatorCounted();
   double RSIBuffer[], MAofRSIBuffer[];   
   
   /* check for possible errors */
   if(counted_bars<0) return(-1);
   
   /* last counted bar will be recounted */
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
 
   /* main loops 1 and 2 */
   for(i=0; i < limit; i++)
      {
        RSIBuffer[i]=iRSI(Symbol(),0,ARRAY_RSI_PERIOD,ARRAY_RSI_PRICE,i);
        MAofRSIBuffer[i]=iMAOnArray(RSIBuffer,0,ARRAY_MA_PERIOD,0,ARRAY_MA_METHOD,i);
        if ((RSIBuffer[i+1] - MAofRSIBuffer[i+1]) * (RSIBuffer[i] - MAofRSIBuffer[i]) < 0) {return(-1);}
        if ((RSIBuffer[i+1] - MAofRSIBuffer[i+1]) * (RSIBuffer[i] - MAofRSIBuffer[i]) > 0) {return(1);}
        
        /*
        
        // OR
        
        if (RSIBuffer[i+1]>MAofRSIBuffer[i+1] && RSIBuffer[i]<MAofRSIBuffer[i]) {return(-1);}
        if (RSIBuffer[i+1]<MAofRSIBuffer[i+1] && RSIBuffer[i]>MAofRSIBuffer[i]) {return(1);}
        */
      }
   

return(0);
}
Simon Gniadkowski
Moderator
18024
Simon Gniadkowski  
puncher:

It doesn't work ! In my EA I have created the following function to check MA on RSI ARRAY. But I have only SELL signal.

Following there is my code. What I made wrong?


You cannot use Indicator code in an EA.  for example,  IndicatorCounted(),  from here:  https://docs.mql4.com/customind

 "These functions cannot be used in experts and scripts."

Piotr
1254
Piotr  
RaptorUK:


You cannot use Indicator code in an EA.  for example,  IndicatorCounted(),  from here:  https://docs.mql4.com/customind

 "These functions cannot be used in experts and scripts."


Hi RaptorUK, Should I create the separate indicator with the following code? And after create indicator should I use iCustom() funtion in order to use it in my EA ?


  int i;
   int limit;
   int counted_bars=IndicatorCounted();
   
   //---- check for possible errors
   if(counted_bars<0) return(-1);
   
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
 
   //--- main loops 1 and 2
   for(i=0; i < limit; i++)
      {
        RSIBuffer[i]=iRSI(Symbol(),0,RSIPeriod,PRICE_CLOSE,i);
      }
  
   for(i=0; i < limit; i++)
      {
        MAofRSIBuffer[i]=iMAOnArray(RSIBuffer,0,MAofRSI,0,MA_method,i);
      }



Simon Gniadkowski
Moderator
18024
Simon Gniadkowski  
puncher:

Hi RaptorUK, Should I create the separate indicator with the following code? And after create indicator should I use iCustom() funtion in order to use it in my EA ?

That would probably be the simplest way.
To add comments, please log in or register