For loop Divergence problem

 
Hi everybody!

Recently I was developing a simple custom indicator that detects divergences based on Stochastic Oscillator. I wanted to test one divergence condition. The indicator inserted the first arrow, but the second one (given that there was a clearly VISIBLE divergence) doesn't get drawn. 
If you test the code it will be more understandable, my main problem is that no errors are reported, but the for loop backwards doesnt seem to work.

I'd appreciate your suggestions! Thank you!

#property indicator_buffers 4                            //indicator properties
#property indicator_separate_window
#property indicator_level1 20
#property indicator_level2 80
#property indicator_maximum 100
#property indicator_minimum 0
#property indicator_color1 clrBlueViolet
#property indicator_color2 clrOrange
#property indicator_levelstyle STYLE_DOT
#property indicator_levelcolor clrWhiteSmoke
#property indicator_levelwidth 1


//+------------------------------------------------------------------+

extern int ib=10,kp=5,dp=3,slw=3;                              //external parameters

double kb[],db[],ema[];
double kbt[],dbt[];
//global arrays

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   int ot=ObjectsTotal();
   if(ot>=0)
      ObjectsDeleteAll();

   IndicatorShortName("MultiStoch_Panel("+(string)kp+","+(string)dp+","+(string)slw+")");

   SetIndexBuffer(0,kb);
   SetIndexBuffer(1,db);

   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,0);

   ObjectCreate("div",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("result",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("barn",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("updiv",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("updiv2",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("updivp",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("result2",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("result",OBJPROP_XDISTANCE,150);
   ObjectSet("result",OBJPROP_YDISTANCE,350);
   ObjectSet("div",OBJPROP_XDISTANCE,50);
   ObjectSet("div",OBJPROP_YDISTANCE,350);
   ObjectSet("barn",OBJPROP_XDISTANCE,200);
   ObjectSet("barn",OBJPROP_YDISTANCE,350);
   ObjectSet("updiv",OBJPROP_XDISTANCE,50);
   ObjectSet("updiv",OBJPROP_YDISTANCE,370);
   ObjectSet("updiv2",OBJPROP_XDISTANCE,50);
   ObjectSet("updiv2",OBJPROP_YDISTANCE,382);
   ObjectSet("updivp",OBJPROP_XDISTANCE,110);
   ObjectSet("updivp",OBJPROP_YDISTANCE,375);
   ObjectSet("result2",OBJPROP_XDISTANCE,120);
   ObjectSet("result2",OBJPROP_YDISTANCE,375);
   ObjectSetText("div","Divergence  : ",8,"Tahoma",Gold);
   ObjectSetText("result","",8,"Tahoma",Gold);
   ObjectSetText("result","",8,"Tahoma",Gold);
   ObjectSetText("updiv","Upcoming",8,"Tahoma",Gold);
   ObjectSetText("updiv2","Divergence",8,"Tahoma",Gold);
   ObjectSetText("updivp",":",8,"Tahoma",Gold);
   ObjectSetText("result2","None at the moment",8,"Tahoma",Gold);

   return(0);
  }
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectsDeleteAll();
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {

   int cb,k;

   cb=IndicatorCounted();

   ci(cb);
   cd(k);

   return(0);
  }
//+------------------------------------------------------------------+

void ci(int cb)
  {

   int l=Bars-cb-1;
   for(int i=l;i>=0;i--)
      cs(i);

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void cs(int i)
  {
   kb[i]=iStochastic(NULL,0,kp,kp,slw,MODE_SMA,0,0,i);
   db[i]=iStochastic(NULL,0,kp,dp,slw,MODE_SMA,0,1,i);

  }
//+------------------------------------------------------------------+
void cd(int k)
  {

   bool dt=false,df=true,pmp=false,tmp=false;
   string bn,rbld,rbrd,hbld,hbrd;
   static datetime t;
   if(t==Time[0])
     {
      return;
     }
   t=Time[0];
   for(k=1;k<=Bars-1;k++)
     {

      if((db[k]<=db[k+1] && db[k]<=db[k-1]) || (db[k]>=db[k+1] && db[k]>=db[k-1]))

        {
         pmp=true;
         ArrayResize(dbt,3);
         ArrayResize(kbt,3);
         ArrayFill(dbt,0,1,db[k]);
         ArrayFill(dbt,1,1,High[k]);
         ArrayFill(dbt,2,1,Low[k]);
         ArrayFill(kbt,0,1,kb[k]);
         ArrayFill(kbt,1,1,High[k]);
         ArrayFill(kbt,2,1,Low[k]);

         bn=DoubleToStr(k,0);

         ObjectSetText("barn","Bar No.: "+k,8,"Tahoma",Green);
         ObjectCreate(0,"arrowd3",OBJ_ARROW_DOWN,0,Time[k],High[k]);
         if(kb[k]<50)
           {
            ObjectCreate(0,"arrowd",OBJ_ARROW_UP,1,Time[k],kb[k]);
            ObjectSet("arrowd",OBJPROP_COLOR,Lime);

           }
         else  ObjectCreate(0,"arrowd",OBJ_ARROW_DOWN,1,Time[k],db[k]);

         break;

        }

     }
   
   for(int y=k;y>=Bars-1;y++)
     {

      if(pmp && (dbt[0]<db[y] && dbt[1]>High[y]))
        {

         
          
            Alert(dbt[0]," ",db[y]," ",dbt[1]," ",High[y]);
            rbld="Regular Bullish Divergence";
            ObjectSetText("result2",rbld,8,"Tahoma",Red);
            ObjectCreate(0,"arrowd2",OBJ_ARROW_UP,1,Time[y],db[y]);
            ObjectSet("arrowd2",OBJPROP_COLOR,Lime);
           
        }
     }

  }
 
It is a good practice to give variables and buffers names that help you to recognise what values they hold, especially when asking others to check your code. It will also help you when looking at your own code when you haven't worked on it for a while.
 
GumRai: It is a good practice to give variables and buffers names that help you to recognise what values they hold, especially when asking others to check your code. It will also help you when looking at your own code when you haven't worked on it for a while.
void ci(int cb)
void cs(int i)
void cd(int k)
  1. ci, cs, cd are meaningless to us. They will be meaningless to you in a few days. Always write meaningful names.
  2. If you can't read your code, out loud, and have it make sense in your language, rename it until it does.
        if(pmp && (dbt[0]<db[y] && dbt[1]>High[y])) // You can't say that
    // vs
        bool isUptrend = dbt[0] < db[y];
        bool isBreakout = dbt[1] >High[y];
        if(isFoundBarn && isUptrend && isBreakout)   // You can say this.
Reason: