Download MetaTrader 5

Problem with divergence indicator

To add comments, please log in or register
You can test an application before buying it. Find out how to do that!
Stefano Specia
44
Stefano Specia 2015.04.07 22:32 

Hello all, I have a problem with a newly written indicator that basically detects four different divergences explained on this site: http://www.babypips.com/school/high-school/trading-divergences/divergence-cheat-sheet.html

My problem is that I want to make a second for loop running inside the start function every first, newly appearing bar on the chart. Unfortunately whenever I try to start and test the indicator, it automatically exits. Is there a problem with my switch operator maybe?

Any suggestions?
Thank you in advance!

#property indicator_buffers 2                            //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 inputBars=15,K_Period=5,D_Period=3,Slowing=3;                              //external parameters

double K[],D[],ema[],K_t[],D_t[],K2[],D2[],EMA_1[];
int period[]={1,5,15,30,60,240,1440};
//global arrays

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

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

   int calcBars,k;

   calcBars=IndicatorCounted();

   calcInd(calcBars);
   for(k=1;k<=Bars-1;k++)
   {
   calcDiv(k);
   }
 
   return(0);
  }
//+------------------------------------------------------------------+
void calcInd(int calcBars)
  {

   int limit=Bars-calcBars-1;
   for(int i=Bars-1;i>=limit;i--){
      calcSto(i); }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void calcSto(int i)
  {
   K[i]=iStochastic(NULL,0,K_Period,D_Period,Slowing,MODE_SMA,0,0,i);
   D[i]=iStochastic(NULL,0,K_Period,D_Period,Slowing,MODE_SMA,0,1,i);
   

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

   int y,divFlag_switch;
   static datetime t;
   static int VisibleBars;

bool        isHighTurn   = D[k]>D[k+1] && D[k]<D[k-1],
            isLowTurn    = D[k]<D[k+1] && D[k]<D[k-1],
            isTurn       = isHighTurn || isLowTurn,
            RegularBullD = D[y]>D[y+1] && D[y]<D[y-1] && Low[z]>Low[y],
            RegularBearD = D[y]>D[y+1] && D[y]>D[y-1] && High[z]<High[y],
            HiddenBullD  = D[y]<D[y+1] && D[y]>D[y-1] && Low[z]>Low[y],
            HiddenBearD  = D[y]<D[y+1] && D[y]<D[y-1] && High[z]<High[y],
            divFlag      = RegularBearD || RegularBullD || HiddenBearD || HiddenBullD;
            
            if(RegularBullD) divFlag_switch=1;
            if(RegularBearD) divFlag_switch=2;
            if(HiddenBullD) divFlag_switch=3;
            if(HiddenBearD) divFlag_switch=4;
/**************************/
   if(t==Time[0])
     {
      return;
     }
   t=Time[0];
   
   if(VisibleBars==Bars-1) return;
   VisibleBars=Bars-1;
/**************************/   
         

         if(isTurn)

           {

            ArrayResize(D_t,3);
            ArrayResize(K_t,3);
            ArrayFill(D_t,0,1,D[k]);
            ArrayFill(D_t,1,1,High[k]);
            ArrayFill(D_t,2,1,Low[k]);
            ArrayFill(K_t,0,1,K[k]);
            ArrayFill(K_t,1,1,High[k]);
            ArrayFill(K_t,2,1,Low[k]);

           

            ObjectSetText("barNum","Bar No.: "+k,8,"Tahoma",Green);
            ObjectCreate(0,"arrowd3",OBJ_ARROW_DOWN,0,Time[k],High[k]);
            if(K[k]<50)
              {

               ObjectCreate(0,"arrowd",OBJ_ARROW_UP,1,Time[k],K[k]);
               ObjectSet("arrowd",OBJPROP_COLOR,Lime);

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

            

           }

      for(y=k;y<Bars-1;y++){
         
   if(divFlag)
     {
      PlaySound("news");
      
      switch(divFlag_switch)
        {
         case 2:
            ObjectSetText("resultDiv","Regular Bearish Divergence",8,"Tahoma",Red);
            ObjectCreate(0,"arrowd2",OBJ_ARROW_UP,1,Time[y],D[y]);
            ObjectSet("arrowd2",OBJPROP_COLOR,Lime);
            break;
         case 1:
            ObjectSetText("resultDiv","Regular Bullish Divergence",8,"Tahoma",LimeGreen);
            ObjectCreate(0,"arrowd2",OBJ_ARROW_UP,1,Time[y],D[y]);
            ObjectSet("arrowd2",OBJPROP_COLOR,Lime);
            break;
         case 4:
            ObjectSetText("resultDiv","Hidden Bearish Divergence",8,"Tahoma",Red);
            ObjectCreate(0,"arrowd2",OBJ_ARROW_UP,1,Time[y],D[y]);
            ObjectSet("arrowd2",OBJPROP_COLOR,Lime);
         break;
         case 3:
            ObjectSetText("resultDiv","Hidden Bullish Divergence",8,"Tahoma",LimeGreen);
            ObjectCreate(0,"arrowd2",OBJ_ARROW_UP,1,Time[y],D[y]);
            ObjectSet("arrowd2",OBJPROP_COLOR,Lime);
         break;
         default:
            ObjectSetText("resultDiv","None at the moment",8,"Tahoma",Yellow);
            break;
        
      
     }
     

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

void graphSet()
  {
   int ot=ObjectsTotal();
   if(ot>=0)
      ObjectsDeleteAll();

   IndicatorShortName("MultiStoch_Panel("+(string)K_Period+","+(string)D_Period+","+(string)Slowing+")");

   SetIndexBuffer(0,K);
   SetIndexBuffer(1,D);

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

   ObjectCreate("div",OBJ_LABEL,0,0,0,0,0);

//ObjectCreate("barNum",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("resultTurn",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("resultEMA",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("resultFIBO",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("resultDiv",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("resultUpDiv",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("turn",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("EMA",OBJ_LABEL,0,0,0,0,0);
   ObjectCreate("FIBONACCI",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("EMA",OBJPROP_XDISTANCE,50);
   ObjectSet("EMA",OBJPROP_YDISTANCE,265);
   ObjectSet("FIBONACCI",OBJPROP_XDISTANCE,50);
   ObjectSet("FIBONACCI",OBJPROP_YDISTANCE,283);
   ObjectSet("turn",OBJPROP_XDISTANCE,50);
   ObjectSet("turn",OBJPROP_YDISTANCE,250);
   ObjectSet("resultTurn",OBJPROP_XDISTANCE,130);
   ObjectSet("resultTurn",OBJPROP_YDISTANCE,250);
   ObjectSet("resultEMA",OBJPROP_XDISTANCE,130);
   ObjectSet("resultEMA",OBJPROP_YDISTANCE,265);
   ObjectSet("resultFIBO",OBJPROP_XDISTANCE,130);
   ObjectSet("resultFIBO",OBJPROP_YDISTANCE,283);
   ObjectSet("resultDiv",OBJPROP_XDISTANCE,130);
   ObjectSet("resultDiv",OBJPROP_YDISTANCE,300);
   ObjectSet("resultUpDiv",OBJPROP_XDISTANCE,130);
   ObjectSet("resultUpDiv",OBJPROP_YDISTANCE,326);
   ObjectSet("div",OBJPROP_XDISTANCE,50);
   ObjectSet("div",OBJPROP_YDISTANCE,300);
   ObjectSet("barn",OBJPROP_XDISTANCE,200);
   ObjectSet("barn",OBJPROP_YDISTANCE,350);
   ObjectSet("updiv",OBJPROP_XDISTANCE,50);
   ObjectSet("updiv",OBJPROP_YDISTANCE,320);
   ObjectSet("updiv2",OBJPROP_XDISTANCE,50);
   ObjectSet("updiv2",OBJPROP_YDISTANCE,335);
   ObjectSet("updivp",OBJPROP_XDISTANCE,119);
   ObjectSet("updivp",OBJPROP_YDISTANCE,325);

   ObjectSetText("div","Divergence     : ",8,"Tahoma",Gold);
   ObjectSetText("turn","Turning Point  : ",8,"Tahoma",Gold);
   ObjectSetText("EMA","eMA                : ",8,"Tahoma",Gold);
   ObjectSetText("FIBONACCI","Fibonacci lvl    : ",8,"Tahoma",Gold);
   ObjectSetText("resultTurn","-",8,"Tahoma",Gold);
   ObjectSetText("resultDiv","-",8,"Tahoma",Gold);
   ObjectSetText("resultUpDiv","None at the moment",8,"Tahoma",Gold);
   ObjectSetText("resultEMA","-",8,"Tahoma",Gold);
   ObjectSetText("resultFIBO","-",8,"Tahoma",Gold);
   ObjectSetText("updiv","Upcoming",8,"Tahoma",Gold);
   ObjectSetText("updiv2","Divergence",8,"Tahoma",Gold);
   ObjectSetText("updivp",":",8,"Tahoma",Gold);

  }
whroeder1
14514
whroeder1 2015.04.08 01:09  
double K[],D[],ema[],K_t[],D_t[],K2[],D2[],EMA_1[];
   for(k=1;k<=Bars-1;k++) calcDiv(k);
:
 void calcDiv(int k){
:
   bool        isHighTurn   = D[k]>D[k+1] && D[k]<D[k-1],
 
  1. D[] has no size, so D[k] is array exceeded. When k == Bars-1, D[k+1] is D[Bars] which would never exist even if you resized it to Bars.
  2.             RegularBullD = D[y]>D[y+1] && D[y]<D[y-1] && Low[z]>Low[y],
    
    Y has no value so you are accessing random elements which likely don't exist
  3. Z is undeclared. Don't post unless your code compiles.
  4.   int limit=Bars-calcBars-1;
       for(int i=Bars-1;i>=limit;i--){
          calcSto(i); }
    Will Only calculate the oldest bar. Do it right,  your lookback is k+1
    #define LOOKBACK 1
       for(int i=Bars-1-MathMax(calcBars, LOOKBACK) ;i>=0;i--){ // All bars
          calcSto(i); }

To add comments, please log in or register