close higher than fractal: doing something wrong

 

Hello Forum, my newbie understanding is getting me into trouble with this code and I desperately need some help after going in circles for hours.

I am trying to adapt the fractals indicator to include additional buffers that draw arrows on the chart when price closes above the current upper fractal or below the current lower fractal.

I am not sure how to get the Close price to reference only the current upper or lower fractal, think I have the rest of the code right (see below)

I experimented with positioning of my definitions for my additional buffers, even duplicated them in each relevant { }

Also when I look at the Data Window, candles that have not got fractals attached, do not have a current fractal value and not sure how to combat this

I tried another version of the fractal indicator specified in terms of iFractal...but again struck problems referencing the current fractal

Hoping for some help.....and yes I am struggling with concept of arrays

thanks in advance

dave


//+------------------------------------------------------------------+
//|                                                     Fractals.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Blue
#property indicator_color3 Green
#property indicator_color4 Red


//---- input parameters
extern int PipBuffer =4;
//---- buffers
double ExtUpFractalsBuffer[];
double ExtDownFractalsBuffer[];
double ExtMapBuffer1[];
double ExtMapBuffer2[];



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator buffers mapping fractals
    SetIndexBuffer(0,ExtUpFractalsBuffer);
    SetIndexBuffer(1,ExtDownFractalsBuffer);
    SetIndexBuffer(2,ExtMapBuffer1);
    SetIndexBuffer(3,ExtMapBuffer2);
       
//---- drawing settings
    SetIndexStyle(0,DRAW_ARROW);
    SetIndexArrow(0,119);
    SetIndexStyle(1,DRAW_ARROW);
    SetIndexArrow(1,119);
//----
    SetIndexEmptyValue(0,0.0);
    SetIndexEmptyValue(1,0.0);
//---- name for DataWindow
    SetIndexLabel(0,"Fractal Up");
    SetIndexLabel(1,"Fractal Down");
    
    //---- indicator buffers mapping price arrows  
       
//---- drawing settings
    SetIndexStyle(2,DRAW_ARROW);
    SetIndexArrow(2,233);
    SetIndexStyle(3,DRAW_ARROW);
    SetIndexArrow(3,234);
//----
    SetIndexEmptyValue(2,0.0);
    SetIndexEmptyValue(3,0.0);
//---- name for DataWindow
    SetIndexLabel(2,"Up Arrow");
    SetIndexLabel(3,"Down Arrow");
    
    
    
    
    
//---- initialization done   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    i,nCountedBars;
   bool   bFound;
   double dCurrent;
   nCountedBars=IndicatorCounted();
//---- last counted bar will be recounted    
   if(nCountedBars<=2)
      i=Bars-nCountedBars-3;
   if(nCountedBars>2)
     {
      nCountedBars--;
      i=Bars-nCountedBars-1;
     }
//----Up and Down Fractals
   while(i>=2)
     {
            
      //----Fractals up
      bFound=false;
      dCurrent=High[i];
      
      
      if(dCurrent>High[i+1] && dCurrent>High[i+2] && dCurrent>High[i-1] && dCurrent>High[i-2])
        {
         bFound=true;
         ExtUpFractalsBuffer[i]=dCurrent;
         
        }
      //----6 bars Fractal
      if(!bFound && (Bars-i-1)>=3)
        {
         if(dCurrent==High[i+1] && dCurrent>High[i+2] && dCurrent>High[i+3] &&
            dCurrent>High[i-1] && dCurrent>High[i-2])
           {
            bFound=true;
            ExtUpFractalsBuffer[i]=dCurrent;
            
           }
        }         
      //----7 bars Fractal
      if(!bFound && (Bars-i-1)>=4)
        {   
         if(dCurrent>=High[i+1] && dCurrent==High[i+2] && dCurrent>High[i+3] && dCurrent>High[i+4] &&
            dCurrent>High[i-1] && dCurrent>High[i-2])
           {
            bFound=true;
            ExtUpFractalsBuffer[i]=dCurrent;
           }
        }  
      //----8 bars Fractal                          
      if(!bFound && (Bars-i-1)>=5)
        {   
         if(dCurrent>=High[i+1] && dCurrent==High[i+2] && dCurrent==High[i+3] && dCurrent>High[i+4] && dCurrent>High[i+5] && 
            dCurrent>High[i-1] && dCurrent>High[i-2])
           {
            bFound=true;
            ExtUpFractalsBuffer[i]=dCurrent;
           }
                   } 
      //----9 bars Fractal                                        
      if(!bFound && (Bars-i-1)>=6)
        {   
         if(dCurrent>=High[i+1] && dCurrent==High[i+2] && dCurrent>=High[i+3] && dCurrent==High[i+4] && dCurrent>High[i+5] && 
            dCurrent>High[i+6] && dCurrent>High[i-1] && dCurrent>High[i-2])
           {
            bFound=true;
            ExtUpFractalsBuffer[i]=dCurrent;
           }
            
        }                                    
      //----Fractals down
      bFound=false;
      dCurrent=Low[i];
      if(dCurrent<Low[i+1] && dCurrent<Low[i+2] && dCurrent<Low[i-1] && dCurrent<Low[i-2])
        {
         bFound=true;
         ExtDownFractalsBuffer[i]=dCurrent;
        }
      //----6 bars Fractal
      if(!bFound && (Bars-i-1)>=3)
        {
         if(dCurrent==Low[i+1] && dCurrent<Low[i+2] && dCurrent<Low[i+3] &&
            dCurrent<Low[i-1] && dCurrent<Low[i-2])
           {
            bFound=true;
            ExtDownFractalsBuffer[i]=dCurrent;
           }                      
        }         
      //----7 bars Fractal
      if(!bFound && (Bars-i-1)>=4)
        {   
         if(dCurrent<=Low[i+1] && dCurrent==Low[i+2] && dCurrent<Low[i+3] && dCurrent<Low[i+4] &&
            dCurrent<Low[i-1] && dCurrent<Low[i-2])
           {
            bFound=true;
            ExtDownFractalsBuffer[i]=dCurrent;
           }                      
        }  
      //----8 bars Fractal                          
      if(!bFound && (Bars-i-1)>=5)
        {   
         if(dCurrent<=Low[i+1] && dCurrent==Low[i+2] && dCurrent==Low[i+3] && dCurrent<Low[i+4] && dCurrent<Low[i+5] && 
            dCurrent<Low[i-1] && dCurrent<Low[i-2])
           {
            bFound=true;
            ExtDownFractalsBuffer[i]=dCurrent;
           }                      
        } 
      //----9 bars Fractal                                        
      if(!bFound && (Bars-i-1)>=6)
        {   
         if(dCurrent<=Low[i+1] && dCurrent==Low[i+2] && dCurrent<=Low[i+3] && dCurrent==Low[i+4] && dCurrent<Low[i+5] && 
            dCurrent<Low[i+6] && dCurrent<Low[i-1] && dCurrent<Low[i-2])
           {
            bFound=true;
            ExtDownFractalsBuffer[i]=dCurrent;
           }  
               
        }                                    
      i--;
     }
   if (Close[i]>ExtUpFractalsBuffer[i]) 
           ExtMapBuffer1[i]=(Low[i]-(PipBuffer)*Point); 
     
    if (Close[i]<ExtDownFractalsBuffer[i])
           ExtMapBuffer2[i]=(High[i]+(PipBuffer)*Point);           
                        
            
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
pullend:

