Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Initialize and Dispose Strange behaviour

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

    Initialize and Dispose Strange behaviour

    I am posting here because I tripped over this in NT7 Beta-18 -- it may well occur in general...
    Consider:

    public override void Dispose()
    {
    // Clean up your resources here
    Print ("In Dispose...");
    base.Dispose();
    }

    protected override void Initialize()
    {
    Print ("In Initialize...");
    }

    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values.
    Print ("Entering -- CurrentBar=1.");
    }

    When I add this indicator to a chart, the output window looks as follows:

    In Dispose...
    In Initialize... (I think these two occur when I select the indicator in the dialog window...)
    In Initialize...
    In Initialize...
    In Initialize...
    In Dispose...
    Entering -- CurrentBar=1.

    I immediately remove the indicator and the following gets added:
    In Dispose...
    In Initialize...
    In Dispose...
    In Initialize...
    In Dispose...
    In Dispose...
    In Dispose...


    The protected override void Initialize() is described as being "called once"...

    Is this correct behavior?
    Why do these routines get called so many times?
    Why does Dispose get called when starting...... If I clean up my resources, I won't be able to start!

    Can anyone explain?

    #2
    I know just enough C# to be dangerous,but consider this:

    OnStartUp() is really only called once, at Start Up.

    OnTermination() is called once on termination.

    try this:

    protected override void OnStartUp()
    {
    Print("++++++++++++++");
    Print(DateTime.Now + " Start Me Up!");
    Print("++++++++++++++");
    }

    protected override void OnTermination()
    {
    Print("++++++++++++++");
    Print(DateTime.Now + " Terminating!");
    Print("++++++++++++++");
    }

    Comment


      #3
      Thanks MXASJ - TS_Gordo - have tried printing in OnStartUp() and OnTermination() instead?

      The Initialize() and Dispose() could indeed be called multiple times, if you're in 6.5 you could for example move this print to the first OnBarUpdate() bar if (CurrentBar ==0 ....) - this would then also be a one time event.

      Comment


        #4
        Thanks MXASJ. OnStartUp() and OnTermination() are the methods I am looking for.

        For NinjaTrader_Bertrand: I got the idea that the Initialize() method was "called once" because of the comment line inserted into indicator code by the wizard.... you might want to ask the development guys to correct that....

        I do understand about using bar # 1 to condition the execution of set up code. In fact if one reads the description of Initialize() in the 6.5 docs, after saying it is "called once" it goes on to explain that it will be called multiple times... and NOT to use it for any set up code....

        The Dispose() method, which I discovered here in the Forum, is a difficult problem -- there is no "bar #1 trick available to catch shut-down... The 6.5 docs do NOT contain any such -- called multile times -- warning... in fact Dispose() is described as the way to clean up resources of a custom indicator..... You might want to ask the development guys to clean that up too.

        A search of the 6.5 docs does not turn up any description for either OnStartUp() or OnTermination() methods!

        Just how is a fellow supposed to discern their existence?

        Are these "normally" used C# methods? Perhaps another item for the development boys???



        One final, sort of related question: Can an indicator "Remove" itself?

        Comment


          #5
          Thanks for the suggestion on the comment line message.

          OnStartUp() and OnTermination() is new to NT7.
          Dispose() is the only way you can clean up custom resources in NT6.5. In NT7 we provided OnTermination() as Dispose() has its limitations and should no longer be used.

          Unfortunately an indicator cannot remove itself.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Thanks Josh, that clears it up nicely.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            581 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            338 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
            554 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            552 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X