PDA

View Full Version : Accessing public data from an indicator


anachronist
07-24-2008, 10:10 PM
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


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 [I]'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

anachronist
07-24-2008, 11:45 PM
Ah. I figured it out! Instead of this:

[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

NinjaTrader_Josh
07-25-2008, 12:02 AM
Please check out this reference sample: http://www.ninjatrader-support.com/vb/showthread.php?t=4991

anachronist
07-25-2008, 05:44 PM
Please check out this reference sample: http://www.ninjatrader-support.com/vb/showthread.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

NinjaTrader_Josh
07-25-2008, 10:50 PM
Many ways to skin a cat I suppose. Just declaring them public variables will work too.