# Fractal strategy/ Help

Hello, I am trying to write a code to compare last 2 fractals up... if fractal up [1] > fractal up [2] we get a sell signal.

thank you in advance.

Fractals are never consecutive. Show your code where you read in the fractals to an array and then compare the last two.

Always post all relevant code (using Code button) or attach the source file.
How To Ask Questions The Smart Way. (2004)
Be precise and informative about your problem

We can't see your broken code.

William Roeder #:

Fractals are never consecutive. Show your code where you read in the fractals to an array and then compare the last two.

Always post all relevant code (using Code button) or attach the source file.
How To Ask Questions The Smart Way. (2004)
Be precise and informative about your problem

We can't see your broken code.

```      double udfractal(int nbr, int mode, int timeframe)
{
int i=3, n;
for(n=0;n<=nbr;n++)
{
while(iFractals(Symbol(),timeframe,mode,i) == 0)
i++;
if(n<nbr)
i++;
}
return(iFractals(Symbol(),timeframe,mode,i));
}

//If you want to see if the last upfractal is lower than the secondlast upfractal, use this code:

if( udfractal(1, MODE_UPPER, PERIOD_CURRENT) < udfractal(0, MODE_UPPER, PERIOD_CURRENT) )

// Enter short position
OrderSend(_Symbol,OP_SELL,0.05,Bid,3,0,Bid-150*_Point,NULL,0,0,Red);```

hello, thank you for your reply. here is the code I was trying to run.

I will attach a screenshot to make it more clear.

Files:
fractal.png  15 kb

Hi

This is a code from mt4 – but it seems to be fine. It could be optimized though – it’s repeating the loops unnecessary.

It would be easier if you would just found two fractals in one loop (set two variables fr1 and fr2, go in a loop on through bars and when you found fractal save it to the predefined variable if(fr1==0) fr1=fractal(i) else fr2==fractal(i).

When you have both variables saved if(fr1!=0 && fr2!=0) break; – then break a loop and compare those two values).

I believe it’s a bit easier.

alib12:

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

1. ```//If you want to see if the last upfractal is lower than the secondlast upfractal, use this code:

if( udfractal(1, MODE_UPPER, PERIOD_CURRENT) < udfractal(0, MODE_UPPER, PERIOD_CURRENT) )```

Your comment doesn't match your code. It tests if the last up fractal is higher.

2. I tend to agree with Marzena. Things could be optimized. I would have coded it thus:

Not tested, not compiled, just typed.

```void udfractal(int& ar[], int nbr, int mode, int timeframe=PERIOD_CURRENT)
{
ArrayResize(ar, nbr+1);
int i=3, n=0; for(; n <= nbr; ++i){
if( 0 != (ar[n] = iFractals(Symbol(),timeframe,mode,i) ) ++n;
}
⋮
//If you want to see if the last upfractal is lower than the secondlast upfractal, use this code:
int up[]; udFractal(up, 1, MODE_UPPER);
if( up[1] <  up[0]) …```

Not tested, not compiled, just typed.

3. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

Reason: