Use iFractal to place the stop loss - MQL4

 

Hi guys it's my first post, I'm not very good in fact I started studying a couple of weeks ago.


I wanted to understand how stoploss can be placed on the previous fractal.


For example my EA sends a buy signal, so the stoploss must be placed at the level of the previous lower fractal.


Thanks =)

Files:
example.png  20 kb
 
Andrea Bevilacqua: Hi guys it's my first post, I'm not very good in fact I started studying a couple of weeks ago. I wanted to understand how stoploss can be placed on the previous fractal. For example my EA sends a buy signal, so the stoploss must be placed at the level of the previous lower fractal.

Scan iFractal backwards in time until you find the fractal level you are looking for. Fractals are never formed on the current or previous bar. There needs to be at least 2 completed bars on the right shoulder before a fractal becomes stable. So consider only scanning iFractal from bar shift 3 and onwards.

 

Thanks, could you give me more precise indications on what to look for online, to be able to solve this problem


Fernando Carreiro #:

Scan iFractal backwards in time until you find the fractal level you are looking for. Fractals are never formed on the current or previous bar. There needs to be at least 2 completed bars on the right shoulder before a fractal becomes stable. So consider only scanning iFractal from bar shift 3 and onwards.

 
Andrea Bevilacqua #: Thanks, could you give me more precise indications on what to look for online, to be able to solve this problem

Search the CodeBase with the keywords "fractal" and "stop". I found these which could help but there are more:

You can also search the Articles and see what you can find.
 
Andrea Bevilacqua #:

Thanks, could you give me more precise indications on what to look for online, to be able to solve this problem



Here is an example of a small chunk of one of my codes that uses fractals and count backwards to find previous fractal etc.
I highlighted the important part for you to learn from. 

bool A_high()
   {
      { 
      if(val1==0 && histo()==false)
         {
         for (int i=0; val1==0; i++)
            {
            val1=iFractals(NULL, 0, MODE_UPPER, i);
            A = val1;
            if(A!=0)
               {
               Print(A, " A high Located");
               //Print(A);
               return(true);
               }
             }
            }
        }
    return(false);                 
   }

Assuming you know what MODE_UPPER and MODE_LOWER is then you would chose your condition via MODE_UPPER or MODE_LOWER

Normally your indicator has a default shift like 0,1,3 etc. Fractals default to shift 3 because it needs more bars to form the fractal. 
For example: 

val1=iFractals(NULL, 0, MODE_UPPER, 3); 

So if you just turned on your computer and there are 5 bars after the fractal then you will not have the price for a previously formed fractal. In that case you count backwards with a for loop. 
This was only a crude example for my needs, but to go further back before the first actual valid fractal you would have to count again or code it to keep counting past the first previous fractal. You can develop counting back to anything or any indicator on the chart. 
This should help you see how it works and get some ideas from code base too.


I would also look at the support and resistance indicator here. It uses fractals to create an indicator so you can learn from this too.

https://www.mql5.com/en/code/401

Hope this helps.   

Support and Resistance
Support and Resistance
  • www.mql5.com
The "Support and Resistance" indicator shows the support and resistance levels using the Fractals indicator by Bill Williams.
Reason: