# [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

SellLimitMagic  = Magic + 11;

SellStopMagic   = Magic + 1111;

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

Pips = 10.0 * Point;

else

Pips = Point;

double price_0;

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

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

// Find the entry price for the current order

// 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--) {

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

// 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(SellEntryPrices, 0);

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

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

} 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.