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

Using StdDev in a formula

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

    Using StdDev in a formula

    Hi K or anyone else that can help;

    If your on line can you take a look at this for me please. I am trying to use the following formula:

    (Math.Log(Close[0]/Close[Period]) / (StdDev((Math.Log(Close[0] / Close[1]),Period)*Math.Sqrt(Period))));

    Should I be referencing the master instrument if I'm using StdDev or do I need to create a 'double' say named denominator or private data series of the 1st half of the denominator?

    The 'Log' is the natural log, period and square root are self explanatory. I am trying to obtain the StdDev of the log value of the close of the current bar divided by the close of the last bar over the period (n bars). |Or the numerator is the log of the current bars close divided by the close 'n' bars ago (obvious) and the denominator should be the standard deviation of 'n' bars logarithmic changes * by the square root of 'n'.

    Any ideas / suggestions gratefully received.

    Thanks;

    Ben.

    #2
    stocktrader,

    I am happy to assist you.

    You are inputting a double value into the standard deviation method. Standard deviation needs a collection of values in order to be calculated. As such, you will need to create dataseries to store these values and then pass them to the standard deviation.

    Something like this would work :

    Code:
    //User defined variables
    private DataSeries denominator;
    
    private override void Initialize()
    {
       //other code here
       
       Add(new Plot(Color.FromKnownColor(KnownColor.InfoText), PlotStyle.Line, "Plot0"));
    
       denominator = new DataSeries(this);
    
    }
    
    
    private override void OnBarUpdate()
    {
    
    // other code
    
    denominator.Set( Math.Log(Close[0] / Close[1]) );
    
    Plot0.Set( Math.Log(Close[0]/Close[Period]) / (StdDev(denominator,Period)[0]*Math.Sqrt(Period))));
    
    }
    Please let me know if I may assist further.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Thought I had to reference the master instrument somewhere when using StdDev, Thanks. All ways seems to be the simple solution that is the answer.

      Ben.

      Comment


        #4
        Originally posted by stocktraderbmp View Post
        Hi K or anyone else that can help;

        If your on line can you take a look at this for me please. I am trying to use the following formula:

        (Math.Log(Close[0]/Close[Period]) / (StdDev((Math.Log(Close[0] / Close[1]),Period)*Math.Sqrt(Period))));

        Should I be referencing the master instrument if I'm using StdDev or do I need to create a 'double' say named denominator or private data series of the 1st half of the denominator?

        The 'Log' is the natural log, period and square root are self explanatory. I am trying to obtain the StdDev of the log value of the close of the current bar divided by the close of the last bar over the period (n bars). |Or the numerator is the log of the current bars close divided by the close 'n' bars ago (obvious) and the denominator should be the standard deviation of 'n' bars logarithmic changes * by the square root of 'n'.

        Any ideas / suggestions gratefully received.

        Thanks;

        Ben.
        StdDev() will need a DataSeries as input, so yes, you will have to create a DataSeries using the first term of your denominator.

        Comment


          #5
          Hi Guys;

          Thought I'd got this but just can't seem to get a plot, have been at it for some hours and head is in bits now.
          Here's what I have so far:

          #region Variables

          private int period = 20; // Default setting for Period
          private DataSeries denominator;

          #endregion

          protected override void Initialize()
          {
          Add(new Plot(Color.Indigo, "CTI"));
          Add(new Plot(Color.Tan, "TrendT_hold"));

          denominator = new DataSeries(this);
          Overlay = false;

          }


          protected override void OnBarUpdate()
          {
          if (CurrentBar == 0)
          {
          Value.Set(0);
          denominator.Set(0);
          TrendT_hold.Set(1);
          }
          else
          {
          denominator.Set(Math.Log(Close[0] / Close[1]));

          double result = (StdDev(denominator, Period))[0];
          Value.Set((Math.Log(Close[0]/Close[Period])) / (result * Math.Sqrt(Period)));
          TrendT_hold.Set(1);
          }
          }

          What have I missed?

          Ben.

          Comment


            #6
            Originally posted by stocktraderbmp View Post
            #region Variables

            private int period = 20; // Default setting for Period
            private DataSeries denominator;

            #endregion

            protected override void Initialize()
            {
            Add(new Plot(Color.Indigo, "CTI"));
            Add(new Plot(Color.Tan, "TrendT_hold"));

            denominator = new DataSeries(this);
            Overlay = false;

            }


            protected override void OnBarUpdate()
            {
            if (CurrentBar == 0)
            {
            Value.Set(0);
            denominator.Set(0);
            TrendT_hold.Set(1);
            return;
            }
            else
            {
            denominator.Set(Math.Log(Close[0] / Close[1]));

            double result = (StdDev(denominator, Period))[0];
            Value.Set((Math.Log(Close[0]/Close[Period])) / (result * Math.Sqrt(Period)));
            TrendT_hold.Set(1);
            }
            }

            What have I missed?

            Ben.
            You missed the part that I added in red. Your coding level tells me that I do not need to explain why, but if you want an explanation, just holler.

            Comment


              #7
              Sorry K; Thats not done it; granted I had missed the obvious but still no plot !!

              Comment


                #8
                Originally posted by stocktraderbmp View Post
                Sorry K; Thats not done it; granted I had missed the obvious but still no plot !!
                Any errors in your log?

                Comment


                  #9
                  Error on calling 'OnBarUpdate' method for indicator on bar 1 : Object reference not set to an instance of an object.

                  Didn't know I'd find this in the log - What does it mean?

                  Comment


                    #10
                    Should I have declared the double 'result' as a variable?

                    Comment


                      #11
                      Nope thats still not done it !! Bed calls me thinks, one for tomorrow.

                      Comment


                        #12
                        Originally posted by stocktraderbmp View Post
                        Error on calling 'OnBarUpdate' method for indicator on bar 1 : Object reference not set to an instance of an object.

                        Didn't know I'd find this in the log - What does it mean?
                        That does seem a bit strange; looking at the code, I would almost expect that to be an error thrown up by bar0, rather than bar1.

                        I am rebuilding my NT development environment. Once done, I will try to load your code and see what I can find. Nothing jumps out at the moment.

                        In the meantime, let us see how you have defined your "Properties", if you do not mind posting same.

                        Comment


                          #13
                          #region Properties
                          [Browsable(false)]
                          [XmlIgnore()]
                          public DataSeries CTI
                          {
                          get { return Values[0]; }
                          }

                          [Browsable(false)]
                          [XmlIgnore()]
                          public DataSeries TrendT_hold
                          {
                          get { return Values[1]; }
                          }

                          [Description("Look-back length (in bars) for calculating the indicator plot. (Default 20).")]
                          [GridCategory("Parameters")]
                          public int Period
                          {
                          get { return period; }
                          set { period = Math.Max(1, value); }
                          }
                          #endregion

                          Comment


                            #14
                            Just a thought but I am assuming I'm using the right log function, there is also a log10 listed. These wouldn't be used to write an entry to the log would they? I assumed as they were preceded by 'Math.' that they were mathematical and not text -ual !!

                            Comment


                              #15
                              Originally posted by stocktraderbmp View Post
                              Error on calling 'OnBarUpdate' method for indicator on bar 1 : Object reference not set to an instance of an object.

                              Didn't know I'd find this in the log - What does it mean?
                              I think you may have passed along the wrong message. What I get is an indexing error, which points me straight at the culprit. You are trying to access Close[Period] when there are not enough bars.

                              Either escape enough bars, or use the bars available.

                              Escape bars with if (CurrentBar < Period) rather than if (CurrentBar == 0) at the top of your if block.

                              The alternative, to use what are available until you have enough bars would be to code:

                              Close[Math.Min(CurrentBar, Period)] instead of Close[Period].

                              Sorry. I really should have seen that one without being lost by getting the wrong error message.

                              Next maybe you would care to explain how the indicator will be used to provide an edge?
                              Last edited by koganam; 01-10-2012, 11:58 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rocketman7, Today, 01:00 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post rocketman7  
                              Started by wzgy0920, 04-20-2024, 06:09 PM
                              2 responses
                              27 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, 02-22-2024, 01:11 AM
                              5 responses
                              32 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, 04-23-2024, 09:53 PM
                              2 responses
                              74 views
                              0 likes
                              Last Post wzgy0920  
                              Started by Kensonprib, 04-28-2021, 10:11 AM
                              5 responses
                              193 views
                              0 likes
                              Last Post Hasadafa  
                              Working...
                              X