Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Two Donchian Channel

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

    Two Donchian Channel

    Good morning. I started creating my indicator again, this time starting from 0.
    The idea is to create an indicator with the characteristics of two Donchian channels within the same indicator and to color certain spaces.
    Before moving on to the color stage, I try to make the two channel segments coexist.
    The code does not show me any errors, however it no longer works in ninjatrader.

    Could this come from the second section of the Addplot?


    Code:
    //
    // Copyright (C) 2023, 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.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    // This namespace holds indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
        /// <summary>
        /// Donchian Channel. The Donchian Channel indicator was created by Richard Donchian.
        ///  It uses the highest high and the lowest low of a period of time to plot the channel.
        /// </summary> [HR][/HR]
        public class CanalDC : Indicator
        {
            private MAX max;
            private MIN min;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                    = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionDonchianChannel;
                    Name                        = "CanalDC";
                    IsOverlay                    = true;
                    IsSuspendedWhileInactive    = true;
                    PeriodH                        = 50;
                    PeriodB                        = 20;
    
                    AddPlot(Brushes.Goldenrod,    NinjaTrader.Custom.Resource.DonchianChannelMean);
                    AddPlot(Brushes.DodgerBlue,    NinjaTrader.Custom.Resource.NinjaScriptIndicatorUpper);
                    AddPlot(Brushes.DodgerBlue,    NinjaTrader.Custom.Resource.NinjaScriptIndicatorLower);
                    
    [COLOR=#e74c3c]                AddPlot(Brushes.Goldenrod,    NinjaTrader.Custom.Resource.DonchianChannelMean);
                    AddPlot(Brushes.DodgerBlue,    NinjaTrader.Custom.Resource.NinjaScriptIndicatorUpper);
                    AddPlot(Brushes.DodgerBlue,    NinjaTrader.Custom.Resource.NinjaScriptIndicatorLower);[/COLOR]
                }
                else if (State == State.DataLoaded)
                {
                    max = MAX(High, [COLOR=#e74c3c]PeriodH[/COLOR]);
                    min    = MIN(Low, [COLOR=#e74c3c]PeriodH[/COLOR]);
                    max = MAX(High, [COLOR=#27ae60]PeriodB[/COLOR]);
                    min    = MIN(Low, [COLOR=#27ae60]PeriodB[/COLOR]);
                }
            }
    
            protected override void OnBarUpdate()
            {
                double max0 = max[0];
                double min0    = min[0];
    
                Value[0]    = (max0 + min0) / 2;
                Upper[0]    = max0;
                Lower[0]    = min0;
                
                double max1 = max[0];
                double min1    = min[0];
    
                Value[0]    = (max1 + min1) / 2;
                Upper[0]    = max1;
                Lower[0]    = min1;
            }
    
            #region Properties
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> Lower
            {
                get { return Values[2]; }
            }
    
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> Mean
            {
                get { return Values[0]; }
            }
    
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "[COLOR=#27ae60]PeriodH[/COLOR]", GroupName = "NinjaScriptParameters", Order = 0)]
            public int [COLOR=#27ae60]PeriodH[/COLOR]
            { get; set; }
    
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> Upper
            {
                get { return Values[1]; }
            }
            #endregion
            
            #region Properties
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> Lower2
            {
                get { return Values[2]; }
            }
    
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> Mean2
            {
                get { return Values[0]; }
            }
    
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "[COLOR=#27ae60]PeriodB[/COLOR]", GroupName = "NinjaScriptParameters", Order = 0)]
            public int [COLOR=#27ae60]PeriodB[/COLOR]
            { get; set; }
    
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> Upper2
            {
                get { return Values[1]; }
            }
            #endregion
        }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
        {
            private CanalDC[] cacheCanalDC;
            public CanalDC CanalDC(int [COLOR=#27ae60]periodH[/COLOR], int [COLOR=#27ae60]periodB[/COLOR])
            {
                return CanalDC(Input, periodH, periodB);
            }
    
            public CanalDC CanalDC(ISeries<double> input, int periodH, int periodB)
            {
                if (cacheCanalDC != null)
                    for (int idx = 0; idx < cacheCanalDC.Length; idx++)
                        if (cacheCanalDC[idx] != null && cacheCanalDC[idx].PeriodH == periodH && cacheCanalDC[idx].PeriodB == periodB && cacheCanalDC[idx].EqualsInput(input))
                            return cacheCanalDC[idx];
                return CacheIndicator<CanalDC>(new CanalDC(){ PeriodH = periodH, PeriodB = periodB }, input, ref cacheCanalDC);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
        public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
        {
            public Indicators.CanalDC CanalDC(int periodH, int periodB)
            {
                return indicator.CanalDC(Input, periodH, periodB);
            }
    
            public Indicators.CanalDC CanalDC(ISeries<double> input , int periodH, int periodB)
            {
                return indicator.CanalDC(input, periodH, periodB);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
        {
            public Indicators.CanalDC CanalDC(int periodH, int periodB)
            {
                return indicator.CanalDC(Input, periodH, periodB);
            }
    
            public Indicators.CanalDC CanalDC(ISeries<double> input , int periodH, int periodB)
            {
                return indicator.CanalDC(input, periodH, periodB);
            }
        }
    }
    
    #endregion
    ​

    #2
    Hello Max238,

    Thank you for your post.

    Can you please describe with more detail how it is no longer working? Are you having compile errors, are there any errors in the Log tab of the Control Center, or something else?

    The syntax for AddPlot() needs to be fixed in the script.

    The syntax for AddPlot is either AddPlot(Brush brush, string name) or AddPlot(Stroke stroke, PlotStyle plotStyle, string name). The name should be in quotation marks.

    Please see the Help Guide for AddPlot():

    https://ninjatrader.com/support/help...t8/addplot.htm

    Comment


      #3
      Thank you for your response,
      I have no compilation errors. No idea,

      I'll go see, I finish my day and go home.

      I seemed to have seen in the various help that I read several forms of AddPlot, you have just confirmed it.

      I tried to insert the name but it gave me errors.
      I will come back to you tomorrow;

      I wish you a good day

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      606 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      351 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      560 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      561 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X