Please I need help with my indicator.

 
Good day!  

I wrote the code (attached below) for the indicator, everything counts correctly. However, only during initialization, during further drawing - the information is drawn incorrectly. I tried to use start () instead of a calculator, but everything is the same.

Only 1 return;

//+------------------------------------------------------------------+
//|                                                 PRODeviation.mq4 |
//|                                                          VockCap |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "VockCap"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 3
#property indicator_plots   3
extern double UpLevel=75; // Уровень перекупленности
extern double DownLevel=25; // Уровень перепроданности
extern int LimitsForDeviation=12; // Лимит для Deviation
extern string IS=" --- Indicator Seetings --- ";
extern bool DrawArrows=false; // Рисовать стрелки
extern double Spread=30; // Расстояние от цен для свечей
extern int BArrowCode=200; // Номер стрелки для бычей свечи
extern int SArrowCode=202; // Номер стрелки для медвежей свечи
extern int BullArrowSize=1; // Размер для бычей свечи
extern int BearArraowSize=1; // Размер для медвежей свечи
input color ExtForBullArrow = Gold; // Цвет для бычей свечи
input color ExtForBearArrow = Gold; // Цвет для медвежей свечи
extern bool UseAlert=false;
string objectNamePrefix="ProD";
//--- plot ProDeviation
#property indicator_label1  "ProDeviation"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_type3   DRAW_HISTOGRAM
#property indicator_color1  clrBlue
#property indicator_color2  clrGreen
#property indicator_color3  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_style2  STYLE_SOLID
#property indicator_style3  STYLE_SOLID
#property indicator_width1  1
#property indicator_width2  2
#property indicator_width3  2
//--- indicator buffers
double ProDeviationBuffer[];
double SDU[];
double SDD[];
double InfoUp[];
double InfoDown[];
double MAUP[];
double MADOWN[];
double Action[1]= {0};
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ProDeviationBuffer);
   SetIndexBuffer(1,SDU);
   SetIndexBuffer(2,SDD);
   SetIndexDrawBegin(0,0);
   SetIndexDrawBegin(1,0);
   SetIndexDrawBegin(2,0);
   SetIndexLabel(0,"ProDeviation");
   SetIndexLabel(1,"Signal Buy");
   SetIndexLabel(2,"Signal Sell");
   Spread=Spread*Point;
//---
   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[])
  {
//---
//Limits"
   int limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars>0)
      counted_bars--;
   limit=Bars-counted_bars;
//ArrayResize:
//ArrayResize(SDU,limit+LimitsForDeviation*2,10);
//ArrayResize(SDD,limit+LimitsForDeviation*2,10);
//ArrayResize(ProDeviationBuffer,limit+LimitsForDeviation*2,10);
   ArrayResize(InfoUp,limit+LimitsForDeviation*2,10);
   ArrayResize(InfoDown,limit+LimitsForDeviation*2,10);
   ArrayResize(MAUP,limit+LimitsForDeviation*2,10);
   ArrayResize(MADOWN,limit+LimitsForDeviation*2,10);
//Calculating:
   for(int i=limit-1; i>=1; i--)
     {
      //Disstribution
      double Different=iClose(NULL,0,i)-iClose(NULL,0,i+1);
      if(Different>0)
        {
         InfoUp[i]=Different;
        }
      if(Different<0)
        {
         InfoDown[i]=MathAbs(Different);
        }
     }
   for(int i=limit-1; i>=1; i--)
     {
      double RSI=0;
      double UP=0, Down=0, AverUP=0, AverDown=0, StdU=0, StDD=0;
      //CalculateResult&Counting
      int CountBear=0, CountBull=0, CountBear2=0, CountBull2=0;
      for(int x=i; x<i+LimitsForDeviation; x++)
        {
         if(InfoUp[x]!=0)
           {
            CountBull=CountBull+1;
           }
         if(InfoDown[x]!=0)
           {
            CountBear=CountBear+1;
           }
        }
      for(int x=i; x<i+LimitsForDeviation; x++)
        {
         if(InfoUp[x]!=0)
           {
            UP=UP+InfoUp[x]; //SUM
           }
         if(InfoDown[x]!=0)
           {
            Down=Down+InfoDown[x]; //SUM
           }
        }
      if(CountBull!=0)
        {
         AverUP=UP/CountBull;
         CountBull2=CountBull;
        }
      if(CountBear!=0)
        {
         AverDown=Down/CountBear;
         CountBear2=CountBear;
        }
      for(int ii=i; ii<i+LimitsForDeviation; ii++)
        {
         if(InfoUp[ii]!=0)
           {
            StdU=StdU+((InfoUp[ii]-AverUP)*(InfoUp[ii]-AverUP));
           }
         if(InfoDown[ii]!=0)
           {
            StDD=StDD+((InfoDown[ii]-AverDown)*(InfoDown[ii]-AverDown));
           }
        }
      double trueStdU=0, trueStdD=0;
      if(CountBull2!=0&&CountBull2!=1)
        {
         trueStdU=MathSqrt(StdU/(CountBull2-1));
        }
      if(CountBear2!=0&&CountBear2!=1)
        {
         trueStdD=MathSqrt(StDD/(CountBear2-1));
        }
      double Y=0;
      if(trueStdU!=0&&trueStdD!=0)
        {
         Y=trueStdU/trueStdD;
        }
      RSI=100-100/(1+Y);
      if(i!=0)
        {
         if(RSI<DownLevel)
           {
            SDU[i]=RSI;
           }
         if(RSI>UpLevel)
           {
            SDD[i]=RSI;
           }
         ProDeviationBuffer[i]=RSI;
        }
     }
//Arrows:
   for(int i=limit-1; i>=1; i--)
     {
      if(i>0)
        {
         if(DrawArrows==true)
           {
            if(ProDeviationBuffer[i+1]<DownLevel&&ProDeviationBuffer[i+1]!=0)
              {
               if(ProDeviationBuffer[i]>DownLevel)
                 {
                  string curName=StringConcatenate(objectNamePrefix,"UP",iTime(NULL,0,i));
                  bool X=ObjectCreate(0,curName,OBJ_ARROW_UP,0,iTime(NULL,0,i),iLow(NULL,0,i)-Spread);
                  ObjectSetInteger(0,curName,OBJPROP_ARROWCODE,BArrowCode);
                  ObjectSetInteger(0,curName,OBJPROP_WIDTH,BullArrowSize);
                  ObjectSetInteger(0,curName,OBJPROP_COLOR,ExtForBullArrow);
                 }
              }
            if(ProDeviationBuffer[i+1]>UpLevel&&ProDeviationBuffer[i+1]!=0)
              {
               if(ProDeviationBuffer[i]<UpLevel)
                 {
                  string curName=StringConcatenate(objectNamePrefix,"Down",iTime(NULL,0,i));
                  bool X=ObjectCreate(0,curName,OBJ_ARROW_DOWN,0,iTime(NULL,0,i),iHigh(NULL,0,i)+Spread*3);
                  ObjectSetInteger(0,curName,OBJPROP_ARROWCODE,SArrowCode);
                  ObjectSetInteger(0,curName,OBJPROP_WIDTH,BearArraowSize);
                  ObjectSetInteger(0,curName,OBJPROP_COLOR,ExtForBearArrow);
                 }
              }
           }
        }
     }
//Allerts:
   if(UseAlert==true)
     {
      if(ProDeviationBuffer[2]<DownLevel)
        {
         if(ProDeviationBuffer[1]>DownLevel)
           {
            if(Action[0]==0||Action[0]==2)
              {
               Alert("Signal UP | ", iTime(NULL,0,1));
               ArrayFill(Action,0,1,1);
              }
           }
        }
      if(ProDeviationBuffer[2]>UpLevel)
        {
         if(ProDeviationBuffer[1]<UpLevel)
           {
            if(Action[0]==0||Action[0]==1)
              {
               Alert("Signal Down | ", iTime(NULL,0,1));
               ArrayFill(Action,0,1,2);
              }
           }
        }
     }
//--- return value of prev_calculated for next call
   return(0);
   return(0);
  }
//+------------------------------------------------------------------+
 
  1. Why did you post your MT4 question in the MT5 Indicators section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2.       if(Different>0) InfoUp[i]=Different;
          if(Different<0) InfoDown[i]=MathAbs(Different);

    What do you think the value of your elements are when not set?

             if(InfoUp[x]!=0) UP=UP+InfoUp[x]; //SUM

    There you assume zero.

 
William Roeder #:
  1. Почему вы разместили свой вопрос по MT 4 в разделе «Индикаторы MT , а не в разделе «MQL 4 » (внизу корневой страницы )?
    Общие правила и лучшие практики Форума. - Общие - Форум программирования на MQL5 ? (20 17 )
    В следующий раз пишите в правильном месте. Скорее всего, модераторы скоро перенесут эту тему туда.

  2. Как вы думаете, какова ценность ваших элементов, когда они не установлены?

    Там вы предполагаете ноль.

There are 0 I think.

 
William Roeder #:
  1. Why did you post your MT4 question in the MT5 Indicators section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. What do you think the value of your elements are when not set?

    There you assume zero.

Please help me, I'm going crazy with this Indicator/