Sometimes StochK returns 0

Hi everyone,
I found some bug on StochK calculation during my backtests, in short - previous operations influence on StochK results to 0 output.
It only works on dataset like META attached below and similar in my dataset.
Here is my script run on Amibroker 6.18.0 (x64) and 6.40 (x64 Trial):

ma1=MA(C,1);
ma2=MA(C,2);
filter1=0;
filter2=((ma1-0)/(ma2-ma1))<1;// if we put zero division check here like =iif( (ma2-ma1)!-...)- bug disappear, or if we remove -0 from upper part - is also disappear
filter3=filter2 and filter1;// If we rewrite this to filter2==true and filter1; - bug also disappear

st_=LastValue(StochK(14, 3));///the previous script result on StochK calculation to zero result here

_TRACE(StrFormat("%g",st_));//0 on bug case

Buy=Sell=Short=Cover=0;

While StochK has its own fixed parameters it returns 0 except for if any above changes made to the script it on following dataset (sorry long data I shorten it to the point where bug still active)
The Data link.

StochK can return value of zero. It is valid value as any value in 0..100 range for StochK.

Second thing - Ma(c,1) doesn't make sense. It is just close price.

Third thing - it is mathematically forbidden to divide by zero. If you denominator can be zero, use SafeDivide function.

1 Like

Thank you Tomasz for your reply,
I shortened my script as possible, sorry for some issues.
StochK can be 0 yes but if those changes made in upper independent script parts it returns correct value 40.3277. The thing is that previous part somehow influence to StochK.
Division by zero - I agree but it does not belong to any part of StochK. Why it changes on that?
Can you check it please?

@Tomasz there is definitely something odd going on here. I have simplified the code a bit more so that you can just comment out one line to change the stochK result. Also, as the OP indicated, the problem does not occur with other symbol data, or at least not with a few random samples I tried. I downloaded his data and ran an Exploration against All Quotes to reproduce the problem.

ma1=MA(C,2);
ma2=MA(C,3);

// StochK works as expected with this line active. StochK result is "inf" with this line commented out
//f2=(ma1)/(ma2-ma1) < 1;

f1=0;
f2=(ma1-0)/(ma2-ma1) < 1;
f3=f2 and f1;

stK=StochK(14, 3);
st_=LastValue(stk);

Filter = True;

AddColumn(H, "H");
AddColumn(L, "L");
AddColumn(C, "C");
AddColumn(ma1, "ma1");
AddColumn(ma2, "ma2");
AddColumn(f1, "f1");
AddColumn(f2, "f2");
AddColumn(f3, "f3");
AddColumn(stK, "StochK");
AddColumn(st_, "Last StochK");
2 Likes

By definition, there is a division in stochastic (by hhv(high, period)-llv(low,period)

If this hhv minus llv is zero (which can only happen when prices do not move completely over entire period) there will be 0/0 division (indefinite number) and that might be what is happening on your end. Check your data and display hhv-llv

The data you have used contains such prolonged series of same quotes.

I can see in the code that StochK function would simply skip the division (since it is illegal to divide by zero) and do nothing. Such array element would be uninitialized (possibly holding values kept in memory from previous calcs).

That may be the case, but then why would uncommenting this line change the result of stochK?

//f2=(ma1)/(ma2-ma1) < 1;

I wrote already, illegal division is NOT done, leaving array element uninitialized. Uninitialized array element might hold some value from previous calc.

PS. Of course I will address that edge case in upcoming versions, but still the output value for that case should be just ZERO. ZERO is correct answer for flat line data.

1 Like

It is worth noting that this data set is essentially faulty. It contains quotations of META when it DID NOT EXIST. "META" did not exist in year 2000 yet this data file contains some artificial, non-existing data that is just flat line equal to 38.23 for all dates in the range 2000-2012 (May) when Facebook IPO took place. Garbage in - garbage out.

1 Like

updated data link (I cant change it in the beginning)

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.