ATR arrow level

Author: Copyright © 2019, Vladimir Karputov
Indicators Used
Indicator of the average true range
0 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

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---