I'm having trouble reading data from an Historical text file. This file, "Support.txt", contains the date and high for each bar and is created successfully by executing "Support2" (first strategy listed below) on say a daily NQ chart. "Support4" (the second strategy listed below) is then run on the same daily chart to read "Support.txt" (into 2 arrays), print the array values to the Output window, and to BackColor certain chart bars. Instead of doing this however the following things happen when "Sipport4" is run: nothing appears in the Output window, no BackColoring takes palce, and "Support.txt" becomes an empty file.
This code by the way is for the purpose of studying static, EOD charts, i.e. its not for real-time, streaming applications.
Your inputs are greatly appreciated..
#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> /// Enter the description of your strategy here /// </summary> [Description("Enter the description of your strategy here")] public class Support2 : Strategy { #region Variables // Wizard generated variables // User defined variables (add any user defined variables below) #endregion /** ***************************************** */ // Create new text file public void ExternalFile_Create(){ using (FileStream stream = File.Open(Cbi.Core.UserDataDir.ToString() + "Support.txt", FileMode.Create)) using (StreamWriter writer = new StreamWriter(stream)) { } } /** ***************************************** */ // Write strings to file public void PrintStatsToExternalFile(){ using (StreamWriter writer = new StreamWriter(Cbi.Core.UserDataDir.ToString() + "Support.txt", true)) { int SomeInt=25; writer.WriteLine(ToDay(Time[0])); writer.WriteLine(Convert.ToString(Highs[0])); } } /// <summary> /// This method is used to configure the strategy and is called once before any strategy method is called. /// </summary> protected override void Initialize() { CalculateOnBarClose = true; ExternalFile_Create(); } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { PrintStatsToExternalFile(); } #region Properties #endregion } }
#region Using declarations using System; using System.IO; 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> /// Enter the description of your strategy here /// </summary> [Description("Enter the description of your strategy here")] public class Support4 : Strategy { #region Variables // Wizard generated variables // User defined variables (add any user defined variables below) private string path = Cbi.Core.UserDataDir.ToString() + "Support.txt"; public int index = 0; public int[] DateInts = new int[100]; public double[] Dbls = new double[100]; #endregion /** ***************************************** */ public void ReadExternalDataFileIntoArrayAndPrintToOutput() { StreamReader fh = new StreamReader (path); string s; int Counter=0; while ((s=fh.ReadLine())!=null) { index++; switch (Counter++) { case 1: DateInts[index] = Convert.ToInt32(s); Print(" Dates["+index+"] = "+DateInts[index]); break; case 2: Dbls[index] = Convert.ToDouble(s); Print(" Dbls["+index+"] = "+Dbls[index]); break; default: break; } if (Counter == 2) Counter = 0; } fh.Close(); } /** ***************************************** */ /// <summary> /// This method is used to configure the strategy and is called once before any strategy method is called. /// </summary> protected override void Initialize() { CalculateOnBarClose = true; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { ReadExternalDataFileIntoArrayAndPrintToOutput(); //* Read external data into array only once */ } #region Properties #endregion } }
Comment