Recording if the last candles of a indicator were positive.

 

Hi guys,

im trying to get a function that checks how many candles from the indicator were positive or negative in a row.

My problem is that it is somehow not working correctly. So lets say: 4 Candles have been positive but it shows only 3 sometimes.

Do you guys have a idea? Thank you!

My try:

//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

//--- input parameters
input int      barcount=5;

// 1. prepare needed values
double maxnow= 0;
double minnow= 0;

// THIS IS RESULT number that you want to know
// how many of last bars had same direction in one sequence
int sequence=0; 

datetime timenow=TimeCurrent();

double maxarray[20];
double minarray[20];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
// 2. set all values to 0 at start
   ArrayInitialize(maxarray,0);
   ArrayInitialize(minarray,0);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
// 4. on new bar save curent values to last closed bar
   if(timenow<Time[0])
     {
      sequence=0;
      string result="negative bars check in order: ";
      //5. move saved values by 1 index so new bar can be added
      for(int i=0; i<barcount; i++)
        {
         maxarray[barcount-i]=maxarray[barcount-i-1];
         minarray[barcount-i]=minarray[barcount-i-1];
         // create positive/negative message to print
         if(minarray[barcount-i-1]>=0){
         
            result+=" +1 ";
            if (sequence<0)sequence=0;
            sequence++;
            
            }
         else // THIS IS CHECK IF BAR WAS NEGATIVE so we can count how many in sequence
            if(minarray[barcount-i-1]<0)
              {
               result +=" -1 ";
               if (sequence>0)sequence=0;
               sequence--;
              }
        }

      maxarray[0]=maxnow;
      minarray[0]=minnow;
      // 6. print info about recording
      Print("last bar histogram max value: ",DoubleToString(maxarray[0],6)," histogram min value: ",DoubleToString(minarray[0],6));
      Print(result);
      
      // 7. FINAL RESULT OF HOW MANY LAST BARS HAD SAME DIRRECTION
      Print("total sequence of bars: ",sequence);


      //reset recording for next bar
      maxnow=0;
      minnow=0;
      timenow=Time[0];
     }

// 3. read indicator output each tick = buffer 3 contains values for Histogram for each color
   double buffer = iCustom(NULL,0,"MacD Custom",3,0);

   if(buffer>maxnow)
      maxnow=buffer;
   if(buffer<minnow)
      minnow=buffer;

  }
//+------------------------------------------------------------------+
 

If this is mq5 – the iCustom cannot be used like that - it’s creating a handle to get the indicators values later from this handle. So, you must create this handle in the OnInit and in OnTick function you can use CopyBuffer to get values from the historical candles. Then you can read the values properly. Remember to set ArraySetAsSeries for used buffers.

 
Marzena Maria Szmit #:

If this is mq5 – the iCustom cannot be used like that - it’s creating a handle to get the indicators values later from this handle. So, you must create this handle in the OnInit and in OnTick function you can use CopyBuffer to get values from the historical candles. Then you can read the values properly. Remember to set ArraySetAsSeries for used buffers.

Oh no, sorry for not mentioning. This is mq4 :)
 
Timiswild #:
Oh no, sorry for not mentioning. This is mq4 :)

Moving topic to MQL4 section.

 
Keith Watford #:

Moving topic to MQL4 section.

Thank you!
Reason: