MQL4 Trailing Stop issue

 
void TrailStops() 
{
    for (int i = 0; i < OrdersTotal(); i++) {
        if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
            continue;
        }
  
        if (OrderSymbol() != Symbol()) {
            continue;
        }
      // trailing stop on Long Positions
        if(OrderType() == OP_BUY) {
            if (Bid - OrderOpenPrice() > stopLoss * Point && OrderStopLoss() < Bid - stopLoss * Point) {
                if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - stopLoss * Point, 0, 0, Green)) {
                    Print("OrderModify error ",GetLastError());
                }
                return;
            }
        }
        //Trailing stop on Short Positions 
            if(OrderType() == OP_SELL) {
            if (Ask + OrderOpenPrice() > stopLoss * Point && OrderStopLoss() < Ask + stopLoss * Point) {
                if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + stopLoss * Point,0 , 0, Red)) {
                    Print("OrderModify error ",GetLastError());
                }
                return;
            }
        }
    }
}

on the long Position everything works great for some reason short selling there is an issue. I was wondering if someone can see an issue with this

There is no errors thats why, I am stuck on it 


 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. if (Ask + OrderOpenPrice() 

    Price plus price is meaningless.

  3. for (int i = 0; i < OrdersTotal(); i++) {
            if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
                continue;
            }
      
            if (OrderSymbol() != Symbol()) {
                continue;
            } 

    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

  4. Don't use negitive logic. Simplifiy your code.
    for (int i = OrdersTotal()-1; i >= 0; --i) if(
       OrderSelect(i, SELECT_BY_POS)
    && OrderSymbol() == Symbol()
    ) {
    

 

Sorry, I will do better at paying attention to the rules. actually never noticed them :-/


William Roeder #:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. Price plus price is meaningless.

  3. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

  4. Don't use negitive logic. Simplifiy your code.