Math.abs driving me crazy

I have an oscillator, and I want to add the absolute oscillator values for the last ten bars.

So I have a variable intOsc and a series seriesOsc.

intOsc = Math.Abs(myOscillator[0])
seriesOsc[0] = intOsc (I’ve tried seriesOsc[0] = Math.Abs(intOsc) also)
sumOsc = SUM(seriesOsc,10)
When I add the last 10 bars with the above line, I get negative numbers!!

How can that be?

Is Math.abs() correct? Or is it Math.Abs()?

It is Math.Abs(), which is what is in my code. I wrote it incorrectly here.

Thanks.

Wait. So did you write Math.ABS() instead of using Math.Abs() and it’s not working?

Have you tried using print statements to see what is going on?

I put in:
int test = -200;
int abstest = Math.Abs(test);
Print("test: " + test);
Print("abstest: " + abstest);

and that works fine.

Output:
test: -200
abstest: 200

Here is an example:
image
Osc[0] is a positive number and prints correctly. Osc[1] is actually a negative number, so it is printing correctly as Math.Abs() has correctly made it a positive number.
Osc_Sum = SUM(seriesOsc, 2) should be adding Osc[1] and Osc[0] and coming up with a positive number, .0188327. It is not. First of all if the series is storing positive numbers, how is the sum a negative number, and not a correct number at that.

Thanks for your reply!

Figured it out.

Osc_Sum = SUM(seriesOsc,2)[0]

Ok. I was gonna say I think you weren’t using SUM correctly. If you have trouble with that, you can always google ninjatrader + whatever you are C# code you’re trying to use, in this case “Ninjatrader Sum” gave me their page of how to use it and what overloads it takes.

2 Likes