PDA

View Full Version : SMA code


maxpi
07-24-2007, 12:32 PM
I'm looking at the code in the SMA indicator with an eye towards copying it for use in another indicator. The onBarUpdate method follows:

if (CurrentBar == 0)
Value.Set(Input[0]);
else
{
double last = Value[1] * Math.Min(CurrentBar,
Period);
if (CurrentBar >= Period)
Value.Set((last + Input[0] - Input[Period])
/ Math.Min(CurrentBar, Period));
else
Value.Set((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
}
}

For one thing it appears that there is one unnecessary math.min method, correct me if I am wrong.

My main question is how does that accomplish an averaging? Is there a looping action implied there somehow?

NinjaTrader_Dierk
07-24-2007, 12:47 PM
We provide the implementations of the NT indicator "as is". Due to bandwidth reasons we are unable to elaborate on implementation details.

Thanks for your understanding.

Some quick hints:
- CurrentBar could be smaller than Period -> Min() is required
- there are several ways to skin a cat: you could calculate a moving average by removing the oldest value and adding the newest value.

maxpi
07-24-2007, 09:22 PM
thanks, I see how the code does it now. C# is starting to make sense.

NinjaTrader_Dierk
07-24-2007, 11:34 PM
Glad to hear that

luxurious_04
07-28-2010, 09:04 PM
double last = Value[1] * Math.Min(CurrentBar, Period);

Anybody wants to give me a hint or explain me about this?
Last is it the LastBar?
What does Value[1] means?
And what is Math.Min(CurrentBar, Period)?What is the use of it?

NinjaTrader_Bertrand
07-29-2010, 06:21 AM
last is just a variable defined as double value -

http://www.ninjatrader-support.com/HelpGuideV6/BasicSyntax.html

Value accesses the main indicator value series -

http://www.ninjatrader-support.com/HelpGuideV6/ValueAndValues.html

For the CurrentBar check, please see this tip -

http://www.ninjatrader.com/support/forum/showthread.php?t=3170

Have you looked at our indicator coding tutorials yet?

http://www.ninjatrader-support.com/HelpGuideV6/Overview18.html