Dark Theme
Light Theme
Trading Articles
Article Categories
Article Tools
Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to
register in order to view the content of the threads and start contributing to our community.
It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Error: Object reference not set to an instance of an object.
Updated December 22, 2014
Top Posters
looks_one
gomad
with 5 posts (0 thanks)
looks_two
Zondor
with 2 posts (5 thanks)
looks_3
stearno
with 2 posts (0 thanks)
looks_4
devdas
with 1 posts (1 thanks)
trending_up
12,143 views
thumb_up
10 thanks given
group
3 followers
forum
11 posts
attach_file
4 attachments
Error: Object reference not set to an instance of an object.
December 18th, 2014, 10:35 AM
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
Can you help answer these questions from other members on NexusFi?
Best Threads (Most Thanked) in the last 7 days on NexusFi
December 22nd, 2014, 08:09 AM
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
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
Last Updated on December 22, 2014