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

code makes indicator goes blank

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

    code makes indicator goes blank

    I was able to change the back ground to blue for an up trend but the code to set back ground to magenta for a down trend make it goes blank.

    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 TRIX (Triple Exponential Average) displays the percentage Rate of Change (ROC) of a triple EMA. Trix oscillates above and below the zero value. The indicator applies triple smoothing in an attempt to eliminate insignificant price movements within the trend that you're trying to isolate.
    	/// </summary>
    	[Description("The TRIX (Triple Exponential Average) displays the percentage Rate of Change (ROC) of a triple EMA. Trix oscillates above and below the zero value. The indicator applies triple smoothing in an attempt to eliminate insignificant price movements within the trend that you're trying to isolate.")]
    	public class TRIXc : Indicator
    	{
    		#region Variables
    		private int	period			= 8;
    		private int	signalPeriod	= 1;
    			private string sellText   = @"SELL"; 	
    			private Font 		pfont = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point);
    			private Color		sColor			= Color.Red;
    		    private string      buyText   = @"BUY"; 	
    		    private Color		bColor			= Color.Black;
    			private int yPixels	=	10;
    		private string upArrow = "Y";
    		#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.Black, "Default"));
    			Add(new Plot(Color.Red,   "Signal"));
    
    			Add(new Line(Color.DarkGray, 0, "Zero line"));
    		//	Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
    		//	Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Plot1"));
    		
    	
    		}
    
    		/// <summary>
    		/// Calculates the indicator value(s) at the current index.
    		/// </summary>
    		protected override void OnBarUpdate()
    		{
    			if (CurrentBar == 0) 
    			{
    				Value.Set(Input[0]);
    		
    				return;
    			}
    
    			EMA tripleEma = EMA(EMA(EMA(Input, period), period), period);
    			double trix = 100 * ((tripleEma[0] - tripleEma[1]) / tripleEma[0]);
    			
    			Default.Set(trix);
    			
    			
    		//	if ((trix > 0) && (trix > ( 100 * ((tripleEma[1] - tripleEma[2]) / tripleEma[1]))))
    		
    			
    			Signal.Set(EMA(Default, signalPeriod)[0]);
    			if (trix < 0) upArrow = "Y";
    			if (trix > 0) 
    			if (trix >  100 * ((tripleEma[1] - tripleEma[2]) / tripleEma[1]))
    				BackColor = Color.LightSeaGreen; // set back ground color
    			else if (upArrow == "Y")
    			{
    				DrawArrowDown("MyArrowDown"+CurrentBar, 0, High[0]+2* TickSize, Color.Red);
    		    //  DrawDot(CurrentBar.ToString(), 0, High[1] + 10 * TickSize, Color.Red);
    			//  DrawText("MyText"+CurrentBar, true,"Sell", 0, High[0] + 12 * TickSize, Color.Red,pfont);
    				DrawText("V"+CurrentBar, true, sellText, 0, High[0] + 8 * TickSize, yPixels, sColor, pfont, 
    				StringAlignment.Center, Color.Empty, Color.Empty, 10) ;
    				upArrow = "N";
    	       	} 
    			
    		
    		// ***********************  trouble code here ******************************	
    		//	if (trix < 0) 
    		//	if (trix >  100 * ((tripleEma[1] - tripleEma[2]) / tripleEma[1]))
    		//		BackColor = Color.LightPink; // set back ground color
    		// *************************************************************************	
    	
    		
    			//	if (CrossBelow(TRIX(period, signalPeriod).Default, TRIX(period, signalPeriod).Signal, 1))	
    	/*		if (CrossBelow(TRIX(period, signalPeriod).Default, 0,1))	
    			{
    				DrawArrowDown("MyArrowDown"+CurrentBar, 0, High[0]+2* TickSize, Color.Red);
    		    //  DrawDot(CurrentBar.ToString(), 0, High[1] + 10 * TickSize, Color.Red);
    			//  DrawText("MyText"+CurrentBar, true,"Sell", 0, High[0] + 12 * TickSize, Color.Red,pfont);
    				DrawText("V"+CurrentBar, true, sellText, 0, High[0] + 8 * TickSize, yPixels, sColor, pfont, 
    				StringAlignment.Center, Color.Empty, Color.Empty, 10) ;
    	       	}
    			
    		  //if (CrossAbove(TRIX(period, signalPeriod).Default, TRIX(period, signalPeriod).Signal, 1))		
    			if (CrossAbove(TRIX(period, signalPeriod).Default, 0,1))
    			{
    				    DrawArrowUp("MyArrowUp"+CurrentBar, 0, Low[0] -2* TickSize, Color.Orange);
                 //     DrawText("MyText"+CurrentBar, "Buy", 0, Low[0] - 6 * TickSize, Color.Green);
    					DrawText("V"+CurrentBar, true, buyText, 0, Low[0] - 10 * TickSize, yPixels, bColor, pfont, 
    				    StringAlignment.Center, Color.Empty, Color.Empty, 10) ;
    			}
          */
    		}
    
    		#region Properties
    		/// <summary>
    		/// </summary>
    		[Description("Numbers of bars used for calculations.")]
    		[Category("Parameters")]
    		public int Period
    		{
    			get { return period; }
    			set { period = Math.Max(1, value); }
    		}
    
    		[Description("Period for the signal line.")]
    		[Category("Parameters")]
    		[Gui.Design.DisplayNameAttribute("Signal period")]
    		public int SignalPeriod
    		{
    			get { return signalPeriod; }
    			set { signalPeriod = Math.Max(1, value); }
    		}
    
    		/// <summary>
    		/// </summary>
    		[Browsable(false)]
    		[XmlIgnore]
    		public DataSeries Signal
    		{
    			get { return Values[1]; }
    		}
    
    		/// <summary>
    		/// </summary>
    		[Browsable(false)]
    		[XmlIgnore]
    		public DataSeries Default
    		{
    			get { return Values[0]; }
    		}
    		#endregion
    	}
    }
    Attached Files

    #2
    Hi nkhoi, do you see any errors in the Log tab of the Control Center when this happens?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      thanks for remind me to look at the log, I added current bar > 1 and it worked.

      Error on calling the 'OnBarUpdate' method for indicator 'TRIXc' on bar 1: Index was out of range. Must be non-negative and less than the size of the collection.

      Comment


        #4
        Same problem.....could you go over the solution?

        I'm having the same problem in retrieving a DataSeries of anything larger than [1] -- same "out of range" error in log. However, I'm not clear about how and where to enter the "CurrentBar > 1" statement. Should it be entered at the beginning of the 'OnBarUpdate' or within the IF statement calling for the DataSeries? What is the syntax? Thanks!

        Comment


          #5
          change
          (CurrentBar == 0)
          to
          (CurrentBar > 1)

          Comment


            #6
            Thanks

            Perfect ! Thanks Nkhoi

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by oviejo, Today, 12:28 AM
            0 responses
            1 view
            0 likes
            Last Post oviejo
            by oviejo
             
            Started by pechtri, 06-22-2023, 02:31 AM
            10 responses
            125 views
            0 likes
            Last Post Leeroy_Jenkins  
            Started by judysamnt7, 03-13-2023, 09:11 AM
            4 responses
            59 views
            0 likes
            Last Post DynamicTest  
            Started by ScottWalsh, Yesterday, 06:52 PM
            4 responses
            36 views
            0 likes
            Last Post ScottWalsh  
            Started by olisav57, Yesterday, 07:39 PM
            0 responses
            7 views
            0 likes
            Last Post olisav57  
            Working...
            X