View Full Version : Instantiating indicators from other indicators
gomifromparis
11-10-2009, 06:09 AM
Hi,
2 problems have appeared with NT7
Problem 1:
Like indicated here: http://www.ninjatrader-support2.com/...ad.php?t=22023 (http://www.ninjatrader-support2.com/vb/showthread.php?t=22023)
you can't instantiate an indicator from another indicator in the Initialize() method. Proposed solution code is to move code to OnBarUpdate().
This is not very cool because
1. Well, instantiation of an indicator is not Bar code, it's an initialization, so it should logically be in Initialize() method
2. There are some things that can't be done in OnBarUpdate(), for instance add plots. So in that case you can't move the code to OnBarUpdate(). So it's really not equivalent.
Problem 2
The Instrument is null on the instantiated indicator. I believe this worked in Beta3 but I'm not 100% sure.
This bug is critical as you can't use properties like TickSize.
NinjaTrader_Dierk
11-10-2009, 07:28 AM
- please make sure you are on NT7B4
- issue 1: what exactly is the issue you experienced based on NT7B4
- issue 2: this is by design. Instrument/TickSize never had been supported in Initialize(). It may or may not have worked in NT6.5
kdoren
11-10-2009, 07:57 AM
I think that any DataSeries objects (except ticker symbols) needs to be Added from OnBarUpdate(); they don't work right for me if Added from Initialize() (but there is no error message).
NT7B4 was the first time I tried doing it, so I don't know if it's a new issue.
Also, it seems that Initialize() is called even for Indicators that are not in use. I have a Print statement in the Initialize() section of one of my indicators, and it prints even when not used in any chart. Is this by design? If so, I would think it would be better to add things in OnBarUpdate() so they don't consume resources unless they are used.
gomifromparis
11-10-2009, 08:00 AM
Yes I'm on NT7B4.
There is already a thread on issue 1 : you can't instantiate another indicator from one indicator's Initialize(), which would be the correct method to instantiate something. You have to instantiate it on OnBarUpdate or you'll run into BarInProgress exception.
This is inelegant (instantiation is an init, not "bar" code), and if you want to do init stuff like adding plots you can't because once in OnBarUpdate it's too late
issue 2 : when you instantiate an indicator from another one, Instrument property is null on the instantiated instrument. Please forget ticksize, but should'nt Instrument be equal to something ? With the debugger I managed to find something useful with the DataSeriesHelper interface but it's not public. The Instrument property is OK inside
(Bars)(((DataSeriesHelper)(this.Input)).DataSeries )
but you can't get there with the code. So please make Instrument property available again.
NinjaTrader_Dierk
11-10-2009, 08:02 AM
On 1): Again, please post some sample code. We tweaked it to make it more convenient. We needed sample code to understand what in your eyes would not be working.
On 2): This will not change: don't access .Instrument in Initialize().
NinjaTrader_Dierk
11-10-2009, 08:03 AM
Also, it seems that Initialize() is called even for Indicators that are not in use.
Same as NT6.5: there are many temporary instances of indicators and strategies where .Initialize() is called.
kdoren
11-10-2009, 08:08 AM
Dierk, I have an indicator that references 2 symbols, the primary series, plus a second symbol which is one of the indicator parameters. Right now I Add() the second symbol in Initialize(), and it seems to work.
Are you saying that I should Add() it in OnbarUpdate()?
Thanks, Kevin
NinjaTrader_Dierk
11-10-2009, 08:10 AM
No, I'm saying you should not access the ".Instrument" property in Initialize(), which you don't need to add a secondary instrument.
kdoren
11-10-2009, 08:38 AM
Dierk, Is this only true for Indicators?
I have a strategy where the secondary series is Added in initialize, and I reference Instrument.MasterInstrument.Name because I use it to determine the (default) name of the secondary instrument. This allows me to backtest/optimize on an instrument list, which I wouldn't be able to do if I had to enter the secondary symbol name for each instrument. It seems to work fine.
Also, in indicators, I am referencing BarsPeriod.Id and BarsPeriod.Value which are needed to make the secondary series have the same period as the primary; I presume this is OK.
Thanks, Kevin
NinjaTrader_Dierk
11-10-2009, 08:41 AM
Please see below: It's true for indicators and strategies.
Please check the NT6.5 docs on what you could access in Initialize() or not (NT7 docs are still in the making). Anything else may or may not work.
gomifromparis
11-10-2009, 09:29 AM
No, I'm saying you should not access the ".Instrument" property in Initialize(), which you don't need to add a secondary instrument.
Yes but suppose that for init purposes you need fullname ?
NT 6.5 and NT 7.0 doc states for Instrument.FullName
"This property can be accessed without a null reference check in the OnBarUpdate() event handler. When the OnBarUpdate() event is triggered, there will always be an Instrument object. Should you wish to access this property elsewhere, check for null reference first. e.g. if (Instrument != null)"
Which means you can access it from Initialize(), but you have to test for nullity first.
It works most of the time, but strangely Instrument is null when instantiated by another indicator (this did work in 6.5)
NinjaTrader_Dierk
11-10-2009, 09:42 AM
Not sure how to make it any clearer: DON'T access .Instrument in Initialize().
gomifromparis
12-16-2009, 06:22 AM
FYI you fixed this non-existent bug in 7b5.
Thanks
NinjaTrader_Dierk
12-16-2009, 06:33 AM
Not sure why you think there was a bug. You had been using a "feature" which may have worked in NT6.5 but was not supported nor documented. As such there is no guarantee it would work as expected nor it wouldn't be broken on an update.
However, we introduced an officially supported feature with NT7 which you now can savely use. This feature is documented in the release notes.