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.
#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 } }
Comment