Watch how to download trading robots for free
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal

Trailing Stop with MagicNumber - expert for MetaTrader 4

Yulianto Hiu | English Español Português 日本語 Deutsch

votes: 18
2021.03.30 13:17

Trailing stop really supports our trading by shifting the stop loss to the profit area so that we can always automatically secure our trades.

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

input    bool     isTrailingStop = true;  //Trailing Stop
input    int      trailingStart  = 15;    //Trailing Start (pips)
input    int      trailingStep   = 5;     //Trailing Step (pips)

input    int      MagicNumber = 0;        //Magic Number

Global Variable

//Variabel Global
double   myPoint    = 0.0;

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

int OnInit()
   if (isTrailingStop && trailingStart <= 0){
      Alert ("Parameters incorrect");
   myPoint     = GetPipPoint(Symbol());

Every time a price movement (tick) occurs on the chart where this EA is paired, the OnTick () function will be executed. Inside the OnTick () function will call the setTrailingStop () function

void OnTick()

Function setTrailingStop()

void setTrailingStop(int magicNumber=0){
   if (isTrailingStop==false) return;
   int      tOrder = 0;
   string   pair = "";
   double   sl = 0.0, tp = 0.0;
   pair = Symbol();
   tOrder = OrdersTotal();
   for (int i=tOrder-1; i>=0; i--){
      bool hrsSelect = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() == magicNumber && StringFind(OrderSymbol(), pair, 0) == 0 ){
         if (OrderType() == OP_BUY){
            if ( (Bid - (trailingStart * myPoint)) >= OrderOpenPrice()
                  && (Bid - ((trailingStart+trailingStep) * myPoint) >= OrderStopLoss() )
               sl = NormalizeDouble(Bid - (trailingStart * myPoint), Digits());
               if (!OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0, clrBlue)){
                  Print ("#", OrderTicket(), " gagal update sl");
         if (OrderType() == OP_SELL){
            if ( (Ask + (trailingStart * myPoint)) <= OrderOpenPrice()
                  && ( (Ask + ((trailingStart+trailingStep) * myPoint) <= OrderStopLoss() ) || OrderStopLoss() == 0.0)
               sl = NormalizeDouble(Ask + (trailingStart * myPoint), Digits() );
               if (!OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0, clrBlue)){
                  Print ("#", OrderTicket(), " gagal update sl");
      } //end if magicNumber
   }//end for

other standard functions required is GetPipPoint() 

// Fungsi GetPipPoint
double GetPipPoint(string pair)
   double point= 0.0;
   int digits = (int) MarketInfo(pair, MODE_DIGITS);
   if(digits == 2 || digits== 3) point= 0.01;
   else if(digits== 4 || digits== 5) point= 0.0001;

If you have any questions, please leave them in the comments or you can also join our group sharing (in Indonesian)

We also provide the SignalForex App

You can support us by downloading and continue to use the SignalForex App to support your trading more profitably

    2 MA Crossing 2 MA Crossing

    For the purpose of learning to create an EA, I will share how to make an EA that uses 2 cross moving average indicators as a trading position entry signal.

    EA - The Simple Trading Panel - MT4 EA - The Simple Trading Panel - MT4

    The simple trading panel is a trading tool that is very interesting because it will allow you to predefine your StopLoss and your TakeProfit in term of pips.

    Close Orders By Target or Cut Loss Close Orders By Target or Cut Loss

    This EA is used as a trading tool to help us close all orders with a specific target in the form of money or cut loss. We can filter orders by magic number.

    Auto Scheduler Auto Scheduler

    It's a auto scheduler.