I get error by using the indicator StochRSI in some time frame

 

Hi,


In the attachment is an indicator which does similar to StochRSI. There are two input paras: RSI and K and D. When I chose RSI=14, K=2 and D=2, I get error message as follow:

...

2017.07.09 21:03:30.843 StochRSI EURUSD,H1: zero divide in 'StochRSI.mq4' (77,56)

...

But by some other timeframe there is no problem.


Why?

Files:
StochRSI.mq4  3 kb
 

% K is calculated as (current value - lowest value in the period) / (highest value in the period - lowest value in the period).

If there is no price movement in the period, the difference between the highest value and the lowest value becomes 0, resulting in "divide by zero" error.

I modified the program to use the previous value in that case.

There were several other mistakes, also I fixed them.

Files:
StochRSI.mq4  4 kb
 
Naguisa Unada:

% K is calculated as (current value - lowest value in the period) / (highest value in the period - lowest value in the period).

If there is no price movement in the period, the difference between the highest value and the lowest value becomes 0, resulting in "divide by zero" error.

I modified the program to use the previous value in that case.

There were several other mistakes, also I fixed them.


Hi Naguisa,


Thanks for the reply. But I got another error no matter by which time frame as I take all the input parameters with the value of 2.

...

2017.07.10 07:11:57.530 StochRSI__1 EURUSD,H1: array out of range in 'StochRSI__1.mq4' (97,27)

...



Besides, I use the following to draw two horizontal lines:

...

#property indicator_level1 85.0

#property indicator_level2 15.0

...

But what I really want is the line should not be dot line and has color of blue. How can I do that?


Cheers

 
thomas2004 :

Hi,

In the attachment is an indicator which does similar to StochRSI. There are two input paras: RSI and K and D. When I chose RSI=14, K=2 and D=2, I get error message as follow:


Extern int RSI_Periods = 14;
Extern int Percent_K = 2;
Extern int Percent_D = 2;

These are your setting, no?

It works normally with this setting.

If you want RSI_Periods also to be 2, line 69 has been modified as follows.

 //limit = Bars - MathMax(RSI_Periods, Percent_K); 
limit = Bars - (RSI_Periods + Percent_K + Percent_D);

For the level lines add the following to line 15.

 #property  indicator_level1 85.0 
 #property  indicator_level2 15.0 
 #property  indicator_levelcolor clrBlue 
 #property  indicator_levelstyle STYLE_SOLID 
In any case, it give me trouble to request anything bit by bit later. Write all the information in the first question.
 
Naguisa Unada:

If you want RSI_Periods also to be 2, line 69 has been modified as follows.

 //limit = Bars - MathMax(RSI_Periods, Percent_K); 
limit = Bars - (RSI_Periods + Percent_K + Percent_D);
That code results in all bars being recalculated every tick. Do your lookbacks correctly.
 
whroeder1:

That code results in all bars being recalculated every tick. Do your lookbacks correctly.
if (counted_bars == 0)
   limit = Bars - (RSI_Period + Percent_K + Percent_D);
else
   limit = Bars - counted_bars;
Is there anything wrong?
 
Naguisa Unada:
Is there anything wrong?

Many thanks, Naguisa! It works fine now!


Another question: seems this indicator is quite interesting, right? Have yo ueven used and got any experience?

 
thomas2004 :
Another question: seems this indicator is quite interesting, right? Have yo ueven used and got any experience?

I have never used this, but I have used Traders Dynamic Index (TDI) which is similar to this one.

Either way, I think that the behavior is too critical to use with your settings 2, 2, 2. I will use it with a larger setting than the original.

 
Naguisa Unada:

I have never used this, but I have used Traders Dynamic Index (TDI) which is similar to this one.

Either way, I think that the behavior is too critical to use with your settings 2, 2, 2. I will use it with a larger setting than the original.


Hi Naguisa,

I would like to say I doubt a little bit if this indicator works correct. Why? When I set all the parameters of 2, for example on EURUSD, I got as follow:

My opinion, if all the parameters set to 2, the values of StochRSI can take only either 100 or 0, not between, right?


Here is the definition of StochRSI:

stochRSI = (rsi- rsi(min)) / (rsi(max) - rsi(min)) * 100


This means, if the parameter is 2, the values of history rsi can only be rsi(min) os rsi(max), so the StochRSI can only be 0 or 100, right?


I mean somewhat like this as below:


 

That indicator calculates stochastic all wrong. This line :

for (int x = 0; x <= Percent_K; x++)

makes it calculate stochastic period 1 period longer than it should be. Same for smoothing

Use some better stochastic rsi indicator (there are a lot of better versions)

 

Mladen gave you right answer.

Until today I was not aware of the original program was wrong.

Fix as follows.

//for (int x = 0; x <= Percent_K; x++)
for (int x = 0; x < Percent_K; x++)

//for (int x = 0; x <= Percent_D; x++) 
for (int x = 0; x < Percent_D; x++) 
Reason: