how do I define current fractal level

 

Hello Forum

Hope someone might assist me with the code below.

I am tring to produce an indicator that draws the most recent high and low fractal levels as lines to the right of the most recent price bar or candle

But I am unsure how to write lines 71 and 81 below, and I suspect the use of 'lookback"

I have marked lines 70 and 80 for convenience

I want the indicator to draw upon the last non zero value of HVal and LVal that would be in the array for occasions when it would otherwise read zero (which I presume would always be the case for the current bar. Just not sure how to achieve this

Thanks in advance

Dave


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

//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 2
  
//---- input parameters  
       extern bool     FRACTAL                     = true;               // set true to display fractals
       extern int      bars_forward                =6;
       double          HVal,LVal;
       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("HighFractal_label");
       ObjectDelete("LowFractal");
       ObjectDelete("LowFractal_label");
        
       return(0);
       }
//+-------------------------------------------------------------------------------------------------------+
int start()
     {   
       int counted_bars=IndicatorCounted();
//---- the last calculated bar will be recalculated   
       if (counted_bars > 0) 
       counted_bars--;
       int limit = Bars - counted_bars;
      
//---- the main loop       
       for(int i = limit; i>0; i--)

//----------------- DETERMINE FRACTAL LEVELS:
       
//--   Define the Upper Fractals buffer as
       ExtUpFractalsBuffer[i]=iFractals(NULL,0,MODE_UPPER,i);

//--   If it is available, put in the array of fractals for higher levels
       if (ExtUpFractalsBuffer[i]!=0)
       HVal=iFractals(NULL,0,MODE_UPPER,i);

//--LINE 70:If it is not available, look back for the most recent HVal value
       else HVal=.........iFractals(NULL,0,MODE_UPPER,i);
                   
//--   Define the Lower Fractals buffer as 
       ExtDownFractalsBuffer[i]=iFractals(NULL,0,MODE_LOWER,i);

//--   If it is available put in the array of lower levels
       if (ExtDownFractalsBuffer[i]!=0)
       LVal=iFractals(NULL,0,MODE_LOWER,i);
       
//--LINE 80:If it is not available, look back for the most recent LVal value
       else LVal=.........iFractals(NULL,0,MODE_LOWER,i);      
      
//---------------- Draw High Fractal Line (most recent HVal level)
       if (FRACTAL) // draw if true
       ObjectCreate ("HighFractal", OBJ_TREND,     0, (Time[0]+500),HVal, (Time[0]+1500),HVal);
       ObjectSet    ("HighFractal", OBJPROP_RAY,   true); 
       ObjectSet    ("HighFractal", OBJPROP_TIME1, (Time[0]+500));
       ObjectSet    ("HighFractal", OBJPROP_TIME2, (Time[0]+1500));
       ObjectSet    ("HighFractal", OBJPROP_COLOR, Lime);
       ObjectSet    ("HighFractal", OBJPROP_WIDTH, 3);   
     
 // -- Draw High Fractal Line Label 
       if (FRACTAL) // draw if true
       if (ObjectFind("HighFractal_label") == -1) 
      
       ObjectCreate ("HighFractal_label", OBJ_TEXT,       0, 0, 0);
       ObjectSet    ("HighFractal_label", OBJPROP_TIME1,  Time[0] + 800 * Period() );
       ObjectSet    ("HighFractal_label", OBJPROP_PRICE1, HVal);
       ObjectSet    ("HighFractal_label", OBJPROP_COLOR,  Lime);
       ObjectSetText("HighFractal_label","PivotHigh", 9,  "Arial", Lime);   
       
 //---------------- Draw Low Fractal Line (most recent LVal level)
       if (FRACTAL) // draw if true
       ObjectCreate ("LowFractal", OBJ_TREND,     0, (Time[0]+500),LVal, (Time[0]+1500),LVal);
       ObjectSet    ("LowFractal", OBJPROP_RAY,   true); 
       ObjectSet    ("LowFractal", OBJPROP_TIME1, (Time[0]+500));
       ObjectSet    ("LowFractal", OBJPROP_TIME2, (Time[0]+1500));
       ObjectSet    ("LowFractal", OBJPROP_COLOR, Lime);
       ObjectSet    ("LowFractal", OBJPROP_WIDTH, 3);   
     
 // -- Draw Low Fractal Line Label 
       if (FRACTAL) // draw if true
       if (ObjectFind("LowFractal_label") == -1) 
      
       ObjectCreate ("LowFractal_label", OBJ_TEXT,      0, 0, 0);
       ObjectSet    ("LowFractal_label", OBJPROP_TIME1, Time[0] + 800 * Period() );
       ObjectSet    ("LowFractal_label", OBJPROP_PRICE1, LVal);
       ObjectSet    ("LowFractal_label", OBJPROP_COLOR,  Lime);
       ObjectSetText("LowFractal_label","PivotHigh", 9,  "Arial", Lime);       
     
 //+-------------------------------------------------------------------------------------------------------+
       return(0);
       }
 //+-------------------------------------------------------------------------------------------------------+
 
 
Your Post
//--   If it is available, put in the array of fractals for higher levels
       if (ExtUpFractalsBuffer[i]!=0)
       HVal=iFractals(NULL,0,MODE_UPPER,i);

//--LINE 70:If it is not available, look back for the most recent HVal value
       else HVal=.........iFractals(NULL,0,MODE_UPPER,i);
