BAT_ATRv1

This tool is designed to provide traders with a dynamic, visual "safety net" on their price charts. It automatically calculates and draws two specific lines—Resistance and Support—that follow the market's movement in real-time, helping users identify potential trend reversals or areas where price might stall.

How the Logic Works

The script operates based on three main components: Volatility, Price Action, and Directional Memory.

1. Measuring Market Volatility (ATR) Instead of using a fixed number of pips to set a safety level, the tool uses a metric called "Average True Range" (ATR). This measures how much the market typically moves over a specific timeframe. By multiplying this measurement by a user-defined factor, the script ensures that the safety lines stay at an appropriate distance from the price—wider during choppy, volatile markets and tighter when the market is calm.

2. Dynamic "Trailing" Movement The logic is designed to be "ratcheted." This means:

  • In an Uptrend: The support line (the floor) will move upward as the price rises, but it is programmed never to drop downward. It only moves up, "trailing" the price from below.
  • In a Downtrend: The resistance line (the ceiling) will move downward as the price falls, but it is programmed never to move back up. It follows the price from above.

3. Automated Trend Switching The script constantly monitors whether the price has "broken" through the existing safety lines.

  • If the price drops below the current support line, the script recognizes a potential shift in momentum and switches its focus to tracking a new resistance ceiling (moving from an uptrend logic to a downtrend logic).
  • Conversely, if the price breaks above the resistance line, it shifts back to tracking a new support floor.

Summary of User Experience

For a trader, this means the indicator provides a clear visual cue of the current market "bias." When the blue and red lines are clearly separated, you can see the trend direction. The lines effectively act as a moving floor and ceiling that automatically adjust their distance based on how volatile the market is currently behaving, providing a sophisticated way to manage risk without having to manually recalculate levels constantly.

Indicators Used
Indicator of the average true range
Miscellaneous
Implements a curve of type %1
9 Views
0 Downloads
0 Favorites
BAT_ATRv1
//+------------------------------------------------------------------+
//|                                            ATR Trailing Stop.mq4 |
//|                                                                  |
//|                                                                   |
//+------------------------------------------------------------------+
#property  copyright "Copyright Team Aphid"
#property  link      ""
//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 2
#property  indicator_color1  Blue
#property  indicator_color2  Red
//---- indicator parameters
extern int    BackPeriod  =1000;
extern int    ATRPeriod  =3;
extern double Factor=3;
extern bool   TypicalPrice=false;
//---- indicator buffers
double     ind_buffer1[];
double     ind_buffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings  
   SetIndexStyle(0,DRAW_LINE,EMPTY,2);
   SetIndexDrawBegin(0,ATRPeriod);
   SetIndexBuffer(0,ind_buffer1);
   SetIndexStyle(1,DRAW_LINE,EMPTY,2);
   SetIndexDrawBegin(1,ATRPeriod);
   SetIndexBuffer(1,ind_buffer2);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("ATR Trailing Stop("+ATRPeriod+" * "+Factor+")");
   SetIndexLabel(0,"Support");
   SetIndexLabel(1,"Resistance");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
   double PrevUp, PrevDn;
   double CurrUp, CurrDn;
   double PriceLvl;
   double LvlUp=0;
   double LvlDn=1000;
   int Dir=1;
   int InitDir;
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- fill in buffervalues
   InitDir=0;
   for(int i=BackPeriod; i>=0; i--)
     {
      if (TypicalPrice) PriceLvl=(High[i] + Low[i] + Close[i])/3;
      else PriceLvl=Close[i];
//----
        if(InitDir==0) 
        {
         CurrUp=Close[i] - (iATR(NULL,0,ATRPeriod,i) * Factor);
         PrevUp=Close[i-1] - (iATR(NULL,0,ATRPeriod,i-1) * Factor);
         CurrDn=Close[i] + (iATR(NULL,0,ATRPeriod,i) * Factor);
         PrevDn=Close[i-1] + (iATR(NULL,0,ATRPeriod,i-1) * Factor);
//----
         if (CurrUp > PrevUp) Dir=1;
         LvlUp=CurrUp;
         if (CurrDn < PrevDn) Dir=-1;
         LvlDn=CurrDn;
         InitDir=1;
        }
      CurrUp=PriceLvl - (iATR(NULL,0,ATRPeriod,i) * Factor);
      CurrDn=PriceLvl + (iATR(NULL,0,ATRPeriod,i) * Factor);
//----
        if (Dir==1) 
        {
           if (CurrUp > LvlUp) 
           {
            ind_buffer1[i]=CurrUp;
            LvlUp=CurrUp;
           }
           else 
           {
            ind_buffer1[i]=LvlUp;
           }
         ind_buffer2[i]=EMPTY_VALUE;
           if (Low[i] < ind_buffer1[i]) 
           {
            Dir=-1;
            LvlDn=1000;
           }
        }
        if (Dir==-1) 
        {
           if (CurrDn < LvlDn) 
           {
            ind_buffer2[i]=CurrDn;
            LvlDn=CurrDn;
           }
           else 
           {
            ind_buffer2[i]=LvlDn;
           }
         ind_buffer1[i]=EMPTY_VALUE;
           if (High[i] > ind_buffer2[i]) 
           {
            Dir=1;
            LvlUp=0;
           }
        }
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments