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

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.

    Read about asynchronous file I/O in .NET. Learn async methods to simplify asynchronous operations, such as ReadAsync, WriteAsync, and more.


    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
      Appends the specified string to the file, creating the file if it does not already exist.


      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:
            Implements a TextWriter for writing characters to a stream in a particular encoding.

            Comment


              #7
              Also, study this one,
              Open and append to a log file using the StreamWriter and StreamReader classes in .NET, which write characters to and read characters from streams.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by alexstox, 10-16-2018, 03:29 PM
              11 responses
              342 views
              0 likes
              Last Post aligator  
              Started by ageeholdings, 05-01-2024, 05:22 AM
              6 responses
              43 views
              0 likes
              Last Post ageeholdings  
              Started by tony_28217, Today, 07:04 PM
              0 responses
              11 views
              0 likes
              Last Post tony_28217  
              Started by flybuzz, Today, 10:33 AM
              1 response
              9 views
              0 likes
              Last Post flybuzz
              by flybuzz
               
              Started by spencerp92, 10-10-2023, 09:56 AM
              4 responses
              311 views
              0 likes
              Last Post flybuzz
              by flybuzz
               
              Working...
              X