Coming from Thinkorswim, I've enjoyed this indicator for a while and tried my first attempt to code in Ninjascript. Here is a very good explanation and the final verion of the ToS code is
declare lower; input length = 20; def closeLog = Log(close[1] / close[2]); def sDevd = stdev(closeLog, length) * sqrt(length/(length-1)); def m= SDevd * close[1]; plot spk = (close[0] - close[1]) / m;
protected override void OnBarUpdate() { // Use this method for calculating your indicator values. Assign a value to each // plot below by replacing 'Close[0]' with your own formula. double sdev; double tmpx; if (CurrentBar < 2) return; CloseLog.Set (Math.Log(Close[1]/Close[2])); if (CurrentBar > len ) { sdev = StdDev(CloseLog, len)[0] * Math.Sqrt(len/(len-1)); tmpx = (Close[0] - Close[1]) / (sdev * Close[1]); Value.Set(tmpx); Plot0.Set( Value[0] ); } }
1. the result is a bit off as compared to ToS's calculation, for example, On AAPL daily chart, the last few days are (date, NT, ToS)
12/5, -2.75, -2.68
12/6, 0.571, 0.557
Since the difference is not significant for me (normally you want to spot big spike), so I can live with this. But
2. The NT version does not calculate the last bar, even though I've set it “on bar close' to true.
I've tried using VisualStudio to step through (a very good exercise for new programmers!). It seems the last bar (in this case 12/7's bar) was never executed.
Any suggestion on why, or where I did wrong in this code?
Thanks.
Comment