Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Reading file in array

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

    Reading file in array

    Hi all,

    I'm trying to read dates from a txt file and put any single date in an array when opening the indicator.

    This code OnStartUp should read the file and put dates into array:
    Code:
    protected override void OnStartUp()
    		{
    		if (File.Exists(path))
    			{
    				
    				/* The try-catch block is used for error handling.
    				In this case it is used to ensure you only have one stream object operating on the same file at any given moment. */
    				try
    				{
    					// Sets the file the StreamReader will read from
    					sr = new System.IO.StreamReader(path);
    	
    					string line;
    					
    					// Read lines 
    					while ((line = sr.ReadLine()) != null) 
    					{
    						
    						string [] arrayDate = line.Split(new Char[] {' '});
    						
    						
    						int Counter = 0;
    						foreach(string s in arrayDate)
    						{
    						 //put dates into array
    						 DateTime dt = DateTime.ParseExact(s, "dd/MM/yyyy",provider);
    						
    						 arrayDateA[Counter] = new DateTime(dt.Year,dt.Month,dt.Day);	
    						 Print(dt.ToString()+"---"+Counter.ToString()+"---"+arrayDateA[Counter].ToString()+"---"+dt.Year.ToString());
    							Counter++;
    						}
    					
    						
    					}
    					sr.Close();
    				}
    				
    				catch (Exception e)
    				{
    					// Outputs the error to the log
    					Log("You cannot write and read from the same file at the same time. Please remove SampleStreamWriter.", NinjaTrader.Cbi.LogLevel.Error);
    					Print(e.ToString());
    					throw;
    				}
    					
    						
    					// If file does not exist, let the user know
    			}
    			else
    			{	
    							
    				    Print("File does not exist.");
    			}
    						
    			
    		}
    in the output window I can correctly visualize the dates, but when I want to use the array in the OnBarUpdate for further use, it seems to be not available.

    Code:
    protected override void OnBarUpdate()
            {
            if (CurrentBar < 1) return;
    		Print(arrayDateA[1].ToString());
            }
    Could you please put me to the right way?

    Thanks.

    #2
    What do you mean you can visualize the dates? Do you mean your Print(arrayDateA[1]) works as expected?

    How do they seem to be not available? How are you determining this? Are you getting errors? Or does it not give you the whole date? For example, only gives you the first character of the date string? Else?

    Would you be able to provide the file you're reading from?
    MatthewNinjaTrader Product Management

    Comment


      #3
      Hi Mattew. What I mean is that on the Output print I can see the single dates like this:

      18/09/2006 00:00:00---0---18/09/2006 00:00:00---2006
      19/03/2013 00:00:00---0---19/03/2013 00:00:00---2013
      24/04/2013 00:00:00---0---24/04/2013 00:00:00---2013
      15/05/2013 00:00:00---0---15/05/2013 00:00:00---2013
      17/06/2013 00:00:00---0---17/06/2013 00:00:00---2013
      19/07/2013 00:00:00---0---19/07/2013 00:00:00---2013
      20/08/2013 00:00:00---0---20/08/2013 00:00:00---2013

      "This is what is printed from the OnStartUp"

      01/01/0001 00:00:00
      01/01/0001 00:00:00
      01/01/0001 00:00:00
      01/01/0001 00:00:00
      01/01/0001 00:00:00
      01/01/0001 00:00:00
      01/01/0001 00:00:00
      01/01/0001 00:00:00
      01/01/0001 00:00:00
      .............................

      "This is what is printed from the OnBarUpdate"


      What I'd like to do is to use the created array in the indicator. As you can see the array seems to be populated (the first 7 rows of the output printed), but when I recall it from the OnBarUpdate, it looks like empty.

      I attached the txt file but I think should be an array code trouble, because the file has been read correctly.

      Thanks.
      Attached Files
      Last edited by MAX; 08-07-2013, 03:30 PM.

      Comment


        #4
        Hi,

        Thanks for that.

        What type of array are you using? Are you using the DateTimeSeries class? If so, this will attempt to assign a value for each bar on the chart. As your txt file only contains a few lines, you will get those "01/01/0001 00:00:00" values on the bar series after the end of your array.

        Depending on exactly how you plan on using this, you may want to store these values in a List rather than the DateTimeSeries class.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Yes, I'm using the DateTime class.

          My plan for using it is to store a list of dates when the indicator starts, then, on each bar, I want to check if the bar date matches one in the array, for plotting a vertical line on that bar.

          I've already done it without using an external file, just declaring the array in the Initialize:

          Code:
          private DateTime [] arrayDate = new DateTime[10];
          protected override void Initialize()
           {
                    	
                      Overlay	= false;		
          			
          	arrayDate[0] = new DateTime(2006,9,18);
          	arrayDate[1] = new DateTime(2013,3,19);
          	arrayDate[2] = new DateTime(2013,4,24);
          	arrayDate[3] = new DateTime(2013,5,15);
          	arrayDate[4] = new DateTime(2013,6,17);
          	arrayDate[5] = new DateTime(2013,7,19);
          	arrayDate[6] = new DateTime(2013,8,20);			
          			
          			
             }
          on each bar the code runs the declared array, searching for the date, and does perfectly the job.

          I'd like just to mimic the same behaviour with an external file, because I've got a lot of dates for different markets and is easier to readdress a file than modifying manually the whole array.

          Hope this clarifies my target.

          Thanks

          Comment


            #6
            Thanks for the clarification.

            I think you're going to be better off using a list as you'll end up with several DateTime objects that do not contain a list of dates, depending on the series you're running on.

            Code:
            	private List<DateTime> dateList = new List<DateTime>();
            	
                    foreach (string s in arrayDate)
            	{
            							
            	DateTime dt = DateTime.ParseExact(s, "dd/MM/yyyy", provider);
            							
            	dateList.Add(dt);
                    }

            Also, this can get a little bit tricky because the DateTime bar value is going to change depending on the time frame and session template you're using, so what I've done here is tried to match the Time[0].Day/Month/Year to the list to avoid having to account for the session time.

            Code:
                    protected override void OnBarUpdate()
                    {
            						
            			foreach(DateTime d in dateList)
            			{
            				
            					
            				if(Time[0].Day == d.Day && Time[0].Month == d.Month && Time[0].Year == d.Year)
            				{
            					Print("Found Date " + d);
            				}
            			}
            			
            		}
            MatthewNinjaTrader Product Management

            Comment


              #7
              Thanks Matthews. It works. I've never used List Class before, but this is a great hint.

              Comment


                #8
                Matthew, just a further hint.

                If I'd like to create a new variable inside the loop (let's say the difference from the actual bar and the ones in the list, expressed in number of days).Something like:
                Code:
                foreach(DateTime d in dateList)
                {				
                barDate = new DateTime(Time[0].Year,Time[0].Month,Time[0].Day);
                expDate = new DateTime(d.Year,d.Month,d.Day);
                TimeSpan dd = expDate - barDate;
                int findDays = dd.Days;
                }
                Now I have, for each date in the list, the difference with the actual bar date.
                Let's say I want to find the second difference (the second date in the list and the actual date), how can I loop through the list. Do I have to create a new one?
                In case, have I to create it outside the first loop?

                Thanks.

                Comment


                  #9
                  I'm not quite sure I follow. Can you elaborate on what you mean by the second difference?

                  I believe you will have to create a new list and then .Add() those members to the list. Then a second for loop would need to be created to find these days.
                  MatthewNinjaTrader Product Management

                  Comment


                    #10
                    I tried something like:

                    Code:
                    protected override void OnBarUpdate()
                    {
                    
                    foreach(DateTime d in dateList)
                    			{
                    				
                    				barDate = new DateTime(Time[0].Year,Time[0].Month,Time[0].Day);
                    				expDate = new DateTime(d.Year,d.Month,d.Day);
                    				TimeSpan dd = expDate - barDate;
                    				int findDays = dd.Days;
                    					dayList.Add(findDays);
                    					for (int i = 0; i < dayList.Count -1 ; i++)
                    					{
                    					Print(Time[0].Date + " "+barDate + " "+expDate+ " "+dayList[i].ToString());
                    				   Print("---------------");
                    					}
                    				
                    				
                    				
                    				if(Time[0].Day == d.Day && Time[0].Month == d.Month && Time[0].Year == d.Year)
                    				{
                    					DrawVerticalLine("test" + CurrentBar,0, Color.DarkGreen,DashStyle.Dot,3);
                    					Print("Found Date " + d);
                    				}
                    				
                    				
                    			}
                    {
                    but NT freezes.
                    I need to step into the new list (dayList) on each new bar to check for values.

                    Thanks.

                    Comment


                      #11
                      If NT is freezing there is likely an infinite loop that's occurring. The other possibility is that the loop is just taking a long time to process and may only appear to freeze.

                      Begin by simplifying this loop so that we can isolate what is causing this hangup.

                      If you remove this

                      for (int i = 0; i < dayList.Count -1 ; i++)
                      {
                      Print(Time[0].Date + " "+barDate + " "+expDate+ " "+dayList[i].ToString());
                      Print("---------------");
                      }

                      Does it still freeze?
                      LanceNinjaTrader Customer Service

                      Comment


                        #12
                        It doesn't.

                        Comment


                          #13
                          In that case you'll want to begin examining the loop

                          When you print out

                          Print(dayList.Count);

                          what number do you get?

                          Keep in mind that for each item in your dateList it's going to be running this loop
                          LanceNinjaTrader Customer Service

                          Comment


                            #14
                            7336 but I suspect this is the number of bars while I should have the number of list members (7 in this case).

                            Comment


                              #15
                              Where are you defining dayList?

                              is it a DataSeries or a list?
                              LanceNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by strategist007, Today, 07:51 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,967 views
                              3 likes
                              Last Post jhudas88  
                              Started by rbeckmann05, Today, 06:48 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post rbeckmann05  
                              Started by rhyminkevin, Today, 04:58 PM
                              4 responses
                              55 views
                              0 likes
                              Last Post dp8282
                              by dp8282
                               
                              Started by iceman2018, Today, 05:07 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post iceman2018  
                              Working...
                              X