Bands Chandelier

Author: Copyright © 2019, Vladimir Karputov
Indicators Used
Indicator of the average true range
0 Views
0 Downloads
0 Favorites
Bands Chandelier
ÿþ//+------------------------------------------------------------------+

//|                                             Bands Chandelier.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 3

#property indicator_plots   2

//--- plot ChandelierUP

#property indicator_label1  "ChandelierUP"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrRoyalBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot ChandelierDOWN

#property indicator_label2  "ChandelierDOWN"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrTomato

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

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

input int      InpShift          = 0;     // Bands Chandelier Shift

input double   InpDeviations     = 2.0;   // Bands Chandelier Deviations

//--- indicator buffers

double         ChandelierUPBuffer[];

double         ChandelierDOWNBuffer[];

double         ATRBuffer[];

//--- global variables

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

int            ExtPeriod,ExtShift;

double         ExtDeviations;

int            ExtPlotBegin   = 0;

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

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- check for input values

   if(Inp_ATR_ma_period<2)

     {

      ExtPeriod=20;

      printf("Incorrect value for input variable  ATR: averaging period=%d. Indicator will use value=%d for calculations.",Inp_ATR_ma_period,ExtPeriod);

     }

   else

      ExtPeriod=Inp_ATR_ma_period;

   if(InpShift<0)

     {

      ExtShift=0;

      printf("Incorrect value for input variable Bands Chandelier Shift=%d. Indicator will use value=%d for calculations.",InpShift,ExtShift);

     }

   else

      ExtShift=InpShift;

   if(InpDeviations==0.0)

     {

      ExtDeviations=2.0;

      printf("Incorrect value for input variable Bands Chandelier Deviations=%f. Indicator will use value=%f for calculations.",InpDeviations,ExtDeviations);

     }

   else

      ExtDeviations=InpDeviations;

//--- indicator buffers mapping

   SetIndexBuffer(0,ChandelierUPBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ChandelierDOWNBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ATRBuffer,INDICATOR_CALCULATIONS);

//--- the 0 (empty) value will mot participate in drawing

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//--- indicator name

   IndicatorSetString(INDICATOR_SHORTNAME,"Bands Chandelier");

//--- indexes draw begin settings

   ExtPlotBegin=ExtPeriod-1;

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPeriod);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtPeriod);

//--- indexes shift settings

   PlotIndexSetInteger(0,PLOT_SHIFT,ExtShift);

   PlotIndexSetInteger(1,PLOT_SHIFT,ExtShift);

//--- number of digits of indicator value

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);

//--- 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[])

  {

//--- check for bars count

   if(rates_total<ExtPlotBegin)

      return(0);

//--- 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!=bars_calculated || rates_total>prev_calculated+1)

     {

      //--- if the iATRBuffer 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 iATRBuffer 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(ATRBuffer,handle_iATR,values_to_copy))

      return(0);

//--- memorize the number of values in the Average True Range indicator

   bars_calculated=calculated;

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

     {

      for(int i=0; i<=ExtPeriod; i++)

        {

         ChandelierUPBuffer[i]=0.0;

         ChandelierDOWNBuffer[i]=0.0;

        }

      limit=ExtPeriod;

     }

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

     {

      double price_highest=DBL_MIN,price_lowest=DBL_MAX;

      int  highest=-1,lowest=-1;

      for(int j=i-ExtPeriod; j<i; j++)

        {

         if(high[j]>price_highest)

           {

            price_highest=high[j];

            highest=j;

           }

         if(low[j]<price_lowest)

           {

            price_lowest=low[j];

            lowest=j;

           }

        }

      ChandelierUPBuffer[i]=low[lowest]+InpDeviations*ATRBuffer[i];

      ChandelierDOWNBuffer[i]=high[highest]-InpDeviations*ATRBuffer[i];

     }

//--- return value of prev_calculated for 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 iATRBuffer 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);

  }

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

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 ---