# How to compute WPR value of an indicator buffer?

162

Hi, all:

I want to calculate the WPR value of a detrended price series.

First, I use an indicator buffer to store the detrended price series. But to simplify the question, I directly transfer the raw prices to the buffer.

Export the so called "detrended price series" into the chart, we can see it's exactly the same as the raw prices.(see below)

Now, the question is how can I use an effective way to calculate the WPR value of the detrended price series.

Because there is no "OnArray" function to use, I guess the only way is to write "for" code to directly calculate the value.

Below is the code I use:

//---- Oscillator of Detrended Signal Computing

for(i=0; i<limit; i++)
{
DtdValueMin[i] = 999999;
DtdValueMax[i] = -999999;
for(int k=0; k<=Osc_Period; k++)
{
if(DtdValue[k] > DtdValueMax[i])
DtdValueMax[i] = DtdValue[k];
if(DtdValue[k] < DtdValueMin[i])
DtdValueMin[i] = DtdValue[k];
}
DtdOsc[i] = 0.02*((DtdValue[i]-DtdValueMin[i])/(DtdValueMax[i]-DtdValueMin[i])*100-50);
if(DtdOsc[i]>0.9999)
DtdOsc[i]=0.9999;
if(DtdOsc[i]<-0.9999)
DtdOsc[i]=-0.9999;
}
return(0);
}

You can see that there is a "for" circle in another "for" circle, which is not a fast algorithm I think.

And the result is odd.......

Theoretically, the calculated result should be exactly the same as iWPR in the MT4.

But the result is not the same, so there must be errors.

saji

162

Below is the wrong result I got:

the first indicator is the value I got, the second indicator is the iWPR value calculate by MT4 function.

Regards.

Moderator
18037

saji:

Hi, all:

I want to calculate the WPR value of a detrended price series.

First, I use an indicator buffer to store the detrended price series. But to simplify the question, I directly transfer the raw prices to the buffer.

Export the so called "detrended price series" into the chart, we can see it's exactly the same as the raw prices.(see below)

You need to show your code . . . where does the value for DtdValue[] come from ?
162

RaptorUK:
You need to show your code . . . where does the value for DtdValue[] come from ?

the code:

int start()
{
//----
int limit,i;
int counted_bars=IndicatorCounted();

//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;

//---- Detrended Signal Computing
switch(LM_Mode)
{
case 0:     //-------Basic Signal (No Local Mean)
for(i=0; i<limit; i++)
{
LM[i]=0;
DtdValue[i]=Close[i] - LM[i];
}
break;
}

"LM_Mode" is the variable I use to choose the algorithm of Local Mean computing, as I said before here i used 0 value which stand for no local mean.

thank you for the attention

162

the rest of it

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red

//----
extern int LM_Mode = 0;
extern int Smooth_Period = 14;
extern int Osc_Period = 18;
extern int Shift_Period = 11;

//---- buffers
double LM[];
double DtdValue[];
double DtdOsc[];
double DtdValueMin[];
double DtdValueMax[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(5);
//---- indicator line
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, DtdOsc);
SetIndexBuffer(1, DtdValue);
SetIndexBuffer(2, LM);
SetIndexBuffer(3, DtdValueMax);
SetIndexBuffer(4, DtdValueMin);
//----
SetIndexDrawBegin(0, Smooth_Period+Shift_Period);
//----
return(0);
}
Moderator
18037

saji:

Hi, all:

I want to calculate the WPR value of a detrended price series.

First, I use an indicator buffer to store the detrended price series. But to simplify the question, I directly transfer the raw prices to the buffer.

Export the so called "detrended price series" into the chart, we can see it's exactly the same as the raw prices.(see below)

Shouldn't this . . .

DtdOsc[i] = 0.02*((DtdValue[i]-DtdValueMin[i])/(DtdValueMax[i]-DtdValueMin[i])*100-50);

. . . be this . . .

DtdOsc[i] = 0.02*( (   DtdValueMax[i]   - DtdValueMin[i] ) / ( DtdValueMax[i] - DtdValueMin[i] ) * 100 - 50);

. . ?

%R = (HIGH(i-n)-CLOSE)/(HIGH(i-n)-LOW(i-n))*100
7371

RaptorUK:

Shouldn't this . . .

. . . be this . . .

. . ?

Please check as this will be always 1.0:

DtdValueMax[i]   - DtdValueMin[i] ) / ( DtdValueMax[i] - DtdValueMin[i] )

may be you meant

DtdValueMax[i]   - DtdValue[i] ) / ( DtdValueMax[i] - DtdValueMin[i] )

?

Gooly

Moderator
18037

gooly:

Please check as this will be always 1.0:

may be you meant

?

Ah yes, well spotted
162

RaptorUK:

Shouldn't this . . .

. . . be this . . .

. . ?

I really doubt about it. I think no matter it is （high-price）or（price-low）,the result should be within [-1,1]. and from the chart you can see the results are always beyond the field. so i think there is other reason. Cause I haven't got a PC on hand, so I can't check what you said right now. but i will check as soon as possible, pls keep in touch! And thank you for the help again.
162

RaptorUK:
Ah yes, well spotted

Hi,RaptorUK:

the exact WPR formula is : -1*(DtdValueMax[i] - DtdValue[i] ) / ( DtdValueMax[i] - DtdValueMin[i] )

so I use the code below:

DtdOsc[i] = 0.02*(-1*(DtdValueMax[i]-DtdValue[i])/(DtdValueMax[i]-DtdValueMin[i])*100-50);
162

the reslut in the chart: the first signal

you can see that there are a lot of values out of the [-1,1] field.