Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1529

 
Oleg Kubenko #:

That's the thing, they are far away and you have to move them closer to the market when the market goes negative, the more negative the stop loss is, the smaller the stop loss is.

Then I don't understand what volatility has to do with it. You know where the maximum minus will be on the candle at the moment of its opening....

 
and another question, where did the books from the marketplace go?
 
Oleg Kubenko #:
and another question, where did the books from the marketplace go?

It's doubtful that there will be an answer in this thread. Probably, it was not very profitable to sell them.....

 
Aleksey Vyazmikin #:

Then I don't understand what volatility has to do with it. On the candle you know where the maximum minus will be at the moment of its opening....

the difference is that if the candle opens too high, say with a sell order, then the stop loss will only approach when it opens.

 
Aleksey Vyazmikin #:

It's doubtful anyone will answer in this thread. Probably not very profitable to sell them.....

where are they now?

 
Oleg Kubenko #:

the difference is that if the candle opens too high, say with a sell order, the stop loss will be approached only after the opening.

Write a detailed algorithm, what you calculate there, because I do not understand why you can not immediately determine the stop....

 
Oleg Kubenko #:

and where are they now?

Seems like there's no point in selling them, so they're gone.

 
Aleksey Vyazmikin #:

Write in detail the algorithm, what you count there, because I do not understand why you can not immediately determine the stop...

okay, I think I've redone it as you say, is that what you mean? (This function is called on every tick.

datetime lastCandleTime = 0;
void UpdateTrailingStops() {
    if (!ActivateTrailStop || !EnableStopLoss) return; // Проверка, включены ли трейлинг-стоп и стоп-лосс

    datetime currentCandleTime = iTime(_Symbol, PERIOD_M1, 0); // Время текущей свечи

    // Проверка, началась ли новая свеча
    if (currentCandleTime != lastCandleTime) {
        lastCandleTime = currentCandleTime; // Обновляем время последней обработанной свечи

        // Перебор всех открытых позиций
        for (int i = PositionsTotal() - 1; i >= 0; --i) {
            ulong ticket = PositionGetTicket(i);
            if (PositionSelectByTicket(ticket)) { // Выбор позиции по билету
                if (PositionGetInteger(POSITION_MAGIC) == MagicNumber) { // Проверка магического числа
                    double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
                    double currentPrice = PositionGetDouble(POSITION_PRICE_CURRENT);
                    double profit = PositionGetDouble(POSITION_PROFIT);
                    double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
                    double stopLoss = PositionGetDouble(POSITION_SL);
                    double trailStopLevel = stopLoss; // Начальное значение стоп-лосса
                    double lastHigh = iHigh(_Symbol, PERIOD_M1, 1); // Получение хая предыдущей свечи
                    double lastLow = iLow(_Symbol, PERIOD_M1, 1); // Получение лоу предыдущей свечи

                    if (profit > 0 && profit / point >= ActivationProfit) {
                        // Логика для прибыльных позиций
                        if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) {
                            trailStopLevel = currentPrice - TrailingStop * point;
                            if (trailStopLevel > openPrice && trailStopLevel > stopLoss) {
                                if (trailStopLevel != stopLoss) {
                                    trade.PositionModify(ticket, trailStopLevel, PositionGetDouble(POSITION_TP));
                                }
                            }
                        } else {
                            trailStopLevel = currentPrice + TrailingStop * point;
                            if (trailStopLevel < openPrice && trailStopLevel < stopLoss) {
                                if (trailStopLevel != stopLoss) {
                                    trade.PositionModify(ticket, trailStopLevel, PositionGetDouble(POSITION_TP));
                                }
                            }
                        }
                    } else if (profit < 0) { // Логика уменьшения убытков при неприбыльных позициях
                        if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) {
                            trailStopLevel = MathMax(stopLoss, currentPrice - (lastLow - stopLoss));
                        } else {
                            trailStopLevel = MathMin(stopLoss, currentPrice + (stopLoss - lastHigh));
                        }
                        if (trailStopLevel != stopLoss) {
                            trade.PositionModify(ticket, trailStopLevel, PositionGetDouble(POSITION_TP));
                        }
                    }
                }
            }
        }
    }
}
 
Oleg Kubenko #:

OK, I think I've redone it as you say, is that what you mean? (This function is called on every tick.

Well, are you happy with the result?

I'm actually looking at the code and I don't understand what's being calculated here...

сurrentPrice - (lastLow - stopLoss)

Well, I think I'm just tired, and you have already checked everything in detail in the visualiser...

 
Aleksey Vyazmikin #:

Well, are you happy with the result?

I'm looking at the code and I don't understand what's been calculated here....

Well, I think I'm just tired, and you've already checked everything in detail in the visualiser....

I calculated the proximity of the stop loss to the high or low price of the previous bar, I haven't checked it yet, I am testing the previous results.

so far on the chart the price is in my favour and only the profit trailing stop is moving.