The value of "i" is bogus here, you are outside of the loop so "i" == -1
Try
for(i=1; ExtUpFractalsBuffer[i]==0; i++) {}
HVal=iFractals(NULL,0,MODE_UPPER,i);
 
WHRoeder:
Your Post
The value of "i" is bogus here, you are outside of the loop so "i" == -1
Try

Think I have this working, compiles and seems to run ok, although still a little uncertain about lines 77 to 85 (marked for convenience) and why I am reading the correct and most recent fractal value

Appreciate your help !!



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

//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 2
  
//---- input parameters  
       extern string   help0                            = "PositionFractalLineToRightOfBar(0)";
       extern int      bars_forward                     = 12;       // NOT USED YET: get lines and labels working first
       extern string   help1                            = "set to true to add fractals to chart";
       extern bool     Draw_FRACTAL                     = true;   // set true to display fractals
       extern string   help2                            = "set to true to add fractal labels to chart";
       extern bool     DrawFractalLabels                = true;   // set true to display fractal labels
       
       double          HVal,LVal;
       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("HighFractal_label");
       ObjectDelete("LowFractal");
       ObjectDelete("LowFractal_label");
       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); 
       
//--   If they are available, put in the array of fractals for higher levels      // LINE 77
       if (ExtUpFractalsBuffer[i]!=0)
       HVal=iFractals(NULL,0,MODE_UPPER,i);
       if (ExtDownFractalsBuffer[i]!=0)
       LVal=iFractals(NULL,0,MODE_LOWER,i);
       }
//--   If they are not available, then must look back to previous       
       for (i=1; ExtUpFractalsBuffer[i]==0; i++) {}
       {
       HVal=iFractals(NULL,0,MODE_UPPER,i);
       }
       for(i=1; ExtDownFractalsBuffer[i]==0; i++) {}
       {
       LVal=iFractals(NULL,0,MODE_LOWER,i);                                       // LINE 85
       }
      
//---------------- Draw High Fractal Line 
       if (Draw_FRACTAL) // draw if true
       {
       if (ObjectFind  ("HighFractal") == -1)
       ObjectCreate    ("HighFractal", OBJ_TREND, 0, 0, 0, 0, 0);
       
       ObjectSet       ("HighFractal", OBJPROP_TIME1,(Time[0]+    1.5*bars_forward * 60 * Period()) );
       ObjectSet       ("HighFractal", OBJPROP_TIME2,(Time[0]+    2*  bars_forward * 60 * Period()) );
        
       ObjectSet       ("HighFractal", OBJPROP_PRICE1, HVal );
       ObjectSet       ("HighFractal", OBJPROP_PRICE2, HVal );
      
       ObjectSet       ("HighFractal", OBJPROP_RAY,   false); 
       ObjectSet       ("HighFractal", OBJPROP_COLOR, Lime);
       ObjectSet       ("HighFractal", OBJPROP_WIDTH, 3);  
       } 
     
 // -- Draw High Fractal Line Label 
       if (DrawFractalLabels) // draw if true
       {
       if (ObjectFind  ("HighFractal_label") == -1) 
       ObjectCreate    ("HighFractal_label", OBJ_TEXT, 0, 0, 0);
       
       ObjectSet       ("HighFractal_label", OBJPROP_TIME1,  (Time[0]+ 1.8* bars_forward * 60 * Period())   );
              
       ObjectSet       ("HighFractal_label", OBJPROP_PRICE1, HVal);
       
       ObjectSet       ("HighFractal_label", OBJPROP_COLOR,  Lime);
       ObjectSetText   ("HighFractal_label","Current High Fractal", 9,  "Arial", Lime);   
       }
       
 //---------------- Draw Low Fractal Line 
       if (Draw_FRACTAL) // draw if true
       {
       if (ObjectFind  ("LowFractal") == -1)
       ObjectCreate    ("LowFractal", OBJ_TREND, 0, 0, 0, 0, 0);
       
       ObjectSet       ("LowFractal", OBJPROP_TIME1, (Time[0]+    1.5*bars_forward * 60 * Period()));
       ObjectSet       ("LowFractal", OBJPROP_TIME2, (Time[0]+    2*  bars_forward * 60 * Period()));
       
       ObjectSet       ("LowFractal", OBJPROP_PRICE1, LVal );
       ObjectSet       ("LowFractal", OBJPROP_PRICE2, LVal );
       
       ObjectSet       ("LowFractal", OBJPROP_RAY,   false); 
       ObjectSet       ("LowFractal", OBJPROP_COLOR, Lime);
       ObjectSet       ("LowFractal", OBJPROP_WIDTH, 3);   
       }
     
 // -- Draw Low Fractal Line Label 
       if (DrawFractalLabels) // draw if true
       {
       if (ObjectFind  ("LowFractal_label") == -1) 
       ObjectCreate    ("LowFractal_label", OBJ_TEXT,      0, 0, 0);
       
       ObjectSet       ("LowFractal_label", OBJPROP_TIME1, (Time[0]+ 1.8* bars_forward * 60 * Period()) );
       
       ObjectSet       ("LowFractal_label", OBJPROP_PRICE1, LVal);
       
       ObjectSet       ("LowFractal_label", OBJPROP_COLOR,  Lime);
       ObjectSetText   ("LowFractal_label","Current Low Fractal", 9,  "Arial", Lime);       
       }
     
 //+-------------------------------------------------------------------------------------------------------+
       return(0);
       }
 //+-------------------------------------------------------------------------------------------------------+