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

Output window and Data box showing different values

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

    Output window and Data box showing different values

    Hello,
    I'm attempting to debug my code and I'm having an issue with the Output window and Data box showing different values.
    I'm using Stochastics(Close,7,14,3).K[0]. It reads consistent on both the Output window and Data box until the Stoch hits 0. Then the Data box shows 0 and the Output window shows 2.3684757867E-14. I was curious before what the E-14 meant. For some reason the Code is not 0 on the stoch so the condition is coming back false. Any ideas why this is happening?
    Thanks
    PS
    I tried using Math.Round on the Stoch value and that portion of my code blew up.
    Last edited by CaptainAmericaXX; 06-25-2017, 05:38 PM.

    #2
    Hello CaptainAmericaXX,

    2.3684757867E-14 = .0000000000000023684757867
    The E-14 means that there are 14 zeros before the number after the decimal.

    Using .ToString("0.0000000000") would show the full value without the scientific exponential notation.
    As a tip, this is not the same as an exponent and is also not the same as a double epsilon.

    It means you got a really really small number, but its still bigger than 0.

    The DataBox is actually rounding this to the places of the decimal seen in the price margin.

    Is the indicator added to the chart by the strategy using the Add() method in Initialize() or was this added separately?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      ChelseaB,
      Thanks for the clarification. I added the indicator separately by stoch_K = Stochastics(Close,7,14,3).K[0];
      I am unfamiliar with how to add in Initialize(). Will this help? If so would you mind showing me how to do that?
      Thanks

      Comment


        #4
        Hello CaptainAmericaXX,

        Below is a link to the help guide on Add().


        This could be done as:

        private Stochastics stoch;

        protected override void Initialize()
        {
        stoch = Stochastics(7,14,3);
        Add(stoch);
        }

        protected override void OnBarUpdate()
        {
        Print(stoch.K[0]);
        }
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks for the response. I did as you suggested and I'm getting an error that states : cannot convert from 'NinjaTrader.Indicator.Stochastics'. to 'NinjaTrader.Gui.Chart Line'. I looked up the error code. It wasn't specific to this case so I'm unsure of how to fix this. Can you help me out again?
          Thanks.

          Comment


            #6
            Hello CaptainAmericaXX,

            What is the line of code generating the error?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              The Add(stoch); line.

              Comment


                #8
                Hello CaptainAmericaXX,

                I am not able to reproduce this behavior.

                Attached is a test script.

                Are you able to reproduce using this test script?
                Attached Files
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Ok. I imported your file and it compiled just fine. Mine still won't. I don't know why this should make a difference, but you sent me a strategy. I'm creating an indicator. What is the difference between .Strategy and .Indicator?

                  Comment


                    #10
                    Hello CaptainAmericaXX,

                    What are you trying to do?

                    A NinjaScript Strategy is a fully automated script that can place trades based on a set of criteria such as the values of indicators or other calculated values.
                    A strategy can add the indicators it is using to a chart with the add method so that as the strategy runs the indicators are automatically added.

                    A NinjaScript Indicator is a script for visual purposes to calculate a value and display this as a plot on a chart or return that plot value to a strategy.
                    An indicator cannot add other indicators. That is not purpose of these. Instead, this would be what a chart template is used for.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I've created an "indicator" in NinjaScript.Indicator that references several different indicator double values simply by
                      "double somevalue = indicator;" So in this case I did stoch_K = Stochastics(Close,7,14,3).K[0];
                      It's simply visual. I'm finding though, as I mentioned, that the values the code seems to be using, as shown by the Output window are not the same values as shown on the Chart, as shown by the data box. Perhaps this is caused because I'm not calling the actual indicator, only a double value of the indicator. Is this what is happening? Is there a way to fix this without rewriting my code into a NinjaScript.Strategy?

                      Comment


                        #12
                        Hello CaptainAmericaXX,

                        With an indicator, you would have to create a plot and set the plot to the value of that indicator.

                        Add(new Plot(Color.Blue, "StochK"));

                        Value[0] = stoch.K[0];

                        This would be the best way to double check.

                        Likely something is different on the data or parameters from the manually added instance of the stochastics and the instance called from your indicator.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Chelsea_B,
                          I did as you suggested and I'm getting an error "Failed to call method 'Initialize" for indicator "GoWithTrend": Object reference not set to an instance of an object." I apologize for my lack of experience here. Can you give me an idea of why this happened?
                          Code:
                          #region Variables
                                   private Stochastics stochK;
                          protected override void Initialize()
                          {
                          Add(new Plot(Color.FromKnownColor(KnownColor.Gray), PlotStyle.Line, "channel_middle_line"));
                          Add(new Plot(Color.FromKnownColor(KnownColor.Gray), PlotStyle.Line, "channel_upper_line"));
                          Add(new Plot(Color.FromKnownColor(KnownColor.Gray), PlotStyle.Line, "channel_lower_line"));

                          Add(new Plot(Color.Blue, "StochK"));
                          stochK = Stochastics(7,3,14);
                          Value[3] = stochK.K[0];
                          CalculateOnBarClose = true;


                          PriceTypeSupported = false;
                          diff = new DataSeries(this);
                          Overlay = true;
                          }

                          Code:
                          [Browsable(false)]    
                                  [XmlIgnore()]        
                                  public DataSeries StochK
                                  {
                                      get { return Values[3]; }
                                  }

                          Comment


                            #14
                            Hello,

                            Thank you for the reply.

                            I believe this is caused by the following syntax being in Initialize:

                            Code:
                            Value[3] = stochK.K[0];
                            Value "singular" would refer specifically to the first plot, Values "plural" refers to the collection of Plots. Value[3] would be the first plot 3 bars ago. Values[3] would be the 3rd plot. To assign a value directly to values it would look like:

                            Code:
                            Values[3][0] =
                            Could you add this to OnBarUpdate instead?

                            Code:
                            protected override OnBarUpdate()
                            {;
                                Value[B]s[/B][3][0] = stochK.K[0];
                                // or
                                StochK[0] = stochK.K[0];
                            ....

                            After making this change please ensure to remove the indicator from the chart and re-add it and not just an F5 refresh as changes to Initialize have been made.

                            Plots should only be valid once the script starts processing bars or on bar 0 of OnBarUpdate. There is no concept of BarsAgo in Initialize as this comes before bars are processed. This also gets called when you open the Indicator menu as well.


                            I look forward to being of further assistance.
                            Last edited by NinjaTrader_Jesse; 06-30-2017, 07:15 AM.
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Thank you Chelsea_B and Jesse for your guidance and instruction on this. Jesse thank you for the explanation on Initialize() and BarsAgo. This makes a great deal more sense now.
                              I did as was suggested and the indicator compiles now, but the price data is all "scrunched" up when the StochK line is colored and the Data box has a value of 0. Actually, as I think of it, the price data is "scrunched" because the StochK has a value of 0. The plot is receiving no value.
                              Here is my code:

                              private Stochastics stochK;

                              protected override void Initialize()
                              {
                              Add(new Plot(Color.Blue, "StochK"));
                              stochK = Stochastics(7,3,14);

                              CalculateOnBarClose = true;
                              }

                              protected override void OnBarUpdate()
                              {

                              Values[3][0] = stochK.K[0];
                              }
                              [Browsable(false)]
                              [XmlIgnore()]
                              public DataSeries StochK
                              {
                              get { return Values[3]; }
                              }

                              What am I missing? Thanks.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, 04-16-2024, 06:09 PM
                              3 responses
                              10 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by RookieTrader, Today, 07:41 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post RookieTrader  
                              Started by maybeimnotrader, Yesterday, 05:46 PM
                              1 response
                              18 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by Perr0Grande, Yesterday, 08:16 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by f.saeidi, Yesterday, 08:12 AM
                              3 responses
                              27 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X