Edilson Santos
Edilson Santos

Reputation: 1

How to calculate a streak duration?

I have made a code to calculate a streak duration (ConnorsRSI) in MQL5. But it doesn't work.

The MQL code is:

/////////////////////////////////////////////////////////


int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[])
  {
//----+   
   //---- check for the presence of bars, sufficient for the calculation
   if (rates_total < (sdPer - 1) + begin)
    return(0);

//---- declaration of local variables 
   int first, bar, i, t;

//---- calculation of starting index first of the main loop
   if(prev_calculated==0) // check for the first start of the indicator
      first=sdPer-1+begin; // start index for all the bars
   else first=prev_calculated-1; // start index for the new bars

//---- main loop of the calculation
   for(bar = first; bar < rates_total - 1; bar++)
    {    
      t = 0;
      //---- avaliation loop for the current bar
      for(i = 0; i < sdPer; i++)

      if(price[bar] > price[bar - i])
      {
         if(t >= 1)
            t = t + 1;
         else t = 1;
      }

      else if(price[bar] < price[bar - i])
      {      
         if(t <= -1)
         t = t - 1;
         else t = -1;
      }

      else if(price[bar] == price[bar - i])
      {
         t = 0;
      }

//---- set the element of the indicator buffer with the value of SMA we have calculated
      ExtLineBuffer[bar]=t;// **streak duration**
    }
//----+     
   return(rates_total);
  }

////////////////////////////////////////////////////////////

I don't identify the error. How is wrong?

Upvotes: 0

Views: 221

Answers (1)

Enivid
Enivid

Reputation: 210

I have tested the code and it works fine. Please make sure that you are doing these things:

  1. Set indicator as a separate window indicator: #property indicator_separate_window.
  2. Define buffers and plots. For example, like this:

    #property indicator_buffers 1
    #property indicator_plots   1
    #property indicator_color1  clrRed
    #property indicator_type1   DRAW_LINE
    #property indicator_style1  STYLE_SOLID
    
  3. Assign index buffer (in OnInit()): SetIndexBuffer(0, ExtLineBuffer, INDICATOR_DATA);.

Upvotes: 1

Related Questions