If you want to use an indicator in a strategy, it is best to add an instance of the indicator to the strategy via the Add() method.
If you want to use your strategy with 10 different instruments, you would first add the 10 instruments in the Initialize() section of the strategy. Bar Index 0 denotes the primary bars, and bar indices 1 to 10 would denote the 10 added bar series (index follows the order in which they have been added).
Within the Initialize() section you can then add indicators to your strategy, also via Add(). This is required if you wish that the indicators are plotted, otherwise you can call indicators directly in OnBarUpdate().
The following 3 users say Thank You to Fat Tails for this post:
Just so that I'm clear, what if I only need the indicator for calculations, but don't care about plotting it?
> If I want to apply this indicator to each of the 10 instruments, should I have to add a separate instance of the indicator for each of the instruments?
> Or, can I somehow add one instance of the indicator and then point that to various DataSeries later in the strategy?
Sorry if I'm not describing it quite clearly (Although, I would imagine that if I could just figure out a way to describe it, I probably wouldn't have any issue programming it...)
Thanks a million for helping with this, I'm very much lost in the dark right now
Thanks very much for your input, but I still haven't been able to find an efficient way of implementing this.
First I tried:
... But NinjaTrader throws an error saying that you can't access BarsArray inside of the Initialize function
So, I then tried:
... But NinjaTrader then throws an error saying that you can't use "Add()" outside of the initialize function
I am actually working with a fairly large list of instruments (300+), so obviously I don't want to type a line manually for each of the indicators that I want to add. Is there some other way than what I've tried to do in the code above to iterate through the list of instruments and add their corresponding indicator?
Also, from a memory usage standpoint: If I'm running an optimization run, and NinjaTrader creates a new instance of each indicator for each set of parameters, will Ninja ever release that memory, or no? Right now, the memory usage gradually explodes (using all 12GB of RAM), and the only way to fix it is to close NT and restart.
I understand that the best way to handle this on a single instrument strategy is to create a unique instance of the indicator, and just reference that instance, but how can you hard code that to iterate automatically? Is there a way in C# to use a dynamic string when declaring variables? Not sure how to explain, but I'm thinking something along the lines of:
Thanks very much, this seemed to work. I had thought that because the ATR calc needed all of the high/low/close/open, it would have to be passed the BarsArray item. Passing Closes returned the same value though, so apparently that's ok
One last thing - Will adding the indicators to the strategy serve the same purpose as declaring a unique named instance of the indicator?
Add(ATR(Closes,ATRPeriod)) --> same performance as: private ATR myATR1 = ATR(Closes,ATRPeriod)
The following user says Thank You to kbeary33 for this post:
If you look at the code of the ATR indicator, it does not use Input anywhere. If it did, the output values would depend on the type of data passed. Therefore ATR(Closes, ATRPeriod) is the same as ATR(Highs, ATRPeriod). I would prefer to write ATR(Inputs, ATRPeriod) as this covers the general case.
The main purpose of addingan indicator via the ADD() method is to have them directly plotted by the strategy. If this is not required, the code below is a simpler solution:
The following 2 users say Thank You to Fat Tails for this post:
Favorite Futures: Gameplay Klownbine® Trading of Globex
Posts: 1,276 since Jul 2009
Thanks: 1,227 given,
Array Containing Ninjascript Data Series objects
An Array can store any type, including DataSeries objects. As an example, let's create an array to hold more than one DataSeries. The rank (number of elements) of the array does not need to be hard coded. It can be user selected or programmatically controlled, and the setting of the object references can be done in a For loop.
Declare the array. At this point the rank (number of elements) is not yet defined and the array is empty.
In the Initialize() method:
Remember, an array is a ZERO BASED collection. So, for example, if an array contains 5 items, the subscripts of the items will be 0 through 4, not 1 through 5. Hence the start and end points of the For loop.
Each of these objects is accessed like any other DataSeries, but don't forget the first subscript that tells you which one is being accessed.
That's it, we're almost done for today. Have a nice day!
But before I return to my undisclosed location, here is an example of how to use this technique, based on the Freedom of Movement Indicator in the April 2014 issue of TASC. By eliminating dependencies on external classes, the performance of the indicator has been improved. (I have other, more ambitious uses in mind for this technique, since for this particular indicator it is not really worth the trouble.)
"If we don't loosen up some money, this sucker is going down." -GW Bush, 2008
“Lack of proof that something is true does not prove that it is not true - when you want to believe.” -Humpty Dumpty, 2014
“The greatest shortcoming of the human race is our inability to understand the exponential function.” Prof. Albert Bartlett
Last edited by Zondor; April 30th, 2014 at 02:32 PM.
The following 3 users say Thank You to Zondor for this post: