Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Advice needed: best practice to prevent freeze of application?

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

    Advice needed: best practice to prevent freeze of application?

    Hi,

    I am looking for any advice that can help to prevent the application from freezing (becoming unresponsive).

    For debugging purposes while programming a strategy I write a lot of comments and other pieces of information to a text file using the command:

    Code:
    File.AppendAllText(FileWithPath, outTextNoLF);
    Please note that I do NOT use:

    Code:
    Print("text");
    as I already learned that this uses a lot of internal ressources of NT7.

    I also installed a RAMdisk and use it to store the file on a very fast drive to minimize any delay that might stem from hardware issues.

    Without the logging turned on my strategy runs fine and without freezing up NT7. Yet, as soon as I turn the logging on NT becomes unresponsive. I can see that my strategy writes data so it is not locked up or in an endless loop.

    Is there any way to log data (maybe through a different function/command) to a text file that puts less strain on NT7? Are there any options that I may have to turn on (or off)?

    Thanks in advance for any help!

    NutFlush

    #2
    Hello NutFlush, and thank you for your question.

    While it would be outside the scope of the support NinjaTrader could provide to develop a solution, any expensive I/O operations with either a filesystem or the internet need to have a multi-threading solution in place, or you will experience what is known as "blocking" .

    Microsoft MSDN puts out publicly available documentation for developing in this situation.



    Please bear this warning from the help guide in mind when developing multi-threaded tools.
    Originally posted by https://ninjatrader.com/support/helpGuides/nt7/multi_threading.htm
    NinjaScript > Educational Resources > Tips >
    Multi-Threading Consideration for NinjaScript


    With the introduction of multi-threading in NinjaTrader when doing things like optimizations special considerations should be made when programming your NinjaScript indicators and strategies. Multi-threading basically allows NinjaTrader to take advantage of multi-core CPUs commonplace in modern computing to do multiple tasks at the same time. What this means for an optimization run is that multiple iterations can be tested by utilizing the various cores available to the computer.

    Should you be using custom resources like text files, static members, etc. it is important to protect your resources from concurrent access. If NinjaTrader tried to use the resource at the same time you would run into errors similar to this one:

    8/20/2010 12:14:29 PM|3|128|Error on calling 'OnBarUpdate' method for strategy 'SampleStrategy/1740b50bfe5d4bd896b0533725622400': The process cannot access the file 'c:\sample.txt' because it is being used by another process.

    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NutFlush View Post
      Hi,

      I am looking for any advice that can help to prevent the application from freezing

      Is there any way to log data (maybe through a different function/command) to a text file that puts less strain on NT7? Are there any options that I may have to turn on (or off)?

      Thanks in advance for any help!

      NutFlush


      That sounds very expensive, opening and closing the file each call to write text.

      You need to open the file once and close when done. That should greatly improve performance.

      Are you debugging strategy after the it is complete? Or are you using in real time?

      Comment


        #4
        Originally posted by sledge View Post
        https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

        That sounds very expensive, opening and closing the file each call to write text.

        You need to open the file once and close when done. That should greatly improve performance.

        Are you debugging strategy after the it is complete? Or are you using in real time?
        Hi, thanks for your comment!
        I use the file only after shutting down the strategy, so I could have the file open all the time until the strategy shuts down. Do you know how to do that? Can you point me to some documentation or the relevant commands?

        Thanks again!

        Comment


          #5
          Hello again NutFlush,

          If you have many small messages to write to a file, I'd like to recommend the built-in Log command. That command is documented here,



          You can use it like this

          Lot(LogLevel.Information, "Your message here");

          It will use minimal resources, and it will write to your most recent file in (My) Documents\NinjaTrader 7\logs .
          Jessica P.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NutFlush View Post
            I use the file only after shutting down the strategy, so I could have the file open all the time until the strategy shuts down. Do you know how to do that? Can you point me to some documentation or the relevant commands?
            I wrote a wrapper (called Debug) around a StreamWriter object that uses the WriteLine() method.

            I open the output file automatically on the first call to Debug(), here are some relevant pieces, you might find these useful,

            Code:
                    private bool                  Debug_SendToNinja                = false;
                    private bool                  Debug_SendToFile                  = false;
                    private StreamWriter    Debug_StreamOutput              = null;
                    private string                Debug_ActualFileName            = null;
            
                    // overload - support for variable arguments
                    private void Debug(string format, params object[] args)
                    {
                        Debug(string.Format(format, args));
                    }
            
                    // write string to debug output destination
                    private void Debug(string s)
                    {
                        if (Debug_SendToNinja)
                            Print(s);
            
                        if (Debug_SendToFile)
                        {
                            if (Debug_StreamOutput == null)
                            {
                                if (Debug_ActualFileName == null)
                                    Debug_ActualFileName = Debug_GetOutputFileName(this.Name);
                                Debug_StreamOutput = File.AppendText(Debug_ActualFileName);
                            }
                            Debug_StreamOutput.WriteLine(s);
                        }
                    }
            
                    // return fully-pathed debug output filename from user-supplied base filename
                    private string Debug_GetOutputFileName(string filnam)
                    {
                        return "C:\\Temp\\" + filnam + ".txt";
                    }
            Check out:

            Comment


              #7
              Also, study this one,

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              580 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              335 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              102 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              554 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              552 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X