As pointed out in the Thread about Forms to fill parameter-data http://www.ninjatrader-support.com/v...ead.php?t=3292
there are multiple calls to the Initialize Method of a strategy.
I bumped into a similar problem as I want to override the parameters of the strategy inside the strategy itself. (Think of one Parameter saying "use your built-in data instead of the parameters supplied"). This would enable me to run the strategy optimizer to determine the optimal parameter set per instrument - and then hardcode them into the strategy or import them from a file.) When opening the chart I would only need to add the strategy and it will be configured automagically with the right parameters for the instrument unless I wish to override the hardcoded settings.
This is tricky as the Setter-Methods of the parameters and the Initialize() Method are called multiple times when a strategy is added to a chart.
(Proof: Just add a 'Print("Called Initialize()");' to the initialize Method and a 'Print("Called Setter()");' to some setter Methods in the Parameters-Section and watch the output window scrolling.)
Is there a common / safe way of knowing, when all initialization is done so I can override the data from within?
I would envision something like this:
- Strategy Object is created. (.new)
- Parameters are passed
- Initialize() is called (once?)
- afterInitialize() is called, exactly once
and can be used to add Indicators as well.
- then OnBarUpdate gets called for every tick.
- Dispose() is called once.
Currently it seems to be like:
- Strategy Object is created (.new)
=== multiple roundtrips of the following steps:
- Initialize is called
- parameters are passed
===
- OnBarUpdate() gets called for tick data.
- Dispose() may or may not be called afterwards. (See another thread on this.)
See attached sample Strategy Code (modified SMAcrossOver) below.
The Dump from the output window looks like:
Loading Parameters for
Loading Parameters for
Call to Initialize()
Loading Parameters for
Loading Parameters for
Call to Initialize()
Loading Parameters for
Call to autoConfig.Set()
Call to Fast.Set()
Call to Slow.Set()
Call to Initialize()
Loading Parameters for DAX_S
Configured DAX_S
Call to autoConfig.Set()
Call to Slow.Set()
Call to Fast.Set()
First call to OnBarUpdate()
OnBarUpdate - Loading Parameters for DAX_S
Configured DAX_S
Currently I do the overriding in Initialize() (multiple times ;-) and once in OnBarUpdate - which now seems to work, but is IMHO not proper coding style...
Any thoughts or suggestions?
Thanks, TomCat.
Comment