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

Custom Indicator Not Using Price Data (NT8)

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

    Custom Indicator Not Using Price Data (NT8)

    I have created a custom indicator to calculate DI+ and DI- using an EMA instead of a SMA and when I apply the indicator to my chart nothing shows up and where it would otherwise say "DMExp(ES 06-16 (15 Minute), 14)", for example, the screen just shows "DMExp(14)". I am attaching a screenshot of the chart as well as pasting the code below. Can someone please tell me what I'm doing wrong?

    Code:
    #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
    {
    	public class DMExp : Indicator
    	{
    		private Series<double> dmPlus;
    		private Series<double> dmMinus;
    		private Series<double> tr;
    		private Series<double> avgTR;
    
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description							= @"Enter the description for your new custom Indicator here.";
    				Name								= "DMExp";
    				Calculate							= Calculate.OnBarClose;
    				IsOverlay							= false;
    				DisplayInDataBox					= true;
    				DrawOnPricePanel					= true;
    				DrawHorizontalGridLines				= true;
    				DrawVerticalGridLines				= true;
    				PaintPriceMarkers					= true;
    				ScaleJustification					= NinjaTrader.Gui.Chart.ScaleJustification.Right;
    				IsSuspendedWhileInactive			= true;
    				Period					= 14;
    				AddPlot(Brushes.Green, "DIPlus");
    				AddPlot(Brushes.Red, "DIMinus");
    			}
    			else if (State == State.Configure)
    			{
    				dmPlus 		= new Series<double>(this);
    				dmMinus 	= new Series<double>(this);
    				tr 			= new Series<double>(this);
    				avgTR		= new Series<double>(this);
    
    			}
    		}
    
    		protected override void OnBarUpdate()
    		{
    			double trueRange = Math.Max(Math.Max(High[0] - Low[0], High[0] - Close[1]), Close[1] - Low[0]);
    			if (CurrentBar == 0)
    			{
    				tr[0] 			= 0;
    				dmPlus[0] 		= 0;
    				dmMinus[0] 		= 0;
    				DIPlus[0]		= 0;
    				DIMinus[0]		= 0;
    				avgTR[0]		= 0;
    			}
    			else
    			{
    				tr[0]			= trueRange;
    				dmPlus[0]		= High[0] - High[1] > Low[1] - Low[0] ? Math.Max(High[0] - High[1], 0) : 0;
    				dmMinus[0]		= Low[1] - Low[0] > High[0] - High[1] ? Math.Max(Low[1] - Low[0], 0) : 0;
    				
    				if (CurrentBar < Period)
    				{
    					DIPlus[0]	= DIPlus[1] + dmPlus[0];
    					DIMinus[0]	= DIMinus[1] + dmMinus[0];
    					avgTR[0]	= 0.5 * (avgTR[1] + tr[0]);
    				}
    				else
    				{
    					avgTR[0]	= (double) EMA(tr,Period)[0];
    					DIPlus[0]	= 100 * (double) EMA(dmPlus,Period)[0] / avgTR[0];
    					DIMinus[0]	= 100 * (double) EMA(dmMinus,Period)[0] / avgTR[0];
    				}
    			}
    		}
    
    		#region Properties
    		[Range(1, int.MaxValue)]
    		[NinjaScriptProperty]
    		[Display(Name="Period", Order=1, GroupName="Parameters")]
    		public int Period
    		{ get; set; }
    
    		[Browsable(false)]
    		[XmlIgnore]
    		public Series<double> DIPlus
    		{
    			get { return Values[0]; }
    		}
    
    		[Browsable(false)]
    		[XmlIgnore]
    		public Series<double> DIMinus
    		{
    			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 DMExp[] cacheDMExp;
    		public DMExp DMExp(int period)
    		{
    			return DMExp(Input, period);
    		}
    
    		public DMExp DMExp(ISeries<double> input, int period)
    		{
    			if (cacheDMExp != null)
    				for (int idx = 0; idx < cacheDMExp.Length; idx++)
    					if (cacheDMExp[idx] != null && cacheDMExp[idx].Period == period && cacheDMExp[idx].EqualsInput(input))
    						return cacheDMExp[idx];
    			return CacheIndicator<DMExp>(new DMExp(){ Period = period }, input, ref cacheDMExp);
    		}
    	}
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    	public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    	{
    		public Indicators.DMExp DMExp(int period)
    		{
    			return indicator.DMExp(Input, period);
    		}
    
    		public Indicators.DMExp DMExp(ISeries<double> input , int period)
    		{
    			return indicator.DMExp(input, period);
    		}
    	}
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    	public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    	{
    		public Indicators.DMExp DMExp(int period)
    		{
    			return indicator.DMExp(Input, period);
    		}
    
    		public Indicators.DMExp DMExp(ISeries<double> input , int period)
    		{
    			return indicator.DMExp(input, period);
    		}
    	}
    }
    
    #endregion
    Attached Files

    #2
    Never mind, I figured it out. The first calculation of trueRange should have happened inside the else statement.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by ZenCortexAuCost, Today, 04:24 AM
    0 responses
    5 views
    0 likes
    Last Post ZenCortexAuCost  
    Started by ZenCortexAuCost, Today, 04:22 AM
    0 responses
    2 views
    0 likes
    Last Post ZenCortexAuCost  
    Started by SantoshXX, Today, 03:09 AM
    0 responses
    15 views
    0 likes
    Last Post SantoshXX  
    Started by DanielTynera, Today, 01:14 AM
    0 responses
    3 views
    0 likes
    Last Post DanielTynera  
    Started by yertle, 04-18-2024, 08:38 AM
    9 responses
    42 views
    0 likes
    Last Post yertle
    by yertle
     
    Working...
    X