ATR arrow level

Download script
Author: Copyright © 2019, Vladimir Karputov

This technical tool is designed to provide visual signals on a trading chart by monitoring market volatility. It specifically focuses on identifying moments when price movement remains within a defined range relative to the average true range of the asset.

Core Logic and Functionality

The script operates based on three primary components:

1. Volatility Monitoring The tool uses the "Average True Range" (ATR) as its primary metric. This is a standard market indicator used to measure how much an asset typically moves over a specific period. The script takes this data and compares it against a user-defined "Level."

2. Signal Generation (The Arrows) The primary purpose of the script is to place visual arrows on the chart. These arrows appear based on a specific condition:

  • The tool checks if the current volatility (ATR) is lower than your specified threshold (the Level).
  • When volatility is low—meaning the market is relatively quiet or "consolidating"—the script identifies the highest price reached during that period.
  • It then places an arrow at that specific price point to highlight a potential area of interest.

3. Dynamic Visual Elements In addition to arrows, the script contains built-in utility functions for creating and manipulating graphical objects on your chart:

  • Rectangle Creation: The code includes a dedicated system to draw rectangles with customizable colors, styles, and widths. This allows for the highlighting of specific price zones or "boxes" on the screen.
  • Dynamic Positioning: It features a mechanism to move these graphical anchors automatically to the current market price if they aren't already set, ensuring that your visual markings can stay relevant as the price moves in real-time.

How it Works for the User

When you apply this script to a chart, it constantly scans the history of the asset. It waits for the market to "calm down" (falling below your volatility level). Once that happens, it marks the peaks with arrows and provides the underlying infrastructure to draw rectangles, helping you visually map out zones where price action is staying within tight boundaries.

Indicators Used
Indicator of the average true range
12 Views
0 Downloads
0 Favorites
ATR arrow level
ÿþ//+------------------------------------------------------------------+

//|                                              ATR arrow level.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   1

//--- plot Consolidation

#property indicator_label1  "ATR arrow"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrSlateBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

input int               Inp_ATR_ma_period = 7;           // ATR: averaging period

input double            Inp_ATR_level     = 0.00130;     // ATR: level

input uchar             Inp_ATR_code      = 174;         // Code (fonts Wingdings)

//--- indicator buffers

double   ExtArrowBuffer[];

double   ExtAtrBuffer[];

//---

int      handle_iATR;            // variable for storing the handle of the iATR indicator

int      ExtBarsCalculated = 0;  // we will keep the number of values in the Average True Range indicator 

//---

string   ExtName           = ""; // rectangle name 

datetime ExtStartTime      = 0;  // first point time ("0" -> D'1970.01.01 00:00:00')

double   ExtStartUpPrice   = 0;  // first point price

datetime ExtEndTime        = 0;  // second point time ("0" -> D'1970.01.01 00:00:00')

double   ExtEndDownPrice   = 0;  // second point price  

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtArrowBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtAtrBuffer,INDICATOR_CALCULATIONS);

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,Inp_ATR_code);

//--- sets first bar from which index will be drawn 

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Inp_ATR_ma_period);

////--- name for DataWindow and indicator subwindow label 

//   IndicatorSetString(INDICATOR_SHORTNAME,"ATR arrow level("+Inp_ATR_ma_period+","+Inp_ATR_level+")");

//--- sets drawing line to empty value 

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//--- set the vertical shift of arrows in pixels 

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-15);

//--- create handle of the indicator iATR

   handle_iATR=iATR(Symbol(),Period(),Inp_ATR_ma_period);

//--- if the handle is not created 

   if(handle_iATR==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code 

      PrintFormat("Failed to create handle of the iATR indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//--- number of values copied from the iATR indicator 

   int values_to_copy;

//--- determine the number of values calculated in the indicator 

   int calculated=BarsCalculated(handle_iATR);

   if(calculated<=0)

     {

      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());

      return(0);

     }

//--- if it is the first start of calculation of the indicator or if the number of values in the iATR indicator changed 

//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history) 

   if(prev_calculated==0 || calculated!=ExtBarsCalculated || rates_total>prev_calculated+1)

     {

      //--- if the ExtAtrBuffer array is greater than the number of values in the iATR indicator for symbol/period, then we don't copy everything  

      //--- otherwise, we copy less than the size of indicator buffers 

      if(calculated>rates_total)

         values_to_copy=rates_total;

      else

         values_to_copy=calculated;

     }

   else

     {

      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate() 

      //--- for calculation not more than one bar is added 

      values_to_copy=(rates_total-prev_calculated)+1;

     }

//--- fill the ExtAtrBuffer array with values of the Average True Range indicator 

//--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operation 

   if(!FillArrayFromBuffer(ExtAtrBuffer,handle_iATR,values_to_copy))

      return(0);

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

   for(int i=limit;i<rates_total;i++)

     {

      if(i<Inp_ATR_ma_period)

         ExtArrowBuffer[i]=0.0;

      else

        {

         if(ExtAtrBuffer[i]<Inp_ATR_level)

           {

            ExtArrowBuffer[i]=high[i];

           }

         else

           {

            ExtArrowBuffer[i]=0.0;

           }

        }

     }

//--- return the prev_calculated value for the next call 

   return(rates_total);

  }

//+------------------------------------------------------------------+ 

//| Filling indicator buffers from the iATR indicator                | 

//+------------------------------------------------------------------+ 

bool FillArrayFromBuffer(double &values[],  // indicator buffer for ATR values 

                         int ind_handle,    // handle of the iATR indicator 

                         int amount         // number of copied values 

                         )

  {

//--- reset error code 

   ResetLastError();

//--- fill a part of the ExtAtrBuffer array with values from the indicator buffer that has 0 index 

   if(CopyBuffer(ind_handle,0,0,amount,values)<0)

     {

      //--- if the copying fails, tell the error code 

      PrintFormat("Failed to copy data from the iATR indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated 

      return(false);

     }

//--- everything is fine 

   return(true);

  }

//+------------------------------------------------------------------+ 

//| Create rectangle by the given coordinates                        | 

//+------------------------------------------------------------------+ 

bool RectangleCreate(const long            chart_ID=0,        // chart's ID 

                     const string          name="Rectangle",  // rectangle name 

                     const int             sub_window=0,      // subwindow index  

                     datetime              time1=0,           // first point time 

                     double                price1=0,          // first point price 

                     datetime              time2=0,           // second point time 

                     double                price2=0,          // second point price 

                     const color           clr=clrRed,        // rectangle color 

                     const ENUM_LINE_STYLE style=STYLE_SOLID, // style of rectangle lines 

                     const int             width=1,           // width of rectangle lines 

                     const bool            fill=false,        // filling rectangle with color 

                     const bool            back=false,        // in the background 

                     const bool            selection=true,    // highlight to move 

                     const bool            hidden=true,       // hidden in the object list 

                     const long            z_order=0)         // priority for mouse click 

  {

////--- set anchor points' coordinates if they are not set 

//ChangeRectangleEmptyPoints(time1,price1,time2,price2);

//--- reset the error value 

   ResetLastError();

//--- create a rectangle by the given coordinates 

   if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE,sub_window,time1,price1,time2,price2))

     {

      Print(__FUNCTION__,

            ": failed to create a rectangle! Error code = ",GetLastError());

      return(false);

     }

//--- set rectangle color 

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set the style of rectangle lines 

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set width of the rectangle lines 

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- enable (true) or disable (false) the mode of filling the rectangle 

   ObjectSetInteger(chart_ID,name,OBJPROP_FILL,fill);

//--- display in the foreground (false) or background (true) 

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of highlighting the rectangle for moving 

//--- when creating a graphical object using ObjectCreate function, the object cannot be 

//--- highlighted and moved by default. Inside this method, selection parameter 

//--- is true by default making it possible to highlight and move the object 

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- hide (true) or display (false) graphical object name in the object list 

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart 

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- successful execution 

   return(true);

  }

//+------------------------------------------------------------------+ 

//| Move the rectangle anchor point                                  | 

//+------------------------------------------------------------------+ 

bool RectanglePointChange(const long   chart_ID=0,       // chart's ID 

                          const string name="Rectangle", // rectangle name 

                          const int    point_index=0,    // anchor point index 

                          datetime     time=0,           // anchor point time coordinate 

                          double       price=0)          // anchor point price coordinate 

  {

//--- if point position is not set, move it to the current bar having Bid price 

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value 

   ResetLastError();

//--- move the anchor point 

   if(!ObjectMove(chart_ID,name,point_index,time,price))

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution 

   return(true);

  }

//+------------------------------------------------------------------+

Comments