Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Circumventing the multiple Initialize() calls during backtest

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Circumventing the multiple Initialize() calls during backtest

    Hello,

    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:

    PHP Code:
    protected override void Initialize()
    {                    
        if (Backtest.isRunning())
           {
                // do somethig here.....             
                OpenFileDialog fd = new OpenFileDialog();
                if (fd.ShowDialog() == DialogResult.OK)
                {                
                    Print(fd.FileName);
                }
            }            
    
    } 
    
    You can use it in multiple startegies, it won't cause problems.

    ---- 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:
    Attached Files

Latest Posts

Collapse

Topics Statistics Last Post
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
0 responses
597 views
0 likes
Last Post Geovanny Suaza  
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
0 responses
343 views
1 like
Last Post Geovanny Suaza  
Started by Mindset, 02-09-2026, 11:44 AM
0 responses
103 views
0 likes
Last Post Mindset
by Mindset
 
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
0 responses
556 views
1 like
Last Post Geovanny Suaza  
Started by RFrosty, 01-28-2026, 06:49 PM
0 responses
555 views
1 like
Last Post RFrosty
by RFrosty
 
Working...
X