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

DrawRegion draw out of the DataSeries area

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

    DrawRegion draw out of the DataSeries area

    I try to modify the CCI indicator to display more information. I add colors to the line, and DrawRegion to the areas over 100 and 200. The outcome looks a bit off the lines.

    Your help is welcome..

    Code:
    // 
    // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    
    #region Using declarations
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.ComponentModel;
    using System.Xml.Serialization;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion
    
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    	/// <summary>
    	/// The Commodity Channel Index (bdtCCI) measures the variation of a security's price from its statistical mean. High values show that prices are unusually high compared to average prices whereas low values indicate that prices are unusually low.
    	/// </summary>
    	[Description("The Commodity Channel Index (bdtCCI) measures the variation of a security's price from its statistical mean. High values show that prices are unusually high compared to average prices whereas low values indicate that prices are unusually low.")]
    	public class bdtCCI : Indicator
    	{
    		#region Variables
    			private int		period		= 14;
    			private Color	upColor 	= Color.Green;
    			private Color 	downColor	= Color.Red;
    			private Color	stallColor 	= Color.Black;
     			private double 	prevValue	 = 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()
    		{
    			Add(new Plot(Color.Orange, PlotStyle.Line, "CCI"));
    			Add(new Line(Color.DarkGray, 200, "Level 2"));
    			Add(new Line(Color.DarkGray, 100, "Level 1"));
    			Add(new Line(Color.DarkGray, 0, "Zero line"));
    			Add(new Line(Color.DarkGray, -100, "Level -1"));
    			Add(new Line(Color.DarkGray, -200, "Level -2"));
    			
    			DrawOnPricePanel = false;
    
    		}
    
    		/// <summary>
    		/// Calculates the indicator value(s) at the current index.
    		/// </summary>
    		protected override void OnBarUpdate()
    		{
    			if (CurrentBar == 0)
    				Value.Set(0);
    			else
    			{
    				double mean = 0;
    				for (int idx = Math.Min(CurrentBar, Period - 1); idx >= 0; idx--)
    					mean += Math.Abs(Typical[idx] - SMA(Typical, Period)[0]);
    				double plotValue = (Typical[0] - SMA(Typical, Period)[0]) / (mean == 0 ? 1 : (0.015 * (mean / Math.Min(Period, CurrentBar + 1))));
    				if (plotValue >= 0)
    				{
    					PlotColors[0][0] = (plotValue >= prevValue ? (plotValue == prevValue ? stallColor: upColor) : downColor);
    				}
    				else
    				{
    					PlotColors[0][0] = plotValue <= prevValue ? (plotValue == prevValue ? stallColor: downColor) : upColor;
    				}
    				Value.Set(plotValue);
    				if (plotValue >= Lines[1].Value) //100
    					DrawRegion("Overbought1-" + CurrentBar, 1, 0, Values[0], Lines[1].Value, upColor, upColor, 2);
    				if (plotValue >= Lines[0].Value) //200
    					DrawRegion("Overbought2-" + CurrentBar, 1, 0, Values[0], Lines[0].Value, upColor, upColor, 5);
    				if (plotValue <= Lines[3].Value) //-100
    					DrawRegion("Oversold1-" + CurrentBar, 1, 0, Values[0], Lines[3].Value, downColor, downColor, 2);
    				if (plotValue <= Lines[4].Value) //-200
    					DrawRegion("Oversold2-" + CurrentBar, 1, 0, Values[0], Lines[4].Value, downColor, downColor, 5);
    				prevValue = plotValue;
    			}
    		}
    
    		#region Properties
    		/// <summary>
    		/// </summary>
    		[Description("Numbers of bars used for calculations")]
    		[GridCategory("Parameters")]
    		public int Period
    		{
    			get { return period; }
    			set { period = 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 bdtCCI[] cachebdtCCI = null;
    
            private static bdtCCI checkbdtCCI = new bdtCCI();
    
            /// <summary>
            /// The Commodity Channel Index (bdtCCI) measures the variation of a security's price from its statistical mean. High values show that prices are unusually high compared to average prices whereas low values indicate that prices are unusually low.
            /// </summary>
            /// <returns></returns>
            public bdtCCI bdtCCI(int period)
            {
                return bdtCCI(Input, period);
            }
    
            /// <summary>
            /// The Commodity Channel Index (bdtCCI) measures the variation of a security's price from its statistical mean. High values show that prices are unusually high compared to average prices whereas low values indicate that prices are unusually low.
            /// </summary>
            /// <returns></returns>
            public bdtCCI bdtCCI(Data.IDataSeries input, int period)
            {
                if (cachebdtCCI != null)
                    for (int idx = 0; idx < cachebdtCCI.Length; idx++)
                        if (cachebdtCCI[idx].Period == period && cachebdtCCI[idx].EqualsInput(input))
                            return cachebdtCCI[idx];
    
                lock (checkbdtCCI)
                {
                    checkbdtCCI.Period = period;
                    period = checkbdtCCI.Period;
    
                    if (cachebdtCCI != null)
                        for (int idx = 0; idx < cachebdtCCI.Length; idx++)
                            if (cachebdtCCI[idx].Period == period && cachebdtCCI[idx].EqualsInput(input))
                                return cachebdtCCI[idx];
    
                    bdtCCI indicator = new bdtCCI();
                    indicator.BarsRequired = BarsRequired;
                    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
                    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
                    indicator.Input = input;
                    indicator.Period = period;
                    Indicators.Add(indicator);
                    indicator.SetUp();
    
                    bdtCCI[] tmp = new bdtCCI[cachebdtCCI == null ? 1 : cachebdtCCI.Length + 1];
                    if (cachebdtCCI != null)
                        cachebdtCCI.CopyTo(tmp, 0);
                    tmp[tmp.Length - 1] = indicator;
                    cachebdtCCI = tmp;
                    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>
            /// The Commodity Channel Index (bdtCCI) measures the variation of a security's price from its statistical mean. High values show that prices are unusually high compared to average prices whereas low values indicate that prices are unusually low.
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.bdtCCI bdtCCI(int period)
            {
                return _indicator.bdtCCI(Input, period);
            }
    
            /// <summary>
            /// The Commodity Channel Index (bdtCCI) measures the variation of a security's price from its statistical mean. High values show that prices are unusually high compared to average prices whereas low values indicate that prices are unusually low.
            /// </summary>
            /// <returns></returns>
            public Indicator.bdtCCI bdtCCI(Data.IDataSeries input, int period)
            {
                return _indicator.bdtCCI(input, period);
            }
        }
    }
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        public partial class Strategy : StrategyBase
        {
            /// <summary>
            /// The Commodity Channel Index (bdtCCI) measures the variation of a security's price from its statistical mean. High values show that prices are unusually high compared to average prices whereas low values indicate that prices are unusually low.
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.bdtCCI bdtCCI(int period)
            {
                return _indicator.bdtCCI(Input, period);
            }
    
            /// <summary>
            /// The Commodity Channel Index (bdtCCI) measures the variation of a security's price from its statistical mean. High values show that prices are unusually high compared to average prices whereas low values indicate that prices are unusually low.
            /// </summary>
            /// <returns></returns>
            public Indicator.bdtCCI bdtCCI(Data.IDataSeries input, int period)
            {
                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.bdtCCI(input, period);
            }
        }
    }
    #endregion
    Attached Files

    #2
    Hi Shai, it looks to be doing exactly what you've coded to do - having the DrawRegion edges as your horizontal line value set?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks Bertrand.

      Yes, it almost does it perfectly. The problem is with the first and the last bars, which visually are out of the area (see the image). Is there a solution for this?
      Attached Files

      Comment


        #4
        According to the resolution you have it draws those correctly as well, for the bar your condition triggered from the series to the linevalue. What it cannot do is split up the bar for example to plot 2/3 rd's - which would be needed here.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          What it cannot do is split up the bar for example to plot 2/3 rd's - which would be needed here.
          Thanks Bertrand.

          What I expect in this situation, when using the a y line as a border is to be able to specify, whether to draw above or below the line. If this was added as an input parameter, then with drawing also the lastValue I can get it perfect.

          Can this be added?

          Code:
          				Value.Set(plotValue);
          				if (CurrentBar > 0)
          				{
          					double lastValue = Values[0][1];
          					if (plotValue >= Lines[1].Value || lastValue >= Lines[1].Value) //100
          						DrawRegion("Overbought1-" + CurrentBar, 1, 0, Values[0], Lines[1].Value, upColor, upColor, 5);
          					if (plotValue >= Lines[0].Value || lastValue >= Lines[0].Value) //200
          						DrawRegion("Overbought2-" + CurrentBar, 1, 0, Values[0], Lines[0].Value, upColor, upColor, 5);
          					
          					if (plotValue <= Lines[3].Value || lastValue <= Lines[3].Value) //-100
          						DrawRegion("Oversold1-" + CurrentBar, 1, 0, Values[0], Lines[3].Value, downColor, downColor, 2);
          					if (plotValue <= Lines[4].Value || lastValue <= Lines[4].Value) //-200
          						DrawRegion("Oversold2-" + CurrentBar, 1, 0, Values[0], Lines[4].Value, downColor, downColor, 5);
          				}
          Attached Files

          Comment


            #6
            Thanks for sharing that thought I will add this to our feedback trackings in product management so it can be considered going forward. Another yet more complicated and undocumented approach would be through custom plotting via C# and thus an overridden plot method, here can directly draw x/y screen location specified (check into the CustomPlotExample that's preinstalled with NT).
            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by algospoke, 04-17-2024, 06:40 PM
            6 responses
            49 views
            0 likes
            Last Post algospoke  
            Started by arvidvanstaey, Today, 02:19 PM
            4 responses
            11 views
            0 likes
            Last Post arvidvanstaey  
            Started by samish18, 04-17-2024, 08:57 AM
            16 responses
            61 views
            0 likes
            Last Post samish18  
            Started by jordanq2, Today, 03:10 PM
            2 responses
            11 views
            0 likes
            Last Post jordanq2  
            Started by traderqz, Today, 12:06 AM
            10 responses
            21 views
            0 likes
            Last Post traderqz  
            Working...
            X