TRAILING STOP CODING HELP

//+------------------------------------------------------------------+
//|                                                        tstop.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <stdlib.mqh>

input string gap30=""; //.
input string infoTStop="___TRAILING STOP SETTINGS___"; //.
input bool enableTStop=true; //Enable Trailing Stop
input int buy_magic_number=198201;
input int sell_magic_number=198202;
input double tstop = 5;
//---
double trade_point = Point;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(Digits==3 || Digits==5) trade_point=Point*10;

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(OrdersTotal()==0)
     {
      int result=OrderSend(Symbol(),OP_BUY,1,Ask,5,Ask-100*trade_point,NULL,NULL,buy_magic_number,0,clrNONE);
      if(result<0)Alert(ErrorDescription(GetLastError()));
     }
//---
   if(enableTStop)
     {
      for(int cnt=0; cnt<OrdersTotal(); cnt++)
        {
         RefreshRates();
         int ticket_select=OrderSelect(cnt,SELECT_BY_POS);
         if(OrderStopLoss()!=NULL
            &&    OrderType()<=OP_SELL
            && OrderSymbol()==Symbol()
            && OrderMagicNumber()==buy_magic_number)
           {
            BuyTrailingStop();
            SellTrailingStop();
           }
        }
     }

  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Expert BuyTrailingStop function                                  |
//+------------------------------------------------------------------+
void BuyTrailingStop()
  {
   for(int cnt=0; cnt<OrdersTotal(); cnt++)
     {
      RefreshRates();
      int ticket_select=OrderSelect(cnt,SELECT_BY_POS);

      if(OrderStopLoss()!=NULL
         &&    OrderType()<=OP_SELL
         && OrderSymbol()==Symbol()
         && OrderMagicNumber()==buy_magic_number)
        {
         if(OrderType()==OP_BUY)
           {
            if(tstop>0)
              {
               int ticket_modify=
                                 OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+(tstop*trade_point),
                                 OrderTakeProfit(),0,clrNONE);
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Expert SellTrailingStop function                                 |
//+------------------------------------------------------------------+
void SellTrailingStop()
  {
   for(int cnt=0; cnt<OrdersTotal(); cnt++)
     {
      RefreshRates();
      int ticket_select=OrderSelect(cnt,SELECT_BY_POS);

      if(OrderStopLoss()!=NULL
         &&    OrderType()<=OP_SELL
         && OrderSymbol()==Symbol()
         && OrderMagicNumber()==sell_magic_number)
        {
         if(OrderType()==OP_SELL)
           {
            if(tstop>0)
              {
               int ticket_modify=
                                 OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss() -(tstop*trade_point),
                                 OrderTakeProfit(),0,clrNONE);
              }
           }
        }
      }
  }
//+------------------------------------------------------------------+

 

 

 

 

I need help in the trailing stop code above

can any one please modify it

it is working continuously with any tick

! 

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
         if(OrderType()==OP_BUY)
           {
            if(tstop>0&&(Bid-tstop*trade_point)>OrderStopLoss())
              {
               int ticket_modify=
                                 OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(tstop*trade_point),
                                 OrderTakeProfit(),0,clrNONE);
              }


         if(OrderType()==OP_SELL)
           {
            if(tstop>0&&(Ask+tstop*trade_point)<OrderStopLoss())
              {
               int ticket_modify=
                                 OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(tstop*trade_point),
                                 OrderTakeProfit(),0,clrNONE);
              }
Lorentzos Roussos:

I didn't test it but I believe the following code ...

if(enableTStop)
     {
      for(int cnt=0; cnt<OrdersTotal(); cnt++)
        {
         RefreshRates();
         int ticket_select=OrderSelect(cnt,SELECT_BY_POS);
         if(OrderStopLoss()!=NULL
            &&    OrderType()<=OP_SELL
            && OrderSymbol()==Symbol()
            && OrderMagicNumber()==buy_magic_number)
           {
            BuyTrailingStop();
            SellTrailingStop();
           }
        }
     }

 Must be like this ...

if(enableTStop)
     {
         BuyTrailingStop();
         SellTrailingStop();
     }
input string infoTStop="___TRAILING STOP SETTINGS___"; //.
input bool enableTStop=true; //Enable Trailing Stop
input int MagicNumber=198202;
input double               TrailingStart        = 30;          // Trailing Start
input double               TrailingStop         = 25;          // Trailing Stop
input double               TrailingStep         = 5;           // Trailing Step

//---
double trade_point = Point;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(Digits==3 || Digits==5) trade_point=Point*10;

//---
   return(INIT_SUCCEEDED);
  }



//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(OrdersTotal()==0)
     {
      int result=OrderSend(Symbol(),OP_BUY,1,Ask,5,Ask-100*trade_point,NULL,NULL,MagicNumber,0,clrNONE);
      if(result<0)Alert(ErrorDescription(GetLastError()));
     }
     
//---
   if(enableTStop)           TrailStop();
}


//-------------------------------------------------------------------+
// Trailing stop Function                                            |
//-------------------------------------------------------------------+
void TrailStop()
   {
      int Res = 0;
      double SLPrice = 0;
      double NewSL   = 0;
      double Range   = 0;
      int    digit   = 0;

      for ( int cnt = 0; cnt < OrdersTotal(); cnt++ )
         if( OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
            {
               while ( IsTradeContextBusy()) Sleep(100); 
               if (  OrderType() == OP_BUY && 
                     OrderMagicNumber() == MagicNumber )
                        {
                           Range = MarketInfo(OrderSymbol(),MODE_BID) - OrderOpenPrice();                     
                           NewSL = MarketInfo(OrderSymbol(),MODE_BID) - TrailingStop*trade_point;
                           NewSL = NormalizeDouble(NewSL,Digits);
                           if (  Range >= TrailingStart*trade_point )
                              if ( NewSL - OrderStopLoss() >= TrailingStep*trade_point )
                                 {
                                    Res = OrderModify ( OrderTicket(),
                                                            OrderOpenPrice(),
                                                            NewSL,
                                                            OrderTakeProfit(),
                                                            0,
                                                            clrNONE);
                                 }
                        }  

               if (  OrderType() == OP_SELL && 
                     OrderMagicNumber() == MagicNumber )
                        {
                           Range = OrderOpenPrice() - MarketInfo(OrderSymbol(),MODE_ASK);
                           NewSL = MarketInfo(OrderSymbol(),MODE_ASK) + TrailingStop*trade_point;
                           NewSL = NormalizeDouble(NewSL,Digits );
                           if (  Range >= TrailingStart*trade_point ) 
                              if ( OrderStopLoss() - NewSL >= TrailingStep*trade_point || OrderStopLoss() == 0 )
                                 {
                                    Res = OrderModify ( OrderTicket(),
                                                            OrderOpenPrice(),
                                                            NewSL,
                                                            OrderTakeProfit(),
                                                            0,
                                                            clrNONE);
                                 }
                        }
            }      
   }

 

      

one of my friends gave me the above code

it seems it is better than my code 

 

 thanksLorentzos &Usama

:)