I'm new to coding and ninjascript. After studying the built-in SMA and EMA indicator in NT8, I'm just wondering why SMA and EMA codes do not have the same defensive programing logic even though they are both moving average indicators using the same input from the same candle (data).
For example:
SMA Code
protected override void OnBarUpdate()
{
// Check if Bar type is RemoveLastBarSupported
if (BarsArray[0].BarsType.IsRemoveLastBarSupported)
{
// Another nested conditional statement.
if (CurrentBar == 0)
Value[0] = Input[0];
else
{
double last = Value[1] * Math.Min(CurrentBar, Period);
// Another nested conditional statement.
if (CurrentBar >= Period)
Value[0] = (last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period);
else
Value[0] = ((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
}
}
else
{
// Check if it's first tick of bar
if (IsFirstTickOfBar)
priorSum = sum;
sum = priorSum + Input[0] - (CurrentBar >= Period ? Input[Period] : 0);
Value[0] = sum / (CurrentBar < Period ? CurrentBar + 1 : Period);
}
And EMA Code
protected override void OnBarUpdate()
{
// No checking for RemoveLastBarSupported
// No additianal nested conditional statements.
// No checking for the first tick of bar
Value[0] = (CurrentBar == 0 ? Input[0] : Input[0] * constant1 + constant2 * Value[1]);
}
I just want to understand why they don't share the same defensive programing logic even though they are the same type of indicator (moving average) using the same input.
Thank you very much.
Comment