• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Where to open and close an output file.

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

    Where to open and close an output file.

    Where should I put the code to open and then close a file? Putting it in Initialise() and dispose() does not work as below demonstrates...

    The file is created when initialise() runs upon selection of "strategy analyser" -> "right-click on an instrument to bring up the backtest dialogue".

    But that appears to lock, and never released, the file. When the strategy is run by clicking "okay" button initialise() gets called again, but the file is still locked and the error is output:

    "Strategy,Failed to call method 'Initialize' for strategy 'testFileOutput': The process cannot access the file 'C:\Users\mb\NToutput\outputFile.txt' because it is being used by another process."

    Could please somebody post some example code of opening a file at the start of the strategy, outputting to the file at each call to OnBarUpdate() and then closing the file when the strategy ends or suggesting what is going wrong in the code below?

    Thanks,
    Matthew.

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    
    using System.IO;
    
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Simple test of file output
        /// </summary>
        [Description("Simple test of file output")]
        public class testFileOutput : Strategy
        {
            #region Variables
            // Wizard generated variables
            // User defined variables (add any user defined variables below)
             System.IO.StreamWriter outputFile;
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
    
                string path = @"C:\\Users\\mb\\NToutput\\outputFile.txt";
                outputFile = new System.IO.StreamWriter(path);
                CalculateOnBarClose = true;
            }
            public override void Dispose()
            {
                // Clean up your resources here
                outputFile.Close();
                base.Dispose();
            }
            
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                outputFile.WriteLine(Time[0].ToString());
            }
    
            #region Properties
            #endregion
        }
    }

    #2
    Actually, the calling of Initialise() and Dispose() is a bit difficult to understand/predict...

    The program below outputs the string "testFileOutput: Init." when Init is called and "testFileOutput: Dispose." when dispose is called.

    On first run from fresh start of NT the output is:
    testFileOutput: Init.
    testFileOutput: Init.
    testFileOutput: Init.

    How come Dispose() is never called? Why is Initialse() called three times?

    On the second run the output is:
    testFileOutput: Dispose.
    testFileOutput: Dispose.
    testFileOutput: Dispose.
    testFileOutput: Init.
    testFileOutput: Init.
    testFileOutput: Init.

    This inidcates that the previous call left 3 seperate instances of the strategy object open (i.e. it did not call Dispose() ) which get destroyed. Then another fresh set of 3 instances are created.

    I don't understand the why/how etc... of that, but accept that that is how NT works. It was not what I expected though :-).

    Anyway, where should I put code that needs to do a traditional (i.e. do it once) initialisation and cleanup? Initialising only once is easy to acheive because I can have a switch such as "bool firstCall = true" which gets flicked to false after the first call [so that if(firstCall) {...} only executes once].

    But how can I tell inside of OnBarUpdate that the current call is the last call?

    Thanks,
    Matthew.

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Simple test of file output
        /// </summary>
        [Description("Simple test of file output")]
        public class testFileOutput : Strategy
        {
            #region Variables
            // Wizard generated variables
            // User defined variables (add any user defined variables below)
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                Print("testFileOutput: Init.");
                CalculateOnBarClose = true;
            }
            public override void Dispose()
            {
                Print("testFileOutput: Dispose.");
                base.Dispose();
            }
            
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
            }
    
            #region Properties
            #endregion
        }
    }

    Comment


      #3
      Hi,
      I also asked a similar question and get an answer that its impossible to know when back test ends.
      http://www.ninjatrader-support2.com/...ad.php?t=24091
      Its not correct so I give NT support another chance to answer it correctly.

      Baruch

      Comment


        #4
        I have not tried it out yet, but this might provide a solution:
        http://www.ninjatrader-support2.com/...logfile&page=3

        Comment


          #5
          Solution.

          Have tried it out now. Download the GlobalStrategy.cs code provided by MrLogik at http://www.ninjatrader-support2.com/...logfile&page=3 to do file output.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by pharofx, Today, 09:30 PM
          0 responses
          9 views
          0 likes
          Last Post pharofx
          by pharofx
           
          Started by swcooke, Today, 08:39 PM
          0 responses
          6 views
          0 likes
          Last Post swcooke
          by swcooke
           
          Started by bboyle1234, Today, 08:10 PM
          1 response
          14 views
          0 likes
          Last Post bboyle1234  
          Started by shodson, 02-11-2020, 07:26 PM
          3 responses
          21 views
          0 likes
          Last Post shodson
          by shodson
           
          Started by ksarin, Today, 04:31 PM
          0 responses
          4 views
          0 likes
          Last Post ksarin
          by ksarin
           
          Working...
          X