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

Is there a function called after all instruments in a strategy?

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

    Is there a function called after all instruments in a strategy?

    Hi, I am looking for a way to do some hand-crafted summary of strategy results. If I run a strategy over an instrument list, it seems that for each instrument, there is a separate strategy object. Is that so? Is there a function (hook) which I can override which is called when all instruments in a list are processed by a strategy?

    Thanks

    #2
    Hi ivannp,

    You are correct, when backtesting over an instrument list, each test will be a separate independent instance and they cannot communicate with each other.

    Your option is to add all the instruments that you would like to a single script as additional dataseries and then backtest over a single instrument.

    It is not supported to do so, but it is possible to add instruments to a script by an instrument list.

    Below is some sample code that can help with this. (Again this is unsupported code)
    Code:
    NinjaTrader.Cbi.InstrumentList list = NinjaTrader.Cbi.InstrumentList.GetObject("Custom");
    if (list != null)
    {
    foreach (Instrument i in list.Instruments)
    {
    //Print(i.FullName);
    Add(i.FullName, PeriodType.Day, 1);
    }
    }
    else
    {
    //Log("AddInstrumentsByListExample: There is no instrument list named Custom", LogLevel.Information);
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Would it be possible to make a feature request for it? I think it is quite useful for summaries, reports etc.

      Comment


        #4
        Hi ivannp,

        I'd be happy to submit a feature request for this in your behalf.

        Thanks for the great suggestion!
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks, can you add a request for similar single Start method? The Start should be called prior to running the strategy over any of the instruments and the Stop should be called after all instruments were processed.

          Not sure whether these could be part of the Strategy itself (i.e. static methods), but user functions or any other form of "hooks" should suffice.

          Thanks again

          Comment


            #6
            Hello ivannp,

            Again, this would be possible if you add the multiple dataseries to a single script. The OnStartUp() and OnTermination() would do what you are asking.

            Also, the summary and statistics would be merged as well like you are asking.

            Merging the performance results of multiple backtests into one large summary and statistic report does sound feasible to implement without any further explanation as they can be merged after the data is produced.

            However, how would you suggest strategies be structured so that it would be possible to run code before running several backtests and then after as well. Would strategies be broken into three separate files? A pre-backtest .cs file and a post-backtest .cs file?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I haven't tried yet to add multiple instruments to a single script, but if I go that path, am I not going to lose the ability to simply select a few instruments from the strategy analyzer GUI and firing off the backtest? I also suspect that the execution is going to be running on a single core, while I have the feeling that multiple instruments are run in parallel.

              What I am currently working on is a backtest over about 30 continuous future contracts, daily data. Once the run finishes, I'd like to be able to extract trading data (buy/sell orders for the next day). First I tried writing to a file, but that's how I discovered that the various instruments are run in paralell - I'd get random file errors. Everything cleared up once I added a static Mutex to my strategy and serialized the file operations. That seems to work. Wouldn't something like that work for a global start/stop inside NT as well - (a static mutex + variable to sync all threads at the start and to drain them at the end and two static methods in the strategy?) although I am not sure how assemblies, global mutexes and other low level stuff operates in C#.

              The problem that I am trying to solve, is that at the end of each instrument, I can store the stats I need into a file, or a database. However, I don't know which is the last thread finishing and I don't think it's easy to figure out without some non-deterministic hacks. If I new for instance how many instruments the strategy is being run for - then a simple atomic counter will tell me which one is the last, but until then - my only choice is to run a separate program at the end to summarize the results. Hopefully in NT 8 at least I will be able to integrate this "final" program better in NT ...

              Comment


                #8
                Hi ivannp,

                If you script was coded to do it, it would be possible to select any instrument and then make an input to select the instrument list you would like to backtest over. You could have that ignore the primary series if you wanted and just backtest the selected list. Currently, you can choose a single instrument to backtest over or an instrument list to backtest over. It is not possible to individually select multiple instruments to backtest over.

                You are correct, when backtesting (and only when backtesting) instances of backtests are run in their own thread in parallel.

                Is your suggestion a special method that is run preceeding a backtest and a special method that is run after all of the backtests have completed?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hi ivannp,

                  Is your suggestion a special method that is run preceeding a backtest and a special method that is run after all of the backtests have completed?
                  Yes, two methods: the first invoked before any of the threads participating in the backtest kicks off, and the other invoked after all threads are done with the back test. For my specific task, I don't care having access to any of the trading data, although one might make the case that this could be useful at exit (within the second method).

                  Hmm, I am starting to think that I can actually implement that even now: What if I add a counter, protected by a global mutex to count the threads (each thread increasing the counter in its own StarUp method). I can repeat the process within the termination routine, with the last thread performing the summary tasks. That should actually work. I will give it a try later this week (or next week). Will update the thread again.

                  Comment


                    #10
                    Hello ivannp,

                    Thank you for taking the time to clarify that for me.

                    I will submit this as a feature request in your behalf.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello ivannp,

                      I wanted to let you know that your suggestion has been entered into our tracking system and is being tracked with ID #2822.

                      Thank you for your great suggestion!

                      Please do not hesitate to contact us for any other NinjaTrader inquiries you may have.
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by arvidvanstaey, Yesterday, 02:19 PM
                      5 responses
                      14 views
                      0 likes
                      Last Post NinjaTrader_Zachary  
                      Started by Rapine Heihei, Yesterday, 08:25 PM
                      1 response
                      12 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by Mongo, Yesterday, 11:05 AM
                      6 responses
                      27 views
                      0 likes
                      Last Post Mongo
                      by Mongo
                       
                      Started by ct, 05-07-2023, 12:31 PM
                      7 responses
                      207 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Started by wzgy0920, Yesterday, 09:53 PM
                      1 response
                      13 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Working...
                      X