Announcement

Collapse
No announcement yet.

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?

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            598 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            343 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            557 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            555 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X