MQL4 trailing stop loss

 

In MQL4, the following trailing stop loss function, why the order will be continuously modified within the same 1 second? The effect I want is to adjust the SL position by 100 points every time the profit increases by 100 points.

Is there anyone who can modify it for me? Thanks!



datetime lastCheckTime = 0;

void Check_Trailing()
{
int orders = OrdersTotal();
datetime currentTime = TimeLocal();


if (currentTime - lastCheckTime >= 0) {
    for(int i = 0; i < orders; i++)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if(OrderSymbol() != Symbol())
                continue;
            
            double buycurrentPrice = MarketInfo(Symbol(), MODE_BID);
            double sellcurrentPrice = MarketInfo(Symbol(), MODE_ASK);
            double openPrice = OrderOpenPrice();
            double profit = OrderProfit();
            double stopLoss = OrderStopLoss();
            double trailingStop = 0;
            int pointMove;
            int trailingStopMove;
            
            if(OrderType() == OP_BUY && profit >= 300 * Point)
            {
                pointMove = (int) ((buycurrentPrice - openPrice) / Point);
                if(pointMove > 0)
                {
                    trailingStopMove = (int) (pointMove / 100) * 100;
                    trailingStop = openPrice + trailingStopMove * Point;
                    
                    if(trailingStop > stopLoss || stopLoss == 0)
                    {
                        if(OrderModify(OrderTicket(), OrderOpenPrice(), stopLoss + trailingStopMove * Point, OrderTakeProfit(), 0, Green))
                        {
                            Print("Trailing stop for order ", OrderTicket(), " has been modified to ", trailingStop);
                        }
                        else
                        {
                            Print("Failed to modify trailing stop for order ", OrderTicket(), " Error code: ", GetLastError());
                        }
                    }
                }
            }
            else if(OrderType() == OP_SELL && profit >= 300 * Point)
            {
                pointMove = (int) ((openPrice - sellcurrentPrice) / Point);
                if(pointMove > 0)
                {
                    trailingStopMove = (int) (pointMove / 100) * 100;
                    trailingStop = openPrice - trailingStopMove * Point;
                    
                    if(trailingStop < stopLoss || stopLoss == 0)
                    {
                        if(OrderModify(OrderTicket(), OrderOpenPrice(), stopLoss - trailingStopMove * Point, OrderTakeProfit(), 0, Red))
                        {
                            Print("Trailing stop for order ", OrderTicket(), " has been modified to ", trailingStop);
                        }
                        else
                        {
                            Print("Failed to modify trailing stop for order ", OrderTicket(), " Error code: ", GetLastError());
                        }
                    }
                }
            }
        }
    }
    lastCheckTime = currentTime;
}
}

OOP in MQL5 by Example: Processing Warning and Error Codes
OOP in MQL5 by Example: Processing Warning and Error Codes
  • www.mql5.com
The article describes an example of creating a class for working with the trade server return codes and all the errors that occur during the MQL-program run. Read the article, and you will learn how to work with classes and objects in MQL5. At the same time, this is a convenient tool for handling errors; and you can further change this tool according to your specific needs.
 
            if(OrderType() == OP_BUY && profit >= 300 * Point)
Profit is in account currency (e.g. $20.00). 300 * 0.00001 = 0.003. Condition will always be true.
 
William Roeder #:
Profit is in account currency (e.g. $20.00). 300 * 0.00001 = 0.003. Condition will always be true.

Please, how can I modify it?

 
hahago #:Please, how can I modify it?

If you want the "profit" in price change then simply subtract the open price from the close price.

double dbPriceChange = OrderClosePrice() - OrderOpenPrice();