NexusFi: Find Your Edge


Home Menu

 





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


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one gomad with 5 posts (0 thanks)
    2. looks_two stearno with 2 posts (0 thanks)
    3. looks_3 Zondor with 2 posts (5 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 12,135 views
    2. thumb_up 10 thanks given
    3. group 3 followers
    1. forum 11 posts
    2. attach_file 4 attachments




 
Search this Thread

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

  #11 (permalink)
 stearno 
Muscat, Oman
 
Experience: Intermediate
Platform: Ninja and MT4
Trading: Forex
Posts: 15 since Apr 2012
Thanks Given: 4
Thanks Received: 3

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
Elite Membership required to download: KnoxvilleDivergence4M2.cs
Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
PowerLanguage & EasyLanguage. How to get the platfor …
EasyLanguage Programming
REcommedations for programming help
Sierra Chart
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
29 thanks
Just another trading journal: PA, Wyckoff & Trends
25 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
21 thanks
GFIs1 1 DAX trade per day journal
16 thanks
  #12 (permalink)
 stearno 
Muscat, Oman
 
Experience: Intermediate
Platform: Ninja and MT4
Trading: Forex
Posts: 15 since Apr 2012
Thanks Given: 4
Thanks Received: 3

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




Last Updated on December 22, 2014


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
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.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts