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

Easy Indicator do not plot....

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

    Easy Indicator do not plot....

    Hello,

    i need a little help with this Indicator:
    Code:
    protected override void Initialize()
    {
           Add(new Plot(new Pen(Color.DarkGreen, 5), PlotStyle.Bar, "UpVol"));
           Add(new Plot(new Pen(Color.Red, 5), PlotStyle.Bar, "DnVol"));
           Add(new Plot(new Pen(Color.Black,1), PlotStyle.Bar, "Lower"));
           Add(new Plot(new Pen(Color.Yellow,3), PlotStyle.Bar, "Greater"));
           CalculateOnBarClose	= true;
           Overlay				= false;
           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
           // plot below by replacing 'Close[0]' with your own formula.
           if(Close[0] > Open[0])
               UpVol.Set(Volume[0]);
           else
               DnVol.Set(Volume[0]);
           HighVolume = MAX(Volume, Period)[1];
           if(Volume[0] > HighVolume)
               Lower.Set(Volume[0]);
           else
               Greater.Set(Volume[0]);
    }
    I am just blind at the moment - i don't see the reason, why this indicator just plot nothing. A little more infos: The panel-scale shows 0 0,5 and 1, so I suppose, that there is an issue with my calculation with Volume[0]. Second: I can compile this indicator without any problems, but i can't export: compiling error.....

    Please help.
    Last edited by xenayoo; 06-04-2010, 03:02 AM.

    #2
    You likely run into this error explained in the tip below, as you do not have a check for enough CurrentBars to be present before continuing in your OnBarUpdate() - http://www.ninjatrader.com/support/f...ead.php?t=3170
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hello Bertrand. Great and fast help. That's it.... Thx a lot.

      Comment


        #4
        next problem

        Ok, now next problem: I want to change the System DMI-Indicator. I just combined the system DMI and the custom plot color sample for the ADX line. I changed nothing in the calculations. But it doesn't work: Error message in Log: "Error calling the 'OnBarUpdate' method for indicator 'ADXmc' on bar1....

        Why does the original indicator work an my indicator not? I copied the whole code from DMI to the new Indicator and add the "AddNewPlot"....

        edit: Here is my code

        Code:
            public class ADXmc : Indicator
            {
               #region Variables
        		private int period = 14;
        
        		private DataSeries dmPlus;
        		private DataSeries dmMinus;
        		private DataSeries sumDmPlus;
        		private DataSeries sumDmMinus;
        		private DataSeries sumTr;
        		private DataSeries tr;
        		private DataSeries adx;
        		#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(new Pen(Color.Blue, 2), "ADXmed"));
        			Add(new Plot(new Pen(Color.Violet, 2), "ADXdown"));
        			Add(new Plot(new Pen(Color.Cyan, 2), "ADXup"));
        			Add(new Plot(Color.Green, "+DI"));
        			Add(new Plot(Color.Red, "-DI"));
        
        			Add(new Line(Color.DarkViolet, 25, "Lower"));
        			Add(new Line(Color.YellowGreen, 75, "Upper"));
        
        			dmPlus = new DataSeries(this);
        			dmMinus = new DataSeries(this);
        			sumDmPlus = new DataSeries(this);
        			sumDmMinus = new DataSeries(this);
        			sumTr = new DataSeries(this);
        			tr = new DataSeries(this);
        			adx = new DataSeries(this);
        		}
        
        		/// <summary>
        		/// Called on each bar update event (incoming tick)
        		/// </summary>
        		protected override void OnBarUpdate()
        		{
        			double trueRange = High[0] - Low[0];
        			if (CurrentBar <= 10)
        			{
        				tr.Set(trueRange);
        				dmPlus.Set(0);
        				dmMinus.Set(0);
        				sumTr.Set(tr[0]);
        				sumDmPlus.Set(dmPlus[0]);
        				sumDmMinus.Set(dmMinus[0]);
        				Adx.Set(50);
        				ADXmed.Set(50);
        			}
        			else
        			{
        				tr.Set(Math.Max(Math.Abs(Low[0] - Close[1]), Math.Max(trueRange, Math.Abs(High[0] - Close[1]))));
        				dmPlus.Set(High[0] - High[1] > Low[1] - Low[0] ? Math.Max(High[0] - High[1], 0) : 0);
        				dmMinus.Set(Low[1] - Low[0] > High[0] - High[1] ? Math.Max(Low[1] - Low[0], 0) : 0);
        
        				if (CurrentBar < Period)
        				{
        					sumTr.Set(sumTr[1] + tr[0]);
        					sumDmPlus.Set(sumDmPlus[1] + dmPlus[0]);
        					sumDmMinus.Set(sumDmMinus[1] + dmMinus[0]);
        				}
        				else
        				{
        					sumTr.Set(sumTr[1] - sumTr[1] / Period + tr[0]);
        					sumDmPlus.Set(sumDmPlus[1] - sumDmPlus[1] / Period + dmPlus[0]);
        					sumDmMinus.Set(sumDmMinus[1] - sumDmMinus[1] / Period + dmMinus[0]);
        				}
        
        				double diPlus = 100 * (sumTr[0] == 0 ? 0 : sumDmPlus[0] / sumTr[0]);
        				double diMinus = 100 * (sumTr[0] == 0 ? 0 : sumDmMinus[0] / sumTr[0]);
        				double diff = Math.Abs(diPlus - diMinus);
        				double sum = diPlus + diMinus;
        				Adx.Set(sum == 0 ? 50 : ((Period - 1) * Value[1] + 100 * diff / sum) / Period);
        				if (Adx[0] == Adx[1])
        				{
        					ADXmed.Set(1,Adx[1]);
        					ADXmed.Set(Adx[0]);
        				}
        				else if (Adx[0] < Adx[1])
        				{
        					ADXdown.Set(1,Adx[1]);
        					ADXdown.Set(Adx[0]);
        				}
        				else 
        				{
        					ADXup.Set(1,Adx[1]);
        					ADXup.Set(Adx[0]);
        				}
        				DiPlus.Set(diPlus);
        				DiMinus.Set(diMinus);
        			}
        		}
        Thank you for help....
        Last edited by xenayoo; 06-28-2010, 09:42 AM. Reason: adding code for indicator

        Comment


          #5
          Did you port over the CurrentBars check from the reference sample into your custom indicator as well? The error suggests this was not done...
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Hi Bertrand,

            i add the code in my previous posting. And yes, you will see, i checked the current bar. In the original Indicator the checking was:

            if (CurrentBar == 0)

            but it doesn't work at all....

            Comment


              #7
              xenayoo, please try adding something like this to the OnBarUpdate() start -

              if (CurrentBar < 1) return;

              Your snippet would not resolve the issue of trying to access a series value where it does not exist...reaching one bar back at the first bar is just not possible.
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Doesn't work....

                Comment


                  #9
                  Please post the code you use so we could take a look - thanks.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_Bertrand View Post
                    Please post the code you use so we could take a look - thanks.
                    Hm, i posted the whole code in my previous posting. But here is the code with latest changes...
                    Again: It'sexactly the same code from NT-DMI-indicator, wich works fine. I just want to change the color of the ADX-line to multicolor....

                    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>
                        /// Enter the description of your new custom indicator here
                        /// </summary>
                        [Description("Enter the description of your new custom indicator here")]
                        public class ADXmc : Indicator
                        {
                           #region Variables
                    		private int period = 14;
                    
                    		private DataSeries dmPlus;
                    		private DataSeries dmMinus;
                    		private DataSeries sumDmPlus;
                    		private DataSeries sumDmMinus;
                    		private DataSeries sumTr;
                    		private DataSeries tr;
                    		private DataSeries adx;
                    		#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(new Pen(Color.Blue, 2), "ADXmed"));
                    			Add(new Plot(new Pen(Color.Violet, 2), "ADXdown"));
                    			Add(new Plot(new Pen(Color.Cyan, 2), "ADXup"));
                    			Add(new Plot(Color.Green, "+DI"));
                    			Add(new Plot(Color.Red, "-DI"));
                    			
                    			Add(new Line(Color.DarkViolet, 25, "Lower"));
                    			Add(new Line(Color.YellowGreen, 75, "Upper"));
                    
                    			dmPlus = new DataSeries(this);
                    			dmMinus = new DataSeries(this);
                    			sumDmPlus = new DataSeries(this);
                    			sumDmMinus = new DataSeries(this);
                    			sumTr = new DataSeries(this);
                    			tr = new DataSeries(this);
                    			adx = new DataSeries(this);
                    		}
                    
                    		/// <summary>
                    		/// Called on each bar update event (incoming tick)
                    		/// </summary>
                    		protected override void OnBarUpdate()
                    		{
                    			double trueRange = High[0] - Low[0];
                    			if (CurrentBar == 0)
                    			{
                    				tr.Set(trueRange);
                    				dmPlus.Set(0);
                    				dmMinus.Set(0);
                    				sumTr.Set(tr[0]);
                    				sumDmPlus.Set(dmPlus[0]);
                    				sumDmMinus.Set(dmMinus[0]);
                    				Adx.Set(50);
                    				ADXmed.Set(50);
                    				ADXup.Set(50);
                    				ADXdown.Set(50);
                    				return;
                    			}
                    			else
                    			{
                    				tr.Set(Math.Max(Math.Abs(Low[0] - Close[1]), Math.Max(trueRange, Math.Abs(High[0] - Close[1]))));
                    				dmPlus.Set(High[0] - High[1] > Low[1] - Low[0] ? Math.Max(High[0] - High[1], 0) : 0);
                    				dmMinus.Set(Low[1] - Low[0] > High[0] - High[1] ? Math.Max(Low[1] - Low[0], 0) : 0);
                    
                    				if (CurrentBar < Period)
                    				{
                    					sumTr.Set(sumTr[1] + tr[0]);
                    					sumDmPlus.Set(sumDmPlus[1] + dmPlus[0]);
                    					sumDmMinus.Set(sumDmMinus[1] + dmMinus[0]);
                    				}
                    				else
                    				{
                    					sumTr.Set(sumTr[1] - sumTr[1] / Period + tr[0]);
                    					sumDmPlus.Set(sumDmPlus[1] - sumDmPlus[1] / Period + dmPlus[0]);
                    					sumDmMinus.Set(sumDmMinus[1] - sumDmMinus[1] / Period + dmMinus[0]);
                    				}
                    
                    				double diPlus = 100 * (sumTr[0] == 0 ? 0 : sumDmPlus[0] / sumTr[0]);
                    				double diMinus = 100 * (sumTr[0] == 0 ? 0 : sumDmMinus[0] / sumTr[0]);
                    				double diff = Math.Abs(diPlus - diMinus);
                    				double sum = diPlus + diMinus;
                    				Adx.Set(sum == 0 ? 50 : ((Period - 1) * Value[1] + 100 * diff / sum) / Period);
                    				if (Adx[0] == Adx[1])
                    				{
                    					ADXmed.Set(1,Adx[1]);
                    					ADXmed.Set(Adx[0]);
                    				}
                    				else if (Adx[0] < Adx[1])
                    				{
                    					ADXdown.Set(1,Adx[1]);
                    					ADXdown.Set(Adx[0]);
                    				}
                    				else 
                    				{
                    					ADXup.Set(1,Adx[1]);
                    					ADXup.Set(Adx[0]);
                    				}
                    				DiPlus.Set(diPlus);
                    				DiMinus.Set(diMinus);
                    			}
                    		}
                    
                    		#region Properties
                    		/// <summary> 
                    		/// </summary>
                    		[Browsable(false)]
                    		[XmlIgnore()]
                    		public DataSeries ADXmed
                    		{
                    			get { return Values[0]; }
                    		}
                    		
                    		[Browsable(false)]
                    		[XmlIgnore()]
                    		public DataSeries ADXdown
                    		{
                    			get { return Values[1]; }
                    		}
                    		
                    		[Browsable(false)]
                    		[XmlIgnore()]
                    		public DataSeries ADXup
                    		{
                    			get { return Values[2]; }
                    		}
                    		
                    		[Browsable(false)]
                    		[XmlIgnore()]
                    		public DataSeries DiPlus
                    		{
                    			get { return Values[3]; }
                    		}
                    
                    		[Browsable(false)]
                    		[XmlIgnore()]
                    		public DataSeries DiMinus
                    		{
                    			get { return Values[4]; }
                    		}
                    		
                    		[Browsable(false)]
                    		[XmlIgnore()]
                    		public DataSeries Adx
                    		{
                    			get { return Values[5]; }
                    		}
                    		
                    		/// <summary>
                    		/// </summary>
                    		[Description("Numbers of bars used for calculations")]
                    		[Category("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 ADXmc[] cacheADXmc = null;
                    
                            private static ADXmc checkADXmc = new ADXmc();
                    
                            /// <summary>
                            /// Enter the description of your new custom indicator here
                            /// </summary>
                            /// <returns></returns>
                            public ADXmc ADXmc(int period)
                            {
                                return ADXmc(Input, period);
                            }
                    
                            /// <summary>
                            /// Enter the description of your new custom indicator here
                            /// </summary>
                            /// <returns></returns>
                            public ADXmc ADXmc(Data.IDataSeries input, int period)
                            {
                                checkADXmc.Period = period;
                                period = checkADXmc.Period;
                    
                                if (cacheADXmc != null)
                                    for (int idx = 0; idx < cacheADXmc.Length; idx++)
                                        if (cacheADXmc[idx].Period == period && cacheADXmc[idx].EqualsInput(input))
                                            return cacheADXmc[idx];
                    
                                ADXmc indicator = new ADXmc();
                                indicator.BarsRequired = BarsRequired;
                                indicator.CalculateOnBarClose = CalculateOnBarClose;
                                indicator.Input = input;
                                indicator.Period = period;
                                indicator.SetUp();
                    
                                ADXmc[] tmp = new ADXmc[cacheADXmc == null ? 1 : cacheADXmc.Length + 1];
                                if (cacheADXmc != null)
                                    cacheADXmc.CopyTo(tmp, 0);
                                tmp[tmp.Length - 1] = indicator;
                                cacheADXmc = 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>
                            /// Enter the description of your new custom indicator here
                            /// </summary>
                            /// <returns></returns>
                            [Gui.Design.WizardCondition("Indicator")]
                            public Indicator.ADXmc ADXmc(int period)
                            {
                                return _indicator.ADXmc(Input, period);
                            }
                    
                            /// <summary>
                            /// Enter the description of your new custom indicator here
                            /// </summary>
                            /// <returns></returns>
                            public Indicator.ADXmc ADXmc(Data.IDataSeries input, int period)
                            {
                                return _indicator.ADXmc(input, period);
                            }
                    
                        }
                    }
                    
                    // This namespace holds all strategies and is required. Do not change it.
                    namespace NinjaTrader.Strategy
                    {
                        public partial class Strategy : StrategyBase
                        {
                            /// <summary>
                            /// Enter the description of your new custom indicator here
                            /// </summary>
                            /// <returns></returns>
                            [Gui.Design.WizardCondition("Indicator")]
                            public Indicator.ADXmc ADXmc(int period)
                            {
                                return _indicator.ADXmc(Input, period);
                            }
                    
                            /// <summary>
                            /// Enter the description of your new custom indicator here
                            /// </summary>
                            /// <returns></returns>
                            public Indicator.ADXmc ADXmc(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.ADXmc(input, period);
                            }
                    
                        }
                    }
                    #endregion
                    Last edited by xenayoo; 06-29-2010, 03:57 AM.

                    Comment


                      #11
                      Thanks, it seems you missed adding the ADX plot to the Initialize() section - if I add this to it, compile and reload works just fine here -

                      Code:
                       
                      Add(new Plot(new Pen(Color.Cyan, 2), "ADX"));
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_Bertrand View Post
                        Thanks, it seems you missed adding the ADX plot to the Initialize() section - if I add this to it, compile and reload works just fine here -

                        Code:
                         
                        Add(new Plot(new Pen(Color.Cyan, 2), "ADX"));
                        Well, that's the original ONE-COLOR line. I want this line with 3 colors: falling, flat and rising... I just use Adx as Dataseries. For ploting, I use ADXup, ADXdown and ADXmed. I can compile my code without error. But the indicator doesn't plot and leave a message in the Log.

                        Comment


                          #13
                          You would then need to use the DataSeries you defined in the code for this and not the public property capitalize (Adx vs adx) - please note NinjaScript is case sensitive.

                          If you replace references to Adx with adx and comment out delete you not needed public property it should work as well.
                          BertrandNinjaTrader Customer Service

                          Comment


                            #14
                            Thank you, Bertrand. That's it. And additionaly a little error in the calculation-Logic... Thank you very much...
                            Last edited by xenayoo; 06-30-2010, 08:36 AM. Reason: wrong posting....

                            Comment


                              #15
                              Great to hear thanks for reporting back.
                              BertrandNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by geddyisodin, Yesterday, 05:20 AM
                              8 responses
                              49 views
                              0 likes
                              Last Post geddyisodin  
                              Started by DayTradingDEMON, Today, 09:28 AM
                              3 responses
                              19 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by Stanfillirenfro, Today, 07:23 AM
                              9 responses
                              23 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by George21, Today, 10:07 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post George21  
                              Started by navyguy06, Today, 09:28 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X