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

Working code to Read Text but I want to move to the next stage

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

    Working code to Read Text but I want to move to the next stage

    Hi,

    I need a little bit of help with the following code. The code is reading a text file and finds the next highest value when referenced against the private int num = 4800; It works as expected.

    Code:
        [Description("Displays Levels")]
        public class MyCustomIndicator2 : Indicator
        {
            #region Variables
            // Wizard generated variables
            // User defined variables (add any user defined variables below)
            private string path = Cbi.Core.UserDataDir.ToString() + "PIn.txt";
            private int[] levels;
            private int count = 0;
            private string readText = "";
            
            private int num = 4800;
               private    int tmp, res = 0;
               private    int? diff = null;
            
            #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[1000];
            }
    
            /// <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("C:\\PIn.txt");
                    
                string [] split = readText.Split(new Char [] {';'});
                    foreach (string s in split)
                    {
                        count++;
                        tmp = int.Parse(s);
                        levels[count] = tmp;
                        
                        if (tmp > num)
                        {
                            if(diff == null)
                                diff = num;
                            if ((tmp - num) < diff)
                            {
                                res = tmp;
                                diff = tmp - num;
                            }
                        }
                    }
                    Print(res);
            }
    
            #region Properties
    
            #endregion
        }
    }
    All I want to do is now is instead of using the private int num = 4800;
    I want to use the Close[0] but I am getting the following error on lines 67 & 71;

    Cannot implicitly convert type 'double' to 'int?'. An explicit conversion exists (are you missing a cast?)

    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 MyCustomIndicator2 : Indicator
        {
            #region Variables
            // Wizard generated variables
            // User defined variables (add any user defined variables below)
            private string path = Cbi.Core.UserDataDir.ToString() + "PIn.txt";
            private int[] levels;
            private int count = 0;
            private string readText = "";
            
               private    int tmp, res = 0;
               private    int? diff = null;
            
            #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[1000];
            }
    
            /// <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("C:\\PIn.txt");
                    
                string [] split = readText.Split(new Char [] {';'});
                    foreach (string s in split)
                    {
                        count++;
                        tmp = int.Parse(s);
                        levels[count] = tmp;
                        
                        if (tmp > Close[0])
                        {
                            if(diff == null)
                                diff = Close[0];
                            if ((tmp - Close[0]) < diff)
                            {
                                res = tmp;
                                diff = tmp - Close[0];
                            }
                        }
                    }
                    Print(res);
            }
    
            #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 MyCustomIndicator2[] cacheMyCustomIndicator2 = null;
    
            private static MyCustomIndicator2 checkMyCustomIndicator2 = new MyCustomIndicator2();
    
            /// <summary>
            /// Displays Levels
            /// </summary>
            /// <returns></returns>
            public MyCustomIndicator2 MyCustomIndicator2()
            {
                return MyCustomIndicator2(Input);
            }
    
            /// <summary>
            /// Displays Levels
            /// </summary>
            /// <returns></returns>
            public MyCustomIndicator2 MyCustomIndicator2(Data.IDataSeries input)
            {
    
                if (cacheMyCustomIndicator2 != null)
                    for (int idx = 0; idx < cacheMyCustomIndicator2.Length; idx++)
                        if (cacheMyCustomIndicator2[idx].EqualsInput(input))
                            return cacheMyCustomIndicator2[idx];
    
                MyCustomIndicator2 indicator = new MyCustomIndicator2();
                indicator.BarsRequired = BarsRequired;
                indicator.CalculateOnBarClose = CalculateOnBarClose;
                indicator.Input = input;
                indicator.SetUp();
    
                MyCustomIndicator2[] tmp = new MyCustomIndicator2[cacheMyCustomIndicator2 == null ? 1 : cacheMyCustomIndicator2.Length + 1];
                if (cacheMyCustomIndicator2 != null)
                    cacheMyCustomIndicator2.CopyTo(tmp, 0);
                tmp[tmp.Length - 1] = indicator;
                cacheMyCustomIndicator2 = 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 Levels
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.MyCustomIndicator2 MyCustomIndicator2()
            {
                return _indicator.MyCustomIndicator2(Input);
            }
    
            /// <summary>
            /// Displays Levels
            /// </summary>
            /// <returns></returns>
            public Indicator.MyCustomIndicator2 MyCustomIndicator2(Data.IDataSeries input)
            {
                return _indicator.MyCustomIndicator2(input);
            }
    
        }
    }
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        public partial class Strategy : StrategyBase
        {
            /// <summary>
            /// Displays Levels
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.MyCustomIndicator2 MyCustomIndicator2()
            {
                return _indicator.MyCustomIndicator2(Input);
            }
    
            /// <summary>
            /// Displays Levels
            /// </summary>
            /// <returns></returns>
            public Indicator.MyCustomIndicator2 MyCustomIndicator2(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.MyCustomIndicator2(input);
            }
    
        }
    }
    #endregion
    Appreciate the help. Kind Regards,
    suprsnipes
    Last edited by suprsnipes; 03-09-2010, 04:29 AM.

    #2
    Your tmp variable is an integer you're attempting to compare to the double value Close[0], this will throw an error when you attempt to compile it. You could try casting it, or declaring tmp as double and then parsing double values into it later on...
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertand,

      Can you please provide me an example (if it's not too much trouble)?

      Regards,
      suprsnipes

      Comment


        #4
        As a start for example, see the private int declaration you have for tmp > define it as double, private double tmp instead.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          I have made some changes to the code as follows and the output screen shows that the text file is being read correctly, except it is rounding down...I thought I had the rounding up issue resolved yet the Math.Ceiling is not working.

          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 MyCustomIndicator2 : Indicator
              {
                  #region Variables
                  // Wizard generated variables
                  // User defined variables (add any user defined variables below)
                  private string path = Cbi.Core.UserDataDir.ToString() + "PIn.txt";
                  private int[] levels;
                  private int count = 0;
                  private string readText = "";
                  
                  private int roundedValue;
                  private    int diff = int.MaxValue;
                     private    int tmp, res = 0;
                     
                  
                  #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("C:\\PIn.txt");
                      
                      int roundedValue = ((int)Math.Ceiling(Close[0])); 
                      
                      string [] split = readText.Split(new Char [] {';'});
                          foreach (string s in split)
                          {
                              count++;
                              tmp = int.Parse(s);
                              levels[count] = tmp;
                              
                              if (tmp > roundedValue)
                              {
                                  if(diff == null)
                                      diff = roundedValue;
                                  if ((tmp - roundedValue) < diff)
                                  {
                                      res = tmp;
                                      diff = tmp - roundedValue;
                                  }
                              }
                          }
                          Print(res);
                  }
          
                  #region Properties
          
                  #endregion
              }
          }
          Thanks in advance to anyone who can provide assistance.

          Comment


            #6
            Have you printed out your roundedValue to check into? A quick test for me here worked out as expected rounding up.
            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by bortz, 11-06-2023, 08:04 AM
            47 responses
            1,610 views
            0 likes
            Last Post aligator  
            Started by jaybedreamin, Today, 05:56 PM
            0 responses
            9 views
            0 likes
            Last Post jaybedreamin  
            Started by DJ888, 04-16-2024, 06:09 PM
            6 responses
            19 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by Jon17, Today, 04:33 PM
            0 responses
            6 views
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            16 views
            0 likes
            Last Post Javierw.ok  
            Working...
            X