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

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

    writing to text file

    Hi,

    I have some questions regarding how I can write to text files as I want to produce a log:

    1) I attempted to put the path name into a string variable, but the compiler won't accept the "\" character as in "D:\MyLogfilesFolder\LogStrategy1.txt". So how do I tell the strategy where the file is located?

    2) I intend to use File.AppendAllText(MyFile, MyText) to add text to the file. Will that work or do I also need to initialize anything (beyond MyFile and MyText as string variables)?

    3) MyFile would need to include path AND filename, right?

    4) Would it be possible to have several strategies all writing to a master log file? From what I read about the command File.AppendAllText() it will close the file right after appending the new text. So it should be ready for other strategies to write to it, or could that lead to an error?

    Thanks,
    NutFlush

    #2
    Hello NutFlush,

    Thanks for your note.

    1) The "\" character is an escape character (in most languages as well).

    To use this character in a string you will need to escape itself by using it twice.

    For example;
    "D:\\MyLogfilesFolder\\LogStrategy1.txt"

    2) The call to File.AppendTextAll looks correct though I have not tested this. Let me know if you have any issues using this call.

    3) Yes, MyFile will need a path and file name.

    4) It is possible to have several strategies writing to the same file, however, there are some extra considerations to prevent NinjaTrader from crashing. Namely, use a try and catch when opening the file for editing and dispose of the handle OnTermination().

    Below is a link to an official reference sample that accomplishes this.
    http://www.ninjatrader.com/support/f...ead.php?t=3475
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      4) It is possible to have several strategies writing to the same file, however, there are some extra considerations to prevent NinjaTrader from crashing. Namely, use a try and catch when opening the file for editing and dispose of the handle OnTermination().
      Thanks, it works!

      Just 2 more things:
      1) The handle you are talking about is a StreamWriter, correct? But I don't seem to create one, so what do you want me to dispose?

      2) The text in the file is all in one line. How do I add a line feed???

      Thanks a lot!
      NutFlush

      Comment


        #4
        Hello NutFlush,

        Yes, I am speaking of the StreamWriter handle. In the reference sample this is created with the following line.

        private System.IO.StreamWriter sw;

        Without disposing of this handle the file will stay locked for editing and other scripts will not able to write to this file.

        Also, the SampleStreamWriter.cs does contain line breaks. Do you have this file open in the NinjaTrader code editor window?
        • Tools -> Edit NinjaScript -> Indicator... -> select SampleStreamWriter -> click OK
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello NutFlush,

          Yes, I am speaking of the StreamWriter handle. In the reference sample this is created with the following line.

          private System.IO.StreamWriter sw;

          Without disposing of this handle the file will stay locked for editing and other scripts will not able to write to this file.

          Also, the SampleStreamWriter.cs does contain line breaks. Do you have this file open in the NinjaTrader code editor window?
          • Tools -> Edit NinjaScript -> Indicator... -> select SampleStreamWriter -> click OK
          I have it open, but that example uses a StreamWriter. It also uses uses a StreamWriter specific command to achieve the line break.

          I was hoping to use File.AppendTextAll as it looks much simpler and it works in respect of sending text to a file (except for the line break). Isn't there a special character or string that I could add to my string that would produce the line break?

          Thanks,
          NutFlush

          Comment


            #6
            Hi NutFlush,

            My mistake, I misunderstood what you meant by newline. You meant adding new lines to the file.

            New lines within a string are \r\n (return and then newline).

            For example:
            myString = "text on first line\r\ntext on second line";

            The sample would accomplish this by having seperate sw.writeline commands.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by usazencort, Today, 01:16 AM
            0 responses
            1 view
            0 likes
            Last Post usazencort  
            Started by kaywai, 09-01-2023, 08:44 PM
            5 responses
            603 views
            0 likes
            Last Post NinjaTrader_Jason  
            Started by xiinteractive, 04-09-2024, 08:08 AM
            6 responses
            22 views
            0 likes
            Last Post xiinteractive  
            Started by Pattontje, Yesterday, 02:10 PM
            2 responses
            21 views
            0 likes
            Last Post Pattontje  
            Started by flybuzz, 04-21-2024, 04:07 PM
            17 responses
            230 views
            0 likes
            Last Post TradingLoss  
            Working...
            X