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

Simple DrawRay Issue

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

    Simple DrawRay Issue

    My first attempt at NinjaScript. Trying to get a ray from the highest High of N bars. Compiles but nothing showing on the chart. Why?
    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;
    #endregion
    
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        /// <summary>
        /// Hi of Bars
        /// </summary>
        [Description("Hi of Bars")]
        public class HiRay : Indicator
        {
            #region Variables
            // Wizard generated variables
                private int periods = 10; // Default setting for Periods
            // User defined variables (add any user defined variables below)
            #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;
            }
    
            /// <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
    
                DrawRay("MyHiRay", periods, MAX(High,periods)[0], 0, MAX(High,periods)[0], Color.Blue, DashStyle.DashDot, 2);
            }
    
            #region Properties
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Plot0
            {
                get { return Values[0]; }
            }
    
            [Description("")]
            [Category("Parameters")]
            public int Periods
            {
                get { return periods; }
                set { periods = Math.Max(1, value); }
            }
            #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 HiRay[] cacheHiRay = null;
    
            private static HiRay checkHiRay = new HiRay();
    
            /// <summary>
            /// Hi of Bars
            /// </summary>
            /// <returns></returns>
            public HiRay HiRay(int periods)
            {
                return HiRay(Input, periods);
            }
    
            /// <summary>
            /// Hi of Bars
            /// </summary>
            /// <returns></returns>
            public HiRay HiRay(Data.IDataSeries input, int periods)
            {
                checkHiRay.Periods = periods;
                periods = checkHiRay.Periods;
    
                if (cacheHiRay != null)
                    for (int idx = 0; idx < cacheHiRay.Length; idx++)
                        if (cacheHiRay[idx].Periods == periods && cacheHiRay[idx].EqualsInput(input))
                            return cacheHiRay[idx];
    
                HiRay indicator = new HiRay();
                indicator.BarsRequired = BarsRequired;
                indicator.CalculateOnBarClose = CalculateOnBarClose;
                indicator.Input = input;
                indicator.Periods = periods;
                indicator.SetUp();
    
                HiRay[] tmp = new HiRay[cacheHiRay == null ? 1 : cacheHiRay.Length + 1];
                if (cacheHiRay != null)
                    cacheHiRay.CopyTo(tmp, 0);
                tmp[tmp.Length - 1] = indicator;
                cacheHiRay = 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>
            /// Hi of Bars
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.HiRay HiRay(int periods)
            {
                return _indicator.HiRay(Input, periods);
            }
    
            /// <summary>
            /// Hi of Bars
            /// </summary>
            /// <returns></returns>
            public Indicator.HiRay HiRay(Data.IDataSeries input, int periods)
            {
                return _indicator.HiRay(input, periods);
            }
    
        }
    }
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        public partial class Strategy : StrategyBase
        {
            /// <summary>
            /// Hi of Bars
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.HiRay HiRay(int periods)
            {
                return _indicator.HiRay(Input, periods);
            }
    
            /// <summary>
            /// Hi of Bars
            /// </summary>
            /// <returns></returns>
            public Indicator.HiRay HiRay(Data.IDataSeries input, int periods)
            {
                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.HiRay(input, periods);
            }
    
        }
    }
    #endregion

    #2
    Hello,

    You don't need to create a namespace etc. like the way you are doing it.

    Lets just start out drawing a ray:


    Then go from there and let me know if you have any questions. Post your code so I can see.
    DenNinjaTrader Customer Service

    Comment


      #3
      Tip 1: When developing your indicators ALWAYS get in the habit of leaving Ninja on the "Log" Tab. That way you will see the error messages when you try to place your indicators on the screen.

      You will see an error message:
      Error on calling 'OnBarUpdate' methods for indicator 'HiRay' on bar 0: HiRay.DrawRay: startBarsAgo out of valid range 0 through 0, was 10

      what that is saying is "When your chart only has 1 bar 'Bar 0' you are trying to draw a line 10 bars long." so your indicator crashed & was not used on any subsequent bar.

      It takes a while to get your head around the fact that your indicator is used when the chart only has 1 bar, then 2 bars, then 3 bars etc.

      Tip 2: Tell your indicator not to do anything untill it has sufficient bars to actually function correctly. In this case it needs "Period" number of bar in the chart or it will die.
      So in almost every indicator you should add code similar to "If (CurrentBar < 4) return;" to fix your specific issue see the 2 new lines of code below.



      protected
      overridevoid OnBarUpdate()
      {
      // Use this method for calculating your indicator values. Assign a value to each
      if (CurrentBar < periods)
      return;



      I trust this will get you started. Have fun, Ninja is extremely powerful once you get you head around it.

      Comment


        #4
        Thanks for the tips David. Issue solved.

        Comment


          #5
          Hello,

          This link may help:
          DenNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by jclose, Today, 09:37 PM
          0 responses
          3 views
          0 likes
          Last Post jclose
          by jclose
           
          Started by WeyldFalcon, 08-07-2020, 06:13 AM
          10 responses
          1,413 views
          0 likes
          Last Post Traderontheroad  
          Started by firefoxforum12, Today, 08:53 PM
          0 responses
          9 views
          0 likes
          Last Post firefoxforum12  
          Started by stafe, Today, 08:34 PM
          0 responses
          10 views
          0 likes
          Last Post stafe
          by stafe
           
          Started by sastrades, 01-31-2024, 10:19 PM
          11 responses
          169 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Working...
          X