Discussion of article "How to Create Your Own Trailing Stop"

 

New article How to Create Your Own Trailing Stop is published:

The basic rule of trader - let profit to grow, cut off losses! This article considers one of the basic techniques, allowing to follow this rule - moving the protective stop level (Stop loss level) after increasing position profit, i.e. - Trailing Stop level. You'll find the step by step procedure to create a class for trailing stop on SAR and NRTR indicators. Everyone will be able to insert this trailing stop into their experts or use it independently to control positions in their accounts.

Figure 11. Buttons and Indicators on the Chart After Starting the Sample_TrailingStop.

Author: Дмитрий

 

Very useful. Thanks.


Steven

 

The trailing example fails with an error.

 
Please specify which one.
 

Programmers, help with mql5, please!!!

Is it possible to use just an ordinary trailing function as in mql4 without any classes? Just like, for example, the TradeSizeOptimised function is implemented in Moving Averages.mq5.

I have already searched everything, in examples, articles, forum - I have not found anything. I've already got completely lost.... Maybe someone has a ready variant, I will be very grateful!!! - I would like to participate in the championship.

 
Setslav:

Programmers, help with mql5, please!!!

Is it possible to use just an ordinary trailing function as in mql4 without any classes? Just like, for example, the TradeSizeOptimised function is implemented in Moving Averages.mq5.

I have already searched everything, in examples, articles, forum - I have not found anything. I've already got completely lost.... Maybe someone has a ready variant, I will be very grateful!!! - I would like to participate in the championship.


here you go

int TrailingStop()
  {
   if(PositionSelect(Symbol())) // select position
     {
      //MqlTradeRequest m_request;// declare the structure of the request to the server
      //MqlTradeResult m_result;// declare the server response structure
      double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);                         // write the bid price to the variable
      double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);                         // write to the variable ask price
      double OpenPrice=PositionGetDouble(POSITION_PRICE_OPEN);                     // write the position opening price into the variable
      double PositionSL=PositionGetDouble(POSITION_SL);                            // write the stop loss level to the variable
      double PositionTP=PositionGetDouble(POSITION_TP);                            // write take profit level into the variable

      if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) // define position type
        {
         if(TrailWhileMinus==true || Bid-OpenPrice>_Point*Trail) // don't trawl until we can reach breakeven with the first stop transfer
           {
            if(Bid-PositionSL>Trail*_Point) //basic condition for the necessity to move the stop loss
              {
               //--- write data to the structure
               request.action = TRADE_ACTION_SLTP;
               request.symbol = Symbol();
               request.sl     = NormalizeDouble(Bid-Trail*_Point,_Digits);
               request.tp     = NormalizeDouble(PositionTP,_Digits);
               //---
               return(OrderSend(request,result));                              // send the request to the server
              }
           }
        }
      else
        {
         if(TrailWhileMinus==true || OpenPrice-Ask>_Point*Trail) // don't trawl until we can reach breakeven with the first stop transfer
           {
            if(PositionSL-Ask>Trail*_Point) //basic condition for the necessity to move the stop loss
              {
               //--- write data to the structure
               request.action = TRADE_ACTION_SLTP;
               request.symbol = Symbol();
               request.sl     = NormalizeDouble(Ask+Trail*_Point,_Digits);
               request.tp     = NormalizeDouble(PositionTP,_Digits);
               //---
               return(OrderSend(request,result));                            // send the request to the server
              }
           }
        }
     }
   return(0);
  }
 
sergey1294:

here you go

I assume (probably not without reason) that result is better passed as a parameter :)

Otherwise there is no way to analyse it. It's not good somehow...

PS

I would also create two functions - TrailingStopBuy and TrailingStopSell

 
Interesting:

I assume (probably not without reason) that result is better passed as a parameter :)

Otherwise there is no way to analyse it. It's not good somehow...

PS

I would also create two functions - TrailingStopBuy and TrailingStopSell.

Well, I gave the person an example, because he has already made his brain how to write an ordinary trailing, and then let him think a little bit, that it would work without errors, in the tester in principle and this construction works normally. in real life did not check
 
sergey1294:
Well, I gave the man an example, because he has already made his brain how to write an ordinary trailing, and then let him think a little, that would work without errors, in the tester in principle, and this design works fine. in real life did not check
As an example of course it will do, but if without analysing the result there will be a lot of errors (sooner or later)...
 
sergey1294:

Here you go.

thank you!
 

sergey1294,

Thanks again, everything works!!!

There is one more small question, how to add a check by magic number to the function? I'm trying to insert such a check:

if (OrderGetInteger(ORDER_MAGIC)==EA_Magic)
{
.....
}
..... but something it breaks the whole trade.....