Download MetaTrader 5

Need help with ATR Trailing stop

To add comments, please log in or register
Forex Trader
114299
Forex Trader  
Could anyone make me a ATR trailing stop indicator for the metatrader plattform or simply provide me with the script. When I define my ATR traling stop it's calculated from this common formula:

ATR Figure = $0.08
Last Close = $6.78

3 x ATR Stop = $6.78 - ($0.08 x 3)
= $6.78 - $0.24
= $6.54

The trailing stop in this case would be set at $6.54

As you can se, this also provides the possibility for multiplying the ATR stop (3 Times in the above example).

Metatrader allready has and average true range indicator but the line that comes from this calculation should be ploted on the price chart( thats the hole idea, so that the time for manualy clalculating the formula and fit it to prices would be removed).

The tralingstop would simply calculate the stop and plot it on the price chart, I do not have the programing knowledge to do this myself but if anyone could it would be greatly appreciated.

Swinger
daniel_boteroc4
7
daniel_boteroc4  

Forex Trader:

Could anyone make me a ATR trailing stop indicator for the metatrader plattform or simply provide me with the script. When I define my ATR traling stop it's calculated from this common formula:


ATR Figure = $0.08
Last Close = $6.78

3 x ATR Stop = $6.78 - ($0.08 x 3)
= $6.78 - $0.24
= $6.54

The trailing stop in this case would be set at $6.54

As you can se, this also provides the possibility for multiplying the ATR stop (3 Times in the above example).

Metatrader allready has and average true range indicator but the line that comes from this calculation should be ploted on the price chart( thats the hole idea, so that the time for manualy clalculating the formula and fit it to prices would be removed).

The tralingstop would simply calculate the stop and plot it on the price chart, I do not have the programing knowledge to do this myself but if anyone could it would be greatly appreciated.

Swinger 

Hi Swinger,

 I made this class, maybe it may help you.

 

//+------------------------------------------------------------------+
//|                                         TrailingParabolicATR.mqh |
//|                                            Daniel Botero Correa. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#include <Expert\ExpertTrailing.mqh>

class CTrailingATR : public CExpertTrailing
  {
protected:
   CiATR             m_atr;            // object-indicator
   //--- adjusted parameters
   double            m_atr_period;

public:
                     CTrailingATR(void);
                    ~CTrailingATR(void);
   //--- methods of setting adjustable parameters
   void              PeriodATR(double period)       { m_atr_period=period;       }
   //--- method of creating the indicator and timeseries
   virtual bool      InitIndicators(CIndicators *indicators);
   //---
   virtual bool      CheckTrailingStopLong(CPositionInfo *position,double &sl,double &tp);
   virtual bool      CheckTrailingStopShort(CPositionInfo *position,double &sl,double &tp);
  };
//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
void CTrailingATR::CTrailingATR(void) : m_atr_period(14)

  {
  }
//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
void CTrailingATR::~CTrailingATR(void)
  {
  }
//+------------------------------------------------------------------+
//| Create indicators.                                               |
//+------------------------------------------------------------------+
bool CTrailingATR::InitIndicators(CIndicators *indicators)
  {
//--- check pointer
   if(indicators==NULL)
      return(false);
//--- add object to collection
   if(!indicators.Add(GetPointer(m_atr)))
     {
      printf(__FUNCTION__+": error adding object");
      return(false);
     }
//--- initialize object
   if(!m_atr.Create(m_symbol.Name(),m_period,m_atr_period))
     {
      printf(__FUNCTION__+": error initializing object");
      return(false);
     }
//--- ok
   return(true);
  }
//+------------------------------------------------------------------+
//| Checking trailing stop and/or profit for long position.          |
//+------------------------------------------------------------------+
bool CTrailingATR::CheckTrailingStopLong(CPositionInfo *position,double &sl,double &tp)
  {
//--- check
   if(position==NULL)
      return(false);
//---
printf("m_symbol.Bid(): " + m_symbol.Bid() + " m_symbol.StopsLevel(): " + m_symbol.StopsLevel() + " m_symbol.Point(): " + m_symbol.Point());
printf("m_atr.Main(1): " + m_atr.Main(1));
   double level =NormalizeDouble(m_symbol.Bid()-m_symbol.StopsLevel()*m_symbol.Point(),m_symbol.Digits());
   double new_sl=NormalizeDouble(m_symbol.Bid() - (m_atr.Main(1)*2),m_symbol.Digits());
   double new_tp=NormalizeDouble(m_symbol.Ask() + (m_atr.Main(1)*3) + m_symbol.Spread()*m_symbol.Point(),m_symbol.Digits());
   double pos_sl=position.StopLoss();
   double base  =(pos_sl==0.0) ? position.PriceOpen() : pos_sl;
   double price = m_symbol.Bid();
//---
   sl=EMPTY_VALUE;
   tp=EMPTY_VALUE;
   if(new_sl>base && new_sl<level)
   {
      sl= new_sl;
      tp=new_tp;
   }
//---
   return(sl!=EMPTY_VALUE);
  }
//+------------------------------------------------------------------+
//| Checking trailing stop and/or profit for short position.         |
//+------------------------------------------------------------------+
bool CTrailingATR::CheckTrailingStopShort(CPositionInfo *position,double &sl,double &tp)
  {
//--- check
   if(position==NULL)
      return(false);
//---
   printf("m_symbol.Ask(): " + m_symbol.Ask() + " m_symbol.StopsLevel(): " + m_symbol.StopsLevel() + " m_symbol.Point(): " + m_symbol.Point());
   printf("m_atr.Main(1): " + m_atr.Main(1));
   double level =NormalizeDouble(m_symbol.Ask() + m_symbol.StopsLevel() * m_symbol.Point(),m_symbol.Digits());
   double new_sl=NormalizeDouble(m_symbol.Ask() + (m_atr.Main(1)*2) + m_symbol.Spread()*m_symbol.Point(),m_symbol.Digits());
   double new_tp=NormalizeDouble(m_symbol.Ask() - (m_atr.Main(1)*3) + m_symbol.Spread()*m_symbol.Point(),m_symbol.Digits());
   double pos_sl=position.StopLoss();
   double base  =(pos_sl==0.0) ? position.PriceOpen() : pos_sl;
   double price = m_symbol.Ask();
//---
   sl=EMPTY_VALUE;
   tp=EMPTY_VALUE;
   if(new_sl<base && new_sl>level)
   {
      sl= new_sl;
      tp= new_tp;
   }
//---
   return(sl!=EMPTY_VALUE);
  }
//+------------------------------------------------------------------+
To add comments, please log in or register