Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing public data from an indicator

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

    Accessing public data from an indicator

    This may be a basic C# question; not sure in the context of how NinjaTrader handles things.

    Simple example: Suppose I write my own version of, say, the StdDev indicator. I call it MyStdDev. I store intermediate calculations in public variables; e.g. the arithmetic mean is a public variable called 'mean'.

    I can call MyStdDev(myseries, length) from another indicator or strategy, and it returns a result. Fine. But that's not all I want to do. I want to access the public variables in it too.

    My question is, how do I access the other public values in MyStdDev?

    I was thinking I have to do the following in my calling code:

    [variables]
    private MyStdDev MyObj; // declare an object of my indicator class

    [Initialize]
    MyObj = new MyStdDev(); // create a unique instance of the indicator

    [OnBarUpdate]
    double sd = MyObj(Close, Period)[0]; // same parameters as MyStdDev
    double mn = MyObj.mean; // get the public value in the indicator

    The compiler complains where I try to call MyObj with parameters. It says 'NonjaTrader.Indicator._MyStdDev.MyObj' is a 'field' but is used like a 'method'. The error code CS0118 says I'm using parentheses where I should be using brackets.

    OK, so maybe I need to pass the parameters during initialization instead. But when I do that, I get an error CS1501, No overload for method 'MyStdDev' takes '2' arguments. But MyStdDev does have such a method.

    I don't know what to do from this point. Suggestions? Am I making this problem more complicated than it really is, or not complicated enough?

    -Alex
    Last edited by anachronist; 07-27-2008, 04:41 PM.

    #2
    Ah. I figured it out! Instead of this:
    Originally posted by anachronist View Post
    [variables]
    private MyStdDev MyObj; // declare an object of my indicator class

    [Initialize]
    MyObj = new MyStdDev(); // create a unique instance of the indicator

    [OnBarUpdate]
    double sd = MyObj(Close, Period)[0]; // same parameters as MyStdDev
    double mn = MyObj.mean; // get the public value in the indicator
    It should really be like this:

    [variables]
    private class MyStdDev MyObj; // declare an object of my indicator class

    [Initialize]
    // don't do anything with MyObj here

    [OnBarUpdate]
    MyObj = MyStdDev(Close, Period);
    double sd = MyObj[0]; // return value of method
    double mn = MyObj.mean; // get the public value in the indicator

    That works, and it's simpler than what I was trying.
    -Alex
    Last edited by anachronist; 07-27-2008, 04:42 PM.

    Comment


      #3
      Please check out this reference sample: http://www.ninjatrader-support.com/v...ead.php?t=4991
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by Josh View Post
        Please check out this reference sample: http://www.ninjatrader-support.com/v...ead.php?t=4991
        Thanks, it more or less confirms the solution I came up with.

        I don't understand, however, why that sample recommends concealing class variables behind methods that return their values. It seems rather inefficient to add the overhead of a function call to get a value than to simply get the value directly from the class variable -- especially for code that nobody but me will ever see.

        I suppose it's a way to make class variables read-only, but then, if you have methods to set and get values, why bother? The class variable becomes effectively read/write through the methods.

        -Alex

        Comment


          #5
          Many ways to skin a cat I suppose. Just declaring them public variables will work too.
          Josh P.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          599 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          344 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          558 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          557 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X