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

  • NinjaTrader_Bertrand
    replied
    suprsnipes, this is specified to use the user data directory -

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

    Leave a comment:


  • suprsnipes
    replied
    With regards to this code where do you place the path location for the txt document?

    Leave a comment:


  • rrover
    replied
    Ah.. perfect. Thank you very much!

    Leave a comment:


  • NinjaTrader_JoshP
    replied
    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.

    Leave a comment:


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

    Leave a comment:


  • NinjaTrader_JoshP
    replied
    What does your text file look like?

    Leave a comment:


  • rrover
    replied
    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

    Leave a comment:


  • NinjaTrader_JoshP
    replied
    Post up your whole code please. Thanks.

    Leave a comment:


  • rrover
    replied
    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.

    Leave a comment:


  • NinjaTrader_JoshP
    replied
    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.

    Leave a comment:


  • rrover
    started a topic Foreach loop causing trouble?

    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?

Latest Posts

Collapse

Topics Statistics Last Post
Started by r68cervera, Today, 05:29 AM
0 responses
2 views
0 likes
Last Post r68cervera  
Started by geddyisodin, Today, 05:20 AM
0 responses
3 views
0 likes
Last Post geddyisodin  
Started by JonesJoker, 04-22-2024, 12:23 PM
6 responses
33 views
0 likes
Last Post JonesJoker  
Started by GussJ, 03-04-2020, 03:11 PM
12 responses
3,239 views
0 likes
Last Post Leafcutter  
Started by AveryFlynn, Today, 04:57 AM
0 responses
6 views
0 likes
Last Post AveryFlynn  
Working...
X