what is wrong in this indicator ? help me :S

 

Hi guys

indicator  not work and Error     array out of range.

can you fix it ?


#property copyright "Copyright 2022, Moal sofware Crop."
#property link      "https://t.me/moalpert"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
//--- plot p
#property indicator_label1  "m"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
input int atr_period=50;//atr period
//--- indicator buffers
double         m[];
double         h[];
double         L[];
double         s[];
double         k[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,m);
   SetIndexStyle(0,DRAW_ARROW,EMPTY,3,clrBlue);
   SetIndexArrow(0,217);
   SetIndexLabel(0,"Uparrow");
   
   
   SetIndexBuffer(1,h);
   SetIndexStyle(1,DRAW_ARROW,EMPTY,3,clrBlue);
   SetIndexArrow(1,218);
   SetIndexLabel(1,"Downarrow");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
  int calcs=rates_total-prev_calculated;
  int from=calcs;
  if(calcs>atr_period){
  from=rates_total-atr_period-6;
  } 
  for(int i=from;i>=0;i--)
  {
  double T=Close[i]-Open[i];
   L[i]=High[i]-Low[i];
   k[i]=0.0;
  if(L[i]>0.0){k[i]=MathAbs(T)/(L[i]);}
  double atr=iATR(_Symbol,_Period,atr_period,i);
  double s_div=(MathMin(Close[i],Open[i])-Low[i]);
  s[i]=0.0;
  if(s_div!=0.0){s[i]=(High[i]-MathMax(Close[i],Open[i]))/s_div;}
  if (L[i+1]>1*atr && 1*s[i+1]<=1*0.25 && 1*low[i+1]==1*iLowest(_Symbol,0,MODE_LOW,5,1)&& k[i+1]<0.5 && Close[i]>high[i+1])
   {
      m[i]=1;
   }
  else if (L[i+1]>1*atr && s[i+1]>=1*4 && 1*high[i+1]==1*iHighest(_Symbol,0,MODE_HIGH,5,1)&& k[i+1]<0.5 && Close[i]<high[i+1])
   {
      h[i]=-1;
    }
  
  } 
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  }
//+------------------------------------------------------------------+
 
aliasgari524: can you fix it ?

The question is can you fix it?

  1. #property indicator_buffers 1
    #property indicator_plots 1
    //--- plot p
    
    //--- indicator buffers mapping
       SetIndexBuffer(0,m);
       ⋮
       SetIndexBuffer(1,h);

    How many buffers are you trying to create? How many did you declare?

  2. double         L[];
    double         s[];
    double         k[];

    What are these empty arrays? They are not buffers. They have no size, yet you try to use them.

  3.  int calcs=rates_total-prev_calculated;
      int from=calcs;
      if(calcs>atr_period){
      from=rates_total-atr_period-6;
      } 
      for(int i=from;i>=0;i--)
    First run, prev_calculated is zero, you try to access Close[rates_total] which does not exist.
              How to do your lookbacks correctly #9#14 & #19 (2016)
 
William Roeder #:

The question is can you fix it?

  1. How many buffers are you trying to create? How many did you declare?

  2. What are these empty arrays? They are not buffers. They have no size, yet you try to use them.

  3. First run, prev_calculated is zero, you try to access Close[rates_total] which does not exist.
              How to do your lookbacks correctly #9#14 & #19 (2016)

Hello ,Actually I want Convert an indicator in PineScript to MQL4  But I am a beginner ... :S

and this is pinescript Code i want convert to mql


float m=0
T=close-open
L=high-low
k=math.abs(T)/(L)
atr=ta.atr(50)
s=(high-math.max(close,open))/(math.min(close,open)-low)

if  L[1]>1*atr and 1*s[1]<=1*0.25 and 1*low[1]==1*ta.lowest(low[1],5) and k[1]<0.5 and close>high[1]
    m:=1
else if L[1]>1*atr and s[1]>=1*4 and 1*high[1]==1*ta.highest(high[1],5) and k[1]<0.5 and close<low[1]
    m:=-1


 
