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.
      Support for the development of custom automated trading strategies using NinjaScript.

      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:

        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 funk10101, Today, 12:02 AM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_LuisH  
          Started by GLFX005, Today, 03:23 AM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_Erick  
          Started by nandhumca, Yesterday, 03:41 PM
          1 response
          12 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by The_Sec, Yesterday, 03:37 PM
          1 response
          11 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by vecnopus, Today, 06:15 AM
          0 responses
          1 view
          0 likes
          Last Post vecnopus  
          Working...
          X