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?
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!
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
NinjaTrader
Foreach loop causing trouble?
Collapse
X
-
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:Tags: None
-
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
-
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
-
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
-
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by maybeimnotrader, Yesterday, 05:46 PM
|
2 responses
20 views
0 likes
|
Last Post Today, 12:20 PM | ||
Started by adeelshahzad, Today, 03:54 AM
|
5 responses
32 views
0 likes
|
Last Post Today, 11:29 AM | ||
Started by stafe, 04-15-2024, 08:34 PM
|
7 responses
32 views
0 likes
|
Last Post Today, 11:19 AM | ||
Started by merzo, 06-25-2023, 02:19 AM
|
10 responses
823 views
1 like
|
Last Post Today, 10:38 AM | ||
Started by frankthearm, Today, 09:08 AM
|
5 responses
22 views
0 likes
|
Last Post Today, 10:19 AM |
Comment