Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Analyzer - custom resources null when OnStateChange State.Terminated

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

    Strategy Analyzer - custom resources null when OnStateChange State.Terminated

    Build - 8.0.0.2 64-bit (Multi-Broker) - BETA

    I create several System.IO.StreamWriter objects for writing data to files when my strategy runs, but by the time I get to the State.Terminated state, all of my stream writer objects are null and I cannot write to them before disposing of the objects properly.

    Also, when I open the output files after the strategy finishes running, I get a warning that a process still has it open for editing. This indicates that the stream writer objects were in fact not disposed of properly. This was not a problem in NT7 when I handled disposal of the stream writers in the OnTermination method.

    I really need these objects not to be null when State.Terminated state is detected. Or I would need a new state, such as State.BeforeTerminate.

    Here are details of my code:

    I declare the variables at the class level:
    Code:
     
     public class cmTimeSliceStatistics : Strategy
     {
       #region Variables
       private System.IO.StreamWriter swStats; // For writing stats to text file
       ...
    I assign a new StreamWriter object during the first call to OnBarUpdate method:
    Code:
     
     protected override void OnBarUpdate()
     {
       try
       {
         if (!initialized)
         {
           swStats = File.AppendText(statsFileName);
           initialized = true;
         }
          ...
    I attempt to manually dispose of the StreamWriter, but it's already null at this point:
    Code:
     
     protected override void OnStateChange()
     {
       if (State == State.Terminated)
       {
         if (swStats != null)
         {
           swStats.WriteLine("Write Final Summary Stats Here.");
           swStats.Dispose();
           swStats = null;
         }
       }
     }
    In case it wasn't apparent, the stream writer objects are created properly and data is being written to them during subsequent calls to OnBarUpdate. The only issue I am having is with writing final information and disposing of the objects properly.

    #2
    Hello,

    Thank you for the question.

    I tried the code you provided in a strategy and I am not seeing the outcome you are.

    I do see the termination print in the document and the objects are being disposed correctly from what I can tell as I can re apply the strategy and re write to the file again.

    Can you provide a script that demonstrates what you are seeing on your end as this seems to be working correctly on my end using only what was provided.

    I look forward of being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      I just created a test script and experienced the same problem as I reported earlier. Here are some more details. When the strategy appears to be done running as a backtest, the process still has a lock on the output file and the processing has not actually completed. However, once I re-run the backtest, the first backtest completes and the final line is written to that file.

      I have tested with and without debugging turned on, and with and without the debugger attached. All cases had the same results.

      I made this test that writes out the number of the currentbar on each onbarupdate call. Once the backtest appears complete, the last line has an incomplete output that omits the bar number, and the line before that has "Bar Number = 20009". After re-running the strategy, the previous file completes output and the last line is "Bar Number = 20090" followed by "Strategy.Terminated reached".

      When debugging in Visual Studio, my breakpoint for Strategy.Terminated gets hit with a swStats object at the beginning of the run with the object from the previous run. At the end of the current run, Strategy.Terminated gets hit with a null object. This is very consistent and looking at the output file with timestamp in the name helps identify this problem.

      I'm attaching my sample strategy and strategy settings file. Not sure if it matters, but at least part of my data was imported from NT7 export because it was missing on your historical NT8 data server. I also use non-adjusted merged contracts.

      I should also mention that I have shutdown and restarted NT8 and run with and without NT7 at the same time. None of these actions changed my results.
      Attached Files
      Last edited by ganamide; 06-22-2015, 02:15 PM.

      Comment


        #4
        Hello,

        Thank you for providing the sample.

        I will run this and see if I can re produce what you are experiencing.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello,

          I tested this in 8.0.0.2 and this does exist as you had described it.

          I further tested this on a current release and it seems to have been resolved sometime after 8.0.0.2.

          The new test results were a print for each bar leading up to termination, once the final bar was met it did write the entire line and also went through the Termination state and wrote that as well.

          Once the 8.0.0.3 is released, this should be included and should be resolved, at that point I would ask that you verify this on your end but you should have the same result.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Thanks! Is there an ETA on the 8.0.0.3 release date?

            Comment


              #7
              Hello,

              With each incremental beta release there is no scheduled time for its release, so unfortunately I can not give you a firm date in this case.

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Can an NT official please confirm whether or not the problem described in this thread is still a problem in NT Version 7.0.1000.31? I believe I am running into a similar problem. My strategy makes final calculations during OnTermination and outputs to various CSV files. The strategy works fine when applied to a chart or backtested in Strategy Analyzer.

                However, the problem arises when I attempt to run it through the optimizer. It seems like it runs through once, but when it moves to the next iteration, one of the CSV files is still in use (while the other is curiously not). Therefore, only one iteration of the optimization occurs and no trades are generated for the rest of the iterations. Thanks for any insight!

                Comment


                  #9
                  Hello,

                  I would be unsure if this was related to the original posters concern.

                  In short the Optimization in NT7 does run multiple tests concurrently so if you are writing to a file you would need to explicitly check if the file is in use before trying to do any file operations or wait.

                  Because this is an NT8 specific thread, I would ask that you provide your specific sample script and question again as a new thread in the NT7 Strategy Development forums for a more specific answer. http://ninjatrader.com/support/forum...splay.php?f=16

                  Please let me know if I may be of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Thank you, Jesse. I understand the issue better now. I will repost in the NT7 Strategy Development forum, but do you have an example of how to either write to a file from multiple streamwriters concurrently (if that is even possible?) or how to get the strategy to wait until the file is available to be written too? Thank you!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by poplagelu, Today, 05:00 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post poplagelu  
                    Started by fx.practic, 10-15-2013, 12:53 AM
                    5 responses
                    5,407 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by Shai Samuel, 07-02-2022, 02:46 PM
                    4 responses
                    98 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by DJ888, Yesterday, 10:57 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by MacDad, 02-25-2024, 11:48 PM
                    7 responses
                    160 views
                    0 likes
                    Last Post loganjarosz123  
                    Working...
                    X