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 bmartz, 03-12-2024, 06:12 AM
            5 responses
            32 views
            0 likes
            Last Post NinjaTrader_Zachary  
            Started by Aviram Y, Today, 05:29 AM
            4 responses
            12 views
            0 likes
            Last Post Aviram Y  
            Started by algospoke, 04-17-2024, 06:40 PM
            3 responses
            28 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by gentlebenthebear, Today, 01:30 AM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by cls71, Today, 04:45 AM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X