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 sjsj2732, 03-23-2026, 04:31 AM
            0 responses
            42 views
            0 likes
            Last Post sjsj2732  
            Started by NullPointStrategies, 03-13-2026, 05:17 AM
            0 responses
            294 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            290 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            135 views
            1 like
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            98 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Working...
            X