Hi guys, I am still quite new to all this EA programming. I have done up a trail EA on my own but I keep encountering some errors. My EA would only open 1 trade and after the first trade, it stops opening trade even after the conditions are met. Please advise if anyone knows what is wrong with my codes.
Criteria:
-
when SMA short crosses SMA Long upwards, only look at long trade. Open trade when LWMA short crosses LWMA long upwards.
-
when SMA short crosses SMA Long downwards, only look at short trade. Open trade when LWMA short crosses LWMA long downwards.
-
only open 1 trade at a time.
-
SL and TP = 100pips
extern double TakeProfit=100.0;
extern double Lots=0.01;
extern double StopLoss=100.0;
//±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
int mainCrossed (double mainline1 , double mainline2)
{
static int mainlast_direction = 0;
static int maincurrent_dirction = 0;
if(mainline1>mainline2)maincurrent_dirction = 1; //main up
if(mainline1<mainline2)maincurrent_dirction = 2; //main down
if(maincurrent_dirction != mainlast_direction) //main changed
{
mainlast_direction = maincurrent_dirction;
return (mainlast_direction);
}
else
{
return (0);
}
}
int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_dirction = 0;
if(line1>line2)current_dirction = 1; //up
if(line1<line2)current_dirction = 2; //down
if(current_dirction != last_direction) //changed
{
last_direction = current_dirction;
return (last_direction);
}
else
{
return (0);
}
}
//±-----------------------------------------------------------------+
//| 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);
mainshortEma = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0);
mainlongEma = iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);
int mainisCrossed = mainCrossed (mainshortEma,mainlongEma);
int isCrossed = Crossed (shortEma,longEma);
total = OrdersTotal();
if(total < 1)
{
if(mainisCrossed == 1)
{
if(isCrossed == 1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,
"My EA",12345,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if(mainisCrossed == 2)
{
if(isCrossed == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLossPoint,
Bid-TakeProfitPoint,âMy EAâ,12345,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
Print(âtotal for mainisCrossed == 2 at end isâ,total);
}
}
return(0);
}
Thanks in advance guys.
Regards
Terrance