hoping someone might explain my error with frcatal indicator construction - page 2

 

You can now see that this code . . .

//--   Define the Lower Fractals buffer as

  . . .  is outside of your 

   //---- main loop
   for(int i=0; i<limit; i++)
 

And this . . .

      for (int Up=1;Up<limit;Up++)
         if (iFractals(NULL,0,MODE_UPPER,i)==EMPTY_VALUE)
            HVal[Up]=iFractals(NULL,0,MODE_UPPER,Up-1);

      HValTime[Up]=Time[i];

  . . .  was the last line meant to be executed with the for loop ?  because it isn't.

Similar for this . . . 

 

      for(int Down=1;Down<limit;Down++)
         if (ExtDownFractalsBuffer[i]==EMPTY_VALUE)          
            LVal[Down]=iFractals(NULL,0,MODE_LOWER,Down-1);

      LValTime[Down]=Time[i];

 

what about these   {  }  braces,  what are they for ?

   //----------------------------------------------------------------------------------------------------------           
      {
      //--   Define the Lower Fractals buffer as
      ExtDownFractalsBuffer[i]=iFractals(NULL,0,MODE_LOWER,i);
      if (iFractals(NULL,0,MODE_LOWER,i)!=EMPTY_VALUE)
         LVal[Down]=iFractals(NULL,0,MODE_LOWER,i);

      for(int Down=1;Down<limit;Down++)
         if (ExtDownFractalsBuffer[i]==EMPTY_VALUE)          
            LVal[Down]=iFractals(NULL,0,MODE_LOWER,Down-1);

      LValTime[Down]=Time[i];
            
      //--   Draw Low Fractal Line 
      if (Draw_FractalsLines) // draw if true
         {
         if (ObjectFind  ("LowFractalsLine") == -1)
            ObjectCreate    ("LowFractalsLine", OBJ_TREND, 0, 0, 0, 0, 0);

         ObjectSet       ("LowFractalsLine", OBJPROP_TIME1,  LValTime[Down+1] );
         ObjectSet       ("LowFractalsLine", OBJPROP_TIME2,  LValTime[Down]   );
         ObjectSet       ("LowFractalsLine", OBJPROP_PRICE1, LVal[Down+1] );
         ObjectSet       ("LowFractalsLine", OBJPROP_PRICE2, LVal[Down]   );
         ObjectSet       ("LowFractalsLine", OBJPROP_RAY,   true); 
         ObjectSet       ("LowFractalsLine", OBJPROP_COLOR, Blue);
         ObjectSet       ("LowFractalsLine", OBJPROP_WIDTH, 4);   
         }
      } 
 

One other thing,  these arrays,  yes they are arrays not buffers,  you have 2 buffers,  they are ExtUpFractalsBuffer and  ExtDownFractalsBuffer   . . .

       double          HVal[],LVal[],HValTime[],LValTime[];

 they all have a size of zero elements . . . you don't give them a size or resize them so they are all size 0,  so any values you assign to them will be forgotten and you will always read zero from them.  So if you draw a line it will be back in 1970 . . .

 
RaptorUK:

Do you find it easy to read your own code without using any kind of indenting to mark blocks of code controlled by   {  }  braces ?  I don't,  i find it a pain in the neck and it takes me at least twice as long to follow,  maybe more . . .

To read your code easily I have to do this . . .

  . . .  that took me 7 minutes.  Now I can read it . . .

Simon give a try to mql5 editor, copy & paste the code in an empty new file in metaeditor5 and then CTRL+,
 
angevoyageur:
Simon give a try to mql5 editor, copy & paste the code in an empty new file in metaeditor5 and then CTRL+,

It may be a good idea, I hope the OP will take note of it.
 
RaptorUK:
It may be a good idea, I hope the OP will take note of it.


Thank you, thank you, thank you Raptor for this help.

It is probably the result of not having any programming background that I both struggle with my loops and practice bad indenting habits.

With respect to the indenting, I did not know it was a bad habit   :-(    but do now! Have tried to improve the indenting in the code below.

To answer one of your questions, I was using the {}braces incorrectly in attempt to control what was in my loops. Had it in my head that I had to separate Time[i] for upper and lower fractals..

I have the code almost working properly now.

It plots the fractal lines as I expected, but when a new fractal is formed, the fractal line is drawn in the new correct position, but then disappears on the open of the next bar.

I have not been able to work out why and would appreciate some advice with this if possible.

When I look at the objects using CtrlB after the line disappears, it shows that I have a zero length trend line of the same start and end point, not sure why?

I assume I am referencing the array values incorrectly.

Also, I noticed the indicator was plotting fractal lines corresponding to the last 2 values in my arrays rather than the first two.

But this was fixed when I changed " for(int i=0; i<limit;i++)" to "for(int i = limit; i>0; i--)"

I am confused about this solution, but it works?

Aslo have not used metaeditor 5.....should I be worried about such things at this early part of my coding schooling?


//+------------------------------------------------------------------+
//|                                                                  |
//|                 Copyright © 1999-2008, MetaQuotes Software Corp. |
//|                                         http://www.metaquotes.ru |
//+------------------------------------------------------------------+

//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 2
  
//---- input parameters  
      
       extern string   help1                    = "set true to add fractal lines to chart";
       extern bool     Draw_FractalsLines       = true;    // set true to display fractals
       double          HVal                     [10000];
       double          LVal                     [10000];
       double          HValTime                 [10000];
       double          LValTime                 [10000];
       double          ExtUpFractalsBuffer[]; 
       double          ExtDownFractalsBuffer[];
       int             Up=0;
       int             Down=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
   {
   //---- indicator buffers mapping  
   SetIndexBuffer(0,ExtUpFractalsBuffer);
   SetIndexBuffer(1,ExtDownFractalsBuffer);
   
   //----
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   
   //---- DataWindow and indicator subwindow label
   IndicatorShortName("current fractal levels");
   SetIndexLabel(0,"CurrentHVal");
   SetIndexLabel(1,"CurrentLVal");
      
   //---- initialization done
   return(0);
   }

//+-------------------------------------------------------------------------------------------------------+
//---- Custom indicator deinitialization function                                                         |
//+-------------------------------------------------------------------------------------------------------+
int deinit()
   {
   ObjectDelete("HighFractalsLine");
   ObjectDelete("LowFractalsLine");
   return(0);
   }

//+-------------------------------------------------------------------------------------------------------+
int start()
   {
   int limit;
   int counted_bars=IndicatorCounted();

   //---- check for possible errors
   if(counted_bars<0) return(-1);

   //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
      limit=Bars-counted_bars;
    double buf = 0; // buffer value of fractal being available; if it is 0, there is no fractal at all
   //---- main loop
   for(int i = limit; i>0; i--)
      {
      
      //--   Define the Upper Fractals buffer as
      ExtUpFractalsBuffer[i]=iFractals(NULL,0,MODE_UPPER,i);
      buf=iFractals(NULL,0,MODE_UPPER,i);
      
         if (buf!=0)
         {
         Up++;
         HVal[Up]=iFractals(NULL,0,MODE_UPPER,i);
         HValTime[Up]=Time[i];
         if(Up>1) 
                 {
                 if (Draw_FractalsLines) // draw if true //--   Draw High Fractal Line
                       {
                        if (ObjectFind      ("HighFractalsLine") == -1)
                        ObjectCreate    ("HighFractalsLine", OBJ_TREND, 0, 0, 0, 0, 0);
          
                        ObjectSet       ("HighFractalsLine", OBJPROP_TIME1,  HValTime[Up-1] );
                        ObjectSet       ("HighFractalsLine", OBJPROP_TIME2,  HValTime[Up]);
                        ObjectSet       ("HighFractalsLine", OBJPROP_PRICE1, HVal[Up-1] );
                        ObjectSet       ("HighFractalsLine", OBJPROP_PRICE2, HVal[Up] );
                        ObjectSet       ("HighFractalsLine", OBJPROP_RAY,   true); 
                        ObjectSet       ("HighFractalsLine", OBJPROP_COLOR, Red);
                        ObjectSet       ("HighFractalsLine", OBJPROP_WIDTH, 4);
                        
                        
                            
                        }
                 }              
         }
//----------------------------------------------------------------------------------------------------------           
       //--   Define the Lower Fractals buffer as
       ExtDownFractalsBuffer[i]=iFractals(NULL,0,MODE_LOWER,i);
       buf=iFractals(NULL,0,MODE_LOWER,i);
           
         if (buf!=0)
         {
         Down++;
         LVal[Down]=iFractals(NULL,0,MODE_LOWER,i);
         LValTime[Down]=Time[i];
         if (Down>1)
                 {
                 if (Draw_FractalsLines) // draw if true//--   Draw Low Fractal Line 
                       {
                        if (ObjectFind      ("LowFractalsLine") == -1)
                        ObjectCreate    ("LowFractalsLine", OBJ_TREND, 0, 0, 0, 0, 0);

                        ObjectSet       ("LowFractalsLine", OBJPROP_TIME1,  LValTime[Down-1] );
                        ObjectSet       ("LowFractalsLine", OBJPROP_TIME2,  LValTime[Down]);
                        ObjectSet       ("LowFractalsLine", OBJPROP_PRICE1, LVal[Down-1] );
                        ObjectSet       ("LowFractalsLine", OBJPROP_PRICE2, LVal[Down]);
                        ObjectSet       ("LowFractalsLine", OBJPROP_RAY,   true); 
                        ObjectSet       ("LowFractalsLine", OBJPROP_COLOR, Blue);
                        ObjectSet       ("LowFractalsLine", OBJPROP_WIDTH, 4);
                        
                       }
                 }
         }  
   }        
   //+-------------------------------------------------------------------------------------------------------+
   return(0);
   }

//+-------------------------------------------------------------------------------------------------------+
 
pullend:


I have not been able to work out why and would appreciate some advice with this if possible.

When I look at the objects using CtrlB after the line disappears, it shows that I have a zero length trend line of the same start and end point, not sure why?

I assume I am referencing the array values incorrectly.

When the Indicator first runs it checks all bars and plots all Fractals,  after that for each tick it only plots the fractals for new bars . . .  and this will only be bar number 1, so you will only use array index 1 . . .

You have 2 trend lines, they each need 4 values,   2 time and 2 price values so in total you need 8 variables,  why do you have 4 arrays each with space for 10,000 values ?  that is space for 40,000 variables when you only need 8 ? 

 

For the lines to be re-draw buf must be non-zero,  have you checked if this is the case for all bars ?  I suspect it is,  you need to investigate this and find out why it is non-zero and fix it.   

 

MetaEditor 5 is for mql5, it was suggested because it will automatically indent your code for you,  once indented you could then copy and paste it back into Meta Editor 4.  mql5 and mql4 are chalk and cheese . . .  

 

One additional thing,  dates and times are not doubles,  they should be datetime variable types . . .

       double          HValTime                 [10000];
       double          LValTime                 [10000];


// should be . . . 


       datetime        HValTime                 [10000];
       datetime        LValTime                 [10000];
 

I would do something like this,  I have left most of the code I have changed in but  //  commented out so you can see the changes . . .

//+------------------------------------------------------------------+
//|                                                                  |
//|                 Copyright © 1999-2008, MetaQuotes Software Corp. |
//|                                         http://www.metaquotes.ru |
//+------------------------------------------------------------------+

//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 2
  
//---- input parameters  
      
extern string   help1                    = "set true to add fractal lines to chart";
extern bool     Draw_FractalsLines       = true;    // set true to display fractals

//       double          HVal                     [10000];
//       double          LVal                     [10000];
//       double          HValTime                 [10000];
//       double          LValTime                 [10000];

//  declare the 8 variables needed for the 2 Trend Lines
   
double          HighPriceStart, HighPriceEnd; 
datetime        HighTimeStart, HighTimeEnd; 

double          LowPriceStart, LowPriceEnd; 
datetime        LowTimeStart, LowTimeEnd; 

//  Indicator buffers
double          ExtUpFractalsBuffer[]; 
double          ExtDownFractalsBuffer[];

int             Up = 0;
int             Down = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
   {
   //---- indicator buffers mapping  
   SetIndexBuffer(0,ExtUpFractalsBuffer);
   SetIndexBuffer(1,ExtDownFractalsBuffer);
   
   //----
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   
   //---- DataWindow and indicator subwindow label
   IndicatorShortName("current fractal levels");
   SetIndexLabel(0,"CurrentHVal");
   SetIndexLabel(1,"CurrentLVal");
      
   //---- initialization done
   return(0);
   }

//+-------------------------------------------------------------------------------------------------------+
//---- Custom indicator deinitialization function                                                         |
//+-------------------------------------------------------------------------------------------------------+
int deinit()
   {
   ObjectDelete("HighFractalsLine");
   ObjectDelete("LowFractalsLine");
   return(0);
   }

//+-------------------------------------------------------------------------------------------------------+
int start()
   {
   int limit;
   int counted_bars = IndicatorCounted();

   //---- check for possible errors
   if(counted_bars<0) return(-1);

   //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
      limit=Bars-counted_bars;
      
   double buf = 0; // buffer value of fractal being available; if it is 0, there is no fractal at all

   //---- main loop
   for(int i = limit; i>0; i--)
      {
      
      //--   Define the Upper Fractals buffer as
      ExtUpFractalsBuffer[i] = iFractals(NULL,0,MODE_UPPER,i);
//      buf = iFractals(NULL,0,MODE_UPPER,i);
      
      if (ExtUpFractalsBuffer[i] != 0)
         {
         // debugging
//         if(i < 11) Print("i is ", i, " high buf = ", ExtUpFractalsBuffer[i]);
         
//         Up++;
//         HVal[Up]=iFractals(NULL,0,MODE_UPPER,i);
//         HValTime[Up]=Time[i];
//         if(Up>1) 
//            {

         if(HighTimeEnd < Time[i])  // check if there is a new fractal
            {
            // copy end price/time to start price/time for new line position
            HighPriceStart = HighPriceEnd; 
            HighTimeStart = HighTimeEnd; 
            
            // set new end price/time
            HighPriceEnd = ExtUpFractalsBuffer[i];
            HighTimeEnd = Time[i];
            }
            
         if (Draw_FractalsLines) // draw if true //--   Draw High Fractal Line
            {
            if (ObjectFind      ("HighFractalsLine") == -1)
               ObjectCreate    ("HighFractalsLine", OBJ_TREND, 0, 0, 0, 0, 0);
          
            ObjectSet       ("HighFractalsLine", OBJPROP_TIME1,  HighTimeStart );
            ObjectSet       ("HighFractalsLine", OBJPROP_TIME2,  HighTimeEnd );
            ObjectSet       ("HighFractalsLine", OBJPROP_PRICE1, HighPriceStart );
            ObjectSet       ("HighFractalsLine", OBJPROP_PRICE2, HighPriceEnd );
            ObjectSet       ("HighFractalsLine", OBJPROP_RAY,   true); 
            ObjectSet       ("HighFractalsLine", OBJPROP_COLOR, Red);
            ObjectSet       ("HighFractalsLine", OBJPROP_WIDTH, 4);
//               }
            }              
         }
      //----------------------------------------------------------------------------------------------------------           
      //--   Define the Lower Fractals buffer as
      ExtDownFractalsBuffer[i] = iFractals(NULL,0,MODE_LOWER,i);
//      buf=iFractals(NULL,0,MODE_LOWER,i);
           
      if (ExtDownFractalsBuffer[i] != 0)
         {
         // debugging
//         if(i < 11) Print("i is ", i, " low buf = ", ExtDownFractalsBuffer[i]);

//         Down++;
//         LVal[Down]=iFractals(NULL,0,MODE_LOWER,i);
//         LValTime[Down]=Time[i];
         
         if(LowTimeEnd < Time[i])  // check if there is a new fractal
            {
            // copy end price/time to start price/time for new line position
            LowPriceStart = LowPriceEnd; 
            LowTimeStart = LowTimeEnd;
            
            // set new end price/time
            LowPriceEnd = ExtDownFractalsBuffer[i];
            LowTimeEnd = Time[i];
            }
            
//         if (Down>1)
//            {
         if (Draw_FractalsLines) // draw if true//--   Draw Low Fractal Line 
            {
            if (ObjectFind      ("LowFractalsLine") == -1)
               ObjectCreate    ("LowFractalsLine", OBJ_TREND, 0, 0, 0, 0, 0);
               
            ObjectSet       ("LowFractalsLine", OBJPROP_TIME1,  LowTimeStart );
            ObjectSet       ("LowFractalsLine", OBJPROP_TIME2,  LowTimeEnd );
            ObjectSet       ("LowFractalsLine", OBJPROP_PRICE1, LowPriceStart );
            ObjectSet       ("LowFractalsLine", OBJPROP_PRICE2, LowPriceEnd );
            ObjectSet       ("LowFractalsLine", OBJPROP_RAY,   true); 
            ObjectSet       ("LowFractalsLine", OBJPROP_COLOR, Blue);
            ObjectSet       ("LowFractalsLine", OBJPROP_WIDTH, 4);
//               }
            }
         }  
      }        
   //+-------------------------------------------------------------------------------------------------------+
   return(0);
   }

//+-------------------------------------------------------------------------------------------------------+
 
Many thanks !!!!!!! I will study the suggested code closely!
Reason: