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

The SMA code analysis and understanding.

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

    The SMA code analysis and understanding.

    Hello,

    I am trying to understand how the SMA indicator (supplied with NT6.5) works so I can write my own indicator (actually a port of DemandIndex from the AmiBroker AFL language).

    Can you answer me what is Value ? I can't find it in the pdf userguide to NT v6 (so presumably it is not a system reserved word). Neither is it declared in the SMA indicator source code so it is not a local variable.

    It is referenced in these two ways
    1: double last = Value[1] * Math.Min ....
    2: Value.Set(last + Input[0] ...... )

    In 1: it would appear to be an array type (IDataSeries) but in 2: it appears to be a single variable (double)

    Also, where does the SimpleMovingAverage code initially add all the prices?
    I would have thought that the code would run along the following lines:
    Code:
    Initialise()
    {
        double last=0;        // running sum of the prices
    
        // now to add up all the prices for an initial total.
        for (int count=0; count<Period; count++)
        {
            last=last + Input[count];
        }
    }
    
    OnBarUpdate()
    {
        // And now on each bar update, add price of new bar and subtract
        // price of last (Period) bar and divide by Period.
        if (CurrentBar >= Period)
        {
            last = (last + Input[0] - Input[Period]) / Period;
        }
        // Plot a line    
        Plot0.Set(last);
    }
    // end


    *** Cheers and thanx, MarkBN

    BTW: I did search the forum to see if someone had perhaps already
    ported this indicator, but sadly for me, it appears no.

    #2
    Hi MarkBN,

    Value is most commonly used when the indicator only has one plot. In the context it is in the SMA it can be accessed as a IDataSeries or you can access the individual double values contained in it.

    Value.Set() essentially sets what the current bar's value for the Value DataSeries will be.

    When you go Value[0] you are accessing the current bar's Value's value. When you do Value[1] you are accessing the double for the previous bar's Value's value.

    You would generally do all logic in the OnBarUpdate() method. It doesn't need a loop to do the initial addition because the indicator is called at each bar. It just aggregates as each "new" bar is processed. When the next bar is greater than the Period for the SMA it simply drops out the oldest term that should no longer be in the SMA calculation and takes on the newest term. Hope that helps.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hello support,

      I am well on my way to getting my new indicator to work except for this one little thing.

      I would like to keep a tally of a value between calls to OnBarUpdate().
      The variable needs to be set to 0 (zero) when the indicator is executed the first time (initialised) and subsequently on executions of OnBarUpdate() the variable gets calculated on and needs to preserve its value after exit from OnBarUpdate().

      Originally I thought of putting the following line into Initialise();
      double DI=0;
      but that didn't work.
      I tried to put this line into OnBarUpdate()
      static double DI=0;
      But no joy.
      The static modifier (in plain vanilla C) would preserve the variable between procedure calls. It appears different in C#. The Microsoft online docs are complicated and this whole object-oriented - method stuff is confusing to me, sorry, just difficult to wrap my head around it. But if I can get this one thing sorted, my indicator should work.

      Another thing I tried:
      In the MFI indicator supplied with NT6.5 it has in Initialise();
      positive = new DataSeries(this);
      but that doesn't seem to work for my indicator either.

      Can you suggest the correct syntax? And in which procedure should it be in, Initialise() or OnBarUpdate() ?

      Regards, MarkBN

      Comment


        #4
        Try this instead (everything in OnBarUpdate() method):

        at the top...

        if (CurrentBar == 0)
        counter = 0;
        else
        {
        // all your logic blah blah
        counter++;
        }
        Josh P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        2 views
        0 likes
        Last Post Javierw.ok  
        Started by timmbbo, Today, 08:59 AM
        2 responses
        10 views
        0 likes
        Last Post bltdavid  
        Started by alifarahani, Today, 09:40 AM
        6 responses
        40 views
        0 likes
        Last Post alifarahani  
        Started by Waxavi, Today, 02:10 AM
        1 response
        18 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by Kaledus, Today, 01:29 PM
        5 responses
        15 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X