Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

MagicNumber Trailing Stop - MetaTrader 4 Expert | MT4 EA Download

author EAcpu | 5 reads | 0 comments |

Trailing Stop really supports our trades by moving the stop loss to the profit area so that we can always automatically protect our trades.

We will start the code by specifying the input trailing stop parameters

 Enter boolean Yes Trailing Stop= True ; //Trailing Stop
Input integer trailing start = 15 ; //Trace starting point (point)
Input integer trailing step = 5 ; //Tracking step size (points)

Input integer magic number = 0 ; //magic number


global variables

 //variable global
double mypoints = 0.0 ;


When we run this EA, the OnInit () function will be executed for the first time, in which we will validate and initialize the input variables.

 Integer initialization ()
  {
 
if (isTrailingStop && TrailingStart <= 0 ){
alert ( "Parameter is incorrect" );
return ( INIT_PARAMETERS_INCORRECT );
  }
  
   myPoint = GetPipPoint( symbol ());
  
Return ( initialization successful );
  }


Whenever a price change (quote change) occurs on the chart of this EA pair, the OnTick () function will be executed. The setTrailingStop() function will be called in the OnTick() function.

 Blank check ()
  { //--- setTrailingStop(MagicNumber);
  
   }


Function setTrailingStop()

Blank to set trailing stop( integer magic number = 0 ){
if (isTrailingStop== wrong ) return ;
  
integer order = 0 ;
string pair = "" ;
Double sl = 0.0 , tp = 0.0 ;
  
   pair= symbol ();
  
   Orders= Total number of orders ();
order ( integer i=t order - 1 ; i >= 0 ; i -) {
boolean hourSelect = orderSelect (i, SELECT BY POS, MODE_TRADES);
if (OrderMagicNumber() == magicNumber && string search (OrderSymbol(), right, 0 ) == 0 ){
if (orderType() == OP_BUY){
if ((Bid - (trailingStart * myPoint)) >= OrderOpenPrice()
                  && (Bid- ((trailingStart+trailingStep) * myPoint) >= OrderStopLoss() )
                )
            {
              sr = normalized double (bid - (trailingStart * myPoint), number ());
if (!OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0 , clear blue )) {
Print ( "#" , Order Ticket(), "Gajal Update SL" );
              }
            }
        }
  
if (orderType() == OP_SELL){
if ((Ask + (trailingStart * myPoint)) <= OrderOpenPrice()
                  && ( (Ask + ((trailingStart+trailingStep) * myPoint) <= OrderStopLoss() ) || OrderStopLoss() == 0.0 )
              )
            {
              sl = normalized double (Ask + (trailingStart * myPoint), number ());
if (!OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0 , clear blue )) {
Print ( "#" , Order Ticket(), "Gajal Update SL" );
              }
            }
        }
      } //End if magicNumber } //End }


The other standard function required is GetPipPoint()

// Fungsi gets PipPoint
Double points gained (pair of strings )
{
Double points = 0.0 ;
Integer number = ( integer ) MarketInfo(pair, MODE_DIGITS);
if (number == 2 || number == 3 ) points = 0.01 ;
else if (number == 4 || number == 5 ) points = 0.0001 ;
return (view);
}


If you have any questions, please leave a message in the comments or you can also join our group sharing (Indonesian)t.me/codeMQL

We also offer the SignalForex application

You can support us by downloading and continuing to use the SignalForex app to support your trading for more profits

https://play.google.com/store/apps/details?id=com.autobotfx.signalforex


Attachment download

📎 addon_trailingstop.mq4 (3.75 KB)

Source: MQL5 #34177

Verification code Refresh