Hi all,
I have come up with a way by using “Bars” function. When a trade is triggered, I will set a integer count with the bars function (e.g. a buy trade is triggered in bar 122, hence the count will be 122) and when my criteria has being triggered again, I will call the bar function again and compared it with the count and the trade will only trigger if the new count is higher then the previous which is 122 in this case.
However, I am still triggering 2 trades together instead of 1 per criteria. Why is this so?
Please help
Thanks and regards
Terrance
//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+
int start()
{
//----
int cnt, ticket, total;
double shortEma, longEma, mainshortEma, mainlongEma;
if(Bars<100)
{
Print(“bars less than 100”);
return(0);
}
if(TakeProfit<10)
{
Print(“TakeProfit less than 10”);
return(0); // check TakeProfit
}
shortEma = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,0);
longEma = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,0);
int isCrossed = 0;
double shortEma1 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,1);
double longEma1 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,1);
double shortEma2 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,2);
double longEma2 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,2);
double diff1 = shortEma1-longEma1;
double diff2 = shortEma2-longEma2;
mainshortEma = iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);
if ((diff1*diff2)<0)
{
if (shortEma1>longEma1)
isCrossed = 1;
else isCrossed = 2;
}
int BarCounter, BarCounter2;
total = OrdersTotal();
if(total < 1)
{
if(isCrossed == 1 && shortEma > mainshortEma && longEma > mainshortEma)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLossPoint,Ask+TakeProfitPoint,
“My EA”,12345,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES ))
Print("BUY order opened : ",OrderOpenPrice());
BarCounter = Bars;
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2 && shortEma < mainshortEma && longEma < mainshortEma)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+S topLossPoint,Bid-TakeProfitPoint,
“My EA”,12345,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES ))
Print("SELL order opened : ",OrderOpenPrice());
BarCounter = Bars;
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
if(total == 1)
{
BarCounter2 = Bars;
if(isCrossed == 1 && shortEma > mainshortEma && longEma > mainshortEma && BarCounter2 > BarCounter)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLossPoint,Ask+TakeProfitPoint,
“My EA”,12345,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES ))
Print("BUY order opened : ",OrderOpenPrice());
BarCounter = BarCounter2;
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2 && shortEma < mainshortEma && longEma < mainshortEma && BarCounter2 > BarCounter)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+S topLossPoint,Bid-TakeProfitPoint,
“My EA”,12345,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES ))
Print("SELL order opened : ",OrderOpenPrice());
BarCounter = BarCounter2;
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
return(0);
}