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

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 judysamnt7, 03-13-2023, 09:11 AM
          4 responses
          53 views
          0 likes
          Last Post DynamicTest  
          Started by ScottWalsh, Today, 06:52 PM
          4 responses
          33 views
          0 likes
          Last Post ScottWalsh  
          Started by olisav57, Today, 07:39 PM
          0 responses
          6 views
          0 likes
          Last Post olisav57  
          Started by trilliantrader, Today, 03:01 PM
          2 responses
          19 views
          0 likes
          Last Post helpwanted  
          Started by cre8able, Today, 07:24 PM
          0 responses
          6 views
          0 likes
          Last Post cre8able  
          Working...
          X