Newbie - struggling

 

I am trying to develop an EA and at the moment trying to get logical parts to work and just can't figure out what is wrong here. Perhaps someone could point me in the right direction

First - the order does not close

Second - GetLastError() appears to return 0 (zero) - i.e. the ALERTS @ 10800 & 10900 are both executed but the OrderSelect error code shown is 0 - zero

if (ExistPositions()) {

if (AccountProfit() < -Stop_loss)
for (int my_i = 0; my_i < OrdersTotal(); my_i++) {
if (OrderSelect(my_i, SELECT_BY_POS, MODE_TRADES))
if (OrderSymbol() == Symbol()) {
t1=OrderTicket(); // t1 used as temporary variable only
Alert ("10790 OrderClose "," OrderTicket() = ",t1," my_i = ",my_i);
OrderClose(t1,Lots,Ask,10,Red);
if (GetLastError() > 0 ) {
Print("10801 OrderSelect failed error code is ",GetLastError());
Alert ("10800 OrderSelect failed error code is ",GetLastError());
Alert ("10900 AccountProfit() = ",AccountProfit()," OrderTicket() = ",t1);
}
return(0);
}
}

}

any help appreciated

 
If your broker is five digit, the 10 points slippage represents 1 point in a four digit broker, and the spread (Ask-Bid) might be higher.
In such a case, it matters whether each trade is a buy or a sell, as illustrated in your code below.
Several consecutive calls of GetLastError(), but the first call puts the internal register to zero. 



Regards.
AM



if (ExistPositions()) {
  if (AccountProfit() < -Stop_loss)
  for (int my_i = 0; my_i < OrdersTotal(); my_i++) {
    if (OrderSelect(my_i, SELECT_BY_POS, MODE_TRADES))
      if (OrderSymbol() == Symbol()) {
        t1=OrderTicket(); // t1 used as temporary variable only
        Alert ("10790 OrderClose "," OrderTicket() = ",t1," my_i = ",my_i);
        if(OrderType()==OP_BUY)
           OrderClose(t1,Lots,Bid,10,Red);
        else if(OrderType()==OP_SELL)
           OrderClose(t1,Lots,Ask,10,Red);
        int last_error= GetLastError(); 
        if (last_error > 0 ) {
           Print("10801 OrderSelect failed error code is ",last_error);
           Alert ("10800 OrderSelect failed error code is ",last_error);
           Alert ("10900 AccountProfit() = ",AccountProfit()," OrderTicket() = ",t1);
        }
        return(0);
      }
  }
}
 
peterhw1:

First - the order does not close

Second - GetLastError() appears to return 0 (zero) - i.e. the ALERTS @ 10800 & 10900 are both executed but the OrderSelect error code shown is 0 - zero

The failure to close may be because it's a buy order, and you are trying to close it at Ask rather than at Bid. It's easiest to use OrderClosePrice() as the price parameter for OrderClose(). This avoids the need for different paths of execution for closing buy and sell orders.

GetLastError() is returning zero because the record of the last error is cleared when you first call GetLastError(); see https://docs.mql4.com/check/GetLastError. You need to do something like the following:

int err = GetLastError();
if (err > 0) {
  Print("10801 OrderSelect failed error code is ", err);
  etc.
}


Your method of selecting the order to close will probably work for your purposes, but it's vulnerable. It is closing the first, and only the first, out of any open orders. Simply doing OrderSelect(0, SELECT_BY_POS) would have the same effect as your loop which is exited after its first iteration. If you trade manually on the account, or run other EAs as well, then the first order does not necessarily belong to this EA.

 

  1. Always count down when closing/modifing/deleting in the presence of multiple orders (multiple charts)
    for (int pos = OrdersTotal()-1; pos >=0; pos--) if(
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol() ){              // and my pair.
        t1=OrderTicket(); // t1 used as temporary variable only
    

  2. OrderClose(t1,Lots,Ask,10,Red);
    if (GetLastError() > 0 ) {
    Print("10801 OrderSelect failed error code is ",GetLastError());
    
    The "if (get" clears the error code so you get no information. You close at the Ask only for sell orders.
    if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 10, Red))
       Alert("OrderClose(",OrderTicket(),"... failed: ", GetLastError());

  3. EA's must adjust for 5 digit brokers. TP, SL, AND slippage. The 10 becomes 10*pips2points
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

 
abstract_mind:


thanks to both of you - problem seems to be resolved. Initially got a 4063 error - my temporary variable t1 was double and not integer
Reason: