# Adding trailingstop to my EA

Hello everyone, I need help with adding a trailing stop to my EA. I'm looking for assistance in identifying the problem and fixing my code👇🏼. Your support would be greatly appreciated. Thank you!

```double trailing_distance = 50.0; // Trailing stop distance in pointsif(PositionSelect(_Symbol) == false)
{
if(StdDevVal>StdDevAVGVal&& Ask > MaValine && MaValine > MAValue && MAValue > MAAvalue)
{
double buy_tp = price + 150*_Point;
}

if(StdDevVal > StdDevAVGVal && Bid < MaValine && MaValine < MAValue && MAValue < MAAvalue)
{
double price = SymbolInfoDouble(_Symbol,SYMBOL_BID);
double sell_tp = price - 150*_Point;
posTicket = trade.Sell(0.1, _Symbol, price, price + 150*_Point, sell_tp);
}
}
else // If position is already open
{
double new_stoploss = 0.0;

{
// Calculate the new stop loss level based on trailing_distance
new_stoploss = Ask - trailing_distance * _Point;
}
{
// Calculate the new stop loss level based on trailing_distance
new_stoploss = Bid + trailing_distance * _Point;
}

// Modify the order with the new stop loss level
}```

I would really appreciate the help🙏🏼.

• www.mql5.com

```      if (trade.PositionType(_Symbol) == POSITION_TYPE_BUY){
new_stoploss = Ask - trailing_distance * _Point;```

1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
My GBPJPY shows average spread = 26 points, average maximum spread = 134.
My EURCHF shows average spread = 18 points, average maximum spread = 106.
Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

David Udoh:

Hello everyone, I need help with adding a trailing stop to my EA. I'm looking for assistance in identifying the problem and fixing my code👇🏼. Your support would be greatly appreciated. Thank you!

I would really appreciate the help🙏🏼.

I have an EA using the trailing stop function, would you like to see how its written?

Your question just got me to advice myself on a breakeven function i was working on. I would just replace it with a trailing stop for now.

```double trailing_distance = 50.0; // Trailing stop distance in pointsif(PositionSelect(_Symbol) == false)
{
if(StdDevVal>StdDevAVGVal&& Ask > MaValine && MaValine > MAValue && MAValue > MAAvalue)
⋮
}
else // If position is already open
{```
There is no else because there is no if. Do not post code that will not compile.

William Roeder #:
There is no else because there is no if. Do not post code that will not compile.

what did you mean?

Did you mean to say there is nothing like "else if"

Henry Asaba Achankeng #:

I have an EA using the trailing stop function, would you like to see how its written?

Your question just got me to advice myself on a breakeven function i was working on. I would just replace it with a trailing stop for now.

Yes I would love to, thank you.

David Udoh #:

Yes I would love to, thank you.

I have it in two pieces of code, the simple one and the complicated one. The first one is simple, just create and input then use the simple logic. The second one is complex and needs so much modification within the entire code, I recommend the first one.

``` {
ulong ticket=PositionGetTicket(posIndex);
if(PositionSelectByTicket(ticket) && PositionGetInteger(POSITION_MAGIC)==MagicNumber)
{
{
if(TrailingStop>0)
{
if(SymbolInfoDouble(_Symbol,SYMBOL_BID)-PositionGetDouble(POSITION_PRICE_OPEN)>MyPoint*TrailingStop)
{
if(PositionGetDouble(POSITION_SL)<SymbolInfoDouble(_Symbol,SYMBOL_BID)-MyPoint*TrailingStop)
{
return;
}
}
}
}

if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
if(TrailingStop>0)
{
{
{
return;
}
}
}
}
}
}
return;
}
```

complex one below

```// Trailing Stop
void sqManageTrailingStop(ulong ticket)
{
if(!PositionSelectByTicket(ticket))
{
Verbose("Cannot select position with ticket ", IntegerToString(ticket));
return;
}

valueIdentificationSymbol = PositionGetString(POSITION_SYMBOL);
int symbolDigits = (int) SymbolInfoInteger(valueIdentificationSymbol, SYMBOL_DIGITS);

double tsValue = NormalizeDouble(sqGetValueByIdentification(sqGetTrailingStop(ticket)), symbolDigits);

if(tsValue > 0)
{
double plValue;
int error;

int valueType = sqGetTrailingStopType(ticket);
ENUM_POSITION_TYPE orderType = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);

{
if(valueType == SLPTTYPE_RANGE)
{
tsValue = NormalizeDouble(sqGetBid(NULL) - tsValue, symbolDigits);
}
}
else
{
if(valueType == SLPTTYPE_RANGE)
{
tsValue = NormalizeDouble(sqGetAsk(NULL) + tsValue, symbolDigits);
}
}

double tsActivation = NormalizeDouble(sqGetValueByIdentification(sqGetTSActivation(ticket)), symbolDigits);
double currentSL = NormalizeDouble(PositionGetDouble(POSITION_SL), symbolDigits);
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double takeProfit = PositionGetDouble(POSITION_TP);

{
plValue = NormalizeDouble(sqGetBid(NULL) - openPrice, symbolDigits);

if(plValue >= tsActivation && (currentSL == 0 || currentSL < tsValue))
{
Verbose("Moving trailing stop for order with ticket: ", IntegerToString(ticket), " to :", DoubleToString(tsValue));
if(!OrderModify(ticket, tsValue, takeProfit))
{
error = GetLastError();
Verbose("Failed, error: ", IntegerToString(error), " - ", ErrorDescription(error),", Ask: ", DoubleToString(sqGetAsk(NULL)), ", Bid: ", DoubleToString(sqGetBid(NULL)), " Current SL: ",  DoubleToString(currentSL));
}
}
}
else     // orderType == OP_SELL
{
plValue = NormalizeDouble(openPrice - sqGetAsk(NULL), symbolDigits);

if(plValue >= tsActivation && (currentSL == 0 || currentSL > tsValue))
{
Verbose("Moving trailing stop for order with ticket: ", IntegerToString(ticket), " to :", DoubleToString(tsValue));
if(!OrderModify(ticket, tsValue, takeProfit))
{
error = GetLastError();
Verbose("Failed, error: ", IntegerToString(error), " - ", ErrorDescription(error),", Ask: ", DoubleToString(sqGetAsk(NULL)), ", Bid: ", DoubleToString(sqGetBid(NULL)), " Current SL: ",  DoubleToString(currentSL));
}
}
}
}
}```

I also just noticed the mql5 program itself has an ea generator where you can choose the trailing stop feature you need and the output looks very simple.

Henry Asaba Achankeng #:

what did you mean?

Did you mean to say there is nothing like "else if"

Without an if statement, you can't have an else or an else if. Look at OPs original posted code.