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

Draw.Region between lines

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

    Draw.Region between lines

    Good morning everybody.

    I'm new to NT 8 and I have some issues abot coloring a region between some horizontal lines.

    Recently, I found the included indicator somewhere else and I just wanted to highlight the regions between the standard deviation levels.

    The original code used lines to plot these levels, I changed them into plots in order to use the Draw.Region() function with two series.but still the region is not drawn.

    I will appreciate any help.

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        /// <summary>
        /// Displays Z Score for the selected instrument.
        /// </summary>
        [Description("Displays Z Score for the selected instrument. Optional Smoothings.")]
        public class TCZScore : Indicator
        {
    		public enum MovingAverageTypes
    		{
    			SMA = 1,
    			WMA = 2,
    			EMA = 3,
    			LinReg = 4
    		}
    		
            #region Variables
            // internal program variables
    		double calculation = 0;
    		
            #endregion
    
    		
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Initialize();	// call the original NT7 Initialize()
    				// DrawOnPricePanel = false;
    			}
    		}
    		
    		
    		public override string DisplayName
    		{
    			get { return Name+"("+averagePeriod.ToString()+","+stDevPeriod.ToString()+")";	}
    		}
    		
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected void Initialize()
            {
    			Name = "TC_ZScore: TradingCoders.com";
                AddPlot(new Stroke(Brushes.DarkOrange,1), PlotStyle.Line, "Z");
    			AddPlot(new Stroke(Brushes.Red), PlotStyle.Line, "ZSmooth");
    			
    			AddPlot(new Stroke(Brushes.DimGray), PlotStyle.Line, "U2");
    			AddPlot(new Stroke(Brushes.DimGray), PlotStyle.Line, "U1");
    			AddPlot(new Stroke(Brushes.DimGray), PlotStyle.Line, "L1");
    			AddPlot(new Stroke(Brushes.DimGray), PlotStyle.Line, "L2");
    						
                AddLine(new Stroke(Brushes.DimGray,1), 0, "ZeroLine");
    
                Calculate = Calculate.OnPriceChange;
    			DrawOnPricePanel = false;
                IsOverlay				= false;
    			//PriceType = PriceType.Typical;	// cannot be set by code as any default in NT8
    			
    			MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
    			
    			averagePeriod = 100; // Default setting for AveragePeriod
                stDevPeriod = 100; // Default setting for StDevPeriod
    			mAType = MovingAverageTypes.SMA;	// 1=SMA, 2=EMA, 3=LinReg
    			smoothingPeriod = 5;	// Smoothing of principle Z (1=no smoothing)
    			doubleSmoothingPeriod = 10; // period of Double Smoothed Z.
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			if (CurrentBar < 5)
    			{
    				Z[0] = (0);
    				return;
    			}
    			// off and running ---->
    
    			switch (mAType)
    			{
    				case MovingAverageTypes.SMA:
    					calculation = SMA(Input,averagePeriod)[0];
    					break;
    				case MovingAverageTypes.WMA:
    					calculation = WMA(Input,averagePeriod)[0];
    					break;
    				case MovingAverageTypes.EMA:
    					calculation = EMA(Input,averagePeriod)[0];
    					break;
    				case MovingAverageTypes.LinReg:
    					calculation = LinReg(Input,averagePeriod)[0];
    					break;
    				default:
    					calculation = SMA(Input,averagePeriod)[0];
    					break;
    			}
    			
    			if (smoothingPeriod > 1)	// smooths price series going into the calculation
    				calculation = (EMA(Input,smoothingPeriod)[0]-calculation)/StdDev(Input,stDevPeriod)[0];
    			else 
    				calculation = (Input[0]-calculation)/StdDev(Input,stDevPeriod)[0];
    			
    			Z[0] = (calculation);
    			
    			if (doubleSmoothingPeriod > 1)
    			{
    				ZSmooth[0] = (EMA(Z,Math.Min(doubleSmoothingPeriod,CurrentBar))[0]);
    			}
    			else
    				ZSmooth.Reset();
    			
    			U2[0] =  2.0;
    			U1[0] =  1.0;
    			L1[0] = -1.0;
    			L2[0] = -2.0;
    			
         		Region myReg = Draw.Region(this, "tag1", CurrentBar, 0, L1, U1, Brushes.Transparent, Brushes.Blue,10);
     
    			// Change the object's opacity
    			myReg.AreaOpacity = 25;
            }
    
    		
    		
            #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 Series<double> Z
            {
                get { return Values[0]; }
            }
    
    		[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 Series<double> ZSmooth
            {
                get { return Values[1]; }
            }
    
    		[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 Series<double> U2
            {
                get { return Values[2]; }
            }
    
    		[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 Series<double> U1
            {
                get { return Values[3]; }
            }
    		
    		[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 Series<double> L1
            {
                get { return Values[4]; }
            }
    		
    		[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 Series<double> L2
            {
                get { return Values[5]; }
            }
    		// --------------------------
    		
    		[Range(1, int.MaxValue), NinjaScriptProperty]
    		[Display(Description = "Length of averaged price",
    		 ResourceType = typeof(Custom.Resource), Name = "Average Period", GroupName = "NinjaScriptParameters", Order = 0)]
    		public int averagePeriod
    		{ get; set; }
    
    		[Range(1, int.MaxValue), NinjaScriptProperty]
    		[Display(Description = "Length of standard deviation of price",
    		 ResourceType = typeof(Custom.Resource), Name = "StdDev Period", GroupName = "NinjaScriptParameters", Order = 1)]
    		public int stDevPeriod
    		{ get; set; }
    		
         	[NinjaScriptProperty]
    		[Display(Description = "Type of moving average",
    		 ResourceType = typeof(Custom.Resource), Name = "MA Type", GroupName = "NinjaScriptParameters", Order = 2)]
    		public NinjaTrader.NinjaScript.Indicators.TCZScore.MovingAverageTypes mAType
    		{ get; set; }
    		
    		[Range(1, int.MaxValue), NinjaScriptProperty]
    		[Display(Description = "Smoothing period for Z",
    		 ResourceType = typeof(Custom.Resource), Name = "Smoothing Period", GroupName = "NinjaScriptParameters", Order = 3)]
    		public int smoothingPeriod
    		{ get; set; }
    		
    		[Range(1, int.MaxValue), NinjaScriptProperty]
    		[Display(Description = "Double Smoothing period",
    		 ResourceType = typeof(Custom.Resource), Name = "DoubleSmoothing Period", GroupName = "NinjaScriptParameters", Order = 4)]
    		public int doubleSmoothingPeriod
    		{ get; set; }
    		
            #endregion
        }
    }

    #2
    Hello Gianpiero,

    Thanks for your post.

    Please note that we have moved your post to a new thread here in NinjaTrader8 Indicator Development. You had posted on a thread in the NT7 indicator development forum.

    I copied your code and it appears to be drawing the region (please see attached).

    What version of NT8 are you using (look in Help>about)?
    Attached Files
    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by DJ888, 04-16-2024, 06:09 PM
    6 responses
    18 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Started by Jon17, Today, 04:33 PM
    0 responses
    1 view
    0 likes
    Last Post Jon17
    by Jon17
     
    Started by Javierw.ok, Today, 04:12 PM
    0 responses
    6 views
    0 likes
    Last Post Javierw.ok  
    Started by timmbbo, Today, 08:59 AM
    2 responses
    10 views
    0 likes
    Last Post bltdavid  
    Started by alifarahani, Today, 09:40 AM
    6 responses
    41 views
    0 likes
    Last Post alifarahani  
    Working...
    X