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

    #16
    Originally posted by ATI user View Post
    this code

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

    gets error 'unrecognized escape sequence'
    Try using a single quote instead, or a double backslash. For instance,

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

    -- or --

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

    Comment


      #17
      thanks KBJ

      Comment


        #18
        KBJ

        please see this thread and let me know if you have had the same problem wherein oif.txt only executes once after NT restart

        thanks

        Comment


          #19
          Originally posted by ATI user View Post
          KBJ

          please see this thread and let me know if you have had the same problem wherein oif.txt only executes once after NT restart

          thanks

          http://www.ninjatrader-support2.com/...ad.php?t=23712
          ATI User: I looked at the thread. However, this is an area that I don't know much about, as I've never tried using an OIF/Order-Instruction-File.

          Good luck.

          Comment


            #20
            oif file

            ATIuser - I assume you solved your once only error?
            I declare a variable like so
            string incomingpath = Cbi.Core.UserDataDir.ToString() + "\\incoming\\oif"

            It works for me in Vista - the double slashes are important.

            Could I add a suggestion / ask for a hint... I would like to place a stop with a negative limit order . I can do this by manipulating chart trader but is there a hint as to how to do it programmatically?
            Obviously it's a synthetic order but is it possible and if it isn't can I ask for it to be added?

            Comment


              #21
              Mindset

              yes...sorry...thought I had posted a final solution to once only issue:

              you must not have the 'Ignore duplicate OIF files' checked at Tools/Options/ATI...see pic

              I originally thought this option was to avoid race conditions...i.e. executing more than once per instance. In fact it stops duplicate files (same name) for the entire session

              re code: as mentioned this code works fine for me..in Vista...did not need double slashes

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

              re negative order, if asking me, I would try to place via AtmStrategyCreate code...have not tested this though
              Attached Files

              Comment


                #22
                How can I write a file to the following location?

                I:\Backtests\november-2010\SampleStrategyForTesting\MSFT_15-11-10[17:27].txt

                I’ve tried to format this in a string in the following way:
                Code:
                  string currentTime = DateTime.Now.ToString("dd-MM-yy[HH:mm]");
                  string fileLocation = string.Format(@"I:\Backtests\{0}\{1}\{2}_{3}.txt",
                                  DateTime.Now.ToString("MMMM-yy"), 
                  Name.ToString(), 
                  Instrument.FullName.ToString(),
                   currentTime);
                However, when I try to write to this file location using SteamWriter, I get the following error:
                “The given path directories aren’t supported” (translated from Dutch)

                However, I can create these directories through the use of “System.IO.Directory.CreateDirectory()”. Besides that, when I write to a file location which is hardcoded in the strategy, the file does gets written. But that won’t be feasible with backtests on multiple instruments.

                So, I can create the directory in NinjaScript, however when I try to write a .txt file (to the same directory) I get an error saying that the directories aren’t supported. Anyone with a suggestion?

                Regards,

                Comment


                  #23
                  jos

                  have you tried \\ ie double backslashes or @?

                  Comment


                    #24
                    J_o_s,

                    Your file string output is this with your current code:

                    I:\Backtests\November-10\SampleStrategyForTesting\ES 12-10_15-11-10[09:41].txt

                    Highlighted in red is the difference area between what you wanted and what you have.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #25
                      Originally posted by Mindset View Post
                      have you tried \\ ie double backslashes or @?
                      Thanks for your comment Mindset. Yes, I’ve tried that to no avail. (Thanks for your comment though )

                      Originally posted by NinjaTrader_Josh View Post
                      J_o_s,
                      Your file string output is this with your current code:
                      I:\Backtests\November-10\SampleStrategyForTesting\ES 12-10_15-11-10[09:41].txt
                      Highlighted in red is the difference area between what you wanted and what you have.
                      Thanks Josh, you’re right, I’ve copied & pasted the wrong line from the Output Window. However, that wasn’t related to the error of the strategy, just my wrong copy & paste.

                      Because the comments of you both pointed to the basics, I’ve started this morning from scratch, and uncovered the part which made the error:
                      Code:
                        // The statement below returns the error
                        string currentTime = DateTime.Now.ToString("dd-MM-yy[HH:mm]");
                        // Removing the “:” in the string does make it work
                        string currentTime = DateTime.Now.ToString("dd-MM-yy[HHmm]");
                      Woops, not that smart because a “:” isn’t an allowed character (see also http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx ). So all in all it was a little mistake which rendered the (abstractly formulated) error.

                      For what’s it’s worth, this is the C# code at the moment:
                      Code:
                        private System.IO.StreamWriter sw;
                                public void WriteStrategyToFile()
                                {
                                    // Generate the string with the date and time
                                    string currentTime = DateTime.Now.ToString("dd-MM-yy[HHmm]");
                                    
                                    // Generate the target directory path
                                    string filePath = string.Format(@"C:\Temp\{0}\{1}\",
                                        DateTime.Now.ToString("MMMM-yy"), Name.ToString());
                         
                                    // Generate the file name
                                    string fileName = string.Format("{0}_{1}.txt", Instrument.FullName.ToString(), currentTime);
                         
                                    // Combine both strings in the complete path
                                    string fileLocation = System.IO.Path.Combine(filePath, fileName);
                         
                                    Print("Current file location: " + fileLocation);
                         
                                    // Make the directory if it's not yet existing
                                    if (!System.IO.Directory.Exists(filePath))
                                    {
                                        System.IO.Directory.CreateDirectory(filePath);
                                    }
                         
                                    // Start writing to file
                                    try
                                    {
                                        sw = System.IO.File.AppendText(fileLocation);
                         
                                        sw.WriteLine("Current time is " + currentTime);
                                        sw.WriteLine("StrategyName: " + Name);
                                    }
                                    // Catch the exceptions
                                    catch (Exception e)
                                    {
                                        // Outputs the error to the log
                                        Log("You cannot write and read from the same file at the same time. Please remove SampleStreamReader.", NinjaTrader.Cbi.LogLevel.Error);
                                        throw;
                                    }
                                    
                                    // Close SteamWriter
                                    sw.Close();
                                }
                      Regards,

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by rajendrasubedi2023, Today, 09:50 AM
                      2 responses
                      14 views
                      0 likes
                      Last Post rajendrasubedi2023  
                      Started by geddyisodin, Today, 05:20 AM
                      4 responses
                      29 views
                      0 likes
                      Last Post geddyisodin  
                      Started by geotrades1, Today, 10:02 AM
                      2 responses
                      8 views
                      0 likes
                      Last Post geotrades1  
                      Started by ender_wiggum, Today, 09:50 AM
                      1 response
                      5 views
                      0 likes
                      Last Post NinjaTrader_Gaby  
                      Started by bmartz, Today, 09:30 AM
                      1 response
                      10 views
                      0 likes
                      Last Post NinjaTrader_Erick  
                      Working...
                      X