Code to find the last Fractal

 
//+------------------------------------------------------------------+
//|                                                SELLMoveLines.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#property show_inputs

extern int AddPipToUpperFrac = 15;

int start()
  { 
   int i ;
   int UpperFractal, LowerFractal ;
   int limit = 100;
  
   for(i=limit; i>0; i--)
   if (iFractals(NULL,NULL,MODE_UPPER,i)>0)
   {
      UpperFractal=i ;
   }
   
   limit=100; 

   for(i=limit; i>0; i--)
   if (iFractals(NULL,NULL,MODE_LOWER,i)>0)
   {
      LowerFractal=i ;
   }
    if (ObjectFind("ORDER_NEW_SL") >= 0)
    {        
      ObjectSet("ORDER_NEW_SL", OBJPROP_PRICE1, High[UpperFractal]+(AddPipToUpperFrac*Point));
    }
    if (ObjectFind("ORDER_NEW_Entry") >= 0)
    {   
      ObjectSet("ORDER_NEW_Entry", OBJPROP_PRICE1, Low[LowerFractal]-(10*Point));
    }
   return(0);
  }
//+------------------------------------------------------------------+

Hi,

I have a question about a bit of code I wrote. I basically want to find the last High Fractal and the last Low Fractal and move two horizontal lines. The higher horizontal line is placed 2 pips above the last high fractal and the low horizontal line is placed 1 pip below the last low fractal. I have written the code below and it does do exactly what I want it to do, but I just don't think it is the best way to write the code as I have used an arbirtary '100' bars to look back to find the fractals. I think 100 is more than enough but what if the fractal is further back than that, how would I code it so that it is very efficient? I don't want it looking 5000 bars back as that is unnecessary.

Your help would be appreciated!

 
DMFX: I have a question about a bit of code I wrote. I basically want to find the last High Fractal and the last Low Fractal and move two horizontal lines. The higher horizontal line is placed 2 pips above the last high fractal and the low horizontal line is placed 1 pip below the last low fractal. I have written the code below and it does do exactly what I want it to do, but I just don't think it is the best way to write the code as I have used an arbirtary '100' bars to look back to find the fractals. I think 100 is more than enough but what if the fractal is further back than that, how would I code it so that it is very efficient? I don't want it looking 5000 bars back as that is unnecessary.

If your only concern is about the 100 bars look-back then you should be fine.

 
ubzen:

If your only concern is about the 100 bars look-back then you should be fine.


Thanks Ubzen - you are absolutely right - I was just being finicky! :)

Cheers.

 
DMFX:


Thanks Ubzen - you are absolutely right - I was just being finicky! :)

Cheers.


Based on my recollection of the code before it was removed:

  • It would be better to start at zero and loop up (to 100, or 5000, or whatever), stopping when you find the first fractal, instead of starting at 100, always continuing down to zero, and keeping the last fractal found
  • If you are concerned about speed, use of iFractals() might not be ideal. If might be faster to do the equivalent test using High[] and Low[], e.g. High[n] > High[n+1] && High[n] > High[n+2] && High[n] > High[n-1] && High[n] > High[n-2]
 
gchrmt4:


Based on my recollection of the code before it was removed:

  • It would be better to start at zero and loop up (to 100, or 5000, or whatever), stopping when you find the first fractal, instead of starting at 100, always continuing down to zero, and keeping the last fractal found
  • If you are concerned about speed, use of iFractals() might not be ideal. If might be faster to do the equivalent test using High[] and Low[], e.g. High[n] > High[n+1] && High[n] > High[n+2] && High[n] > High[n-1] && High[n] > High[n-2]


The code is back again now. Thanks for the reply - I shall have a play around with the code now. :)
 
DMFX:
The code is back again now. Thanks for the reply - I shall have a play around with the code now. :)



Definitely better to start at zero and work upwards. For example:

   int UpperFractal = -1;
   for (int i = 3; i < 100; i++) {
      if (High[i] > High[i+1] && High[i] > High[i+2] && High[i] > High[i-1] && High[i] > High[i-2]) {
         UpperFractal = i;
         break;
      }
   }

You can safely increase i from 100 to 100000 - or to Bars - to handle incredibly rare scenarios where there is no fractal in the last 100 bars.

If you are prepared to accept unconfirmed fractals, you can change i = 3 to i = 2

 
gchrmt4:



Definitely better to start at zero and work upwards. For example:

You can safely increase i from 100 to 100000 - or to Bars - to handle incredibly rare scenarios where there is no fractal in the last 100 bars.

If you are prepared to accept unconfirmed fractals, you can change i = 3 to i = 2


Brilliant - thank you VERY much! :)
 
gchrmt4: to Bars - to handle incredibly rare scenarios where there is no fractal in the last 100 bars.
Not rare at all. In the tester you only are guaranteed 100 bars at start of test (no history.)
 
WHRoeder:
Not rare at all. In the tester you only are guaranteed 100 bars at start of test (no history.)


Yes, if you are only 10 bars into a backtest, then those 10 bars may not contain a fractal.

However, DMFX's concern was with having to look back more than 100 bars to find the most recent fractal, i.e. whether starting a scan at bar 100 and looping to present is likely to find the most recent fractal - if there is one.

Reason: