Orders not closing with order close signal

 

hi everyone, i don't understand why my positions oppen themselves and do not close, could you help me, thanks.

****

 

Please fix your post. Edit it (don't create a new post) and place your code properly (with "</>" or Alt-S), pasted from the original text, or just attach the original file directly with "+ Attach".

 
here it is
 {
 string signal="";
 double RSIvalue1 = iRSI(_Symbol, PERIOD_H1, 2, PRICE_CLOSE,1);
 double RSIvalue0 = iRSI(_Symbol, PERIOD_H1, 2, PRICE_CLOSE,0);
 
 if (RSIvalue1>70 && RSIvalue0<=70)
 {
   signal="sell";
   }
 
 if (RSIvalue1<30 && RSIvalue0>=30)
 {
   signal="buy";
   }
{
if (signal=="buy" && OrdersTotal()==0)
  int ticket = OrderSend("EURUSD", OP_BUY, 0.01, Ask, 2, 0, 0, NULL, 1, 0, Green);  
 }
   {
if (signal=="sell" && OrdersTotal()==0)
    ticket = OrderSend("EURUSD", OP_SELL, 0.01, Bid, 2, 0, 0, NULL, 2, 0, Red);
}
  { 
if (OrdersTotal()==1 && RSIvalue0<=30 && OrderType()==OP_SELL)
   bool ticket2 = OrderClose(OrderTicket(), OrderLots(), Ask, 2, Navy);
}
   { 
if (OrdersTotal()==1 && RSIvalue0>=70 && OrderType()==OP_BUY)
    ticket2 = OrderClose(OrderTicket(), OrderLots(), Bid, 2, Navy);
   }  
   
   }
 
Merlinou #: here it is
Did I not state ... " Edit it (don't create a new post) "?
 
  1. You were asked to edit your original post, not to create new ones.

  2.  double RSIvalue1 = iRSI(_Symbol, PERIOD_H1, 2, PRICE_CLOSE,1);
     double RSIvalue0 = iRSI(_Symbol, PERIOD_H1, 2, PRICE_CLOSE,0);

    Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  3. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

  4. if (signal=="buy" && OrdersTotal()==0)

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

  5. if (OrdersTotal()==1 && RSIvalue0<=30 && OrderType()==OP_SELL)

    MT4: You can not use any Trade Functions until you first select an order.

  6.    bool ticket2 = OrderClose(OrderTicket(), OrderLots(), Ask, 2, Navy);
    OrderClose does not return a ticket.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)

 

Sorry i'm new on mql4 so i do not exactly know how the platform is working.

Is it better like this if i stay on the chart to avoid history errors problems?

I tried to use what you told me

void OnTick()
 {
 string signal="";
 double RSIvalue1 = iRSI(_Symbol, PERIOD_H1, 2, PRICE_CLOSE,1);
 double RSIvalue0 = iRSI(_Symbol, PERIOD_H1, 2, PRICE_CLOSE,0);
 
 if (RSIvalue1>70 && RSIvalue0<=70)
 {
   signal="sell";
   }
 
 if (RSIvalue1<30 && RSIvalue0>=30)
 {
   signal="buy";
   }
{
if (signal=="buy" && OrdersTotal()==0)
  int ticket = OrderSend("EURUSD", OP_BUY, 0.01, Ask, 2, 0, 0, NULL, 1, 0, Green);  
 }
   {
if (signal=="sell" && OrdersTotal()==0)
    ticket = OrderSend("EURUSD", OP_SELL, 0.01, Bid, 2, 0, 0, NULL, 2, 0, Red);
}
bool select = OrderSelect(ticket, SELECT_BY_TICKET);
 
if (OrdersTotal()==1 && RSIvalue0<=30 && OrderType()==OP_SELL)
  {
  bool Ans = OrderClose(OrderTicket(), OrderLots(), Ask, 2, Navy); 
   }
if (OrdersTotal()==1 && RSIvalue0>=70 && OrderType()==OP_BUY)
  { 
   Ans = OrderClose(OrderTicket(), OrderLots(), Bid, 2, Navy);
   }  
   
 }
     
 
Merlinou #: I tried to use what you told me
bool select = OrderSelect(ticket, SELECT_BY_TICKET);
  1. The variable ticket is only valid on the tick where you opened an order.

    On the next tick you have lost it and the OrderSelect fails. You must remember it or find it.

    EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.

    On a network disconnection you might get ERR_NO_RESULT or ERR_TRADE_TIMEOUT. There is nothing to be done, but log the error, return and wait for a reconnection and a new tick. Then reevaluate. Was an order opened or is the condition still valid.

  2. Do not silence the compiler, it is trying to help you.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)


 
William Roeder #:
  1. The variable ticket is only valid on the tick where you opened an order.

    On the next tick you have lost it and the OrderSelect fails. You must remember it or find it.

    EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.

    On a network disconnection you might get ERR_NO_RESULT or ERR_TRADE_TIMEOUT. There is nothing to be done, but log the error, return and wait for a reconnection and a new tick. Then reevaluate. Was an order opened or is the condition still valid.

  2. Do not silence the compiler, it is trying to help you.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)


void OnTick()
 
{
 string signal="";
 double RSIvalue1 = iRSI(_Symbol, PERIOD_H1, 2, PRICE_CLOSE,1);
 double RSIvalue0 = iRSI(_Symbol, PERIOD_H1, 2, PRICE_CLOSE,0);
 
 if (RSIvalue1>70 && RSIvalue0<=70)
 {
   signal="sell";
   }
 
 if (RSIvalue1<30 && RSIvalue0>=30)
 {
   signal="buy";
   }
{
if (signal=="buy" && OrdersTotal()==0)
  int ticket = OrderSend("EURUSD", OP_BUY, 0.01, Ask, 2, 0, 0, NULL, 1, 0, Green);  
 }
   {
if (signal=="sell" && OrdersTotal()==0)
    ticket = OrderSend("EURUSD", OP_SELL, 0.01, Bid, 2, 0, 0, NULL, 2, 0, Red);
}
for(int i=OrdersTotal()-1; i>=0; i--)
 bool select =  OrderSelect(i, SELECT_BY_POS , MODE_TRADES);
 {
if (OrdersTotal()==1 && RSIvalue0<=30 && OrderType()==OP_SELL)
  bool Ans = OrderClose(OrderTicket(), OrderLots(), Ask, 2, Navy); 
   }
if (OrdersTotal()==1 && RSIvalue0>=70 && OrderType()==OP_BUY)
  { 
   Ans = OrderClose(OrderTicket(), OrderLots(), Bid, 2, Navy);
   } 

is this correctly selecting order?

Could you help me i do not know how to silence or not the compiler?

 
  1. Merlinou #: I tried to use what you told me

    You did? Your post still has problems #4.3, #4.4, #4.5, #4.6, #6.1, and #6.2. You've done nothing.


  2. Merlinou #: Could you help me i do not know how to silence or not the compiler?

    What part of the link in #6.2 was unclear?

Reason: