My strategy uses several indicators - as many as ten - each applied to multiple time frames of the same instrument. The indicators can be fairly complex; for example, some have offsets or displacements applied, some are custom indicators with three or four values in their parameter list. I make multiple references to these indicators throughout my strategy, so I wind up writing code that looks like this:
if (MyStochastic(15,20,3,3).KValue[0] < MyStochastic(15,20,3,3).DValue[0] &&
CrossAbove(Close[1][0], MyTripleEMA(BarsArray[1], 20, 3))
EnterLong( ...yada yada.... )
Writing out the indicators by hand, when they're referenced multiple times, is both annoying, ugly, and prone to fat-finger error.
I had thought that I could just declare instances of the indicators in the Variables region and instantiate them in the Initialize() method. This also seemed appealing, because it would let me give more pleasant logical names to multiple indicators of the same type:
private MyStochastic myStochLong;
private MyStochastic myStochShort;
myStochLong = MyStochastic(15,20,3,3); // In Initialize()
myStochLong.Displacement = 2;
myStochShort = MyStochastic(10,5,2,2);
myStochShort.Displacement = 1;
if (CrossAbove(myStochLong.KLine, myStochShort.DLine, 1) etc etc ....
And this approach seems to work ... as long as I'm just using one single time frame. Everything falls apart once I try to incorporate the use of BarsArray[] into this approach. I've tried to create arrays of indicators - my idea being an array of, say, TripleEMA's, one for each time frame - but NinjaScript seems to really, really dislike that (or maybe I'm just doing it wrong?)
I've scanned several dozen of the sample strategy code files, looking for useful reference code, but I haven't stumbled across a solution yet. I have a sneaking suspicion that I need to create arrays of DataSeries objects, but I'd like to get some confirmation on that. Or am I barking up the wrong tree?
Comment