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 cmoran13, 04-16-2026, 01:02 PM
            0 responses
            43 views
            0 likes
            Last Post cmoran13  
            Started by PaulMohn, 04-10-2026, 11:11 AM
            0 responses
            26 views
            0 likes
            Last Post PaulMohn  
            Started by CarlTrading, 03-31-2026, 09:41 PM
            1 response
            163 views
            1 like
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            98 views
            1 like
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            158 views
            2 likes
            Last Post CaptainJack  
            Working...
            X