2017-12-05 13:16:14 +01:00
//+------------------------------------------------------------------+
//| WPR.mq5 |
//| Copyright 2009-2017, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009-2017, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property description "Larry Williams' Percent Range"
//---- indicator settings
#property indicator_separate_window
#property indicator_level1 -20.0
#property indicator_level2 -80.0
#property indicator_levelstyle STYLE_DOT
#property indicator_levelcolor Silver
#property indicator_levelwidth 1
#property indicator_maximum 0.0
#property indicator_minimum -100.0
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 DodgerBlue
//---- input parameters
input int InpWPRPeriod=14; // Period
//---- buffers
double ExtWPRBuffer[];
//--- global variables
int ExtPeriodWPR;
//
2021-04-28 17:27:12 +02:00
#include <AZ-INVEST/CustomBarConfig.mqh>
2017-12-05 13:16:14 +01:00
//
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- check for input value
if(InpWPRPeriod<3)
{
ExtPeriodWPR=14;
Print("Incorrect InpWPRPeriod value. Indicator will use value=",ExtPeriodWPR);
}
else ExtPeriodWPR=InpWPRPeriod;
//---- name for DataWindow and indicator subwindow label
IndicatorSetString(INDICATOR_SHORTNAME,"%R"+"("+string(ExtPeriodWPR)+")");
//---- indicator's buffer
SetIndexBuffer(0,ExtWPRBuffer);
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPeriodWPR-1);
//--- digits
IndicatorSetInteger(INDICATOR_DIGITS,2);
//----
}
//+------------------------------------------------------------------+
//| 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[])
{
//
2021-04-28 17:27:12 +02:00
// Process data through MedianRenko indicator
//
2017-12-05 13:16:14 +01:00
2020-02-23 16:21:15 +01:00
if(!customChartIndicator.OnCalculate(rates_total,prev_calculated,time,close))
2017-12-05 13:16:14 +01:00
return(0);
2021-04-28 17:27:12 +02:00
2020-02-23 16:21:15 +01:00
if(!customChartIndicator.BufferSynchronizationCheck(close))
return(0);
2021-04-28 17:27:12 +02:00
//
// Make the following modifications in the code below:
//
// customChartIndicator.GetPrevCalculated() should be used instead of prev_calculated
//
// customChartIndicator.Open[] should be used instead of open[]
// customChartIndicator.Low[] should be used instead of low[]
// customChartIndicator.High[] should be used instead of high[]
// customChartIndicator.Close[] should be used instead of close[]
//
// customChartIndicator.IsNewBar (true/false) informs you if a renko brick completed
//
// customChartIndicator.Time[] shold be used instead of Time[] for checking the renko bar time.
// (!) customChartIndicator.SetGetTimeFlag() must be called in OnInit() for customChartIndicator.Time[] to be used
//
// customChartIndicator.Tick_volume[] should be used instead of TickVolume[]
// customChartIndicator.Real_volume[] should be used instead of Volume[]
// (!) customChartIndicator.SetGetVolumesFlag() must be called in OnInit() for Tick_volume[] & Real_volume[] to be used
//
// customChartIndicator.Price[] should be used instead of Price[]
// (!) customChartIndicator.SetUseAppliedPriceFlag(ENUM_APPLIED_PRICE _applied_price) must be called in OnInit() for customChartIndicator.Price[] to be used
//
2020-02-23 16:21:15 +01:00
int _prev_calculated = customChartIndicator.GetPrevCalculated();
2017-12-05 13:16:14 +01:00
2021-04-28 17:27:12 +02:00
//
//
2017-12-05 13:16:14 +01:00
//
//---- insufficient data
if(rates_total<ExtPeriodWPR)
return(0);
//--- start working
int i=_prev_calculated-1;
//--- correct position
if(i<ExtPeriodWPR-1) i=ExtPeriodWPR-1;
//--- main cycle
while(i<rates_total && !IsStopped())
{
//--- calculate maximum High
2020-02-23 16:21:15 +01:00
double dMaxHigh=MaxAr(customChartIndicator.High,ExtPeriodWPR,i);
2017-12-05 13:16:14 +01:00
//--- calculate minimum Low
2020-02-23 16:21:15 +01:00
double dMinLow=MinAr(customChartIndicator.Low,ExtPeriodWPR,i);
2017-12-05 13:16:14 +01:00
//--- calculate WPR
if(dMaxHigh!=dMinLow)
2020-02-23 16:21:15 +01:00
ExtWPRBuffer[i]=-(dMaxHigh-customChartIndicator.Close[i])*100/(dMaxHigh-dMinLow);
2017-12-05 13:16:14 +01:00
else
ExtWPRBuffer[i]=ExtWPRBuffer[i-1];
//--- 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);
}
//+------------------------------------------------------------------+
