• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Making a variable public

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

    Making a variable public

    Hello support,

    I have an indicator of my making, it plots swingrays of the price, however the specifics of the indicator are not important.

    I want to be able to see (from outside the indi) where a swingray was broken. Hence, I declare two variables in the "Variables" section...

    Code:
    /*  bars on which we broke HighSwing/LowSwing the last time	*/
    private int 	_LastHighSwingBreakBar 	= -1;
    private int 	_LastLowSwingBreakBar 	= -1;
    Into OnBarUpdate() I put

    Code:
    if ( swingbroken... )
    {
    /*	Set values shared by SwingRays2 with the rest of the world	*/
    _LastHighSwingBreakBar 		= CurrentBar;
    Print("_LastHighSwingBreakBar=" + _LastHighSwingBreakBar);
    }
    Now, due to the Print I know the _LastHighSwingBreakBar is being set to proper values.
    So, in order to make the value of this variable public, I put ...

    Code:
           [Browsable(false)]	
            [XmlIgnore()]		
            public int LastHighSwingBreakBar
            {
                get { Update(); return _LastHighSwingBreakBar; }
            }
    into the "Properties" region. However, when I try to access this variable from some other indicator, as e.g.

    Code:
    private SwingRays2 SwingRays;
    ...
    SwingRays = SwingRays2();
    ...
    Print(SwingRays.LastHighSwingBreakBar);
    the last Print always prints "-1". Hence, I would like to ask what I am doing wrong.

    Many thanks,
    Daniel

    #2
    Eubie,

    I am happy to assist you.

    Here is a reference sample on accessing items in an indicator that are not plots.

    http://www.ninjatrader.com/support/f...ead.php?t=4991

    I would suggest using it to make these variables accessible.

    Please let me know if I may assist further.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Hello Adam,

      thanks for a quick reply. As a first thing, I tried to make all the shared variables *Series. That didnt work. So I tried renaming the internal private variables, so that the NinjaScript convention is used ( variable myVariable is accessed via MyVariable) - I know this has nothing to do with programming, but I couldnt find any other thing I could change on the code. Still the problem is there. What can I do?

      Daniel

      Comment


        #4
        Eubie,

        You may be missing the declaration in the "properties" section, however I noticed before you knew how to add it for that variable.

        That reference sample should work, so I would suggest following it. Without seeing more of your code I wouldn't be able to know exactly what is going on there. You can post it as an attachment here if you are comfortable doing so.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          I can't find any difference of my solution as opposed to the sample one, so Im more than happy to post the code.

          To make searching for the core stuff easy, here are the important lines...

          37 - 41 -> Variables region
          121 - 122 -> Setting variables pt1
          148 - 149 -> Setting variables pt2
          190 - 222 -> Creating Properties

          I tried posting this question on BMT but people there are just as confused as I am.

          Many thanks in advance,
          Daniel
          Attached Files

          Comment


            #6
            Adding the code for checking as well...
            Attached Files

            Comment


              #7
              Eubie,

              Thanks for the files, we will look into this and get back to you as soon as we can.
              Adam P.NinjaTrader Customer Service

              Comment


                #8
                Eubie, for the check indicator you posted calling the exposed values I see -

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

                Do you see this in your tests as well when the access fails?
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Ahhh, yes I see it. However, in previous bars the error does not appear, i.e. I get

                  Bar=11 LastLowSwingBreakBar=0LastLowSwingBreakPrice=0
                  Bar=12 LastLowSwingBreakBar=0LastLowSwingBreakPrice=0
                  Bar=13 LastLowSwingBreakBar=0LastLowSwingBreakPrice=0
                  Error on calling 'OnBarUpdate' method for indicator 'SwingRays2' on bar 14: Object reference not set to an instance of an object.
                  Bar=14 LastLowSwingBreakBar=0LastLowSwingBreakPrice=0
                  Bar=15 LastLowSwingBreakBar=0LastLowSwingBreakPrice=0
                  Bar=16 LastLowSwingBreakBar=0LastLowSwingBreakPrice=0
                  as the error is not produced by SwingRays2 per se and the Checker only accesses int variables (no constructors, no new operators), I dont see why this problem arises. What I mean to say is that by calling

                  Code:
                  Swings.LastLowSwingBreakBar
                  I am not accessing anything that needs to be dynamically allocated as opposed to e.g. if LastLowSwingBreakBar was DataSeries. I hope it is clear what I mean.

                  Thank you Bertrand.
                  Last edited by Eubie; 05-03-2012, 03:39 AM.

                  Comment


                    #10
                    Correct, the problem is likely not the caller but the called indicator here - you access an empty object in your code - I would suggest going through all sections dealing with your IDrawObjects and Stack calls to ensure the proper checks are in place as needed.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Thanks Bertrand but how can that be the case if the called indicator per se does not give this error message?

                      Daniel

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by woodyfox, Yesterday, 08:33 PM
                      0 responses
                      2 views
                      0 likes
                      Last Post woodyfox  
                      Started by rblunsom, Yesterday, 11:19 AM
                      2 responses
                      26 views
                      0 likes
                      Last Post guidoisot  
                      Started by drewski1980, Yesterday, 02:38 PM
                      1 response
                      15 views
                      0 likes
                      Last Post NinjaTrader_ChrisL  
                      Started by jfep1977, Yesterday, 02:31 PM
                      2 responses
                      13 views
                      0 likes
                      Last Post jfep1977  
                      Started by dmking, Yesterday, 12:39 PM
                      6 responses
                      24 views
                      0 likes
                      Last Post dmking
                      by dmking
                       
                      Working...
                      X