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 information to a file

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

    Writing information to a file

    NinjaScript does not provide convenience methods for reading and writing files. You must use classes within .NET framework for File IO.

    The System.IO name space provides types for reading and writing to files.

    http://msdn2.microsoft.com/en-us/library/system.io.aspx

    Following is a conceptual sample of using the System.IO.StreamWriter class.

    Create a method:
    privatevoid WriteFile(string fileName, string line)
    {
    // Creates the file path
    string file = NinjaTrader.Cbi.Core.UserDataDir + @"log" + fileName;

    // Writes out a line to the specified file name
    try
    {
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(file))
    {
    writer.WriteLine(line);
    }
    }
    catch (System.Exception exp)
    {
    Log(
    "File write error for file name '" + fileName + "' Error '" + exp.Message + "'", LogLevel.Warning);
    }
    }



    Then call this method from within OnBarUpdate():

    protected overridevoid OnBarUpdate()
    {
    if (condition == true)
    WriteFile(
    "myFile", "The condition is true");
    }



    Ray
    RayNinjaTrader Customer Service

    #2
    imported post

    I got the following error when I tried to compile this:

    'NinjaTrader.Cbi.Globals' does not contain a definition for 'InstallDir' Strategy\Strategy12.cs 421 33
    Any ideas on what to do about this?

    Also, I need a way to flush and close the file at the end of back testing.

    Is it possible that there is another method that would only be called when the Strategy Analyzer completes running backtesting, where someone clould put close commands and stuff like that so one doesn't have to restart NinjaTrader to close a file?

    Thanks.

    KBJ

    Comment


      #3
      imported post

      To fix the compile issue, please try
      Code:
      NinjaTrader.Cbi.Core.UserDataDir

      Comment


        #4
        imported post

        Is this, perhaps, version dependent? I am using the field test (6.0.0.9).

        Applying your recommendation, I now get the following message:

        The type or namespace name 'Core' does not exist in the namespace 'NinjaTrader' (are you missing an assembly reference?)
        Also, I need a way to flush and close the file at the end of back testing.

        Does NinjaTrader have any method that could be defined in my application that would be called when the Strategy Analyzer completes a backtest, so that I may places code in that method that would close a file?

        I have coded a work-around for problem I was experiencing, but I still need a way to close a file at the end of backtesting. My current method is to stop NinjaTrader and restart it after every test. This is slow and boring. Can you offer a faster way of doing this?

        Comment


          #5
          imported post

          Sorry. Try

          NinjaTrader.Cbi.Core.UserDataDir
          Regarding flush and close the file...Could you eloborate on what you want to do? Not sure what you mean by stop NT, restart after every test.

          Thanks
          RayNinjaTrader Customer Service

          Comment


            #6
            imported post

            That worked.... thanks.

            You edited your original posting, so now all my earlier comments look like they're imaginary. I'll have to be more careful what I complain about!

            What I'm talking about with "closing" is this...

            When I couldn't get the code snippet you have below to work, I went to MSDN and found a cose sample to write into a file using FileInto and CreateText to open the file and then WriteLine to do the output.

            When using this approach, what I've found is that after running a back test from the Strategy Analyzer, if I try to do another backtest, I get this error message:

            3/31/2007 6:25:05 PM Strategy Error on calling 'OnBarUpdate' method for strategy 'Strategy12': The process cannot access the file 'Cocuments and SettingsNew UserMy DocumentsNinjaTrader 6trace.txt' because it is being used by another process.
            The only way I've found to prevent this error is to close the file by exiting NinjaTrader after each and every backtest, which as you might imagine gets a little tedious.

            So, I was thinking that if there were a way for my application to get control back one final time AFTER all of the backtest data has been processed by OnBarUpdate, perhaps by defining another method just for this purpose, then it could contain some code to close the file that was created.

            Or, if on the very last call to OnBarUpdate, there were some indicator to signify that it was the last of the backtest data, then I could do the close before exiting (although this approach does not handle closing the file if the script aborts before its completely run.)

            And as far as a "flush" goes, I guess that would be done implicitly by the close. I was just uncertain as to whether all of the data was being written to the file, thinking that perhaps some was still left in a buffer.

            Anyway, those were my thoughts on it. Since I don't understand NinjaTrader's internals, I may have suggested a less than optimum solution.

            Nevertheless, I feel that there should be functionality of this type available, and I'd hope that it wouldn't be something difficult to implement.

            Thanks,

            KBJ

            Comment


              #7
              imported post

              You could override the Dispose() method to close the file. This should get called when a backtest is complete.

              See the Help Guide Dispose() method for additional information.

              Ray
              RayNinjaTrader Customer Service

              Comment


                #8
                Ray

                I am trying to write an OIF, in NT7beta5, to the 'incoming' folder and have the wrong path...i.e. it is writing...somewhere....no errors in the Log

                BTW your code is missing a space between 'private' and 'void'

                code:


                private void WriteFile(string fileName, string line )
                {
                // Creates the file path
                string file = NinjaTrader.Cbi.Core.UserDataDir + @"incoming" + fileName;

                // Writes out a line to the specified file name
                try
                {
                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(file))
                {
                writer.WriteLine(line);
                }
                }
                catch (System.Exception exp)
                {
                Log("File write error for file name '" + fileName + "' Error '" + exp.Message + "'", LogLevel.Warning);
                }
                }

                Comment


                  #9
                  actually I tried in 7 and 6.5 and in both cases it wrote the file to the NinjaTrader folder

                  and named if first 'logoif.txt' and then with the above code 'incomingoif.txt' and now 'oif.txt

                  so clearly that code appends a suffix

                  tried several iterations for path....no luck

                  please specify path to NinjaTrader7/incoming..

                  thanks

                  Comment


                    #10
                    ATI user,

                    What you can do is print the string of the path to see what exactly it is coming out as. Then you can make amends/changes however you see fit to match your directory.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      thanks Josh

                      this code prints...
                      string file = NinjaTrader.Cbi.Core.UserDataDir + @"" + fileName;
                      Print (" string file = "+file);

                      C:\Users\Scott\Documents\NinjaTrader 7\oif.txt

                      I want it to print....to get the file in the 'incoming' folder

                      C:\Users\Scott\Documents\NinjaTrader 7\incoming\oif.txt

                      however, everything I try to change NinjaTrader.Cbi.Core.UserDataDir will not compile

                      and this code


                      string file = "C:\Users\Scott\Documents\NinjaTrader 7\incoming\oif.txt";

                      gets error 'unrecognized escape sequence'
                      Last edited by ATI user; 12-15-2009, 09:42 AM.

                      Comment


                        #12
                        It is just a matter of you manipulating the string. Before fileName part of your string concatenation, just add another for the \incoming directory.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          that just adds it to the front of the filename

                          string file = C:\Users\Scott\Documents\NinjaTrader 7\incomingoif.txt

                          this code won't compile

                          string file = NinjaTrader.Cbi.Core.UserDataDir + "\incoming\"+ fileName;


                          I need something other than

                          NinjaTrader.Cbi.Core.UserDataDir

                          could you please try this and tell me the path/code I need...I have wasted hours on this

                          thanks
                          Last edited by ATI user; 12-15-2009, 10:04 AM.

                          Comment


                            #14
                            You need to use string literals for \. Please see this reference sample: http://www.ninjatrader-support2.com/...ad.php?t=19174
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              right

                              this code works fine

                              string file = NinjaTrader.Cbi.Core.UserDataDir + @"\incoming\"+ fileName;

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by trilliantrader, Today, 08:16 AM
                              2 responses
                              6 views
                              0 likes
                              Last Post trilliantrader  
                              Started by samish18, Today, 08:31 AM
                              1 response
                              1 view
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by Creamers, 09-08-2023, 10:26 AM
                              6 responses
                              157 views
                              0 likes
                              Last Post JonyGurt  
                              Started by funk10101, Today, 08:14 AM
                              1 response
                              2 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by bill2023, Yesterday, 08:51 AM
                              3 responses
                              22 views
                              0 likes
                              Last Post bltdavid  
                              Working...
                              X