# Trailing stop loss not working as it should

Hi Everyone.

Could anyone please advise me on the following. I have an EA with a trailing stop Loss for the sell position. What I do not understand is that when the open positions goes into profit the my TP moves up and not the SL to come down. It should work that once an position is in Profit then the SL for that position should then move down and trail the profit position with 3000 points in order to prevent it from running out my profit if the market change. This is to be used on cash 500.I cant seem to see where I have gone wrong as now when the position is in profit , my take profit moved closer thus causing me to loose out on extra profit but not helping if the market change on me. If anyone could please just point me in the correct direction I would be very happy please.  I am new to coding and took me a while to compile this EA but now I am just stuck at this one point.

int numOrdersOpened = 0;
int MAX_ORDERS = 6;
double stopLossDistance = 3000 * _Point;

void OnTick()
{
// calculate Ask and Bid prices
double Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);

// create an array for several RSI values
double RSIArray[];
ArraySetAsSeries(RSIArray, true);

// define the properties of the RSI
int RSIDefinition = iRSI(_Symbol, _Period, 15, PRICE_CLOSE);

// copy the RSI values into the array
CopyBuffer(RSIDefinition, 0, 0, 3, RSIArray);

// get the RSI value for the current candle
double RSIValue = RSIArray[0];

// check if there are any open positions
if (PositionsTotal() == 0)
{
// open sell order when RSI reaches value of 90
if (RSIValue >= 90)
{
trade.Sell(0.20, NULL, Bid, (Bid + 150000 * _Point), Bid - 15000 * _Point);
numOrdersOpened = 1;
}
}
else
{
// check if 6 one-minute candles have passed since opening the last trade
if (TimeCurrent() - tradeOpenTime >= PeriodSeconds(PERIOD_M1) * 6)
{
// check if we have reached the maximum number of orders allowed
if (numOrdersOpened >= MAX_ORDERS)
{
return;
}

// check if the RSI condition is met
if (RSIValue >= 90)
{
trade.Sell(0.20, NULL, Bid, (Bid + 150000 * _Point), Bid - 15000 * _Point);
numOrdersOpened++;
}
}

// iterate through all open positions and add trailing stop loss
for (int i = 0; i < PositionsTotal(); i++)
{
// get the position ticket number
ulong ticket = PositionGetTicket(i);

// get the profit of the position in points
double profitPoints = PositionGetDouble(POSITION_PROFIT) / _Point;

// check if the position is profitable
if (profitPoints > 0)
{
// calculate the stop loss level
double stopLossLevel = PositionGetDouble(POSITION_PRICE_OPEN) - stopLossDistance;

// update the stop loss
}
}
}
}

Control the parameters of the functions call, use the debugger:

Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272

Code debugging - Developing programs - MetaEditor Help
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...

I do not get any errors or warning and the ea is running fine. Its only the trailing stop loss set that is moving my tp line up and not bring my sl line down to trail the price when in profit

Dawid Aaron Zinserling #:

I do not get any errors or warning and the ea is running fine. Its only the trailing stop loss set that is moving my tp line up and not bring my sl line down to trail the price when in profit

If a program compiles without errors but doesn't behave as intended, use the debugger to find out where and why!

int RSIDefinition = iRSI(_Symbol, _Period, 15, PRICE_CLOSE);

// copy the RSI values into the array
CopyBuffer(RSIDefinition, 0, 0, 3, RSIArray)

Perhaps you should read the manual, especially the examples.
How To Ask Questions The Smart Way. (2004)