OrderClose()
Same thing for OrderOpen.
I see that and I understand OrderClose() is a bool type
However I do not understand why there needs any checking if it returns true or false by default
I changed it to this and checking produces no errors but I do not completely understand why it would not return true or false without the checking ?
However I do not understand why there needs any checking if it returns true or false by default
I changed it to this and checking produces no errors but I do not completely understand why it would not return true or false without the checking ?
void macdclosetrade() { double faster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);//MODE_MAIN double slower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL double prevfaster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);//MODE_MAIN previous faster double prevslower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2); //MODE_SIGNAL previous slower for(int i = OrdersTotal()-1; i >= 0 ; i--) { if(OrderSelect(i, SELECT_BY_POS)) if(OrderMagicNumber() == MagicNumber) if(OrderSymbol()== Symbol()) if(OrderType() == OP_BUY) if(prevfaster > prevslower && faster < slower) //if direction down close order int result = OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue); if(result == false) Print("Order",OrderTicket(),"failed to close Error",GetLastError()); Print(" MACD DOWN"); } }
It would happen if broker is busy especially when there are news event or something like that.
Agent86: I changed it to this and checking produces no errors but I do not completely understand why it would not return true or false without the checking ?
- It does return "true or false without the checking." It is not an error, it is a compiler warning - reminding you that you should be checking them. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
- What happens if it doesn't work? Temporary network disconnection, broker busy, code broken. It's your money you're going to loose. EA's must be coded to recover.
- But if you really don't care, assign it do a variable like you did or just cast it to void.Not compiled, not tested.
(void) OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue);
Not compiled, not tested.
WHRoeder:
Perfect, thanks
- It does return "true or false without the checking." It is not an error, it is a compiler warning - reminding you that you should be checking them. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
- What happens if it doesn't work? Temporary network disconnection, broker busy, code broken. It's your money you're going to loose. EA's must be coded to recover.
- But if you really don't care, assign it do a variable like you did or just cast it to void.Not compiled, not tested.Not compiled, not tested.
However, It does not close my open orders
//+------------------------------------------------------------------+ //| functiontest.mq4 | //| function testing | //| Function Testing | //+------------------------------------------------------------------+ #property copyright "Open Function Testing" //---- input parameters extern double LotSize=0.1; extern int TakeProfit=20; extern int StopLoss=10; extern int AB_distance=10; extern int MagicNumber=123486; //+++++ user can change retrace and/or extension: extern double fibo_retrace=0.500; //Got something screwed up here extern double fibo_extension=0.618; //Got to fix this too //reference: http://www.forexfibonacci.com/calculate_fibonacci_levels/04/ double val1; double val2; //+------------------------------------------------------------------+ //| put custom functions here first | //+------------------------------------------------------------------+ //++++ These are adjusted for 5 digit brokers. int pips2points; // slippage 3 pips 3=points 30=points double pips2dbl; // Stoploss 15 pips 0.0015 0.00150 double mlots = AB_distance*10; //A to B distance normalizer // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { if (Digits == 5 || Digits == 3) { // Adjust for five (5) digit brokers. pips2dbl = Point*10; pips2points = 10; } else { pips2dbl = Point; pips2points = 1; } // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { static datetime PreviousTime=0; if(PreviousTime==Time[0]) return(0); else PreviousTime=Time[0]; // Should prevent another this bar if it runs through and returns back to the top of start. macdclosetrade(); macdsignaltrade(); //+------------------------------------------------------------------+ //| end start function | //+------------------------------------------------------------------+ return(0); } //+------------------------------------------------------------------+ //| My Functions (testing) | //+------------------------------------------------------------------+ void macdsignaltrade() { double faster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);//MODE_MAIN double slower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL double prevfaster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);//MODE_MAIN previous faster double prevslower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2); //MODE_SIGNAL previous slower //make one for shift 3 and 4 to evaluate the fractals if(prevfaster < prevslower && faster > slower)OrderEntry(0); //Print(" MACD UP"); if(prevfaster > prevslower && faster < slower)OrderEntry(1); //Print(" MACD DOWN"); } void engulfingsignal() //Bullish Engulfing Candle { if(Open[2] > Close[2] //bear candle identified && Open[1] < Close[1] //bull candle identified && Open[1]<= Close[2] //bull engulfing condition && Open[2] < Close[1]) Print(Close[1]," Bullish Engulfing Candle "," and engulfingsize = ",DoubleToStr(Close[1]-Open[1],Digits)); //DoubleToStr( Close[1]-Open[1],Digits) forums help if(Open[2]<Close[2]//bull candle identified && Open[1]>Close[1]//bear candle identified && Open[1]>= Close[2] //bear engulfing condition && Open[2] > Close[1]) Print(Close[1]," Bearish Engulfing Candle "," and engulfingsize = ",DoubleToStr(Close[1]-Open[1],Digits)); //DoubleToStr( Close[1]-Open[1],Digits) forums help); } void martingale() { //future function // to double lot sizes each time a trade loses } int fractaltrade() { //future function // needs to determine the ABDC's and trade entries return(0); } void macdclosetrade() { double faster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);//MODE_MAIN double slower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL double prevfaster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);//MODE_MAIN previous faster double prevslower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2); //MODE_SIGNAL previous slower for(int i = OrdersTotal()-1; i >= 0 ; i--) int result; { if(OrderSelect(i, SELECT_BY_POS)) if(OrderMagicNumber() == MagicNumber) if(OrderSymbol()== Symbol()) if(OrderType() == OP_BUY) if(prevfaster > prevslower && faster < slower) //if direction goes down on a buy order then close the order { result = OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue); if(result == false) Print("Order",OrderTicket(),"failed to close Error",GetLastError()); Print(" MACD DOWN CLOSE"); } if(OrderType() == OP_SELL) if(prevfaster < prevslower && faster > slower) //if direction goes up on a sell order then close the order { result = OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue); if(result == false) Print("Order",OrderTicket(),"failed to close Error",GetLastError()); Print(" MACD UP CLOSE"); } } } //end macdclosetrade() int OrderEntry(int direction) { int ticket; if(direction==0) if(OrdersTotal()==0) ticket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,3*pips2points,Ask-(StopLoss*pips2dbl),Ask+(TakeProfit*pips2dbl),NULL,MagicNumber,0,Green); if(ticket<0) Alert("OrderSend Failed: ", GetLastError()); if(direction==1) if(OrdersTotal()==0) ticket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,3*pips2points,Bid+(StopLoss*pips2dbl),Bid-(TakeProfit*pips2dbl),NULL,MagicNumber,0,Green); if(ticket<0) Alert("OrderSend Failed: ", GetLastError()); return(0); } //+------------------------------------------------------------------+
return value of 'OrderClose' should be checked