hoping someone might explain my error with frcatal indicator construction

 

Hello Forum, I am attempting to put together an indicator that draws a horizontal line of fixed length  to the right of current price action at the level of the most recent high and low fractals,

and also draws trendlines through the 2 most recent high fractal positions 2 most recent low fractal positions.

I have had some success previously in drawing a horizontal line at current fractal levels (but not today, perhaps a loop problem that I can't see as am too close to it...)

But I remain confused about how to find the second non zero, not empty  buffer values for the low and high fractals and then the order I use them to create my trend lines

I have attempted to code below

Hoping someone might help me find the problems and help educate me, any advice appreciated

thanks Dave

//+------------------------------------------------------------------+
//|                                                                  |
//|                 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 to true to add fractals to chart";
       extern bool     Draw_FRACTALS            = true;    // set true to display fractals
       extern string   help2                    = "set to true to add FRACTALS_Trendlines to chart";
       extern bool     Draw_FRACTALS_TrendLines = true;    // set true to FRACTALS_Trendlines   
       extern string   help                     = "positions fractal Level Line to right of T[0]";
       extern string   helpBF                   = "MoveFractalLineToRightOfBar(0)";
       extern int      bars_forward             = 6;       //
       extern string   helpLL                   = "sets length of fractal horizontal lines";
       extern double   LevelLineLength          = 6;       // sets length of Level Line
       extern string   helpSF                   = "reduce SF below 1 for small screens";
       extern double   SF                       = 1.0;     // screen factor
       double          HVal,HVal2,LVal,LVal2;
       double          ExtUpFractalsBuffer[]; 
       double          ExtDownFractalsBuffer[];
       
//+-------------------------------------------------------------------------------------------------------------------------------------------------+
//| 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("HighFractal");
       ObjectDelete("LowFractal");
       ObjectDelete("HighFractalTrendLine");
       ObjectDelete("LowFractalTrendLine");
       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;
//--------------------------------------------------------------------------------------------------------------------------------------------------|
//---- main loop                                                                                                                                    |
//--------------------------------------------------------------------------------------------------------------------------------------------------|
       for(int i=0; i<limit; i++)
     {
//--   Define the Upper Fractals buffer as
       ExtUpFractalsBuffer[i]=iFractals(NULL,0,MODE_UPPER,i);
//     if (ExtUpFractalsBuffer[i]!=0) a high fractal should be visible on the chart      
       
//--   Define the Lower Fractals buffer as 
       ExtDownFractalsBuffer[i]=iFractals(NULL,0,MODE_LOWER,i); 
//     if (ExtDownFractalsBuffer[i]!=0) a low fractal should be visible on the chart
     }             
//--   Current bar can't by definition be a fractal so
       for (i=1; ExtUpFractalsBuffer[i]!=0; i++) 
       {
       HVal=iFractals(NULL,0,MODE_UPPER,i);      // corresponds to the most recent high fractal of price equal to HVaL
//--   But we need the 2 most recent HVal values  (time and price) to allow construction of high fractal trend lines       
       HVal2=iFractals(NULL,0,MODE_UPPER,i+1);   //--   2nd most recent high fractal is HVal2  

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//-----Reminders for me:
//--   bool ObjectCreate( string name, int type, int window, datetime time1, double price1, datetime time2=0, double price2=0, datetime time3=0, double price3=0)         
//--   Time[0] + SF*(LabelAdjHorizontal+bars_forward +5*LevelLineLength)* 60 * Period());
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
      
//---- Draw Current High Fractal Level Line 
       if (Draw_FRACTALS) // draw if true
       {
       if (ObjectFind  ("HighFractal") == -1)
       ObjectCreate    ("HighFractal", OBJ_TREND, 0, 0, 0, 0, 0);
       
       ObjectSet       ("HighFractal", OBJPROP_TIME1,       Time[0]+                   SF* bars_forward * 60 * Period() );
       ObjectSet       ("HighFractal", OBJPROP_TIME2,       Time[0]+ SF*(bars_forward +LevelLineLength) * 60 * Period() );
        
       ObjectSet       ("HighFractal", OBJPROP_PRICE1, HVal );
       ObjectSet       ("HighFractal", OBJPROP_PRICE2, HVal );
      
       ObjectSet       ("HighFractal", OBJPROP_RAY,   false); 
       ObjectSet       ("HighFractal", OBJPROP_COLOR, Red);
       ObjectSet       ("HighFractal", OBJPROP_WIDTH, 4);  
       }    
//---- Draw High Fractals Trend Line 
       if (Draw_FRACTALS_TrendLines) // draw if true
       {
       if (ObjectFind  ("HighFractalTrendLine") == -1)
//--   ObjectCreate    ("HighFractalTrendLine", OBJ_TREND, 0, datetime time1, double price1, datetime time2=0, double price2=0);
       ObjectCreate    ("HighFractalTrendLine", OBJ_TREND, 0, 0, 0, 0, 0);
       
       ObjectSet       ("HighFractalTrendLine", OBJPROP_TIME1, i+1 );
       ObjectSet       ("HighFractalTrendLine", OBJPROP_TIME2, i);
        
       ObjectSet       ("HighFractalTrendLine", OBJPROP_PRICE1, HVal2);
       ObjectSet       ("HighFractalTrendLine", OBJPROP_PRICE2, HVal );
      
       ObjectSet       ("HighFractalTrendLine", OBJPROP_COLOR, Red);
       ObjectSet       ("HighFractalTrendLine", OBJPROP_WIDTH, 4);  
       }
       }
//-------------------------------------------------------------------------------       
       
//--   Current bar can't by definition be a fractal so
       for(i=1; ExtDownFractalsBuffer[i]!=0; i++)
       {
       LVal=iFractals(NULL,0,MODE_LOWER,i);      // corresponds to the most recent low fractal of price equal to LVaL at Time[i]
//--   But we need the 2 most recent low Lval values (time and price) to allow construction of the low fractal trend lines
       LVal2= iFractals(NULL,0,MODE_LOWER,i+1);                                     
       
//---------------- Draw Low Fractal Line 
       if (Draw_FRACTALS) // draw if true
       {
       if (ObjectFind  ("LowFractal") == -1)
       ObjectCreate    ("LowFractal", OBJ_TREND, 0, 0, 0, 0, 0);
       
       ObjectSet       ("LowFractal", OBJPROP_TIME1,        Time[0]+                  SF* bars_forward * 60 * Period() );
       ObjectSet       ("LowFractal", OBJPROP_TIME2,        Time[0]+ SF*(bars_forward +LevelLineLength)* 60 * Period() );
       
       ObjectSet       ("LowFractal", OBJPROP_PRICE1, LVal );
       ObjectSet       ("LowFractal", OBJPROP_PRICE2, LVal );
       
       ObjectSet       ("LowFractal", OBJPROP_RAY,   false); 
       ObjectSet       ("LowFractal", OBJPROP_COLOR, Blue);
       ObjectSet       ("LowFractal", OBJPROP_WIDTH, 4);   
       }
 //---- Draw Low Fractals Trend Line 
       if (Draw_FRACTALS_TrendLines) // draw if true
       {
       if (ObjectFind  ("LowFractalTrendLine") == -1)
//--   ObjectCreate    ("LowFractalTrendLine", OBJ_TREND, 0, datetime time1, double price1, datetime time2=0, double price2=0);
       ObjectCreate    ("LowFractalTrendLine", OBJ_TREND, 0, 0, 0, 0, 0);
       
       ObjectSet       ("LowFractalTrendLine", OBJPROP_TIME1, i+1 );
       ObjectSet       ("LowFractalTrendLine", OBJPROP_TIME2, i);
        
       ObjectSet       ("LowFractalTrendLine", OBJPROP_PRICE1, LVal2);
       ObjectSet       ("LowFractalTrendLine", OBJPROP_PRICE2, LVal );
      
       ObjectSet       ("LowFractalTrendLine", OBJPROP_COLOR, Blue);
       ObjectSet       ("LowFractalTrendLine", OBJPROP_WIDTH, 4);  
       } 
       }
          
 //+-------------------------------------------------------------------------------------------------------+
       return(0);
       }
 //+-------------------------------------------------------------------------------------------------------+
 
 
pullend:

Hello Forum, I am attempting to put together an indicator that draws a horizontal line of fixed length  to the right of current price action at the level of the most recent high and low fractals,

and also draws trendlines through the 2 most recent high fractal positions 2 most recent low fractal positions.

I have had some success previously in drawing a horizontal line at current fractal levels (but not today, perhaps a loop problem that I can't see as am too close to it...)

But I remain confused about how to find the second non zero, not empty  buffer values for the low and high fractals and then the order I use them to create my trend lines

Read this:  https://www.mql5.com/en/forum/143563
 

Thanks Raptor, I had been stuck, taken a break from the coding and not realised WHRoeder had shared an additional piece of the answer 10 days ago.

Will reproduce it below and  see how far I can progress this indicator now. IT addresses the non zero bit of my question.

Thanks as always !!


 
RaptorUK:
Read this:  https://www.mql5.com/en/forum/143563


Still having problems here:

Have tried to simplify and take on advice from previous post, but must be a problem in my understanding of the outcome of the standard if statements at lines 73, 75, 100 and 102

(and consequently the advice in  https://www.mql5.com/en/forum/143563

Whilst the code below compiles without error

A check using CtrlB reveals that I am not getting any objects created, let alone incorrect objects

Some help with this would be greatly appreciated, I have been going round in circles for days.

My aim is to have this indicator draw trendlines through the most recent 2 high and low fractal values...

//+------------------------------------------------------------------+
//|                                                                  |
//|                 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                    = "use to add fractal lines to chart";
       extern bool     Draw_FractalsLines       = true;    // set true to display fractals
       double          HVal[],LVal[],HValTime[],LValTime[];
       double          ExtUpFractalsBuffer[]; 
       double          ExtDownFractalsBuffer[];
       int             Up;
       int             Down;
//+------------------------------------------------------------------+
//| 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;
//---- main loop
       for(int i=0; i<limit; i++)
       {
//--   Define the Upper Fractals buffer as
       ExtUpFractalsBuffer[i]=iFractals(NULL,0,MODE_UPPER,i);
//--   Define the Lower Fractals buffer as
       ExtDownFractalsBuffer[i]=iFractals(NULL,0,MODE_LOWER,i);
       
//----------------------------------------------------------------------------------------------------------
//--   AIM OF INDICATOR TO DRAW TRENDLINES THROUGH THE MOST RECENT 2 ExtUpFractalsBuffer  
//--       so want the last 2 NotEmpty Values of both Buffers
//--       call the most recent time that the up buffer is not an empty value: time = Up
//--       call the 2nd most recebr tome that the up buffer is not an empty value: time = (Up+1)  
//--       similar for down buffer, times are: Down and (Down+1)
//----------------------------------------------------------------------------------------------------------      
       
       if (ExtUpFractalsBuffer[i]!=EMPTY_VALUE)               //--Line 73
       return (HVal[Up]);
       if (ExtUpFractalsBuffer[i]==EMPTY_VALUE)               //--Line 75
       return (EMPTY);
//     put these values in an array of up values
       HVal[Up]=iFractals(NULL,0,MODE_UPPER,i);
       HValTime[Up]=Time[i];
       if (Up>1)
       { 
//--   Draw High Fractal Line 
       if (Draw_FractalsLines) // draw if true
       {
       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);  
       }       
       }
//----------------------------------------------------------------------------------------------------------           
       if (ExtDownFractalsBuffer[i]!=EMPTY_VALUE)          //-- Line 100
       return (LVal[Down]);
       if (ExtDownFractalsBuffer[i]==EMPTY_VALUE)          //-- Line 102
       return (EMPTY);
//--   put the values in the array of down fractals
       LVal[Down]=iFractals(NULL,0,MODE_LOWER,i);
       LValTime[Down]=Time[i];
       if (Down>1)
       {       
//--   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);   
       }
       }
      
      }
 
