[MQL4] Coding Trailing Stop Loss

Hello,

I am attempting to develop a trailing stop loss feature for my EA. Here is the logic:

  1. Obtain the entry price of the open trade.

  2. Determine the trailing price: Entry Price + Take Profit.

  3. Define the trailing gap as follows: For Buy orders, Bid - Trailing Gap; For Sell orders, Ask + Trailing Gap.

  4. Adjust the stop loss of each individual open trade according to the above conditions.

Code :

extern double Trailing = 5.0;

extern double Trailing_TP = 30.0;  //Take Profit

extern int Magic = 1; // Trailing Gap


BuyLimitMagic   = Magic + 1;

SellLimitMagic  = Magic + 11;

BuyStopMagic    = Magic + 111;

SellStopMagic   = Magic + 1111;


   if(Digits == 3 || Digits == 5)

      Pips = 10.0 * Point;

   else

      Pips = Point;


void trailing_buy(int trailingbuy_magic, double& BuyEntryPrices[]) {

    double price_0;

    if (trailingbuy_magic == BuyLimitMagic)

        price_0 = EntryPriceRecentOrder(BuyLimitMagic, BuyEntryPrices);

    else if (trailingbuy_magic == BuyStopMagic)

        price_0 = EntryPriceRecentOrder(BuyStopMagic, BuyEntryPrices);


    for (int pos_8 = OrdersTotal() - 1; pos_8 >= 0; pos_8--) {

        OrderSelect(pos_8, SELECT_BY_POS, MODE_TRADES);

        if (OrderSymbol() == Symbol() && OrderMagicNumber() == trailingbuy_magic) {

            if (OrderType() == OP_BUY) {

                // Find the entry price for the current order

                double entryPrice = BuyEntryPrices[OrderTicket()];

                // Calculate the trailing stop level based on the entry price

                double trailingStopLevel = entryPrice + Pips * Trailing;


                // Check if current Bid is above the trailing stop level

                if (Bid > trailingStopLevel) {

                    // Check if the current stop loss level is less than the new trailing stop level

                    if (OrderStopLoss() < trailingStopLevel) {

                        // Modify the stop loss to the new trailing stop level

                        OrderModify(OrderTicket(), entryPrice, trailingStopLevel, OrderTakeProfit(), 0, Blue);

                        return;

                    }

                }

            }

        }

    }

}


void trailing_sell(int trailingsell_magic, double& SellEntryPrices[]) {

    double price_0;

    if (trailingsell_magic == SellLimitMagic)

        price_0 = EntryPriceRecentOrder(SellLimitMagic, SellEntryPrices);

    else if (trailingsell_magic == SellStopMagic)

        price_0 = EntryPriceRecentOrder(SellStopMagic, SellEntryPrices);


    for (int pos_8 = OrdersTotal() - 1; pos_8 >= 0; pos_8--) {

        OrderSelect(pos_8, SELECT_BY_POS, MODE_TRADES);

        if (OrderSymbol() == Symbol() && OrderMagicNumber() == trailingsell_magic) {

            if (OrderType() == OP_SELL) {

                // Find the entry price for the current order

                double entryPrice = SellEntryPrices[OrderTicket()];

                // Calculate the trailing stop level based on the entry price

                double trailingStopLevel = entryPrice - Pips * Trailing;


                // Check if current Ask is below the trailing stop level

                if (Ask < trailingStopLevel) {

                    // Check if the current stop loss level is greater than the new trailing stop level

                    // Also, check if the current stop loss is not already at 0.0 (uninitialized)

                    if (OrderStopLoss() > trailingStopLevel || OrderStopLoss() == 0.0) {

                        // Modify the stop loss to the new trailing stop level

                        OrderModify(OrderTicket(), entryPrice, trailingStopLevel, OrderTakeProfit(), 0, Red);

                        return;

                    }

                }

            }

        }

    }

}


double EntryPriceRecentOrder(int EntryPriceRecentOrder_magic, double& BuyEntryPrices[], double& SellEntryPrices[]) {

    ArrayResize(BuyEntryPrices, 0);

    ArrayResize(SellEntryPrices, 0);


    for (int i = 0; i < OrdersTotal(); i++) {

        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

            if (OrderSymbol() == Symbol() && OrderMagicNumber() == EntryPriceRecentOrder_magic) {

                if (OrderType() == OP_BUY) {

                    ArrayResize(BuyEntryPrices, ArraySize(BuyEntryPrices) + 1);

                    BuyEntryPrices[ArraySize(BuyEntryPrices) - 1] = OrderOpenPrice();

                } else if (OrderType() == OP_SELL) {

                    ArrayResize(SellEntryPrices, ArraySize(SellEntryPrices) + 1);

                    SellEntryPrices[ArraySize(SellEntryPrices) - 1] = OrderOpenPrice();

                }

            }

        }

    }

}

I am not sure what mistake I am making; it is not working as expected.