First run, prev_calculated is zero, you try to access Close[rates_total] which does not exist.
          How to do your lookbacks correctly #9#14 & #19 (2016)

    That going wrong is more rare than aliens landing on London tomorrow to flirt with my ex wife :P . It is a valid observation though.

    aliasgari524 #:

    Hello ,Actually I want Convert an indicator in PineScript to MQL4  But I am a beginner ... :S

    and this is pinescript Code i want convert to mql



    This is the same indicator as before ? you want to place a buy and sell arrow i assume that is why the values were so extreme ?

    Points 1 + 2 of willian are correct  

    Try this (if there were no alterations to the original Trading view code)

    #property version   "1.00"
    #property strict
    #property indicator_chart_window
    #property indicator_buffers 2
    #property indicator_plots   2
    //--- plot up
    #property indicator_label1  "U"
    #property indicator_type1   DRAW_ARROW
    #property indicator_color1  clrRoyalBlue
    #property indicator_style1  STYLE_SOLID
    #property indicator_width1  3
    //--- plot down
    #property indicator_label2  "D"
    #property indicator_type2   DRAW_ARROW
    #property indicator_color2  clrCrimson
    #property indicator_style2  STYLE_SOLID
    #property indicator_width2  3
    input int atr_period=50;//atr period
    //you provide a deviation in points for your arrows from the high (for the sell arrow) and from the low (for the buy arrow)
    input int breath_room=200;//breathing room in points
    //--- indicator buffers
    double         up[],down[];
    
    int OnInit()
      {
    //--- indicator buffers mapping
       SetIndexBuffer(0,up);
       SetIndexArrow(0,217);
       SetIndexBuffer(1,down);
       SetIndexArrow(1,218);
    //---
       return(INIT_SUCCEEDED);
      }
    
    int OnCalculate(const int rates_total,
                    const int prev_calculated,
                    const datetime &time[],
                    const double &open[],
                    const double &high[],
                    const double &low[],
                    const double &close[],
                    const long &tick_volume[],
                    const long &volume[],
                    const int &spread[])
      {
    //---
      int calcs=rates_total-prev_calculated;
      int from=calcs;
      if(calcs>atr_period){
      from=rates_total-atr_period-6;
      }else if(prev_calculated==0){
      from=calcs-1;
      }
      for(int i=from;i>=0;i--)
      {
      double T=Close[i]-Open[i];
      double L=High[i]-Low[i];
      double k=0.0;
      if(L>0.0){k=MathAbs(T)/(L);}
      double atr=iATR(_Symbol,_Period,atr_period,i);
      double s_div=(MathMin(Close[i],Open[i])-Low[i]);
      double s=0.0;
      if(s_div!=0.0){s=(High[i]-MathMax(Close[i],Open[i]))/s_div;}
      if(High[i+2]>High[i+1]&&High[i+2]>High[i]&&High[i+2]>High[i+3]&&High[i+2]>High[i+4]&&Close[i]<Open[i]&&Close[i+1]<Open[i+1]&&Close[i+3]>Open[i+3]&&Close[i+4]>Open[i+4]&&Low[i]<Low[i+4]){ 
        //p[i]=1000*(-1+High[i+2]/MathMin(Low[i+1],Low[i]));
        up[i]=Low[i]-((double)breath_room)*_Point;//minus the breathing room to set it lower
        }
      else if(Low[i+2]<Low[i+1]&&Low[i+2]<Low[i]&&Low[i+2]<Low[i+3]&&Low[i+2]<Low[i+4]&&Close[i]>Open[i]&&Close[i+1]>Open[i+1]&&Close[i+3]<Open[i+3]&&Close[i+4]<Open[i+4]&&High[i]>High[i+4]){ 
        //p[i]=1000*(-1+Low[i+2]/MathMax(High[i+1],High[i]));
        down[i]=High[i]+((double)breath_room)*_Point;//plus the breathing room to set it higher
        }
      else{
        //p[i]=0.0;
        }
      }
    //--- return value of prev_calculated for next call
       return(rates_total);
      }
    
    
     
    William Roeder #: 3. First run, prev_calculated is zero, you try to access Close[rates_total] which does not exist.
    @Lorentzos Roussos #: That going wrong is more rare than aliens landing on London tomorrow to flirt with my ex wife :P . It is a valid observation though.

    It is not rare at all! It is guaranteed to happen every single time when an Indicator is first placed on a chart. On first call, prev_calculated is zero.

     
    Fernando Carreiro #:

    It is not rare at all! It is guaranteed to happen every single time when an Indicator is first placed on a chart. On first call, prev_calculated is zero.

    But in this specific interpretation of an indicator if the rates are more than the atr period something happens , right ?  😊

    So it is rare that from will request rates_total , very very rare . But as i said , rare does not mean never . 


     
    Lorentzos Roussos #:

    That going wrong is more rare than aliens landing on London tomorrow to flirt with my ex wife :P . It is a valid observation though.

    This is the same indicator as before ? you want to place a buy and sell arrow i assume that is why the values were so extreme ?

    Points 1 + 2 of willian are correct  

    Try this (if there were no alterations to the original Trading view code)

    Your help was really great for this,You fixed the previous indicator problem., thank you :S.

    Actually I wanted to convert this indicator to mql and I still have the same problem... :(( I am very confused :S

    float m=0
    T=close-open
    L=high-low
    k=math.abs(T)/(L)
    atr=ta.atr(50)
    s=(high-math.max(close,open))/(math.min(close,open)-low)
    
    if  L[1]>1*atr and 1*s[1]<=1*0.25 and 1*low[1]==1*ta.lowest(low[1],5) and k[1]<0.5 and close>high[1]
        m:=1
    else if L[1]>1*atr and s[1]>=1*4 and 1*high[1]==1*ta.highest(high[1],5) and k[1]<0.5 and close<low[1]
        m:=-1
     
    Lorentzos Roussos #: But in this specific interpretation of an indicator if the rates are more than the atr period something happens , right ?  😊 So it is rare that from will request rates_total , very very rare . But as i said , rare does not mean never . 

    If that is how you wish to view it ... then who am I to disagree.

    But I will ask this .. and what happens when rates_total is also zero?

    It does happen — often, in fact!

    EDIT: And what about when prev_calculated is greater than rates_total?

    Yes, it also happens, sometimes!

     
    aliasgari524 #:

    Your help was really great for this,You fixed the previous indicator problem., thank you :S.

    Actually I wanted to convert this indicator to mql and I still have the same problem... :(( I am very confused :S

    I see , so this is a slight alteration from the original . 

    Okay when you see [x] brackets in Pine Script it indicates a deviation to the past from the current value so in this case

    L[1] becomes 

    double L=High[1]-Low[1];

    similarly to Pine script MQL4 denotes the distance from the live bar with brackets so High[1] and Low[1] are the bar one position to the left from the latest bar

    As above

    s[1] becomes

    double s=(High[1]-MathMax(Close[1],Open[1]))/(MathMin(Close[1],Open[1])-Low[1]);

    Then you have ta.lowest(low,5) which means find the lowest of the lows for the last 5 bars after the live one .

    Which is tricky . You will use iLowest for this one , but iLowest will give you a bar index so to get its low you will have to access the result of the lowest.

    double lowest=Low[iLowest(_Symbol,_Period,MODE_LOW,5,1)];
    //so get the Low , which low ? the low at the index with the lowest low starting from bar 1 till bar 5

    Then k[1] is also tricky where your [1]s cascade into the individual calculations of T and L so you get :

    double k=MathAbs(Close[1]-Open[1])/(High[1]-Low[1]);
    

    and close>high[1] means if the current close is higher than the previous close 

    So these are the basic formulas for the first check is it done though ? No .

    We are checking every bar in index i so all the [1] become [i+1] and all the [0] become [i] , i will attach the full code in case you want to take a look directly instead of trying out yourself.

    The though of creating extra buffers to keep everything neatly tucked in there was correct , but let's do it the easy way first.

    Files:
    tv_3.mq4  5 kb
     
    Lorentzos Roussos #:

    I see , so this is a slight alteration from the original . 

    Okay when you see [x] brackets in Pine Script it indicates a deviation to the past from the current value so in this case

    L[1] becomes 

    similarly to Pine script MQL4 denotes the distance from the live bar with brackets so High[1] and Low[1] are the bar one position to the left from the latest bar

    As above

    s[1] becomes

    Then you have ta.lowest(low,5) which means find the lowest of the lows for the last 5 bars after the live one .

    Which is tricky . You will use iLowest for this one , but iLowest will give you a bar index so to get its low you will have to access the result of the lowest.

    Then k[1] is also tricky where your [1]s cascade into the individual calculations of T and L so you get :

    and close>high[1] means if the current close is higher than the previous close 

    So these are the basic formulas for the first check is it done though ? No .

    We are checking every bar in index i so all the [1] become [i+1] and all the [0] become [i] , i will attach the full code in case you want to take a look directly instead of trying out yourself.

    The though of creating extra buffers to keep everything neatly tucked in there was correct , but let's do it the easy way first.

    Wow, you are really awesome And I learned a lot from you thank you

     
    aliasgari524 #:

    Wow, you are really awesome And I learned a lot from you thank you

    Anytime  ☕️

    Reason: