Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to Read from Historical File

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

    How to Read from Historical File

    Hello:
    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..

    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>
        /// 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
        }
    }
    Code:
    #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
        }
    }

    #2
    Hello tradenj,

    This maybe due to the fact that you are calling a new instance of StreamWriter inside of the Initialize() method of Support2.

    You may not want to do this as Initialize() can be called any time you bring up a NinjaScript menu item.

    You may want to place this inside of OnBarUpdate() so that it happens only once when OnBarUpdate() is forming. Here are a few references that you may view at the following link:

    StreamWriter: http://www.ninjatrader.com/support/f...ead.php?t=3475
    JCNinjaTrader Customer Service

    Comment


      #3
      Hello N T_JC:

      Yes, this produced some printing to the Output window. Thanks for the tip and for the file references. -Will be studying these further.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      620 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      359 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      562 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      566 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X