Please help me with my EA

 

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. I edited the code abit based on Coders Guru's code. Please advise if anyone knows what is wrong with my codes.

Criteria:

1) when SMA short crosses SMA Long upwards, only look at long trade. Open trade when LWMA short crosses LWMA long upwards.

2) when SMA short crosses SMA Long downwards, only look at short trade. Open trade when LWMA short crosses LWMA long downwards.

3) only open 1 trade at a time.

4) 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+StopLoss*Point,

Bid-TakeProfit*Point,"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

 

Please help me with my EA

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:

1) when SMA short crosses SMA Long upwards, only look at long trade. Open trade when LWMA short crosses LWMA long upwards.

2) when SMA short crosses SMA Long downwards, only look at short trade. Open trade when LWMA short crosses LWMA long downwards.

3) only open 1 trade at a time.

4) 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+StopLoss*Point,

Bid-TakeProfit*Point,"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

 

Here you go...

Working in 4 and 5 digits broker.

yourea.mq4

Files:
yourea.mq4  4 kb
 
PANNEK:
Here you go...

Working in 4 and 5 digits broker.

yourea.mq4

Hi Pannek, the problem still persist.

 
tkuan77:
Hi Pannek, the problem still persist.

I add new parametre, MaxOpenOrders=10.

yourea_1.mq4

Files:
yourea_1.mq4  4 kb
 
PANNEK:
I add new parametre, MaxOpenOrders=10. yourea_1.mq4

Hi Pannek, for some reason, I am still only open 1 trade throughout the entire backtest of my EA. What could be the problem here?

Regards

Terrance

Reason: