I've just find a solution to cirumvent the notorious "multiple Initialize() calls" behavior of NinjaTrader. The startegies Initialize() functions called :
- when you open up the Backtest window
- _twice_ after you click the OK button
So, we need to catch that second run, to do something like open a form, dialog, or else.
A possible solution is: in the Initialize() function, we check if the "Please wait..." window is exists and this is the second run.
I wrote a little class, and a sample strategy to demonstrate the solution. (If I find the upload button, you can even download it... :-))
Copy both Backtest.cs, and SingleInitSample.cs to your Documents\NinjaTrader 6.5\bin\Custom\Strategy directory.
To test:
Compile them, and run a backtest on SingleInitSample with OpenDialog parameter set to true, and you will get one OpenFileDialog
The Backtest is the main class, you can use it's isRunning() function from your strategy's Initialize function. For example:
protected override void Initialize()
{
if (Backtest.isRunning())
{
// do somethig here.....
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
Print(fd.FileName);
}
}
}
---- WARNING :
This solution is a fast hack. There will be situations, where it may not work, or crash your Ninja.
KNOWN BUGS :
THIS CODE WILL CRASH NINJATRADER IF YOU TRY TO USE THE ADD() FUNCTION IN THE SECOND RUN TO ADD NEW INSTRUMENTS TO THE STRATEGY! INDICATORS IS OK, BUT INSTRUMENTS NOT.
Happy coding !
-------------
This code is based on the following sample:
