Hi
When my EA is supposed to make an order but it does not, I found out that GetLastError() returns 0, which means there is no error? So why doesn't my EA make the order then? Is there anyway to rectify this?
Thanks
- check your return codes - What are Function return values ? How do I use them ? - MQL4 forum Only when you get an error do you call GLE
- Do you think we are mind readers? We can not see your code. How do you expect us to know why?
- Fix your broken code.
ticket=OrderSend(...) if(ticket>0) { Print("The spread is: ", MathAbs(Ask-Bid)); if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice(), "opened at: ", OrderOpenTime()); } else Print("Error opening BUY order : ",GetLastError()); return(0);This is my code. Eventhough GetLastError returns 0 meaning no error, my order is not placed at all although I see in the expert log that my EA attempts to make an order
This is my code. Eventhough GetLastError returns 0 meaning no error, my order is not placed at all although I see in the expert log that my EA attempts to make an order
//for buy order: ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Hoang EA",140492,0,Green); //for close order: ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"Hoang EA",140492,0,Red);Yeah my OrderSend is called every tick provided that OrdersTotal() is 0. Do you think GetLastError() returns 0 because my slippage parameter in the OrderSend() function is too small (3 points which is 0.3 pip for my broker)?
- GLE is irrelevant. Only look at it when OrderSend returns negative.
- Now do what RaptorUK asked "you need to show your code . . . not just a snippet which doesn't help"
//+------------------------------------------------------------------+ //| Hoang's EA.mq4 | //| Hoang Nguyen | //| diatrochoi@yahoo.com.vn | //+------------------------------------------------------------------+ #property copyright "Hoang Nguyen" #property link "diatrochoi@yahoo.com.vn" //--- input parameters extern double Lots=0.1; extern double distance=0.01; extern int time_waited_last_loss=1200; extern double distancefrommidpoint=0.005; extern double distancefromslowEMA=0.01; extern double lossthreshold=-3.0; int mostcurrentcrossing=0; int secondmostcurrentcrossing=0; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int Crossed (double line1 , double line2) { static int last_direction = 0; static int current_direction = 0; if (last_direction==0) { if(line1>line2)current_direction = 1; //up if(line1<line2)current_direction = 2; //down } else { if (last_direction==1) { if(line1>line2)current_direction = 1; //up if(line1<=line2)current_direction = 2; //down } else { if(line1>=line2)current_direction = 1; //up if(line1<line2)current_direction = 2; //down } } if(current_direction != last_direction && last_direction == 0) { last_direction = current_direction; return (0); } else if(current_direction != last_direction && last_direction != 0) { if (secondmostcurrentcrossing==0) { secondmostcurrentcrossing=TimeCurrent(); mostcurrentcrossing=TimeCurrent(); } else { mostcurrentcrossing=TimeCurrent(); } last_direction = current_direction; return (last_direction); } else { return (0); } } int twentycheck() { int check20=0; for (int k=1;k<21;k++) { if (MathAbs(iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,k)-iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,k))>=distance) { check20=1; break; } } return (check20); } int between2crossingcheck() { int checkbetween2crossing=0; for (int q=iBarShift(NULL,0,secondmostcurrentcrossing,false);q>=iBarShift(NULL,0,mostcurrentcrossing,false);q--) { if (MathAbs(iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,q)-iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,q))>=distance) { checkbetween2crossing=1; break; } } return(checkbetween2crossing); } int timelapsecheck() { int timelapse=0; OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY); if (OrderProfit()>lossthreshold || TimeCurrent()-OrderCloseTime()>time_waited_last_loss) { timelapse=1; } return (timelapse); } int ordernature (int a) { static int checkforstrong=0; static int gapbetweencheck=0; OrderSelect(0, SELECT_BY_POS, MODE_TRADES); if (a==1) { if (OrderType()==OP_BUY) { if (Close[0]<iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,1)-distancefromslowEMA) { return (0); } else { return (1); } } else { if (Close[0]>iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,1)+distancefromslowEMA) { return (0); } else { return (1); } } } if (a==2) { if (OrderType()==OP_BUY) { if (Close[0]<iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,1)-distancefromslowEMA) { checkforstrong++; if (checkforstrong==1) { gapbetweencheck=TimeCurrent(); } if (checkforstrong==2) { if (TimeCurrent()-gapbetweencheck<60) //give 60 seconds to correct { checkforstrong--; } } } } else { if (Close[0]>iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,1)+distancefromslowEMA) { checkforstrong++; if (checkforstrong==1) { gapbetweencheck=TimeCurrent(); } if (checkforstrong==2) { if (TimeCurrent()-gapbetweencheck<60) //give 60 seconds to correct { checkforstrong--; } } } } if (checkforstrong!=2) { return (1); } else { checkforstrong=0; gapbetweencheck=0; return (0); } } } int start() { //---- int ticket, total; static int natureoforder =0; double shortEma, longEma; shortEma = iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,1); longEma = iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,1); int isCrossed = Crossed (shortEma,longEma); total = OrdersTotal(); if (total==1) { OrderSelect(0, SELECT_BY_POS, MODE_TRADES); if(OrderType()==OP_BUY) // long position is opened { // should it be closed? if (ordernature(natureoforder)==0 || iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,0)<iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,0)) { OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position natureoforder=0; return(0); // exit } } else // go to short position { // should it be closed? if (ordernature(natureoforder)==0 || iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,0)>iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,0)) { OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position natureoforder=0; return(0); // exit } } } if (total<1) { if (isCrossed==1) { if ((twentycheck()==1 || between2crossingcheck()==1) && timelapsecheck()==1) { Print("haha"); if (Open[1]>=Close[1]) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Hoang EA",140492,0,Green); natureoforder=2; Comment("aa"); } else { if (Ask<=(Close[1]+Open[1])/2+distancefrommidpoint) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Hoang EA",140492,0,Green); natureoforder=1; Comment("bb"); } } if(ticket>0) { Print("The spread is: ", MathAbs(Ask-Bid)); if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice(), "opened at: ", OrderOpenTime()); } else Print("Error opening BUY order : ",GetLastError()); Print("twentycheck, between2crossing, timelapsecheck:", twentycheck(),between2crossingcheck(),timelapsecheck()); return(0); } } if(isCrossed == 2) { if ((twentycheck()==1 || between2crossingcheck()==1) && timelapsecheck()==1) { if (Open[1]<=Close[1]) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"Hoang EA",140492,0,Red); natureoforder=2; } else { if (Bid>=(Close[1]+Open[1])/2-distancefrommidpoint) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"Hoang EA",140492,0,Red); natureoforder=1; } } if(ticket>0) { Print("The spread is: ", MathAbs(Ask-Bid)); if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice(), "opened at: ", OrderOpenTime()); } else Print("Error opening SELL order : ",GetLastError()); Print("twentycheck, between2crossing, timelapsecheck:", twentycheck(),between2crossingcheck(),timelapsecheck()); return(0); } } return(0); } //---- return(0); } //+------------------------------------------------------------------+This is my whole program. It's for USDJPY at 1min timeframe
OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY); if (OrderProfit()>lossthreshold || TimeCurrent()-OrderCloseTime()>time_waited_last_loss)
total = OrdersTotal(); if (total==1){ OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
- If you have no previous orders code fails. What are Function return values ? How do I use them ? - MQL4 forum
- That makes the EA incompatible with every other EA, manual trading, and itself on other charts. Filter my magic number: order accounting - MQL4 forum
if (Open[1]>=Close[1]){ ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Hoang EA",140492,0,Green); natureoforder=2; Comment("aa"); } else if (Ask<=(Close[1]+Open[1])/2+distancefrommidpoint){ ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Hoang EA",140492,0,Green); natureoforder=1; Comment("bb"); } if(ticket>0)
What happens if both IFs are false? Ticket will be zero, you call GetLastError and see zero.OrderSelect(0, SELECT_BY_POS, MODE_TRADES); if(OrderType()==OP_BUY){ // long position is opened // should it be closed? if(ordernature(natureoforder)==0 || iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,0)<iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,0) ){ OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
You select an order (See #1) THEN, ordernature() selects an order. You assume your OrderClose is closing the same order as you selected. Bad style, will break easily.Don't use 0, 1 when you mean true and false. int timelapsecheck(){ int timelapse=0; OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY); if (OrderProfit()>lossthreshold || TimeCurrent()-OrderCloseTime()>time_waited_last_loss) { timelapse=1; } return (timelapse); } : if ((twentycheck()==1 || between2crossingcheck()==1) && timelapsecheck()==1)
Learn to use booleans, simplify your code. Name your functions for readability. bool isDelayFinished(){ OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY); return( OrderProfit()>lossthreshold || TimeCurrent()-OrderCloseTime()>time_waited_last_loss) ); } : if ((twentycheck()==1 || between2crossingcheck()==1) && isDelayFinished())
This is my whole program. It's for USDJPY at 1min timeframe
OK what I was saying about GetLastError(), for example, when you first run the EA on an account with no open Orders . . . total = 0
if (total<1) // total is 0 so is less than 1 { if (isCrossed==1) { if ((twentycheck()==1 || between2crossingcheck()==1) && timelapsecheck()==1) { Print("haha"); if (Open[1]>=Close[1]) // if this is false . . . { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Hoang EA",140492,0,Green); natureoforder=2; Comment("aa"); } else { if (Ask<=(Close[1]+Open[1])/2+distancefrommidpoint) // and this is false, possible ? { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Hoang EA",140492,0,Green); natureoforder=1; Comment("bb"); } } if(ticket>0) // ticket is still at it's initialised value of 0 . . . { Print("The spread is: ", MathAbs(Ask-Bid)); if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice(), "opened at: ", OrderOpenTime()); } else Print("Error opening BUY order : ",GetLastError()); // . . . so this is executed. Print("twentycheck, between2crossing, timelapsecheck:", twentycheck(),between2crossingcheck(),timelapsecheck()); return(0); }
you need to move the error checking and error reporting into the same block as the OrderSend() so that the error checking is only performed when an OrderSend() is executed . . . for example . . .
if (total<1) // total is 0 so is less than 1 { if (isCrossed==1) { if ((twentycheck()==1 || between2crossingcheck()==1) && timelapsecheck()==1) { Print("haha"); if (Open[1]>=Close[1]) // if this is false . . . { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Hoang EA",140492,0,Green); if(ticket>0) { Print("The spread is: ", MathAbs(Ask-Bid)); if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice(), "opened at: ", OrderOpenTime()); } else Print("Error opening BUY order : ",GetLastError()); natureoforder=2; Comment("aa"); } else { if (Ask<=(Close[1]+Open[1])/2+distancefrommidpoint) // and this is false, possible ? { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Hoang EA",140492,0,Green); if(ticket>0) { Print("The spread is: ", MathAbs(Ask-Bid)); if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice(), "opened at: ", OrderOpenTime()); } else Print("Error opening BUY order : ",GetLastError()); natureoforder=1; Comment("bb"); } }
Thanks a lot, I get it now :))
I have one more question. In my original code, as long as new ticks are received but the conditions are not fulfilled, I should see multiple messages saying GetLastError()=0. The thing is that I always only see one such message and then that's it. What could possibly be the reasons here?

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi
When my EA is supposed to make an order but it does not, I found out that GetLastError() returns 0, which means there is no error? So why doesn't my EA make the order then? Is there anyway to rectify this?
Thanks