Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Why do SMA and EMA not use same defensive programing logic?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Why do SMA and EMA not use same defensive programing logic?

    Hi
    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.

    #2
    Hi thebigarch,

    I will need to ask our development about this.

    I appreciate your patience while we wait for a response.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea
      Did you get any response from the development team?
      Thanks in advance.

      Comment


        #4
        Look at NT7 code and compare.

        There was a NT video over at futures io a long time ago explaining something about this, that they were changing the code for indicators. I wish I remember the exact reason. I believe it was NT_Ray offering the explanation.

        Comment


          #5
          Thanks, Sledge.

          Comment


            #6
            Hello thebigarch,

            I have received information from our lead.

            "The EMA does not utilize the concept of IsFirstTickOfBar which was implemented in several indicators with "faster" logic. The EMA's code is apparently as optimized as it can be. Not much more you could do with it's basic code of multiply input by constant1 then add the prior value multiplied by constant2.

            However, this "faster" logic resulted in some incorrect caching on the back end with a bar type that redraws such as Renko. The SMA and WMA for example implement logic of running sum with IsFirstTickOfBar. This concept is to help optimize the code. However, bar types that re-draw their bars at times (Renko for example) were not optimizing properly due to some caching issues, so the original (NT7) code was implemented for those cases."
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              As a person with software development background, I would add that the two pieces of code you have quoted have most probably been written by two different people. You can tell from the coding style difference between the two indicators .... as you noticed.
              In addition, I would say the EMA code is more hackish Of course I have no objection to the latter if it does the job

              Originally posted by thebigarch View Post
              Hi
              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

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by GussJ, 03-04-2020, 03:11 PM
              14 responses
              3,244 views
              0 likes
              Last Post GussJ
              by GussJ
               
              Started by lorem, Today, 09:18 AM
              0 responses
              3 views
              0 likes
              Last Post lorem
              by lorem
               
              Started by hazylizard, Today, 08:38 AM
              4 responses
              11 views
              0 likes
              Last Post hazylizard  
              Started by geddyisodin, Today, 05:20 AM
              2 responses
              20 views
              0 likes
              Last Post geddyisodin  
              Started by Max238, Today, 01:28 AM
              5 responses
              48 views
              0 likes
              Last Post Max238
              by Max238
               
              Working...
              X