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

Can someone from NT explain this?

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

    Can someone from NT explain this?

    Very simple code, very crazy results.
    Code:
     
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion
    namespace NinjaTrader.Indicator
    {
        public class SMATest : Indicator
        {
            #region Variables
                private int period = 2; 
                private DataSeries test;
            #endregion
            protected override void Initialize()
            {
                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "SMAT"));
                test = new DataSeries(this);
                CalculateOnBarClose = true;
                Overlay    = true;
                PriceTypeSupported = false;
            }
            protected override void OnBarUpdate()
            {
              test.Set(High[0]);
              if(CurrentBar==0) return;
              test.Set(1, Close[1]);
              Print("test[1]: " + test[1].ToString() 
                  + " test[0]: " + test[0].ToString() 
                  + " SMA(test, 2)[0]: " + SMA(test, 2)[0].ToString());
            }
            #region Properties
            [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]  // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries SMAT
            {
                get { return Values[0]; }
            }
            [Description("")]
            [Category("Parameters")]
            public int Period
            {
                get { return period; }
                set { period = Math.Max(1, value); }
            }
            #endregion
        }
    }
    Output:

    test[1]: 916.5 test[0]: 916.75 SMA(test, 2)[0]: 3264.625
    test[1]: 916.5 test[0]: 916.75 SMA(test, 2)[0]: 3264.75
    test[1]: 916.5 test[0]: 916.5 SMA(test, 2)[0]: 3264.75
    test[1]: 916.5 test[0]: 916.5 SMA(test, 2)[0]: 3264.75

    there are 2 last values printed and SMA of those 2 values.
    I expect totally different printout from SMA.

    Any ideas?

    thanks
    Last edited by roonius; 05-06-2009, 06:15 PM.

    #2
    roonius,

    I suggest you print the actual received values from the SMA. You will find you are not running your manual calculations with the correct values.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      roonius,

      I suggest you print the actual received values from the SMA. You will find you are not running your manual calculations with the correct values.
      I am sorry Josh, which calculations you are refering to are "manual"?

      Comment


        #4
        Manual would be your test[1] + test[0] / 2. That is what I am assuming you are using.

        If you created a SMA indicator variant that actually prints out the components of the calculations it uses you will find you are not using the same values in your manual calculation.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Josh View Post
          Manual would be your test[1] + test[0] / 2. That is what I am assuming you are using.

          If you created a SMA indicator variant that actually prints out the components of the calculations it uses you will find you are not using the same values in your manual calculation.
          Thank you Josh,

          I have figured that out already, as I stated in the other thread, but it is quite misleading.

          If test[1] == 2 and test[0] == 4 then we should expect SMA(test,2)[0] == 3; however it is not (if you changed test[1] value during the bar 0) due to the way how SMA is calculated.

          Comment


            #6
            roonius,

            We will look into it.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              roonius,

              SMA will not use updated values to reprocess its values. If you do in fact wish for this behavior you will need to create your own custom variant of the SMA indicator.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Josh View Post
                roonius,

                SMA will not use updated values to reprocess its values. If you do in fact wish for this behavior you will need to create your own custom variant of the SMA indicator.
                Thanks. That's exactly what I did.
                This is not regarding SMA only. Any indicator which is calculated using it's previous values will not use updated values...

                Comment


                  #9
                  That is correct.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by roonius View Post
                    Thanks. That's exactly what I did.
                    This is not regarding SMA only. Any indicator which is calculated using it's previous values will not use updated values...
                    Hi roonius,

                    would you mind posting an example SMA EMA or what every you have changed at 'my' thread



                    thanks a lot

                    cbaer

                    Comment


                      #11
                      Originally posted by cbaer View Post
                      Hi roonius,

                      would you mind posting an example SMA EMA or what every you have changed at 'my' thread



                      thanks a lot

                      cbaer
                      You have to make your own SMA which does not use stored values.
                      For example:

                      private double MySMA(IDataSeries inp, int length)
                      {
                      double sum = 0;
                      for(int idx = 0; idx <= Math.Min(length - 1, CurrentBar); idx ++)
                      sum += inp[idx];
                      return sum / Math.Min(length, CurrentBar + 1);
                      }

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by martin70, 03-24-2023, 04:58 AM
                      15 responses
                      114 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by The_Sec, Today, 02:29 PM
                      1 response
                      6 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by jeronymite, 04-12-2024, 04:26 PM
                      2 responses
                      31 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Started by Mindset, 05-06-2023, 09:03 PM
                      10 responses
                      265 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Started by michi08, 10-05-2018, 09:31 AM
                      5 responses
                      743 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Working...
                      X