Using Fractals in an EA

 

Pls I'm presently faced with a problem, and that's using the Bill Williams indicator( Fractals)

here's the code pls direct me.

// initializations

double fraCurrent, fraPrevious;


//Calculate indicators' value

// Fractal Current
fraCurrent = iFractals(NULL, 0, MODE ?, 0); original statement: iFractals( string symbol, int timeframe, int mode, int shift );

// Fractal Previous
fraPrevious = iFractals(NULL, 0, MODE?, 1);


// Check for BUY, SELL, and CLOSE signal

isBuying = (fraCurrent <= Ask && fraPrevious>fraCurrent );

isSelling = (fraCurrent >= Bid && fraPrevious<fraCurrent );

isClosing = false;


I've tried everything, though new to this language, but a C# programmer, I need your assistance. Thanks!




 

okaforik :

I am not sure what the problem is but i can see that you have not picked a mode, there are two options MODE_UPPER or MODE_LOWER, so it would look something like this:

fraCurrent = iFractals(NULL, 0, MODE_UPPER, 0);

You can check the available modes on this link:

https://docs.mql4.com/constants/lines

I am not an expert on iFractals but I believe, and if some one has other information please feel free to correct me, that to get an iFractals result you will have to set the shift to at least be 2 or more, for it to be calculated correctly. Other wise you will always get a result of 0.

If that’s not what you’re asking then please do clarify what it is you want to get done.

Hope this helps,

Hazem

 
hmmlotfy:

okaforik :

You Know what, I just tried out that here's my code it still doesn't work



int start()
{
//----
f=iFractals(NULL,0,MODE_UPPER,2);
g=iFractals(NULL,0,MODE_LOWER,2);
if(f!=0 )
{
ticket=OrderSend("GBPUSD",OP_BUY,0.1,Ask,3,0,Ask+20*Point,"My buy order #2",16384,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
OrderModify(OrderTicket(),OrderOpenPrice(),Ask-10*Point,OrderTakeProfit(),0,Green);
return(0);
}
else Print("Error opening SELL order : ",GetLastError());

}

if (g!=0 )

{
ticket=OrderSend("GBPUSD",OP_SELL,0.1,Bid,3,0,Bid-20*Point,"My sell order #2",16384,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
OrderModify(OrderTicket(),OrderOpenPrice(),Bid+10*Point,OrderTakeProfit(),0,Red);
return(0);
}
else Print("Error opening SELL order : ",GetLastError());

}
//----
return(0);
}
//============================================================
 

A fractal won't appear on the chart on bar 0 or 1.

A fractal at bar 2 is unstable, and may disappear if the current bar goes higher/lower than bar 2 with upper/lower fractal.

Therefore, a stable fractal signal does not occur until the fractal bar is bar 3 or greater.

 

okaforik:

First Thank you Mr. phy for clarifying the use of fractal, now I tested you code Mr. Okaforik and it worked for me, the only thing that might cause you a problem is if you where testing it on other currency pairs other than "GBPUSD" because you hard coded it into your code, if so then you might want to write ‘Symbol()’ instead of "GBPUSD" in your ordersend function. Otherwise you might want to check if there is an error message in the journal tab in the tester, this might give you a hint of why it was not working.

Hope this helps

Hazem

 
phy wrote >>

A fractal won't appear on the chart on bar 0 or 1.

A fractal at bar 2 is unstable, and may disappear if the current bar goes higher/lower than bar 2 with upper/lower fractal.

Therefore, a stable fractal signal does not occur until the fractal bar is bar 3 or greater.

If my chart is set at M15 timeframe and it has been running for 5 days and I issue this for loop for H4 timeframe:

int start() 
  {for (int i=1; i<=200; i++)
       {if (iFractals(NULL,240,MODE_UPPER,i) > 0)
           {break ; 
           }
       }
   Print(i);
  }

Is the above statement going to give me the latest information?

I am worried that the iFractals do not exist for a timeframe that I haven't brought up on the screen.

The data may still be 5 days old.

Is MetaTrader continously forming the bars for other timeframes even though I do not have them on the screen?

Thank you very much for your response.

 
fdeguzman:

Is MetaTrader continously forming the bars for other timeframes even though I do not have them on the screen?

If u look at the history folder then u would c that hst files are formed only for the time-frames which are open (on charts)... That's not a definitive answer, cause maybe the terminal does so in memory if u r using a different time-frame in code (don't know...?).

But to insure it's updated, just open another chart (with same symbol) and put it on that time-frame.

Reason: