WPR Reversal Arrow

Author: Copyright © 2021, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
WPR Reversal Arrow
ÿþ//+------------------------------------------------------------------+

//|                                           WPR Reversal Arrow.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.000"

//---- indicator settings

#property indicator_separate_window

#property indicator_levelstyle STYLE_DOT

#property indicator_levelwidth 1

#property indicator_maximum    0.0

#property indicator_minimum    -100.0

#property indicator_buffers    3

#property indicator_plots      3

//--- plot WPR

#property indicator_label1  "WPR"         // Name of a plot for the Data Window 

#property indicator_type1   DRAW_LINE     // Type of plotting is line 

#property indicator_color1  clrDarkOrchid // Line color 

#property indicator_style1  STYLE_SOLID   // Line style 

#property indicator_width1  1             // Line Width 

//--- plot Upward reversal

#property indicator_label2  "Upward reversal"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrForestGreen

#property indicator_width2  1

//--- plot Downward reversal

#property indicator_label3  "Downward reversal"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrForestGreen

#property indicator_width3  1

//---- input parameters

input group             "WPR"

input int                  Inp_WPR_Period       = 14;             // Period

input color                Inp_WPR_Color        = clrDodgerBlue;  // Color line

input int                  Inp_WPR_Width        = 1;              // Width

input int                  Inp_WPR_Level1       = -20.0;          // Value Level #1

input double               Inp_WPR_Level2       = -80.0;          // Value Level #2

input group             "Arrow"

input uchar                InpUpwardCode        = 228;            // Upward: code (font Wingdings)

input int                  InpUpwardShift       = 5;              // Upward: vertical shift of arrows in pixels

input uchar                InpDownwardCode      = 230;            // Downward: code (font Wingdings)

input int                  InpDownwardShift     = 5;              // Downward: vertical shift of arrows in pixels

//---- buffers

double    iWPRBuffer[];

double    UpwardBuffer[];

double    DownwardBuffer[];

//--- global variables

int       m_period_wpr;

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

//| Custom indicator initialization function                         |

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

void OnInit()

  {

//--- check for input value

   if(Inp_WPR_Period<3)

     {

      m_period_wpr=14;

      Print("Incorrect InpWPRPeriod value. Indicator will use value=",m_period_wpr);

     }

   else

      m_period_wpr=Inp_WPR_Period;

//--- indicator buffers mapping

   SetIndexBuffer(0,iWPRBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,UpwardBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,DownwardBuffer,INDICATOR_DATA);

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

   PlotIndexSetInteger(1,PLOT_ARROW,InpUpwardCode);

   PlotIndexSetInteger(2,PLOT_ARROW,InpDownwardCode);

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

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-InpUpwardShift);

   PlotIndexSetInteger(2,PLOT_ARROW_SHIFT,InpDownwardShift);

//--- set as an empty value 0.0

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);

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

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,m_period_wpr-1);

//--- digits

   IndicatorSetInteger(INDICATOR_DIGITS,2);

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

   IndicatorSetString(INDICATOR_SHORTNAME,"%R Reversal Arrow"+"("+IntegerToString(m_period_wpr)+")");

//--- custom parameters

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,Inp_WPR_Color);

   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,Inp_WPR_Width);

   IndicatorSetInteger(INDICATOR_LEVELS,2);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,Inp_WPR_Level1);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,Inp_WPR_Level2);

//---

  }

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

//| Williams  Percent Range                                          |

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

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

  {

//---- insufficient data

   if(rates_total<m_period_wpr)

      return(0);

//--- start working

   int i=prev_calculated-1;

//--- correct position

   if(i<m_period_wpr-1)

     {

      i=m_period_wpr-1;

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

        {

         UpwardBuffer[j]=EMPTY_VALUE;

         DownwardBuffer[j]=EMPTY_VALUE;

        }

     }

//---  main cycle

   while(i<rates_total && !IsStopped())

     {

      //--- calculate maximum High

      double dMaxHigh=MaxAr(high,m_period_wpr,i);

      //--- calculate minimum Low

      double dMinLow=MinAr(low,m_period_wpr,i);

      //--- calculate WPR

      if(dMaxHigh!=dMinLow)

         iWPRBuffer[i]=-(dMaxHigh-close[i])*100.0/(dMaxHigh-dMinLow);

      else

         iWPRBuffer[i]=iWPRBuffer[i-1];

      //--- arrow

      UpwardBuffer[i]=EMPTY_VALUE;

      DownwardBuffer[i]=EMPTY_VALUE;

      if(i>0)

        {

         if(iWPRBuffer[i-1]<Inp_WPR_Level2 && iWPRBuffer[i]>Inp_WPR_Level2)

            UpwardBuffer[i]=iWPRBuffer[i];

         if(iWPRBuffer[i-1]>Inp_WPR_Level1 && iWPRBuffer[i]<Inp_WPR_Level1)

            DownwardBuffer[i]=iWPRBuffer[i];

        }

      //--- increment i for next iteration

      i++;

     }

//--- return new prev_calculated value

   return(rates_total);

  }

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

//| Maximum High                                                     |

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

double MaxAr(const double &array[],int period,int cur_position)

  {

   double Highest=array[cur_position];

   for(int i=cur_position-1; i>cur_position-period; i--)

     {

      if(Highest<array[i])

         Highest=array[i];

     }

   return(Highest);

  }

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

//| Minimum Low                                                      |

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

double MinAr(const double &array[],int period,int cur_position)

  {

   double Lowest=array[cur_position];

   for(int i=cur_position-1; i>cur_position-period; i--)

     {

      if(Lowest>array[i])

         Lowest=array[i];

     }

   return(Lowest);

  }

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

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