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

Foreach loop causing trouble?

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

    Foreach loop causing trouble?

    I'm working on an indicator that will read an external semicolon delimited text file and store the data in an array for further analysis. The following code reads the text file and stores each part in it's own part of the array, I can print the array from within the foreach loop perfectly but I can not do anything after it. I can't even get that Print("test"); to show up in the output window. Everything compiles and runs without error. Any ideas?

    Code:
    readText = File.ReadAllText(path);
    				
    string [] split = readText.Split(new Char [] {';'});		
    	foreach (string s in split)
    		{
    		count++;
    		myarray[count] = int.Parse(s);
                    Print(myarray[count]); //I can print my array here perfectly
    		}
    Print("test"); //but I can't print anything down here?

    #2
    Try using a try-catch block and see if you are able to catch an exception: http://www.ninjatrader-support.com/v...ead.php?t=9825

    Here is the reference sample for reading from a file: http://www.ninjatrader-support.com/v...ead.php?t=3476 May or may not be useful. You might have advanced ahead of the sample's level of complexity.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      The error I get is:
      Error on calling the 'OnBarUpdate' method for indicator 'MyIndicator' on bar 0: Input string was not in a correct format.

      If I remove the code that is in the foreach loop, I don't get the error. But what confuses me is that I can print out that array from within the loop and it prints out perfectly so everything is getting into it correctly.

      When using a try-catch, I get an error for each string that it is reading from the text file.
      Last edited by rrover; 09-10-2008, 08:58 AM.

      Comment


        #4
        Post up your whole code please. Thanks.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          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.Gui.Chart;
          using System.IO;
          #endregion
          
          // This namespace holds all indicators and is required. Do not change it.
          namespace NinjaTrader.Indicator
          {
              /// <summary>
              /// Displays Levels
              /// </summary>
              [Description("Displays Levels")]
              public class DailyLevels : Indicator
              {
                  #region Variables
                  // Wizard generated variables
                  // User defined variables (add any user defined variables below)
          		private string path = Cbi.Core.UserDataDir.ToString() + "levels.txt";
          		private int[] levels;
          		private int count = 0;
          		private string readText = "";
                  #endregion
          
                  /// <summary>
                  /// This method is used to configure the indicator and is called once before any bar data is loaded.
                  /// </summary>
                  protected override void Initialize()
                  {
                      CalculateOnBarClose	= true;
                      Overlay				= true;
                      PriceTypeSupported	= false;	
          			
          			levels = new int[100000];
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                      // Use this method for calculating your indicator values. Assign a value to each
                      // plot below by replacing 'Close[0]' with your own formula.
          
          			readText = File.ReadAllText(path);
          				
          			string [] split = readText.Split(new Char [] {';'});	
          				foreach (string s in split)
          				{
          					count++;
          					levels[count] = int.Parse(s);
          					//Print(levels[count]);
          				}
          				Print("test");
          
                  }
          
                  #region Properties
          
                  #endregion
              }
          }
          
          #region NinjaScript generated code. Neither change nor remove.
          // This namespace holds all indicators and is required. Do not change it.
          namespace NinjaTrader.Indicator
          {
              public partial class Indicator : IndicatorBase
              {
                  private DailyLevels[] cacheDailyLevels = null;
          
                  private static DailyLevels checkDailyLevels = new DailyLevels();
          
                  /// <summary>
                  /// Displays Levels
                  /// </summary>
                  /// <returns></returns>
                  public DailyLevels DailyLevels()
                  {
                      return DailyLevels(Input);
                  }
          
                  /// <summary>
                  /// Displays Levels
                  /// </summary>
                  /// <returns></returns>
                  public DailyLevels DailyLevels(Data.IDataSeries input)
                  {
          
                      if (cacheDailyLevels != null)
                          for (int idx = 0; idx < cacheDailyLevels.Length; idx++)
                              if (cacheDailyLevels[idx].EqualsInput(input))
                                  return cacheDailyLevels[idx];
          
                      DailyLevels indicator = new DailyLevels();
                      indicator.BarsRequired = BarsRequired;
                      indicator.CalculateOnBarClose = CalculateOnBarClose;
                      indicator.Input = input;
                      indicator.SetUp();
          
                      DailyLevels[] tmp = new DailyLevels[cacheDailyLevels == null ? 1 : cacheDailyLevels.Length + 1];
                      if (cacheDailyLevels != null)
                          cacheDailyLevels.CopyTo(tmp, 0);
                      tmp[tmp.Length - 1] = indicator;
                      cacheDailyLevels = tmp;
                      Indicators.Add(indicator);
          
                      return indicator;
                  }
          
              }
          }
          
          // This namespace holds all market analyzer column definitions and is required. Do not change it.
          namespace NinjaTrader.MarketAnalyzer
          {
              public partial class Column : ColumnBase
              {
                  /// <summary>
                  /// Displays all applicable Daily Levels
                  /// </summary>
                  /// <returns></returns>
                  [Gui.Design.WizardCondition("Indicator")]
                  public Indicator.DailyLevels DailyLevels()
                  {
                      return _indicator.DailyLevels(Input);
                  }
          
                  /// <summary>
                  /// Displays all applicable Daily Levels
                  /// </summary>
                  /// <returns></returns>
                  public Indicator.DailyLevels DailyLevels(Data.IDataSeries input)
                  {
                      return _indicator.DailyLevels(input);
                  }
          
              }
          }
          
          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
              public partial class Strategy : StrategyBase
              {
                  /// <summary>
                  /// Displays all applicable Daily Levels
                  /// </summary>
                  /// <returns></returns>
                  [Gui.Design.WizardCondition("Indicator")]
                  public Indicator.DailyLevels DailyLevels()
                  {
                      return _indicator.DailyLevels(Input);
                  }
          
                  /// <summary>
                  /// Displays Levels
                  /// </summary>
                  /// <returns></returns>
                  public Indicator.DailyLevels DailyLevels(Data.IDataSeries input)
                  {
                      if (InInitialize && input == null)
                          throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
          
                      return _indicator.DailyLevels(input);
                  }
          
              }
          }
          #endregion

          Comment


            #6
            What does your text file look like?
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Code:
              127550;126925;127150;126125;127350;126925;128975;128500;127850;126125;128075;128800;127850;126225;128775;128700;127025;127225;128850;128850;128125;128950;

              Comment


                #8
                The problem is in your text file. The very last thing in your text file is a semicolon. When it sees this it wants to make a split, but there is nothing to afterwards. Remove that semicolon and you will be good to go.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Ah.. perfect. Thank you very much!

                  Comment


                    #10
                    With regards to this code where do you place the path location for the txt document?

                    Comment


                      #11
                      suprsnipes, this is specified to use the user data directory -

                      Code:
                       
                      private string path = Cbi.Core.UserDataDir.ToString()
                      BertrandNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Christopher_R, Today, 12:29 AM
                      0 responses
                      9 views
                      0 likes
                      Last Post Christopher_R  
                      Started by sidlercom80, 10-28-2023, 08:49 AM
                      166 responses
                      2,235 views
                      0 likes
                      Last Post sidlercom80  
                      Started by thread, Yesterday, 11:58 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post thread
                      by thread
                       
                      Started by jclose, Yesterday, 09:37 PM
                      0 responses
                      8 views
                      0 likes
                      Last Post jclose
                      by jclose
                       
                      Started by WeyldFalcon, 08-07-2020, 06:13 AM
                      10 responses
                      1,415 views
                      0 likes
                      Last Post Traderontheroad  
                      Working...
                      X