PDA

View Full Version : A very basic question: Can't get the right result from a simple operation...


Miggers
01-28-2010, 11:14 AM
Sorry, this seems so elemental that feels embarrissing to ask (the problem of ignorance...), hence the apologies in advanced...

I created a new indicator using the original NT's code for the ATR indicator, my aim is simply to plot ATR/Close for each bar on a chart.

Using the original NT's code with the required adjustments to the properties region, etc. works nicely. if I set my variable to ATR it plots ATR exactly as plotted using the original NT's indicator. If I set my variable to Close[0], it plots the close correctly too. BUT, when I set my variable to ATR/Close[0] it plots wrong values.

If I print ATR/Close[0] to the output window for debugging while my variable is set to (say...) ATR, then it prints the right values on the window, but if I set my variable to ATR/Close[0] then the output window gives the same wrong valus as the plot.

What am I doing wrong?

Attached is the code for my indicator. Below the key part of the OnBarUpdate:

double trueRange = High[0] - Low[0];
trueRange = Math.Max(Math.Abs(Low[0]... as in NT's original code
double ATRtemp;
ATRtemp = (((Math.Min(CurrentBar + 1, ... as in NT's original code
double todayClose = Close[0];
double ATRpctg = ATRtemp/todayClose;
Print(ATRpctg);
ATRPercentage.Set(ATRpctg); <--- if I set this to a different value (say Close[0]) the output window prints ATRpctg correctly

Thank you for your help!

NinjaTrader_Bertrand
01-28-2010, 11:24 AM
Welcome to the forums, you just see a full double value displayed (precision wise), the chart would round automatically, so for the output window value is the 'raw' one...try for example Math.Round on the value to make it easier readable in the ouput.

Miggers
01-28-2010, 12:02 PM
Dear Bertrand, thank you for your prompt reply.

The incorect output doesn't look like rounding error. I tried your suggestion anyway and it didn't work.

For example, for an ATR=0.707 and a Close=44.35 the expected value should be around 0.0159 (0.707/44.35), in the ouput window I get 0,0159506 (which is ok!)... when I set my variable value to my double ATRtemp, but if I set my variable to the double ATRpctg (=ATRtemp/todayClose) the output window prints 0,00137.... which is a long way off.

I see the problem occurring when setting my indicator's value. Could this be related to variables definition or data series?

NinjaTrader_Bertrand
01-28-2010, 12:40 PM
Hi Miggers, could't reproduce the incorrect value, you would see the full precision in the output window expressed in exponential manner in your case of a very small number. From what I tested, I believe your calcs hitting home.

Miggers
01-28-2010, 01:18 PM
Hi Betrand,

Attached you will find an example with the QQQQ chart (scroll to the left to show the last bar as for the 26/Jan) using the code I posted earlier.

You will find in the attachment that for the 26/Jan the Close = 44.35, ATR = 0.707, and the MVATRPctg = 0.00137

However but for those values 0.707/44.35 = 0.0159, the result in my chart is more than 10 times smaller.

Are you getting the same results as in the attached picture when you run my code?

NinjaTrader_Bertrand
01-28-2010, 02:36 PM
Miggers, tried on AAPL daily and printed just the calculated ATRtemp value from you to compare to the ATR indicator, this seems off by a large degree, thus resulting in the unexpected results you get - please try with dividing the Close into the standard ATR() method we offer per default in NinjaScript.

Miggers
01-28-2010, 04:19 PM
Betrand, I tried with NT's default code, but because it is read only the canges on the code after copiling are not reflected on the chart following reload.

So, I created yet another fresh new ATR indicator, called the input value "Period" as per the default NT's code, copied the code from the the NT's default code and pasted it in the new editor (only different bit is the name of the oscillator), then compile it, included it in a chart and plotted it below the default ATR for comparison, in this case the results are exactly as NT's default ATR. Then I set the value to what it is divided by Close[0] and got the same unexpected results again...

This is actually getting quite fustrating, if such a simple operation gets this difficult, I can't imagine how much harder would be to create a whole new indicator full of conditionals an maths...

What would you do if you wanted to create a new indicator to plot ATR/Close? Would you use the same approach as I did or something different?

Miggers
01-29-2010, 09:31 AM
Ok, I solved it!

I created a new dataseries variable to calculate ATR, instead of using a double variable.

Thank you for your support.

NinjaTrader_Bertrand
01-29-2010, 09:50 AM
Miggers, glad to hear it's resolved for you.