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

Writing to text file OnTermination()

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

    Writing to text file OnTermination()

    Hi,

    I am overriding the OnTermination() method of my strategy to write to a text file.

    When running the the market analyser for multiple instruments my understanding is that it is creating a new thread for each strategy. This is fine and I have put a lock around the OnTermination() method, however i need a way to determine if the Strategy is the first to be processed for the backtest run.

    I need to write to the text file with the conditions:
    (i) If it is the first strategy to be complete in the backtest then FileMode.Create
    (ii) If it is NOT the first strategy to be processed in the backtest run then FileMode.Append

    I tried to make a static bool firstRun = true which is set to false in OnTermination but this variable is never reset after running the first backtest.

    I hope this makes sense

    #2
    Beach01,

    Thank you for your note.

    I'm not sure I quite follow.

    When you say backtest, you are referring to a backtest of the strategy, correct?

    If not, could you please elaborate?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Hi Cal,

      Thanks for your reply, sorry for the confusion not sure if i have the terminology right...I am going to the Strategy Analyzer and running a back test on a bunch of stocks eg DOW 30 for my Strategy.

      Basically what I want to achieve is to be able to get the data for all the trades for all the stocks (each stock has a trades tab) and dump them into a single excel file.


      Thanks Mike

      Comment


        #4
        Beach01,

        You can use the Microsoft.Office.Interop.Excel dll to read or write to an excel file.
        http://msdn.microsoft.com/en-us/libr....interop.excel

        Additionally, you can right click on the backtest results and export it to an Excel spreadsheet as well.

        Let me know if I can be of further assistance.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Hi Cal,

          Thanks for the reply. The right click results and export to Excel is great for a few stocks but having to do this for 50+ stocks for each back test is not really feasible.

          I will look into the Microsoft.Office.Interop.Excel but feel its over-complicating things when i can just write CSV to a text file and open in excel.

          Is there an event i can hook onto when the back test kicks off? this would really help me to solve my problem.

          Thanks,
          Mike

          Comment


            #6
            Beach01,

            OnTermination should still be called in the backtest. You can use that to hook into,
            http://www.ninjatrader.com/support/h...rmination2.htm
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              Hi Cal,

              I don't think you are understanding the problem correctly. OnTermination() Gets called multiple times when running the backtest for multiple instruments.

              I found a post with a similar with a similar problem please see here they may explain it better than i have



              You can see that they have used a static variable but because the Market Analyser does not dispose of the strategy object after a run, the next run the variable is not reset.

              So the person needed to recompile each time to force re-initialisation of the static variable.

              So my question is...Is there some sort of back test run id we can access from the Market Analyser or an event when the backtest first runs? Does the Strategy reference the Market Analyser object?

              I have a dirty solution at the moment which is assuming that back tests cannot be run within 2 seconds of each other

              protected override void OnStartUp()
              {
              lock(_syncLock)
              {
              // this is used because the static variables only get initialised once not once every backtest run
              // so assume the last
              if (DateTime.Now.Subtract(_lastRun).TotalMilliseconds > 2000)
              _isFirstRun = true;

              _lastRun = DateTime.Now;
              }
              }

              It would be more robust if we could read a backtest id or event or something similar
              Attached Files

              Comment


                #8
                Hi,

                There is no backtest event you can hook onto to know when the backtest has started or completed.

                However I can understand how this would be useful and will submit this as a feature request to consider in a potential future version.
                MatthewNinjaTrader Product Management

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by BarzTrading, Today, 07:25 AM
                2 responses
                13 views
                1 like
                Last Post BarzTrading  
                Started by devatechnologies, 04-14-2024, 02:58 PM
                3 responses
                19 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Started by tkaboris, Today, 08:01 AM
                0 responses
                3 views
                0 likes
                Last Post tkaboris  
                Started by EB Worx, 04-04-2023, 02:34 AM
                7 responses
                162 views
                0 likes
                Last Post VFI26
                by VFI26
                 
                Started by Mizzouman1, Today, 07:35 AM
                1 response
                9 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Working...
                X