Self-learning the MQL5 language from scratch - page 56

 
Valeriy Yastremskiy:

Generally speaking, there are two motivations with opposite effects. Closer SL reduces the loss and makes the probability of closing on the SL higher. If the SL will be close in relation to volatility, then of course your option is better, if at a normal level and pulling up the SL will not affect the frequency of triggering, then mine.

You have already run into the realm of strategy ;) teach me tofirst set 1 bu and then move it

 
VVT:

You have already run into the realm of strategy;) teach me how tofirst set 1 boo and then move it

Hello! On the one hand you are right - you can just stop at one breakeven and write a code only for it. But, in my opinion, if you initially have no idea how a trailing stop should work in general, it is not the best option. In addition, almost all Expert Advisors are written based on a clearly defined strategy. As they say in such cases, "we must come to an agreement on the coast".

I feel that I am "awakening" the programmer again.

Regards, Vladimir.

 
Vasiliy Sokolov:

That's my understanding. You have two functions of re-positioning trailing stops. The first one moves trailing stop-loss to Breakeven, guided by parameter "Trailing level", the second function pulls stop-loss further behind the price, guided by parameter "Trailing step". Imho, I would call the first parameter "Stop Loss Breakeven Level" - because it is not a trailing stop loss but rather a stop loss transfer.

Yes, Vasily, that's right! You have correctly understood and formulated my idea of a trailing stop. The parameter should have had the same name from the beginning: " Trailing Stop Loss Level to Breakeven". My terminology is still not perfect. Thank you!

Sincerely, Vladimir.

 
MrBrooklin:

Hello! On the one hand you are right - you can just stop at one breakeven and write a code only for it. But, in my opinion, if you initially have no idea how a trailing stop should work in general, this is also not the best option. In addition, almost all Expert Advisors are written based on a clearly defined strategy. As they say in such cases, "we must come to an agreement on the coast".

I feel that I am "awakening" the programmer again.

Regards, Vladimir.

Hello! If you learn to move the Stop Loss step by step once, you can move it 100 times later, if necessary, if you have enough room for it ;)

The Expert Advisor is adjusted to the strategy, not vice versa

 
MrBrooklin:

Yes, Vasily, absolutely right! You have correctly understood and formulated my idea of a trailing stop. The parameter was originally intended to be called that: " Trailing Stop Loss Level to Breakeven". My terminology is still not perfect. Thank you!

Sincerely, Vladimir.


Good day Vladimir. Look at this post. You can modify in trawls from profit level and do not bother with the dedicated level of transfer to breakeven
https://www.mql5.com/ru/forum/352460/page55#comment_18711100
 
Aleksey Masterov:

Good afternoon, Vladimir. Take a look at this post of mine. There in trawls you can modify from profit level and don't bother with the highlighted transfer level to breakeven
https://www.mql5.com/ru/forum/352460/page55#comment_18711100

Hello Alexey! Sorry for not reacting to your post immediately. This link is very interesting. I have looked through all 11 trailing codes and function libraries. It is all very interesting, even though it is written in MQL4. To be honest, I have never imagined that there are so many types of trailing stops. Thank you very much for your support!

Sincerely, Vladimir.

 

Good morning everyone and good mood!

I continue learning the MQL5 programming language. Taking into account Vasily Sokolov's corrections, the algorithm of trailing Stop Loss at open positions now

looks as follows:
  1. Create an EA for automating work on trailing (tracking) the Stop Loss level of an open position.
  2. In the Expert Advisor, create a block of input parameters with two parameters: "Stop Loss level to Breakeven" and "Trailing Step".
  3. When new quotes come in, process them using OnTick( ). Trailing stops only when a new tick comes on the current symbol.
  4. We request data at the moment of receipt of OnTick event.
  5. For each Buy
  6. position we determine where the current price is relative to the open position price.
  7. If the current price is higher than the open position price, we check at what level it has risen.
  8. If the current price has reached the "Stop Loss without loss" level specified in the input parameters, we move the Stop Loss to a level without loss equal to the opening price of theBuy position. Otherwise we do nothing.
  9. If the current price has exceeded the "Stop Loss Breakeven Level" by the value equal to "Trailing Stop", the Stop Loss is moved from the opening price level of Buy position by the value equal to the "Trailing Stop Level" and so on, until the price reaches the Take Profit level, set for this position
  10. .
  11. If the price turns and re aches the level ofStop Loss, the position is closed
. [ Simplified version of Vasily Sokolov's trailing stop description:
  1. Trailing stop processing occurs at new tick receipt, in OnTick function
.
  1. Trailing Stop consists of two consecutive parts:
  2. Part 1
  3. .
  4. For each open position
  5. , the price is calculated, and as soon as it is reached, its stop-loss is moved to Breakeven.
  6. Part 2. After the stop loss has been moved to breakeven, the stop pull-up algorithm is activated for the active position, following the price.

You should then follow the pattern:

Part 1. Breakeven:
  • To buy;
  • to sell;
Part 2. Pulling Stop:
  • To buy;
  • To sell;

This variant of the algorithm of the Trailing Stop Loss of the open position is final

, and I continue writing program code by following it.

Regards, Vladimir.

 
VVT:

Hello! If you learn to move the stop loss step by step once, then you can move it 100 times if necessary, just as long as there is room for it ;)

The Expert Advisor is adjusted to the strategy, not vice versa

Hello! I already mentioned in my post earlier that you are right in your judgement. The thing is that with the help of Vasily Sokolov I relatively quickly formed an algorithm for trailing Stop Loss in an open position, so I will follow it.

Regards, Vladimir.

 
You have a clever approach to writing the Expert. And you won't need a market.
 

I continue learning the MQL5 programming language. Previously I published the code of the loop that starts the enumeration of open positions. Now, after the loop has been started, we start working with the symbol on the current chart:

     {
     /* Для работы с символом создадим переменную _Symbol, в которой будем хранить имя символа текущего графика.
        Делаем запрос на сервер. Сервер возвращает нам символ соответствующей открытой позиции и автоматически
        выбирает позицию для дальнейшей работы с ней при помощи функций PositionGetDouble, PositionGetInteger,
        PositionGetString. Если получим от сервера ответ о том, что для текущего символа была выбрана позиция для 
        дальнейшей работы с ней, то в торговом терминале выводим соответствующее сообщение во вкладке "Эксперт".*/
      if(_Symbol==PositionGetSymbol(i))
         Print("Выбираем позицию для дальнейшей работы с ней"); //
     }

I will periodically post the written code with my own comments for providing a prompt feedback. I ask the participants of this topic to correct me, if there are any inaccuracies in my code or comments.

Regards, Vladimir.