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

SMA code

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

    SMA code

    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?

    #2
    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.

    Comment


      #3
      thanks, I see how the code does it now. C# is starting to make sense.

      Comment


        #4
        Glad to hear that

        Comment


          #5
          Give me a hint:

          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?

          Comment


            #6
            last is just a variable defined as double value -



            Value accesses the main indicator value series -



            For the CurrentBar check, please see this tip -



            Have you looked at our indicator coding tutorials yet?

            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Spiderbird, Today, 12:15 PM
            2 responses
            10 views
            0 likes
            Last Post Spiderbird  
            Started by WHICKED, Today, 12:45 PM
            0 responses
            3 views
            0 likes
            Last Post WHICKED
            by WHICKED
             
            Started by FrazMann, Today, 11:21 AM
            2 responses
            6 views
            0 likes
            Last Post NinjaTrader_ChristopherJ  
            Started by rjbtrade1, 11-30-2023, 04:38 PM
            2 responses
            80 views
            0 likes
            Last Post DavidHP
            by DavidHP
             
            Started by lorem, Yesterday, 09:18 AM
            5 responses
            18 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X