iFractals custom code problem. Need help please.

 

I have the following code:

The script must op_buy or op_sell depends of the last cfu or cfd.

If the last indicator is "cfu" the expert advisor must sell only if the cfu <= Bid - 10pips and it must buy if the last indicator is "cfd" and only if cfd <= Bid + 10pips only one order opened.

Problem: The expert advisor only buy or only sell. I know I shoud also include a function to find the last indicator.

Please let me know if you can help me. Thank you.

int start()
{
TradeSymbol=Symbol();
comment="m Fractured Fractal";
spread=Ask-Bid;
fu=iFractals(Symbol(),0,MODE_UPPER,2);//Print(fu);
fd=iFractals(Symbol(),0,MODE_LOWER,2);//Print(fd);
//----
if(fu>0) { cfu=fu; } else pfu=cfu;
if(fd>0) { cfd=fd; } else pfd=cfd;
if(TotalTradesThisSymbol(TradeSymbol)==0) { b.1=0;s.1=0; }
if(TotalTradesThisSymbol(TradeSymbol)>0)
{
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==TradeSymbol)
{
if(OrderMagicNumber()==101) {b.1=OrderTicket(); }
if(OrderMagicNumber()==102) {s.1=OrderTicket(); }
}
}
}

double bSL = Ask - BuyStoploss5*PipValue*Point;
if (BuyStoploss5 == 0) bSL = 0;
double bTP = Ask + BuyTakeprofit5*PipValue*Point;
if (BuyTakeprofit5 == 0) bTP = 0;

if(b.1==0 && cfd<=(Bid-multip))
{
ticket=OrderSend(Symbol(),
OP_BUY,
BuyLots,
Ask,
0,
bSL,
bTP,
Period()+comment,
101,
0,
Blue);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{ b.1=ticket; Print(ticket); }
else Print("Error Opening Buy Order: ",GetLastError());
return(0);
}
}
double sSL = Ask - BuyStoploss5*PipValue*Point;
if (BuyStoploss5 == 0) sSL = 0;
double sTP = Ask + BuyTakeprofit5*PipValue*Point;
if (BuyTakeprofit5 == 0) sTP = 0;


if(s.1==0 && cfu<=(Bid+multip))
{
ticket=OrderSend(Symbol(),
OP_SELL,
SellLots,
Ask,
0,
sSL,
sTP,
Period()+comment,
102,
0,
Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
{ s.1=ticket; Print(ticket); }
else Print("Error Opening Buy Order: ",GetLastError());
return(0);
}
}

//+------------------------------------------------------------------+

int TotalTradesThisSymbol(string TradeSymbol)
{
int i, TradesThisSymbol=0;
for(i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS);
if(OrderSymbol()==TradeSymbol &&
OrderMagicNumber()==101 ||
OrderMagicNumber()==102) { TradesThisSymbol++; }
}
return(TradesThisSymbol);
} //end TotalTradesThisSymbol

 

I would highly recommend you to read this book

https://www.mql4.com/files/mql4bookenglish.chm

Regardless:

 
fd=iFractals(Symbol(),0,MODE_LOWER,2);//Print(fd);
if(fu>0) { cfu=fu; } else pfu=cfu;
if(b.1==0 && cfd<=(Bid-multip)) 
// fd/fu is a number not a price
// use cfu=high[fu];
 
WHRoeder:


Thank you man!

That was realy helpful. Thank you.

 
fd=iFractals(Symbol(),0,MODE_LOWER,2);//Print(fd);

Just be aware that as coded this will result in "false positives" stemming from the fact that you are calling the ifractals indicator to give you a value based on the existing close value for the currently forming candle. As that close value changes in price while the existing candle is "forming" the ifractals indicator can give you false postives and yet when the candle is "closed" the final close price results in the ifractal value being zero (no fractal value was set by the most recent 5 candles).

I use the fractal indicator, and like myself I would say most people who do use it will intentionally stick to a "shift of 3" such that the value produced by the ifractals indicator is "confirmed" since none of the five candles used in the fractal calc are still open.

In other words:

fd=iFractals(Symbol(),0,MODE_LOWER,3);//Print(fd); // minimum shift value of 3 should be used when calling iFractals to avoid potential for false-positives
Reason: