Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can I call a custom method from a strategy run just once, on a list of symbols

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

    Can I call a custom method from a strategy run just once, on a list of symbols

    Hello. I'm trying to write a mini framework of sorts to export multiple values (EMA, OBV, price comparisons etc) for each trade generated by a strategy to SQL Server, so I can data mine it in Analysis Services. I'm running the strategy as a backtest on a list of symbols. I see that Initialize() and Terminate() seem to be called for each symbol. Since the backtest runs multi- threaded, I'm getting errors from Sql server that connections in the connection pool are used up.

    I'm still thinking through my approach, but is there a way to call a custom method just once for the entire backtest run, across multiple symbols? That way I can load all my data into a List<>, and at the very end write it to the database more smoothly.

    Thanks gurus...

    #2
    Hello CSharpTrader,

    You may use the Multi-Instrument approach to trade multiple instruments inside of one Strategy.

    http://www.ninjatrader.com/support/h...nstruments.htm

    Let us know if we can be of further assistance.
    JCNinjaTrader Customer Service

    Comment


      #3
      Thank you, JC. My question was specific to how Initialize() and Terminate() are called when running a strategy backtest on a list of symbols. Maybe someone can weigh in on it...

      Comment


        #4
        Hello CSharpTrader,

        Thank you for your response.

        The Initialize() method is called only once when the strategy is first started.
        Information on Initialize() can be found at the following link: http://www.ninjatrader.com/support/h...initialize.htm

        Each instrument in a basket test is given it's own instance of the startegy, so Initialize() is called on each instrument in the basket test.
        However, with the idea that JC has presented you could instead add each symbol into the strategy and just run the backtest on one instrument and the strategy would use each instrument in the strategy added with add().

        This can get complicated depending on the amount of instruments you wish to test, as each instrument would of course need it's own entries and exits. In addition, the backtest results would combine each instrument into the results. This means that the results would not be separated for each instrument as in the case of a basket test.

        For information on using multiple instruments in your strategy please visit the following link: http://www.ninjatrader.com/support/h...nstruments.htm

        Please advise what you are referring to in "terminate", do you mean Disable()?

        I look forward to your response.

        Comment


          #5
          Hi Patrick. I thought that was what he was referring to. I just wanted to clarify how the strategy was being called. That sounds complicated... i'm backtesting a list of 900 symbols. I haven't gotten the sql server errors the past few runs, so I'm ok for now. To answer your question, by Terminate I meant OnTermination(). I override this and add the database logic I need to export the data. As a side note, OnStartup() isn't working for me, the method doesn't seem to be getting called, so I use Initialize().

          Comment


            #6
            Hello CSharpTrader,

            Thank you for your response.

            The OnStartUp() should only be called once for each instrument during the Basket Test and this should be called after the Initialize() method.

            You can place a Print() statement within the OnStartUp() method to see that it is being called for each instrument in the Basket Test.

            If your calculation logic is not called within the OnStartUp() method then we recommend debugging your NinjaScript Strategy. For information on Debugging your NinjaScript Strategy please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=3418

            Please let me know if you have any questions.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            648 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            369 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            572 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            574 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X