Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Multiple Calls to "Initialize()" Method on Strategy Start

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

    Multiple Calls to "Initialize()" Method on Strategy Start

    Hello !

    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:
    Call Initialize()
    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
    I cannot put my initialization into OnBarUpdate() and only run it once as 'Add(SMA(myParameter));' will have the wrong parameter set and cannot be changed later.
    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.
    Attached Files
    Last edited by tomcat; 11-04-2007, 09:17 AM. Reason: Added File with Sample Strategy and Sample Output

    #2
    Unfortunately I am also unfamiliar with how many times Initialize() will be called, but I looked through your code and I believe it is unnecessary to call your Configure() method in the OnBarUpdate(). By the time it gets there everything is already said and done so setting them one more time is duplicate work.

    It would seem one set of prints is generated simply by bringing up the Strategy Dialog window, another few sets after you click OK.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Calling Configure in OnBarUpdate()

      Hi Josh !

      Thanks for looking at the code!

      When you look at the "Call-Trace", after the last Initialize() and "Configured DAX_S" line there are more calls to setup parameters "Call to *.Set" - which will override the settings that have been made in "Configured DAX_S".

      Maybe we are looking at a bug as well, as after the last Initialize() there are more calls to set Parameters...

      To work around I need to call Configure() one more time - in OnBarUpdate() to make sure I have the parameters that I want...

      Quite tricky...

      Comment


        #4
        You are right. My bad. I was only looking at the valuation up top in the Initialize() method without looking at them being used in the OnBarUpdate() method as well.
        Josh P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Johnny Santiago, 10-11-2019, 09:21 AM
        95 responses
        6,193 views
        0 likes
        Last Post xiinteractive  
        Started by xiinteractive, 04-09-2024, 08:08 AM
        2 responses
        11 views
        0 likes
        Last Post xiinteractive  
        Started by Irukandji, Today, 09:34 AM
        1 response
        3 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by RubenCazorla, Today, 09:07 AM
        1 response
        5 views
        0 likes
        Last Post RubenCazorla  
        Started by TraderBCL, Today, 04:38 AM
        3 responses
        25 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X