Hello Forum, my newbie understanding is getting me into trouble with this code and I desperately need some help after going in circles for hours.

I am trying to adapt the fractals indicator to include additional buffers that draw arrows on the chart when price closes above the current upper fractal or below the current lower fractal.

I am not sure how to get the Close price to reference only the current upper or lower fractal, think I have the rest of the code right (see below)

I experimented with positioning of my definitions for my additional buffers, even duplicated them in each relevant { }

Also when I look at the Data Window, candles that have not got fractals attached, do not have a current fractal value and not sure how to combat this

I tried another version of the fractal indicator specified in terms of iFractal...but again struck problems referencing the current fractal

Firstly you need to understand the code you are trying to modify . . . go through it line by line, looking up any functions you do not understand. If you will not or cannot do this then you don't have much hope of correctly modifying it.

The buffers that hold the fractal values, ExtUpFractalsBuffer, ExtDownFractalsBuffer will not contain a price value for every bar, they will only have the High/Low where the bar in question is a Fractal . . . so to find the last/current upper fractal you have to look back and find it.
 

Strangely enough I re-wrote an indicator the other day to do exactly this.

Remember that buffers start each new bar with EMPTY_VALUE, whereas internal variables remain at the same value unless actively changed. One simple method is to use two type double variables (declared on a global scale at the beginning) - one for the "top" fractal and one for the "bottom" fractal.

Whenever a "top" fractal occurs, in the same command section where the buffer to display that peak is, you could set the relevant variable to that value. That variable will then stay as that value forevermore, until a new "top" fractal occurs.

Then you simply have a subsequent conditional which checks the close of the current bar against that variable, and draws a new buffer in whenever it is triggered. Reverse for the other direction.

This requires less code than having to check back x many bars every time. It just effectively keeps a running tally, and has no limit on the number of bars to check back.

 
RaptorUK:
Firstly you need to understand the code you are trying to modify . . . go through it line by line, looking up any functions you do not understand. If you will not or cannot do this then you don't have much hope of correctly modifying it.

The buffers that hold the fractal values, ExtUpFractalsBuffer, ExtDownFractalsBuffer will not contain a price value for every bar, they will only have the High/Low where the bar in question is a Fractal . . . so to find the last/current upper fractal you have to look back and find it.


thanks really good advice, generally it is individual functions that trouble me, and working out exactly how they work, regards David (ps realyl appreciate your assistance)

 
clerin6:

Strangely enough I re-wrote an indicator the other day to do exactly this.

Remember that buffers start each new bar with EMPTY_VALUE, whereas internal variables remain at the same value unless actively changed. One simple method is to use two type double variables (declared on a global scale at the beginning) - one for the "top" fractal and one for the "bottom" fractal.

Whenever a "top" fractal occurs, in the same command section where the buffer to display that peak is, you could set the relevant variable to that value. That variable will then stay as that value forevermore, until a new "top" fractal occurs.

Then you simply have a subsequent conditional which checks the close of the current bar against that variable, and draws a new buffer in whenever it is triggered. Reverse for the other direction.

This requires less code than having to check back x many bars every time. It just effectively keeps a running tally, and has no limit on the number of bars to check back.


thanks for your help

will try and implement suggested ideas

 
pullend:

thanks really good advice, generally it is individual functions that trouble me, and working out exactly how they work, regards David (ps realyl appreciate your assistance)

Yup, that's all part of the process of learning a programming language, you don't need to know all the functions, you need to have an appreciation of the groups of functions and some of the more common ones . . . I'm always checking my syntax in the Documentation, I still don't remember the order of the parameters in OrderSend() but I know where to find OrderSend() in the documentation so it's very quick for me to look up. :-)
 
"you don't need to know all the functions," and you can find them all here.
 
WHRoeder:
"you don't need to know all the functions," and you can find them all here.
 
pullend:

thanks everyone

took me some trial and error until I understood, but got a version working that plots an arrow of one color if previous fractal was support and now the close is below that fractal level

and an arrow of a different color if the previous fractal was resistance and candle now closing above that fractal level (code attached if useful for others)