Need help in the coding of a simple Indicator (newbie inside ;) )
I am getting started coding my first indicator and I am running on a couple of difficulties.
I am coding a smoothed version of the RsRank where:
RsRank : ( ( Close-Close + Close-Close ) / 2 ) / 10 Day ATR )
and Smoothed: ( rsRankValue + rsRankValue*2 + rsRankValue*2 + rsRankValue )/6;
- My Closes and Closes are cast in stone (I created variables "LT_Close" and "ST_Close" but I cant add them to the formula without trigerring an error when compiling)
- how do I code a RsRank_Smoothed.Set as I need to call RsRank[n bars ago] in the calculation?
I understand that RsRank is a normalized average calculated from a momentum(140) and a momentum(20). In a second step you wish to apply further smoothing. I further assume that you wish to display both RsRank and RsRank_Smoothed on your chart.
In that case you would add two plots in the Initialize() section of your indicator. You also need to defined the properties for those two plots in the Properties section of the indicator.
The code in OnBarUpdate would be pretty simple:
-> You do not need to use Closes, this would be the second bar series added to a multi-timeframe indicator. For your purposes the primary bar series should be ok, which is called as Close.
-> Although the ATR usually does not hold the value zero, this may happen at rare occasions.
-> Your smoothing idea uses a triangular moving average with the period 2, which is already available with NinjaTrader.
-> To calculate RsRank_Smoothed you need to use RsRank as input series for the TMA.
Last edited by Fat Tails; November 23rd, 2014 at 11:30 AM.
I will try your suggestions as soon as I can get access to my laptop.
I think I get the gist (and good idea to take into account a possible 0 value of the ATR).
I like the use of NT momentum method (at least I won't have to reinvent the wheel...)
I will look into the triangular MA as I wasn't aware of it. The big idea was to use a FIR filter (Ehler) from what I read in an article.
By the way: what would you recommend I read to get up to speed quickly on Ninjascript? (I have some coding background, self taught pop a long time ago and some basic understanding of java/c#)
Thanks again for your input on my code, it really makes huge impact on my understanding of NT as this early stage
-> The "try/catch" statement just shows that you do not fully understand what you are doing. Otherwise you would not use it here. However, it might help you to find bugs as long as the code is not working.
-> The average true range is used to normalize the indicator. To explain why I used ATR(....), we need to differentiate between running an indicator in CalculateOnBarClose = true and CalculateOnBarClose = false. Let us go back to Welles Wilder jr. who created the ATR. First of all it is important to understand that he probably used exponential moving averages because they were easier to calculate. Back in the 70s there were no PCs or pocket calculators available. Also he applied those indicators to printed daily charts, which reflected the prices at the end of the trading session. The indicator is not fit to be applied to intraday screenshots of daily charts.
CalculateOnBarClose = true: The indicator uses the final current bar to calculate the average true range. This is fine and you can also use the current value ATR(...).
CalculateOnBarClose = false: The problem here is that the new bar starts out with a single tick. This means that the true range for the current bar is typically 0, when the new bar opens. As a consequence the ATR(...) is systematically skewed to the downside, while the bar is developing. While the bar develops, the range of the current bar gradually builds up and the systematic error of the ATR becomes smaller.
In fact the ATR belongs to a class of indicators that cannot be used with COBC = false. However, you can take the ATR of the prior bar which is correct, as the prior bar is already complete. Therefore, if you wish to run an indicator in mode CalculateOnBarClose = false, you need to use the ATR(...) and not the ATR.
Another advantage of using the ATR(....) is that you only need to calculate it once during the life of the current bar. This reduces CPU load. Here is the code
The formula for the triangular moving average TMA should be identical to your formula with the period p=4 (above I had written p = 2), another variation would be SMA(SMA(RSRank, 2), 3).
Last edited by Fat Tails; November 24th, 2014 at 07:50 AM.
The following 3 users say Thank You to Fat Tails for this post: