Trailing stop based on fractals

Hello guys. I wanted to develop a trailing stop based on fractals.

Basically I wrote two routines to get the fractals when they unfold and another function that is main

```double last_high() {

int j = 3;
double fractal = 0;

fractal = iFractals(Symbol(), Period(), MODE_UPPER, j);

if (fractal == 0) {
while (fractal == 0) {
j = j + 1;
fractal = iFractals(Symbol(), Period(), MODE_UPPER, j);
}
}

return (fractal);
}

double last_low() {

int j = 3;
double fractal = 0;

fractal = iFractals(Symbol(), Period(), MODE_LOWER, j);

if (fractal == 0) {
while (fractal == 0) {
j = j + 1;
fractal = iFractals(Symbol(), Period(), MODE_LOWER, j);
}
}

return (fractal);
}

int trailing_stop(int mode, int id_order) {

double act_stop, act_dist, last_fractal;
int order_handler, min_stop;
min_stop = MarketInfo(Symbol(), MODE_STOPLEVEL);

act_stop = NormalizeDouble(OrderStopLoss(), Digits);

if (mode == 1) {
last_fractal = last_low() - 1 * point_value;
act_dist = NormalizeDouble(last_fractal - act_stop, Digits);
min_stop = min_stop * point_value;
if (last_fractal != 0) {
if ((last_fractal > act_stop) && (Bid > last_fractal) && (act_dist > min_stop)) {
order_handler = OrderModify(id_order, OrderOpenPrice(), last_fractal, OrderTakeProfit(), 0, Green);
if (order_handler > 0) {
if (trailing_stop == false) {
trailing_stop = true;
}
Print("Stop loss updated at @: " + last_fractal);
return (0);
} else {
Print("Impossible to trail the order: ", GetLastError());
return (0);
}
}
}
}

if (mode == 2) {
last_fractal = last_high() + 1 * point_value;
act_dist = NormalizeDouble(act_stop - last_fractal, Digits);
min_stop = min_stop * point_value;
if (last_fractal != 0) {
if ((last_fractal < act_stop) && (Ask < last_fractal) && (act_dist > min_stop)) {
order_handler = OrderModify(id_order, OrderOpenPrice(), last_fractal, OrderTakeProfit(), 0, Green);
if (order_handler > 0) {
if (trailing_stop == false) {
trailing_stop = true;
}
Print("Stop loss updated at @: " + last_fractal);
return (0);
} else {
Print("Impossible to trail the order: ", GetLastError());
return (0);
}
}
}
}
}
```

I tried to do all the checks to avoid 130 errors, but as I can see I still get them. Unfortunately I tried to check min_distance and I checked if price was above/below the level I wanted to update the stop to.

So I am wondering I am doing anything else wrong.

Any comment is appreciated.

@zen4x: Great work! This is a BIG job! I have been working on the same thing alone, so welcome aboard ;) It is taking me zeons of hours of staring at fractal indicator code to analyze its logic, but I consistently make progress.

I suppose the best advice that I have for you is to follow my instructions. (if ya like)

Realize that fractals are simply a pattern of highs and lows.... So here is what I propose that you do...

Upper Fractal pattern = High[2]>High[1] && High[2]>High[3] && High[2]>High[0] && High[2]>High[4]; // the opposite for sell signals;

Ok, now, you know what the signal logic is...

The question for you now, is how do you record highs and lows?

5. The answer is simple. Use vectors aka arrays https://book.mql4.com/variables/arrays AND Boolean data type to record previous fractal highs and lows. I could just give you the code, but you would not receive the glory and satisfaction that you deserve by doing it yourself.

First, try SUPER HARD and if you get so frustrated and give up, private message me. OR you can private message me for hints or suggestions. If you are wiling to sacrifice for glory like myself, I do not hesitate to lend a hand.

I have full confidence in myself and if you believe in yourself, you can code anything as well.

Thank you!

Hello WooDoo22, nice to meet you.

From what I see, you do not use ifractal to find fractals ? Do you find the function unreliable ?

Take care, Francesco.

Hi,

The only thing you have to do is to get the value of the IFractals() function for bar 1. If value is different than zero, update the trailing stop.

Cheers.

zen4x:

Hello WooDoo22, nice to meet you.

From what I see, you do not use ifractal to find fractals ? Do you find the function unreliable ?

Take care, Francesco.

@ zen4x: You are right, I do not use ifractals to find fractals. I will provide the reason why...

When I write code projects this big, the code can get SUPER COMPLICATED. I try very hard to do everything in my power to keep the code as simple as possible by using indents, organizing blocks properly, using comments, etc. Even after all the simplification, the code can still drive me to madness sometimes because I "get lost" in the code. Using functions from icustom indicators in my code can complicate my code. Why use icustom functions when I can simply use the parameters of the icustom indicator that the icustom indicator uses to draw the picture on the chart? I just slap the icustom indicator in my expert advisor for visualization. Also, it makes my life easier (i like that) by not having so many problems isolating code buggies or errors.

"Do you find the function unreliable?".

No, it is VERY reliable. Slap the signal I provided in the previous post to an EA sometime just to put me to the test. :)

Its nice to meet you too!

Thank you n' take care, RunnerUp.