Error: Object reference not set to an instance of an object. - NinjaTrader Programming | futures io social day trading
futures io futures trading


Error: Object reference not set to an instance of an object.
Updated: Views / Replies:4,459 / 11
Created: by gomad Attachments:4

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 4  
 
Thread Tools Search this Thread
 

Error: Object reference not set to an instance of an object.

  #11 (permalink)
Elite Member
Muscat, Oman
 
Futures Experience: Intermediate
Platform: Ninja and MT4
Favorite Futures: Forex
 
Posts: 17 since Apr 2012
Thanks: 4 given, 3 received

I get this error message with my indicator

I get the following error messages in the Output Window when I use this indicator with Market Analyzer.

"Sell set value[2] loc=2 the low being set:0.9676
01/12/2014 03:36:00 System.NullReferenceException: Object reference not set to an instance of an object.
at NinjaTrader.Indicator.KnoxvilleDivergence4M2.RSISellCheck(Int32 loc)
at NinjaTrader.Indicator.KnoxvilleDivergence4M2.OnBarUpdate()"

I put Print() throughout my code and was able to 'track down' which line of code is causing the error.

Code Excerpt:
 
Code
Values[2].Set(loc, Low[loc]); 
Print("Sell set value[2]" + " " + "loc="+loc+ " " + "the low being set:"+ Low[loc] );
So three questions:
1. I tried to use Try and Catch, but it did not seem to work on my custom methods (in this case: RSISellCheck(2) & RSIBuyCheck(2). Does Try and Catch work in only the standard methods (like OnBarUpdate) or does it also work in custom methods?
2. Why is this error coming and why is it only happening occassionally and not every bar. The value is always 2. So sometimes it assigns the Low[2] okay and sometimes it gives this error.
3. Why is it giving an error when it shows in the Print the actual Low price value?

Thank you for your help!

-Stearno

 
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
{
    [Description("Enter the description of your new custom indicator here")]
    public class KnoxvilleDivergence4M2 : Indicator
    {
        #region Variables
        
		private int candlesBack = 150; // Default setting for CandlesBack
        private int rSIPeriod = 21; // Default setting for RSIPeriod
        private int momentumPeriod = 20; // Default setting for MomentumPeriod
		
        private int validBars=4;
				int lastBarDivB=10000;
				int lastBarDivS=10000;
		
		private RSI rsi;
		private KnoxvilleMomentum mom;
		private DataSeries mydivergence;
		
        #endregion
//_______________________________________________________________________________________________________________________________________________________________
        protected override void Initialize()
        {
			Add(new Plot(Color.Yellow, "Momentum"));
			Add(new Plot(Color.Green, "DivBuy"));
			Add(new Plot(Color.Red, "DivSell"));
			Add(new Plot(Color.Blue, "Div"));
			Add(new Plot(Color.Blue, "AuxB"));
            Add(new Plot(Color.Blue, "AuxS"));
            Overlay				= false;
        }
//_______________________________________________________________________________________________________________________________________________________________
		protected override void OnStartUp()
		{
			rsi=RSI(rSIPeriod,1);
			mom=KnoxvilleMomentum(momentumPeriod);
		}
//_______________________________________________________________________________________________________________________________________________________________
        protected override void OnBarUpdate()
        {
			try
			{
			if (CurrentBar<candlesBack)return;
			if (CurrentBar<10)return;
			
			
			//BarsNew();
			Mom.Set(mom[0]);

			Values[1].Set(0.00);   // divBuy
			Values[2].Set(0.00);   // divSell
			
			//Print(Time[0]+"    "+Values[4][0]+"  "+Values[5][0]+"      "+Values[4][1]+"  "+Values[5][1]);
			if(CurrentBar>=lastBarDivB && CurrentBar-lastBarDivB<=validBars)Values[4].Set(1); else Values[4].Set(0.00);  
			if(CurrentBar>=lastBarDivS && CurrentBar-lastBarDivS<=validBars)Values[5].Set(1); else Values[5].Set(0.00);  
			//BarsNew();

			RSISellCheck(2);
			
			RSIBuyCheck(2);
			}
			catch (Exception e)
			{
				/* With our caught exception we are able to generate log entries that go to the Control Center logs and also print more detailed information
				about the error to the Output Window. */
				
				// Submits an entry into the Control Center logs to inform the user of an error
				Log("SampleTryCatch Error: Please check your indicator for errors.", LogLevel.Error);
				
				// Prints the caught exception in the Output Window
				Print(Time[0] + " " + e.ToString());
			}
				//Print(Time[0]+"... "+AuxB[0]+"  "+AuxS[0]+"      "+Values[1][0]+"  "+Values[2][0]);
			BarsNew();
			if(CurrentBar>=lastBarDivB && CurrentBar-lastBarDivB<=validBars)Values[4].Set(1); else Values[4].Set(0.00);  
			if(CurrentBar>=lastBarDivS && CurrentBar-lastBarDivS<=validBars)Values[5].Set(1); else Values[5].Set(0.00);  
        }

//_______________________________________________________________________________________________________________________________________________________________
	void BarsNew(){ int N=0, n=0;
		for(N=0;N<candlesBack;N++){
			if(Values[1][N]>0){for(n=N;n>N-validBars;n--){ if(n>=0)Values[4].Set(n, 1); } }
			if(Values[2][N]>0){for(n=N;n>N-validBars;n--){ if(n>=0)Values[5].Set(n, 1); } }
		}
				
	}
//_______________________________________________________________________________________________________________________________________________________________
		// FormatPriceMarker method of a custom indicator
 		public override string FormatPriceMarker(double price)
		{
    		// Formats price values to 4 decimal places
    		return price.ToString("N4");
		}
//_______________________________________________________________________________________________________________________________________________________________
		private void RSISellCheck(int loc)
		{
			double rsiMain = rsi[loc];
			if (rsiMain<50)
				return;
			for (int x=loc-1;x<=loc+2;x++)
			{
				if (High[x]>High[loc])
					return;
			}
	
			for (int y=loc+4;y<(loc+candlesBack);y++)
			{
				if (High[y]>High[loc])
					break;
				int s=y;
	
				for (int z=y-2;z<=y+2;z++)
				{
					if (High[z]>High[y])
					{
						y++;
						break;
					}
				}
				if (s!=y)
				{
					y--;
					continue;
				}
				bool ob = false;
		
				for (int k=loc-1;k<=y;k++)
				{
					double rsiob = rsi[k];
					if (rsiob>70)
					{
						ob=true;
						break;
					}
				}
				if (!ob)
					continue;

				double mom1 = mom[loc];
				double mom2 = mom[y];
				
				if (mom1>mom2)
					continue;
	
				Print("Sell set value[2]" + " " + "loc="+loc+ " " + "the low being set:"+ Low[loc] );
				Values[2].Set(loc, Low[loc]);  
				lastBarDivS=CurrentBar;       //Print(Time[0]+"B   lastB:"+lastBarDivB+" lastS:"+lastBarDivS);
				
				ChartObjectCollection coc = ChartControl.ChartObjects;  
				for (int x=0;x<coc.Count;x++)
				{
					ChartObject c = coc[x];
					if (c.DrawType==DrawType.Line)
					{
						ILine line = (ILine)c;
						if (line.StartBarsAgo==y)
							RemoveDrawObject(line);
					}
				}
				
				DrawLine(Time[loc].ToString()+"High",y,High[y],loc,High[loc],Color.Red);
				DrawOnPricePanel=false;
				DrawLine(Time[loc].ToString()+"MomHigh",y,mom2,loc,mom1,Color.Red);
				DrawOnPricePanel=true;
			}
		}
//_______________________________________________________________________________________________________________________________________________________________		
		private void RSIBuyCheck(int loc)
		{
			double rsiMain = rsi[loc];
			if (rsiMain>50)
				return;

			for (int x=loc-1;x<=loc+2;x++)
			{
				if (Low[x]<Low[loc])
					return;
			}

			for (int y=loc+4;y<(loc+candlesBack);y++)
			{
				if (Low[y]<Low[loc])
					break;
				int s=y;

				for (int z=y-2;z<=y+2;z++)
				{
					if (Low[z]<Low[y])
					{
						y++;
						break;
					}
				}
				if (s!=y)
				{
					y--;
					continue;
				}
				bool ob = false;

				for (int k=loc-1;k<=y;k++)
				{
					double rsiob = rsi[k];
					if (rsiob<30)
					{
						ob=true;
						break;
					}
				}
				if (!ob)
					continue;

				double mom1 = mom[loc];
				double mom2 = mom[y];
				if (mom1<mom2)
					continue;
	
				Print("Buy set value[2]" + " " + "loc="+loc+ " " + "the high being set:"+ High[loc] );
				Values[1].Set(loc, High[loc]);  
				lastBarDivB=CurrentBar;  //Print(Time[0]+"B   lastB:"+lastBarDivB+" lastS:"+lastBarDivS);
				
				
				ChartObjectCollection coc = ChartControl.ChartObjects;
				for (int x=0;x<coc.Count;x++)
				{
					ChartObject c = coc[x];
					if (c.DrawType==DrawType.Line)
					{
						ILine line = (ILine)c;
						if (line.StartBarsAgo==y)
							RemoveDrawObject(line);
					}
				}
				
				DrawLine(Time[loc].ToString()+"Low",y,Low[y],loc,Low[loc],Color.Red);
				DrawOnPricePanel=false;
				DrawLine(Time[loc].ToString()+"MomLow",y,mom2,loc,mom1,Color.Red);
				DrawOnPricePanel=true;
			}
		}
//_______________________________________________________________________________________________________________________________________________________________

        #region Properties
		
		[Browsable(false)]
		[XmlIgnore()]
		public DataSeries Mom
		{
			get { return Values[0]; }
		}
		[Browsable(false)]
		[XmlIgnore()]
		public DataSeries DivBuy
		{
			get { return Values[1]; }
		}		
		[Browsable(false)]
		[XmlIgnore()]
		public DataSeries DivSell
		{
			get { return Values[2]; }
		}
		
		[Browsable(false)]
		[XmlIgnore()]
		public DataSeries Div
		{
			get { return Values[3]; }
		}
		
		[Browsable(false)]
		[XmlIgnore()]
		public DataSeries AuxB
		{
			get { return Values[4]; }
		}
		[Browsable(false)]
		[XmlIgnore()]
		public DataSeries AuxS
		{
			get { return Values[5]; }
		}

        [Description("")]
        [GridCategory("Parameters")]
        public int CandlesBack
        {
            get { return candlesBack; }
            set { candlesBack = Math.Max(1, value); }
        }

        [Description("")]
        [GridCategory("Parameters")]
        public int RSIPeriod
        {
            get { return rSIPeriod; }
            set { rSIPeriod = Math.Max(1, value); }
        }

        [Description("")]
        [GridCategory("Parameters")]
        public int MomentumPeriod
        {
            get { return momentumPeriod; }
            set { momentumPeriod = Math.Max(1, value); }
        }
		
		[Description("")]
        [GridCategory("Parameters")]
        public int ValidBars
        {
            get { return validBars; }
            set { validBars = 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 KnoxvilleDivergence4M2[] cacheKnoxvilleDivergence4M2 = null;

        private static KnoxvilleDivergence4M2 checkKnoxvilleDivergence4M2 = new KnoxvilleDivergence4M2();

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public KnoxvilleDivergence4M2 KnoxvilleDivergence4M2(int candlesBack, int momentumPeriod, int rSIPeriod, int validBars)
        {
            return KnoxvilleDivergence4M2(Input, candlesBack, momentumPeriod, rSIPeriod, validBars);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public KnoxvilleDivergence4M2 KnoxvilleDivergence4M2(Data.IDataSeries input, int candlesBack, int momentumPeriod, int rSIPeriod, int validBars)
        {
            if (cacheKnoxvilleDivergence4M2 != null)
                for (int idx = 0; idx < cacheKnoxvilleDivergence4M2.Length; idx++)
                    if (cacheKnoxvilleDivergence4M2[idx].CandlesBack == candlesBack && cacheKnoxvilleDivergence4M2[idx].MomentumPeriod == momentumPeriod && cacheKnoxvilleDivergence4M2[idx].RSIPeriod == rSIPeriod && cacheKnoxvilleDivergence4M2[idx].ValidBars == validBars && cacheKnoxvilleDivergence4M2[idx].EqualsInput(input))
                        return cacheKnoxvilleDivergence4M2[idx];

            lock (checkKnoxvilleDivergence4M2)
            {
                checkKnoxvilleDivergence4M2.CandlesBack = candlesBack;
                candlesBack = checkKnoxvilleDivergence4M2.CandlesBack;
                checkKnoxvilleDivergence4M2.MomentumPeriod = momentumPeriod;
                momentumPeriod = checkKnoxvilleDivergence4M2.MomentumPeriod;
                checkKnoxvilleDivergence4M2.RSIPeriod = rSIPeriod;
                rSIPeriod = checkKnoxvilleDivergence4M2.RSIPeriod;
                checkKnoxvilleDivergence4M2.ValidBars = validBars;
                validBars = checkKnoxvilleDivergence4M2.ValidBars;

                if (cacheKnoxvilleDivergence4M2 != null)
                    for (int idx = 0; idx < cacheKnoxvilleDivergence4M2.Length; idx++)
                        if (cacheKnoxvilleDivergence4M2[idx].CandlesBack == candlesBack && cacheKnoxvilleDivergence4M2[idx].MomentumPeriod == momentumPeriod && cacheKnoxvilleDivergence4M2[idx].RSIPeriod == rSIPeriod && cacheKnoxvilleDivergence4M2[idx].ValidBars == validBars && cacheKnoxvilleDivergence4M2[idx].EqualsInput(input))
                            return cacheKnoxvilleDivergence4M2[idx];

                KnoxvilleDivergence4M2 indicator = new KnoxvilleDivergence4M2();
                indicator.BarsRequired = BarsRequired;
                indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
                indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
                indicator.Input = input;
                indicator.CandlesBack = candlesBack;
                indicator.MomentumPeriod = momentumPeriod;
                indicator.RSIPeriod = rSIPeriod;
                indicator.ValidBars = validBars;
                Indicators.Add(indicator);
                indicator.SetUp();

                KnoxvilleDivergence4M2[] tmp = new KnoxvilleDivergence4M2[cacheKnoxvilleDivergence4M2 == null ? 1 : cacheKnoxvilleDivergence4M2.Length + 1];
                if (cacheKnoxvilleDivergence4M2 != null)
                    cacheKnoxvilleDivergence4M2.CopyTo(tmp, 0);
                tmp[tmp.Length - 1] = indicator;
                cacheKnoxvilleDivergence4M2 = tmp;
                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.KnoxvilleDivergence4M2 KnoxvilleDivergence4M2(int candlesBack, int momentumPeriod, int rSIPeriod, int validBars)
        {
            return _indicator.KnoxvilleDivergence4M2(Input, candlesBack, momentumPeriod, rSIPeriod, validBars);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public Indicator.KnoxvilleDivergence4M2 KnoxvilleDivergence4M2(Data.IDataSeries input, int candlesBack, int momentumPeriod, int rSIPeriod, int validBars)
        {
            return _indicator.KnoxvilleDivergence4M2(input, candlesBack, momentumPeriod, rSIPeriod, validBars);
        }
    }
}

// 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.KnoxvilleDivergence4M2 KnoxvilleDivergence4M2(int candlesBack, int momentumPeriod, int rSIPeriod, int validBars)
        {
            return _indicator.KnoxvilleDivergence4M2(Input, candlesBack, momentumPeriod, rSIPeriod, validBars);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public Indicator.KnoxvilleDivergence4M2 KnoxvilleDivergence4M2(Data.IDataSeries input, int candlesBack, int momentumPeriod, int rSIPeriod, int validBars)
        {
            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.KnoxvilleDivergence4M2(input, candlesBack, momentumPeriod, rSIPeriod, validBars);
        }
    }
}
#endregion

Attached Files
Register to download File Type: cs KnoxvilleDivergence4M2.cs (15.5 KB, 6 views)
Reply With Quote
 
  #12 (permalink)
Elite Member
Muscat, Oman
 
Futures Experience: Intermediate
Platform: Ninja and MT4
Favorite Futures: Forex
 
Posts: 17 since Apr 2012
Thanks: 4 given, 3 received

Solved the error

I figured out that the problem was I was calling this indicator either in strategy or in Market Anlayzer, and it had drawing functions in the indicator. For some reason, that piece of code works fine on a chart, but breaks when calling it in the other two ways. So I just made two indicators and deleted the drawing functions in one. Now no errors when using MA and Strategies.

-Stearno



stearno View Post
I get the following error messages in the Output Window when I use this indicator with Market Analyzer.

"Sell set value[2] loc=2 the low being set:0.9676
01/12/2014 03:36:00 System.NullReferenceException: Object reference not set to an instance of an object.
at NinjaTrader.Indicator.KnoxvilleDivergence4M2.RSISellCheck(Int32 loc)
at NinjaTrader.Indicator.KnoxvilleDivergence4M2.OnBarUpdate()"

I put Print() throughout my code and was able to 'track down' which line of code is causing the error.

Code Excerpt:
 
Code
Values[2].Set(loc, Low[loc]); 
Print("Sell set value[2]" + " " + "loc="+loc+ " " + "the low being set:"+ Low[loc] );
So three questions:
1. I tried to use Try and Catch, but it did not seem to work on my custom methods (in this case: RSISellCheck(2) & RSIBuyCheck(2). Does Try and Catch work in only the standard methods (like OnBarUpdate) or does it also work in custom methods?
2. Why is this error coming and why is it only happening occassionally and not every bar. The value is always 2. So sometimes it assigns the Low[2] okay and sometimes it gives this error.
3. Why is it giving an error when it shows in the Print the actual Low price value?

Thank you for your help!

-Stearno

 
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
{
    [Description("Enter the description of your new custom indicator here")]
    public class KnoxvilleDivergence4M2 : Indicator
    {
        #region Variables
        
		private int candlesBack = 150; // Default setting for CandlesBack
        private int rSIPeriod = 21; // Default setting for RSIPeriod
        private int momentumPeriod = 20; // Default setting for MomentumPeriod
		
        private int validBars=4;
				int lastBarDivB=10000;
				int lastBarDivS=10000;
		
		private RSI rsi;
		private KnoxvilleMomentum mom;
		private DataSeries mydivergence;
		
        #endregion
//_______________________________________________________________________________________________________________________________________________________________
        protected override void Initialize()
        {
			Add(new Plot(Color.Yellow, "Momentum"));
			Add(new Plot(Color.Green, "DivBuy"));
			Add(new Plot(Color.Red, "DivSell"));
			Add(new Plot(Color.Blue, "Div"));
			Add(new Plot(Color.Blue, "AuxB"));
            Add(new Plot(Color.Blue, "AuxS"));
            Overlay				= false;
        }
//_______________________________________________________________________________________________________________________________________________________________
		protected override void OnStartUp()
		{
			rsi=RSI(rSIPeriod,1);
			mom=KnoxvilleMomentum(momentumPeriod);
		}
//_______________________________________________________________________________________________________________________________________________________________
        protected override void OnBarUpdate()
        {
			try
			{
			if (CurrentBar<candlesBack)return;
			if (CurrentBar<10)return;
			
			
			//BarsNew();
			Mom.Set(mom[0]);

			Values[1].Set(0.00);   // divBuy
			Values[2].Set(0.00);   // divSell
			
			//Print(Time[0]+"    "+Values[4][0]+"  "+Values[5][0]+"      "+Values[4][1]+"  "+Values[5][1]);
			if(CurrentBar>=lastBarDivB && CurrentBar-lastBarDivB<=validBars)Values[4].Set(1); else Values[4].Set(0.00);  
			if(CurrentBar>=lastBarDivS && CurrentBar-lastBarDivS<=validBars)Values[5].Set(1); else Values[5].Set(0.00);  
			//BarsNew();

			RSISellCheck(2);
			
			RSIBuyCheck(2);
			}
			catch (Exception e)
			{
				/* With our caught exception we are able to generate log entries that go to the Control Center logs and also print more detailed information
				about the error to the Output Window. */
				
				// Submits an entry into the Control Center logs to inform the user of an error
				Log("SampleTryCatch Error: Please check your indicator for errors.", LogLevel.Error);
				
				// Prints the caught exception in the Output Window
				Print(Time[0] + " " + e.ToString());
			}
				//Print(Time[0]+"... "+AuxB[0]+"  "+AuxS[0]+"      "+Values[1][0]+"  "+Values[2][0]);
			BarsNew();
			if(CurrentBar>=lastBarDivB && CurrentBar-lastBarDivB<=validBars)Values[4].Set(1); else Values[4].Set(0.00);  
			if(CurrentBar>=lastBarDivS && CurrentBar-lastBarDivS<=validBars)Values[5].Set(1); else Values[5].Set(0.00);  
        }

//_______________________________________________________________________________________________________________________________________________________________
	void BarsNew(){ int N=0, n=0;
		for(N=0;N<candlesBack;N++){
			if(Values[1][N]>0){for(n=N;n>N-validBars;n--){ if(n>=0)Values[4].Set(n, 1); } }
			if(Values[2][N]>0){for(n=N;n>N-validBars;n--){ if(n>=0)Values[5].Set(n, 1); } }
		}
				
	}
//_______________________________________________________________________________________________________________________________________________________________
		// FormatPriceMarker method of a custom indicator
 		public override string FormatPriceMarker(double price)
		{
    		// Formats price values to 4 decimal places
    		return price.ToString("N4");
		}
//_______________________________________________________________________________________________________________________________________________________________
		private void RSISellCheck(int loc)
		{
			double rsiMain = rsi[loc];
			if (rsiMain<50)
				return;
			for (int x=loc-1;x<=loc+2;x++)
			{
				if (High[x]>High[loc])
					return;
			}
	
			for (int y=loc+4;y<(loc+candlesBack);y++)
			{
				if (High[y]>High[loc])
					break;
				int s=y;
	
				for (int z=y-2;z<=y+2;z++)
				{
					if (High[z]>High[y])
					{
						y++;
						break;
					}
				}
				if (s!=y)
				{
					y--;
					continue;
				}
				bool ob = false;
		
				for (int k=loc-1;k<=y;k++)
				{
					double rsiob = rsi[k];
					if (rsiob>70)
					{
						ob=true;
						break;
					}
				}
				if (!ob)
					continue;

				double mom1 = mom[loc];
				double mom2 = mom[y];
				
				if (mom1>mom2)
					continue;
	
				Print("Sell set value[2]" + " " + "loc="+loc+ " " + "the low being set:"+ Low[loc] );
				Values[2].Set(loc, Low[loc]);  
				lastBarDivS=CurrentBar;       //Print(Time[0]+"B   lastB:"+lastBarDivB+" lastS:"+lastBarDivS);
				
				ChartObjectCollection coc = ChartControl.ChartObjects;  
				for (int x=0;x<coc.Count;x++)
				{
					ChartObject c = coc[x];
					if (c.DrawType==DrawType.Line)
					{
						ILine line = (ILine)c;
						if (line.StartBarsAgo==y)
							RemoveDrawObject(line);
					}
				}
				
				DrawLine(Time[loc].ToString()+"High",y,High[y],loc,High[loc],Color.Red);
				DrawOnPricePanel=false;
				DrawLine(Time[loc].ToString()+"MomHigh",y,mom2,loc,mom1,Color.Red);
				DrawOnPricePanel=true;
			}
		}
//_______________________________________________________________________________________________________________________________________________________________		
		private void RSIBuyCheck(int loc)
		{
			double rsiMain = rsi[loc];
			if (rsiMain>50)
				return;

			for (int x=loc-1;x<=loc+2;x++)
			{
				if (Low[x]<Low[loc])
					return;
			}

			for (int y=loc+4;y<(loc+candlesBack);y++)
			{
				if (Low[y]<Low[loc])
					break;
				int s=y;

				for (int z=y-2;z<=y+2;z++)
				{
					if (Low[z]<Low[y])
					{
						y++;
						break;
					}
				}
				if (s!=y)
				{
					y--;
					continue;
				}
				bool ob = false;

				for (int k=loc-1;k<=y;k++)
				{
					double rsiob = rsi[k];
					if (rsiob<30)
					{
						ob=true;
						break;
					}
				}
				if (!ob)
					continue;

				double mom1 = mom[loc];
				double mom2 = mom[y];
				if (mom1<mom2)
					continue;
	
				Print("Buy set value[2]" + " " + "loc="+loc+ " " + "the high being set:"+ High[loc] );
				Values[1].Set(loc, High[loc]);  
				lastBarDivB=CurrentBar;  //Print(Time[0]+"B   lastB:"+lastBarDivB+" lastS:"+lastBarDivS);
				
				
				ChartObjectCollection coc = ChartControl.ChartObjects;
				for (int x=0;x<coc.Count;x++)
				{
					ChartObject c = coc[x];
					if (c.DrawType==DrawType.Line)
					{
						ILine line = (ILine)c;
						if (line.StartBarsAgo==y)
							RemoveDrawObject(line);
					}
				}
				
				DrawLine(Time[loc].ToString()+"Low",y,Low[y],loc,Low[loc],Color.Red);
				DrawOnPricePanel=false;
				DrawLine(Time[loc].ToString()+"MomLow",y,mom2,loc,mom1,Color.Red);
				DrawOnPricePanel=true;
			}
		}
//_______________________________________________________________________________________________________________________________________________________________

        #region Properties
		
		[Browsable(false)]
		[XmlIgnore()]
		public DataSeries Mom
		{
			get { return Values[0]; }
		}
		[Browsable(false)]
		[XmlIgnore()]
		public DataSeries DivBuy
		{
			get { return Values[1]; }
		}		
		[Browsable(false)]
		[XmlIgnore()]
		public DataSeries DivSell
		{
			get { return Values[2]; }
		}
		
		[Browsable(false)]
		[XmlIgnore()]
		public DataSeries Div
		{
			get { return Values[3]; }
		}
		
		[Browsable(false)]
		[XmlIgnore()]
		public DataSeries AuxB
		{
			get { return Values[4]; }
		}
		[Browsable(false)]
		[XmlIgnore()]
		public DataSeries AuxS
		{
			get { return Values[5]; }
		}

        [Description("")]
        [GridCategory("Parameters")]
        public int CandlesBack
        {
            get { return candlesBack; }
            set { candlesBack = Math.Max(1, value); }
        }

        [Description("")]
        [GridCategory("Parameters")]
        public int RSIPeriod
        {
            get { return rSIPeriod; }
            set { rSIPeriod = Math.Max(1, value); }
        }

        [Description("")]
        [GridCategory("Parameters")]
        public int MomentumPeriod
        {
            get { return momentumPeriod; }
            set { momentumPeriod = Math.Max(1, value); }
        }
		
		[Description("")]
        [GridCategory("Parameters")]
        public int ValidBars
        {
            get { return validBars; }
            set { validBars = 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 KnoxvilleDivergence4M2[] cacheKnoxvilleDivergence4M2 = null;

        private static KnoxvilleDivergence4M2 checkKnoxvilleDivergence4M2 = new KnoxvilleDivergence4M2();

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public KnoxvilleDivergence4M2 KnoxvilleDivergence4M2(int candlesBack, int momentumPeriod, int rSIPeriod, int validBars)
        {
            return KnoxvilleDivergence4M2(Input, candlesBack, momentumPeriod, rSIPeriod, validBars);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public KnoxvilleDivergence4M2 KnoxvilleDivergence4M2(Data.IDataSeries input, int candlesBack, int momentumPeriod, int rSIPeriod, int validBars)
        {
            if (cacheKnoxvilleDivergence4M2 != null)
                for (int idx = 0; idx < cacheKnoxvilleDivergence4M2.Length; idx++)
                    if (cacheKnoxvilleDivergence4M2[idx].CandlesBack == candlesBack && cacheKnoxvilleDivergence4M2[idx].MomentumPeriod == momentumPeriod && cacheKnoxvilleDivergence4M2[idx].RSIPeriod == rSIPeriod && cacheKnoxvilleDivergence4M2[idx].ValidBars == validBars && cacheKnoxvilleDivergence4M2[idx].EqualsInput(input))
                        return cacheKnoxvilleDivergence4M2[idx];

            lock (checkKnoxvilleDivergence4M2)
            {
                checkKnoxvilleDivergence4M2.CandlesBack = candlesBack;
                candlesBack = checkKnoxvilleDivergence4M2.CandlesBack;
                checkKnoxvilleDivergence4M2.MomentumPeriod = momentumPeriod;
                momentumPeriod = checkKnoxvilleDivergence4M2.MomentumPeriod;
                checkKnoxvilleDivergence4M2.RSIPeriod = rSIPeriod;
                rSIPeriod = checkKnoxvilleDivergence4M2.RSIPeriod;
                checkKnoxvilleDivergence4M2.ValidBars = validBars;
                validBars = checkKnoxvilleDivergence4M2.ValidBars;

                if (cacheKnoxvilleDivergence4M2 != null)
                    for (int idx = 0; idx < cacheKnoxvilleDivergence4M2.Length; idx++)
                        if (cacheKnoxvilleDivergence4M2[idx].CandlesBack == candlesBack && cacheKnoxvilleDivergence4M2[idx].MomentumPeriod == momentumPeriod && cacheKnoxvilleDivergence4M2[idx].RSIPeriod == rSIPeriod && cacheKnoxvilleDivergence4M2[idx].ValidBars == validBars && cacheKnoxvilleDivergence4M2[idx].EqualsInput(input))
                            return cacheKnoxvilleDivergence4M2[idx];

                KnoxvilleDivergence4M2 indicator = new KnoxvilleDivergence4M2();
                indicator.BarsRequired = BarsRequired;
                indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
                indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
                indicator.Input = input;
                indicator.CandlesBack = candlesBack;
                indicator.MomentumPeriod = momentumPeriod;
                indicator.RSIPeriod = rSIPeriod;
                indicator.ValidBars = validBars;
                Indicators.Add(indicator);
                indicator.SetUp();

                KnoxvilleDivergence4M2[] tmp = new KnoxvilleDivergence4M2[cacheKnoxvilleDivergence4M2 == null ? 1 : cacheKnoxvilleDivergence4M2.Length + 1];
                if (cacheKnoxvilleDivergence4M2 != null)
                    cacheKnoxvilleDivergence4M2.CopyTo(tmp, 0);
                tmp[tmp.Length - 1] = indicator;
                cacheKnoxvilleDivergence4M2 = tmp;
                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.KnoxvilleDivergence4M2 KnoxvilleDivergence4M2(int candlesBack, int momentumPeriod, int rSIPeriod, int validBars)
        {
            return _indicator.KnoxvilleDivergence4M2(Input, candlesBack, momentumPeriod, rSIPeriod, validBars);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public Indicator.KnoxvilleDivergence4M2 KnoxvilleDivergence4M2(Data.IDataSeries input, int candlesBack, int momentumPeriod, int rSIPeriod, int validBars)
        {
            return _indicator.KnoxvilleDivergence4M2(input, candlesBack, momentumPeriod, rSIPeriod, validBars);
        }
    }
}

// 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.KnoxvilleDivergence4M2 KnoxvilleDivergence4M2(int candlesBack, int momentumPeriod, int rSIPeriod, int validBars)
        {
            return _indicator.KnoxvilleDivergence4M2(Input, candlesBack, momentumPeriod, rSIPeriod, validBars);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public Indicator.KnoxvilleDivergence4M2 KnoxvilleDivergence4M2(Data.IDataSeries input, int candlesBack, int momentumPeriod, int rSIPeriod, int validBars)
        {
            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.KnoxvilleDivergence4M2(input, candlesBack, momentumPeriod, rSIPeriod, validBars);
        }
    }
}
#endregion


Reply With Quote

Reply



futures io > > > > > Error: Object reference not set to an instance of an object.

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
object name ? sam028 NinjaTrader Programming 3 March 14th, 2011 10:55 AM
ninja trader object reference not set to an instance kk240 NinjaTrader Programming 4 December 29th, 2010 11:16 AM
need help :: cant fix :: Object reference not set to an instance of an object Trader.Jon NinjaTrader Programming 9 November 3rd, 2010 11:55 AM
Object reference not set to an instance... dsraider NinjaTrader Programming 5 July 30th, 2010 09:17 AM
EasyLanguage Object Reference philloo TradeStation 2 June 18th, 2010 03:56 AM


All times are GMT -4. The time now is 09:12 PM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts
Page generated 2017-12-17 in 0.09 seconds with 20 queries on phoenix via your IP 54.226.34.209