KDJ indicator question

Hi all,

I am trying to write my own KDJ indicator, I basically copy the code from Tradingview: https://www.tradingview.com/script/h51ZIRlX-KDJ-Indicator-iamaltcoin/ and try to make it work on QuantScript.

This is what I have so far:

However, when I compare the J value from my Debugger to the one on TradingView, my J value is showing the previous bar’s J value on TradingView. I am guessing it is something to do with the function I have but I can’t figure it out.

Any help is appreciated. Thank you!

I have also brought in RSI, MACD and MACDSignal and they are all showing the value from the previous 15 min bar, so I guess it is supposed to be this way?

Is it something to do with close price from the previous bar? Can someone explain please? :fearful:

This is expected.

It’s covered in the docs here and here.

The time you’re seeing in the debugger is of the “open bar” for which the script executes. This “open bar” does not have high, low and close prices. These are taken from the previous bar.

Of course I’ve put “open bar” in quotes as, in reality, the bar is closed as it is in the past, however this “bar opening” behaviour is simulated for the backtest/debugging, as explained in the docs.

Thanks for your reply @Nikola I will def check the docs out.

Sorry but I have got another questions, please see the screenshots above.

For NDAQ100 starting from May 25 2020 at 17:30 UTC time, the build-in variables are all the same until 22:15 UTC on the same day. That result my formular to provide unknown values (all the previous value are okay), so when I do my backtest I have to avoid that period and only backtest from June 2020. Is there solution/workaround to that please?

Thank you.

1 Like

You’re probably dividing by zero, which causes unknown values. Add some zero-division handling for every division you’re making and you should be fine.


function SafeDivision(x, y): x / y if y != 0 else 0

SafeDivision(2, 0) -> this returns 0, instead of unknown

Depending on your use-case you may want to return something other than 0, of course.

1 Like

Thanks, I will look into it and update my scripts.

This should be it, right? It’s based on Stochastic’s K and D line which are calculated by calling Stochastic and StochasticSignal respectively.

I’ll add it as a built-in function later today. Let me know if I’ve missed something.

1 Like

Thanks! yes it should be something similar, I will look into the variable and see if I can tweak it a bit.

Thanks for adding it in!