Skip to main content

Code for Trailing Stop

Subject

#1
Do any one has code for trailing stop for MT4? I am trying to build an ea but havent found any useful code.
Yuv
Basic EA Programmer
Reply
#2
(10-20-2019, 01:57 PM)yuv Wrote: Do any one has code for trailing stop for MT4? I am trying to build an ea but havent found any useful code.

Check the code below... I got it coded by a paid coder. Hope this helps.




void TrailingStopAll(bool Journaling,double TarilingStopLock,double TrailingStopDist,double TrailingStopBuff,int Magic,int K)
  {
  for(int i=OrdersTotal()-1; i>=0; i--) // Looping through all orders
    {
      bool Modify=false;
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
        {
        int err=HandleTradingEnvironment();
        if(err==ERR_NO_ERROR)
          {
            //If trailing stop has not begun yet
            if(OrderType()==OP_BUY && OrderStopLoss()==0 && Bid-OrderOpenPrice()>TrailingStopLockAt*K*Point)
              {
              if(Journaling)Print("EA Journaling: Trying to modify order "+(string)OrderTicket()+" ...");
              Modify=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStopDist*K*Point,OrderTakeProfit(),0,CLR_NONE);
              if(Journaling && !Modify)Print("EA Journaling: Unexpected Error has happened. Error Description: "+ErrorDescription(GetLastError()));
              if(Journaling && Modify)Print("EA Journaling: Order successfully modified, trailing stop changed.");
              }
            if(OrderType()==OP_SELL && OrderStopLoss()==0 && OrderOpenPrice()-Ask>TrailingStopLockAt*K*Point)
              {
              if(Journaling)Print("EA Journaling: Trying to modify order "+(string)OrderTicket()+" ...");
              Modify=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStopDist*K*Point,OrderTakeProfit(),0,CLR_NONE);
              if(Journaling && !Modify)Print("EA Journaling: Unexpected Error has happened. Error Description: "+ErrorDescription(GetLastError()));
              if(Journaling && Modify)Print("EA Journaling: Order successfully modified, trailing stop changed.");
              }

            //if trailing stop has already begun
            if(OrderType()==OP_BUY && OrderStopLoss()!=0 && (Bid-OrderStopLoss()>(TrailingStopDist+TrailingStopBuff)*K*Point))
              {
              if(Journaling)Print("EA Journaling: Trying to modify order "+(string)OrderTicket()+" ...");
              Modify=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStopDist*K*Point,OrderTakeProfit(),0,CLR_NONE);
              if(Journaling && !Modify)Print("EA Journaling: Unexpected Error has happened. Error Description: "+ErrorDescription(GetLastError()));
              if(Journaling && Modify)Print("EA Journaling: Order successfully modified, trailing stop changed.");
              }
            if(OrderType()==OP_SELL && OrderStopLoss()!=0 && (OrderStopLoss()-Ask>((TrailingStopDist+TrailingStopBuff)*K*Point)))
              {
              if(Journaling)Print("EA Journaling: Trying to modify order "+(string)OrderTicket()+" ...");
              Modify=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStopDist*K*Point,OrderTakeProfit(),0,CLR_NONE);
              if(Journaling && !Modify)Print("EA Journaling: Unexpected Error has happened. Error Description: "+ErrorDescription(GetLastError()));
              if(Journaling && Modify)Print("EA Journaling: Order successfully modified, trailing stop changed.");
              }
          }
        else
          {
            ReportErr(err);
          }
        }
    }
  }
Reply

Create an account or login to comment

You must login to comment

or