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

Values Printing but not plotting

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

    Values Printing but not plotting

    Hi I have a problem I hope someone can help me with.

    The attached indicator calculates the y-value of a ray for the current bar from y=mx+b.

    For some reason the indicator prints the correct values and draws the dots from the correct bar, but only starts to plot the values a few bars later.

    When I try to call up the y plot values from a strategy it says “on bar 11 bject reference not set to an instance of an object.”


    I have also tried
    if (CurrentBar < 100 )
    {
    return;
    }

    in the strategy even though the plot starts earlier but I get the same error.

    I would be very grateful if someone could give me a few pointers here.

    Thanks!
    Attached Files

    #2
    Originally posted by fevs7 View Post
    Hi I have a problem I hope someone can help me with.

    The attached indicator calculates the y-value of a ray for the current bar from y=mx+b.

    For some reason the indicator prints the correct values and draws the dots from the correct bar, but only starts to plot the values a few bars later.

    When I try to call up the y plot values from a strategy it says “on bar 11 bject reference not set to an instance of an object.”


    I have also tried
    if (CurrentBar < 100 )
    {
    return;
    }

    in the strategy even though the plot starts earlier but I get the same error.

    I would be very grateful if someone could give me a few pointers here.

    Thanks!
    Where the Plot starts is controlled by the BarsRequired property.

    ref: http://ninjatrader.com/support/helpG...rsrequired.htm

    Comment


      #3
      Originally posted by koganam View Post
      Where the Plot starts is controlled by the BarsRequired property.

      ref: http://ninjatrader.com/support/helpG...rsrequired.htm
      Thanks koganam.

      That solves the values only plotting a few bars later. I am, however, still getting the same error message when I try to print the values from the strategy "... on bar 11 :Object reference not set to an instance of an object ." I rechecked the indicator and the values are printing for the relevant bars but somehow the strategy does not see it

      Comment


        #4
        Originally posted by fevs7 View Post
        Thanks koganam.

        That solves the values only plotting a few bars later. I am, however, still getting the same error message when I try to print the values from the strategy "... on bar 11 :Object reference not set to an instance of an object ." I rechecked the indicator and the values are printing for the relevant bars but somehow the strategy does not see it
        Just to add to my above post, when I change the indicator to only start calculating from bar 100 the strategy picks up the correct values up to bar 110 and then I get the same error message “on bar 110: Object reference not set to an instance of an object”

        Comment


          #5
          Originally posted by fevs7 View Post
          Just to add to my above post, when I change the indicator to only start calculating from bar 100 the strategy picks up the correct values up to bar 110 and then I get the same error message “on bar 110: Object reference not set to an instance of an object”
          Which means that you are trying to access something that is still null.

          Nobody can tell you which line of code is the issue in a vacuum. You will either have to post the code so that we can see what the issue might be, or you have to isolate the errant line yourself.

          The easiest way to do the latter is to Print() out checkpoints/values in your code to isolate which statement does not return, then resolve the issue as necessary.

          Comment


            #6
            Originally posted by koganam View Post
            Which means that you are trying to access something that is still null.

            Nobody can tell you which line of code is the issue in a vacuum. You will either have to post the code so that we can see what the issue might be, or you have to isolate the errant line yourself.

            The easiest way to do the latter is to Print() out checkpoints/values in your code to isolate which statement does not return, then resolve the issue as necessary.
            Hi Koganam
            I am still fairly new to this so I thought if the values were printing (which is the case in the indicator ) the null problem was sorted out. Here is the code of the indicator posted previously. Thanks A LOT for your help with this

            #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

            // This namespace holds all indicators and is required. Do not change it.
            namespace NinjaTrader.Indicator
            {
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            [Description("Enter the description of your new custom indicator here")]
            public class FComboRayLow : Indicator
            {
            #region Variables

            private int period1 = 10; // Default setting for Period1
            private int period2 = 5; // Default setting for Period2
            private double slope=0;
            private double y = 1;





            #endregion

            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {

            Overlay = true;

            Add(new Plot(Color.Orange, "low"));
            BarsRequired = period1+1 ;


            }

            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {

            if (CurrentBar < BarsRequired ) //based on logic that is used in the BW Pivot Calculations period1+
            {
            return;
            }



            if (CurrentBar >= BarsRequired )

            {

            IRay ray = DrawRay("lows", period1, Low[period1], period2, Low[period2], Color.LimeGreen);

            slope=(Low[period2]-Low[period1])/(period1-period2);

            y=slope*(ray.Anchor2BarsAgo)+Low[ray.Anchor2BarsAgo];
            Value.Set(y);
            DrawDot("My dot" + CurrentBar, false, 0, slope*(ray.Anchor2BarsAgo)+Low[ray.Anchor2BarsAgo], Color.Blue);

            Print(Time[0]);

            Print(y);

            Print(Value[0]);




            }

            }

            #region Properties

            [Description("Anchor1")]
            [GridCategory("Parameters")]
            public int Period1
            {
            get { return period1; }
            set { period1 = Math.Max(1, value); }
            }

            [Description("Anchor2")]
            [GridCategory("Parameters")]
            public int Period2
            {
            get { return period2; }
            set { period2 = Math.Max(0, value); }
            }



            [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 Plot0
            {
            get { return Values[0]; }
            }






            #endregion
            }
            }

            Comment


              #7
              Originally posted by fevs7 View Post
              Hi Koganam
              I am still fairly new to this so I thought if the values were printing (which is the case in the indicator ) the null problem was sorted out. Here is the code of the indicator posted previously. Thanks A LOT for your help with this

              #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

              // This namespace holds all indicators and is required. Do not change it.
              namespace NinjaTrader.Indicator
              {
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              [Description("Enter the description of your new custom indicator here")]
              public class FComboRayLow : Indicator
              {
              #region Variables

              private int period1 = 10; // Default setting for Period1
              private int period2 = 5; // Default setting for Period2
              private double slope=0;
              private double y = 1;





              #endregion

              /// <summary>
              /// This method is used to configure the indicator and is called once before any bar data is loaded.
              /// </summary>
              protected override void Initialize()
              {

              Overlay = true;

              Add(new Plot(Color.Orange, "low"));
              BarsRequired = period1+1 ;


              }

              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {

              if (CurrentBar < BarsRequired ) //based on logic that is used in the BW Pivot Calculations period1+
              {
              return;
              }



              if (CurrentBar >= BarsRequired )

              {

              IRay ray = DrawRay("lows", period1, Low[period1], period2, Low[period2], Color.LimeGreen);

              slope=(Low[period2]-Low[period1])/(period1-period2);

              y=slope*(ray.Anchor2BarsAgo)+Low[ray.Anchor2BarsAgo];
              Value.Set(y);
              DrawDot("My dot" + CurrentBar, false, 0, slope*(ray.Anchor2BarsAgo)+Low[ray.Anchor2BarsAgo], Color.Blue);

              Print(Time[0]);

              Print(y);

              Print(Value[0]);




              }

              }

              #region Properties

              [Description("Anchor1")]
              [GridCategory("Parameters")]
              public int Period1
              {
              get { return period1; }
              set { period1 = Math.Max(1, value); }
              }

              [Description("Anchor2")]
              [GridCategory("Parameters")]
              public int Period2
              {
              get { return period2; }
              set { period2 = Math.Max(0, value); }
              }



              [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 Plot0
              {
              get { return Values[0]; }
              }






              #endregion
              }
              }
              Here is the code from the strategy

              // This namespace holds all strategies and is required. Do not change it.
              namespace NinjaTrader.Strategy
              {
              /// <summary>
              /// Enter the description of your strategy here
              /// </summary>
              [Description("Enter the description of your strategy here")]
              public class FComboTrendlineDiv : Strategy
              {
              #region Variables
              // Wizard generated variables
              private int myInput0 = 1; // Default setting for MyInput0
              // User defined variables (add any user defined variables below)
              #endregion

              /// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              protected override void Initialize()
              {
              CalculateOnBarClose = true;
              BarsRequired=11;
              }

              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {



              if (CurrentBar<100
              ) //based on logic that is used in the BW Pivot Calculations
              {
              return;
              }




              Print(Time[0]);


              Print(FComboRayLow(10, 5).Plot0[0] );



              }

              Comment


                #8
                Originally posted by fevs7 View Post
                Here is the code from the strategy

                // This namespace holds all strategies and is required. Do not change it.
                namespace NinjaTrader.Strategy
                {
                /// <summary>
                /// Enter the description of your strategy here
                /// </summary>
                [Description("Enter the description of your strategy here")]
                public class FComboTrendlineDiv : Strategy
                {
                #region Variables
                // Wizard generated variables
                private int myInput0 = 1; // Default setting for MyInput0
                // User defined variables (add any user defined variables below)
                #endregion

                /// <summary>
                /// This method is used to configure the strategy and is called once before any strategy method is called.
                /// </summary>
                protected override void Initialize()
                {
                CalculateOnBarClose = true;
                BarsRequired=11;
                }

                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {



                if (CurrentBar<100
                ) //based on logic that is used in the BW Pivot Calculations
                {
                return;
                }




                Print(Time[0]);


                Print(FComboRayLow(10, 5).Plot0[0] );



                }
                You do not have a "Plot0" in your indicator code, so "Plot0" does not exist. If you try to access a non-existent public property's value, you get an object access violation. In this case, you are being told that no value was assigned to Plot0, (which as you never defined it, cannot exist, and so, of course, was not assigned).

                You can either address the Plot by its correct name that you gave it in the indicator or you can use the base identifier, Plots[0], the latter of which, makes your syntax:
                Code:
                Print(FComboRayLow(10, 5).Plots[0][0] );
                Last edited by koganam; 06-13-2016, 03:42 AM.

                Comment


                  #9
                  Originally posted by koganam View Post
                  You do not have a "Plot0" in your indicator code, so "Plot0" does not exist. If you try to access a non-existent public property's value, you get an object access violation. In this case, you are being told that no value was assigned to Plot0, (which as you never defined it, cannot exist, and so, of course, was not assigned).

                  You can either address the Plot by its correct name that you gave it in the indicator or you can use the base identifier, Plots[0], the latter of which, makes your syntax:
                  Code:
                  Print(FComboRayLow(10, 5).Plots[0][0] );
                  Hi Koganam
                  If I print ---- Print(Plot0[0])in the indicator; it prints the correct values at the correct times so I suppose that was why I thought it was correctly defined.

                  I currently have Plot0 under the properties section as

                  [Browsable(false)]
                  [XmlIgnore()]
                  public DataSeries Plot0
                  {
                  get { return Values[0]; }
                  }

                  I got that from looking at other indicators but I suppose I am missing something obvious.
                  There are a few more elements to the indicator. Could you perhaps give me a hint as to how to get it correctly defined and then I will apply it to the rest?

                  Cheers!

                  Comment


                    #10
                    Hello fevs7,

                    Thank you for your post.

                    The only thing you need to do concerning your first post is setting BarsRequired to 0 in Initialize(). This should correct any errors you see.
                    Code:
                            protected override void Initialize()
                            {
                    
                                Overlay				= true;		
                    						
                    			Add(new Plot(Color.Orange, "low"));
                    			
                    			[B]BarsRequired = 0;[/B]
                    
                            }

                    Comment


                      #11
                      Originally posted by NinjaTrader_PatrickH View Post
                      Hello fevs7,

                      Thank you for your post.

                      The only thing you need to do concerning your first post is setting BarsRequired to 0 in Initialize(). This should correct any errors you see.
                      Code:
                              protected override void Initialize()
                              {
                      
                                  Overlay				= true;		
                      						
                      			Add(new Plot(Color.Orange, "low"));
                      			
                      			[B]BarsRequired = 0;[/B]
                      
                              }
                      Hi Patrick

                      Thanks for your post. The real problem I have is that I am not able to get access to the values of Plot0 (so the yValue) in a strategy. Initially I thought it was because it was not plotting but now I think it might be something else.

                      Plot0 prints correctly in the indicator (Print(Plot0[0])

                      but when I try to print it in a strategy (Print(FComboRayLow(10, 5).Plot0[0])

                      it says "on bar 11 :Object reference not set to an instance of an object ." and prints a value of 0.

                      If I set the indicator to only start calculating from bar 100, the strategy prints the correct value up to bar 110 and then I get the same error message.

                      If you could steer me in the right direction that would be awesome
                      Last edited by fevs7; 06-13-2016, 09:16 AM.

                      Comment


                        #12
                        apologies for all the emoticons- I still need to switch the auto inclusion off

                        Comment


                          #13
                          Hello fevs7,

                          Thank you for your response.

                          Can you attach both the calling strategy and the updated indicator to your response?

                          Comment


                            #14
                            Originally posted by NinjaTrader_PatrickH View Post
                            Hello fevs7,

                            Thank you for your response.

                            Can you attach both the calling strategy and the updated indicator to your response?
                            Attached... Thanks.
                            Attached Files

                            Comment


                              #15
                              Hello fevs7,

                              Thank you for your response.

                              The strategy does not add the indicator to the chart and the indicator never draws anything if not added to the chart. So Y is not valid until the ray is drawn.

                              You will need to add the indicator in Initialize() to correct this:
                              Code:
                                      protected override void Initialize()
                                      {
                              			Add(FComboRayLow(10, 5));
                                          CalculateOnBarClose =true;
                                      }
                              
                                      protected override void OnBarUpdate()
                                      {
                                          // Condition set 1
                                          if (CurrentBar>100)
                                          {
                              				
                              				Print("s"+ FComboRayLow(10, 5).Plot0[0]);
                              				
                                          }
                                      }

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by frankthearm, Today, 09:08 AM
                              5 responses
                              14 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              3 responses
                              43 views
                              0 likes
                              Last Post jeronymite  
                              Started by yertle, Today, 08:38 AM
                              5 responses
                              15 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by adeelshahzad, Today, 03:54 AM
                              3 responses
                              19 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by bill2023, Yesterday, 08:51 AM
                              6 responses
                              27 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Working...
                              X