StochasticOscillator returns NaN for D
ardawan opened this issue · 4 comments
Describe the bug
I'm not sure if this is a bug or if I'm doing something wrong here. But when logging the result of the StochasticOscillator()
I can see that the result for D is always NaN
.
To Reproduce
Steps to reproduce the behavior:
chunk:=closingPrices[len(closingPrices)-14:]
h:=indicator.Max(14, closingPrices)
l:=indicator.Min(14, closingPrices)
k, d := indicator.StochasticOscillator(h, l, chunk)
fmt.Println(d)
- Output - D:
[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]
- Output - K:
[NaN 0 0 0 22.26345083488181 0 10.936227951150597 47.89687924016866 9.525101763903766 25.617367706917406 11.234735413838756 13.622795115334153 21.81818181818505 35.278154681141146]
Desktop (please complete the following information):
- OS: ubuntu
- Version 22.04
Thank you for reporting it. Let me take a quick look.
I wonder if it has to do with the fact that StochasticOscillator is also doing its own 14-day max and min of the given high and low prices.
highestHigh14 := Max(14, high)
lowestLow14 := Min(14, low)
Instead of calculating the high and low from the closing prices, do you have access to the actual high and low prices for each day?
This was my test for it by the way:
indicator/momentum_indicators_test.go
Line 131 in c5c6b9e
I've tried to chunk the prices to 14 in length and pass it to all three parameters like this
indicator.StochasticOscillator(chunk, chunk, chunk)
but the output still is the same.
If the High and Low are calculated within the function, why not only accept the closing prices as parameter instead of the "high, low, closingPrices" parameters?
Well, actually I'm collecting only closing prices every 60 seconds. So the only data I do have is the closing prices.
Sorry for the slow response on this. I finally found some time to take a deeper look, and I noticed that the NaN value from the K is causing the SMA to keep returning NaN values. I made a quick change to handle that for this indicator, but it does feel like something that I should handle everywhere else. It doesn't happen as much since most indicators are expecting that they will be given high, low, and closing separately, and not just closing, but definitely worth handling it across the board later.
If you see any other issue, please feel free to send a bug. I'm closing it for now assuming that it solved your issue, but please feel free to reopen if it didn't.