//+-------------------------------------------------------------------------------------------------------+
       return(0);
       }
//+-------------------------------------------------------------------------------------------------------+
 
 
pullend:

Still having problems here:

Have tried to simplify and take on advice from previous post, but must be a problem in my understanding of the outcome of the standard if statements at lines 73, 75, 100 and 102

(and consequently the advice in  https://www.mql5.com/en/forum/143563

Whilst the code below compiles without error

A check using CtrlB reveals that I am not getting any objects created, let alone incorrect objects

Have a think about this and explain how the code gets past these lines . . .

       if (ExtUpFractalsBuffer[i]!=EMPTY_VALUE)               //--Line 73
       return (HVal[Up]);

       if (ExtUpFractalsBuffer[i]==EMPTY_VALUE)               //--Line 75
       return (EMPTY);
 
RaptorUK:

Have a think about this and explain how the code gets past these lines . . .

 


Thanks for persisting with helping me here. Can't believe how much trouble I am having with this concept.

I drew myself a diagram, attached to show to myself that in the pictured example, I would want to take my Upper Buffer values from bars 2 and 11, and my lower fractal values from bars 4 and 12.

By my logic that means that iFractals(NULL,0,MODE_UPPER,i) will return an empty value from bars 0 and 1 and a not empty value at Bar2, then empty values for bars 3 through to 10, then, non empty value at Bar 11

My thoughts then are that the buffer containing upper fractal values for the purposes of fractal line drawing contains at Up=1 the value for bar 2 and at Up=2 the value for bar 11

Similarly for the buffer containing lower fractal values for the purpose of fractal line drawing contains at Down=1, the value of the bar 4 and at Down=2, the value of the bar 12

perhaps this is where I am coding inaccurately as I thought the above words equate to lines 73-76 and 100-103 below.

Now the indicator registers as having high and low fractal lines (CtrlB) but no values assigned to price or time to allow them to plot ????

//+------------------------------------------------------------------+
//|                                                                  |
//|                 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                    = "use to add fractal lines to chart";
       extern bool     Draw_FractalsLines       = true;    // set true to display fractals
       double          HVal[],LVal[],HValTime[],LValTime[];
       double          ExtUpFractalsBuffer[]; 
       double          ExtDownFractalsBuffer[];
       int             Up;
       int             Down;
//+------------------------------------------------------------------+
//| 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;
//---- main loop
       for(int i=0; i<limit; i++)
       {
//--   Define the Upper Fractals buffer as
       ExtUpFractalsBuffer[i]=iFractals(NULL,0,MODE_UPPER,i);
//--   Define the Lower Fractals buffer as
       ExtDownFractalsBuffer[i]=iFractals(NULL,0,MODE_LOWER,i);
       
//----------------------------------------------------------------------------------------------------------
//--   AIM OF INDICATOR TO DRAW TRENDLINES THROUGH THE MOST RECENT 2 ExtUpFractalsBuffer  
//--       so want the last 2 NotEmpty Values of both Buffers
//--       call the most recent time that the up buffer is not an empty value: time = Up
//--       call the 2nd most recebr tome that the up buffer is not an empty value: time = (Up+1)  
//--       similar for down buffer, times are: Down and (Down+1)
//----------------------------------------------------------------------------------------------------------      
       
       if (ExtUpFractalsBuffer[i]!=EMPTY_VALUE)               //--Line 73
       HVal[Up]=iFractals(NULL,0,MODE_UPPER,i);
       if (ExtUpFractalsBuffer[i]==EMPTY_VALUE)               //--Line 75
       HVal[Up]=iFractals(NULL,0,MODE_UPPER,Up);    
       HValTime[Up]=Time[i];
       Up++;
       if (Up>1)
       { 
//--   Draw High Fractal Line 
       if (Draw_FractalsLines) // draw if true
       {
       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);  
       }       
       }
//----------------------------------------------------------------------------------------------------------           
       if (ExtDownFractalsBuffer[i]!=EMPTY_VALUE)          //-- Line 100
       LVal[Down]=iFractals(NULL,0,MODE_LOWER,i);
       if (ExtDownFractalsBuffer[i]==EMPTY_VALUE)          //-- Line 102
       LVal[Down]=iFractals(NULL,0,MODE_LOWER,Down);
       LValTime[Down]=Time[i];
       Down++;
       if (Down>1)
       {       
//--   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);   
       }
       }
      
      }
 
//+-------------------------------------------------------------------------------------------------------+
       return(0);
       }
//+-------------------------------------------------------------------------------------------------------+
 
 
pullend:

Thanks for persisting with helping me here. Can't believe how much trouble I am having with this concept.


Perhaps you could answer the question I asked ?  there was a good reason for asking it . . .
 
RaptorUK:
Perhaps you could answer the question I asked ?  there was a good reason for asking it . . .


My apologies, when I read your question, I jumped to the answer and assumed it obviously can't get past these lines.

I actually had never used "return" in my simple programming so far, and only tried using it because of WHRoeders post.

Now reading the documentation again.

I believe I would have to set up the code in away to read something like this, mind you I have no experience of formatting it properly

This way the HVal[Up] buffer only gets populated when Up=i


for(i=0;i<limit;i++)
{
if iFractals(NULL,0,MODE_UPPER,i)!=EMPTY_VALUE
return (Up=i);return (Up=EMPTY);}
HVal[Up]=iFractals(NULL,0,MODE_UPPER,i);
}

Am I on the right track here? That being that the calculations have to be completed within the {} to allow the code to move forward?

 
pullend:

My apologies, when I read your question, I jumped to the answer and assumed it obviously can't get past these lines.

I actually had never used "return" in my simple programming so far, and only tried using it because of WHRoeders post.

Now reading the documentation again.

I believe I would have to set up the code in away to read something like this, mind you I have no experience of formatting it properly

This way the HVal[Up] buffer only gets populated when Up=i

Am I on the right track here? That being that the calculations have to be completed within the {} to allow the code to move forward?

It seems you do not understand what return does . . . .  what calls the start() function ?  to what are you returning a value ?  why do you want the start() function to exit there ?

WHRoeder used return at the end of his user defined function. 

 
RaptorUK:

It seems you do not understand what return does . . . .  what calls the start() function ?  to what are you returning a value ?  why do you want the start() function to exit there ?

WHRoeder used return at the end of his user defined function. 


While I try and learn how to use return correctly, thought I would go back to your advice in the other post referenced above "..so create a loop,  for or while,  they are interchangeable,  that starts at 0,  the last arrow position and loops through checking if Buffer[i] is != EMPTY_VALUE,  when you find the next one that isn't EMPTY_VALUE it is your next arrow....."

If possible, would you please explain why the relevant loop below is not delivering the desired fractal object values. This newbie is getting really stressed at not being able to work this out

thanks as always

//+------------------------------------------------------------------+
//|                                                                  |
//|                 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                    = "use to add fractal lines to chart";
       extern bool     Draw_FractalsLines       = true;    // set true to display fractals
       double          HVal[],LVal[],HValTime[],LValTime[];
       double          ExtUpFractalsBuffer[]; 
       double          ExtDownFractalsBuffer[];
       int             Up;
       int             Down;
//+------------------------------------------------------------------+
//| 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;
//---- main loop
       for(int i=0; i<limit; i++)
     {
//--   Define the Upper Fractals buffer as
       ExtUpFractalsBuffer[i]=iFractals(NULL,0,MODE_UPPER,i);
       if (iFractals(NULL,0,MODE_UPPER,i)!=EMPTY_VALUE)
       HVal[Up]=iFractals(NULL,0,MODE_UPPER,i);
       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];
     
//--   Draw High Fractal Line
       
       if (Draw_FractalsLines) // draw if true
       {
       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);
       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);   
       }
      } 
//+-------------------------------------------------------------------------------------------------------+
       return(0);
       }
//+-------------------------------------------------------------------------------------------------------+
 
pullend:

While I try and learn how to use return correctly, thought I would go back to your advice in the other post referenced above "..so create a loop,  for or while,  they are interchangeable,  that starts at 0,  the last arrow position and loops through checking if Buffer[i] is != EMPTY_VALUE,  when you find the next one that isn't EMPTY_VALUE it is your next arrow....."

If possible, would you please explain why the relevant loop below is not delivering the desired fractal object values. This newbie is getting really stressed at not being able to work this out

thanks as always

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 . . .

//+------------------------------------------------------------------+
//|                                                                  |
//|                 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                    = "use to add fractal lines to chart";
       extern bool     Draw_FractalsLines       = true;    // set true to display fractals
       double          HVal[],LVal[],HValTime[],LValTime[];
       double          ExtUpFractalsBuffer[]; 
       double          ExtDownFractalsBuffer[];
       int             Up;
       int             Down;
//+------------------------------------------------------------------+
//| 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;
   
   //---- main loop
   for(int i=0; i<limit; i++)
      {
      //--   Define the Upper Fractals buffer as
      ExtUpFractalsBuffer[i]=iFractals(NULL,0,MODE_UPPER,i);
      if (iFractals(NULL,0,MODE_UPPER,i)!=EMPTY_VALUE)
         HVal[Up]=iFractals(NULL,0,MODE_UPPER,i);

      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];
     
      //--   Draw High Fractal Line
      if (Draw_FractalsLines) // draw if true
         {
         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);
      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);   
         }
      } 
   //+-------------------------------------------------------------------------------------------------------+
   return(0);
   }

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

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

Reason: