NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 12-22-2009, 04:26 AM   #1
mefTrader
Senior Member
 
Join Date: Nov 2009
Posts: 205
Thanks: 0
Thanked 0 times in 0 posts
Default Help Debugging my indicator

Hi,

I have compiled and added my indicator to my chart but dont see any plot - trying to debug it using print statements to the output window yet dont see anything there either.. This is my first custom script....

Another question - when you recompile the indicator does the chart get automatically updated? or do you have to remove and add again?

Here are my variables

#region Variables:
// Wizard generated variables
private int length = 10; // Default setting for Length
private int length1 = 14; // Default setting for Length1
// User defined variables (add any user defined variables below)
private int Trend_flo =0;
private DataSeries PercentR; // Define a DataSeries variable
private int PerR =0;
private double StDev =0;
private int LastBar =0;
private DataSeries TrStop;
#endregion


Here is my initialize region:

protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Trend"));
CalculateOnBarClose = true;
Overlay = false;
PriceTypeSupported = true;
}

Here is main code:

protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
if(Time[0].Date != Time[1].Date)
{
Trend_flo =0;
Print("Trend_flo initial condition: "+ Trend_flo);

}
double StDev = StdDev(length1)[0];
//double PerR = PercentR(length);
double HH =0.0;
double Divisor = 0.0;
HH = MAX(High, length)[0];
Divisor = HH - MIN(Low, length)[0];
if (Divisor != 0)
PercentR.Set(100 - ( ( HH - Close[0] ) / Divisor ) * 100);
else
PercentR.Set(0.0);

if ((PercentR[0] >= 70) && (PercentR[1] < 70) && (Trend_flo < 1) && (Close[0] > TrStop[0]))
{
TrStop.Set(Low[0] - StDev*1.382);
Trend_flo = 1;
Print("Trend_flo Long: "+ Trend_flo);
LastBar = CurrentBar;
}

if ((PercentR[0] <= 30) && (PercentR[1] > 30) && (Trend_flo > -1) && (Close[0] < TrStop[0]))
{
TrStop.Set(High[0] + StDev*1.382);
Trend_flo = -1;
Print("Trend_flo Short: "+ Trend_flo);
LastBar = CurrentBar;
}

if (Trend_flo == 1)
{
if ((Open[0] <= TrStop[0]) | (High[0] <= TrStop[0]) | (Low[0] <= TrStop[0]) | (Close[0] <= TrStop[0]))
{
Trend_flo =0;
Print("End of Trend_flo Long: "+ Trend_flo);
}
else
{
if (LastBar < CurrentBar)
{
if (TrStop[0] == 0) TrStop.Set(Close[0]);
if (Close[0] > Close[1]) TrStop.Set(Low[0] - StDev * 1.382);
if (Low[0] - High[1] > StDev) TrStop.Set(Low[0] - StDev * 0.618);
if (TrStop[0] < TrStop[1]) TrStop.Set(TrStop[1]);
}

}
}

if (Trend_flo == -1)
{
if ((Open[0] >= TrStop[0]) | (High[0] >= TrStop[0]) | (Low[0] >= TrStop[0]) | (Close[0] >= TrStop[0]))
{
Trend_flo =0;
Print("End of Trend_flo Short: "+ Trend_flo);
}
else
{
if (LastBar < CurrentBar)
{
if (TrStop[0] == 0) TrStop.Set(Close[0]);
if (Close[0] < Close[1]) TrStop.Set(High[0] + StDev * 1.382);
if (Low[1] - High[0] > StDev) TrStop.Set(High[0] + StDev * 1.382);
if (TrStop[0] > TrStop[1]) TrStop.Set(TrStop[1]);
}

}
}

// Set the plot value
Trend.Set(Trend_flo);
//Trend.Set(1);

}
mefTrader is offline  
Reply With Quote
Old 12-22-2009, 06:15 AM   #2
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

Check the log tab for any errors.
Yes, you must refresh (F5) the chart after you compile
"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"

PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 12-22-2009, 06:23 AM   #3
mefTrader
Senior Member
 
Join Date: Nov 2009
Posts: 205
Thanks: 0
Thanked 0 times in 0 posts
Default

Checked the Logfile after doing a F5 and got the following message

"Error on calling the 'OnBarUpdate' method for indicator 'mfPercentRBrkout' on bar 0: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name:index

I presume it is referring to PercentR[1] in the following command ?

if ((PercentR[0] >= 70) && (PercentR[1] < 70) && (Trend_flo < 1) && (Close[0] > TrStop[0]))
Do I have initialiase a data series ? How does one do that?
mefTrader is offline  
Reply With Quote
Old 12-22-2009, 06:52 AM   #4
mefTrader
Senior Member
 
Join Date: Nov 2009
Posts: 205
Thanks: 0
Thanked 0 times in 0 posts
Default

I have changed the start of the main code to

protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
if (CurrentBar < length1)
{
//PercentR.Set(0,0.0);
//PercentR.Set(1,0.0);
return;
}
if(Time[0].Date != Time[1].Date)
{
Trend_flo =0;
Print("This is a message");
Print("Trend_flo initial condition: "+ Trend_flo);

}
double StDev = StdDev(length1)[0];
Print("StDev: "+ StDev);
//double PerR = PercentR(length);
double HH =0.0;
double Divisor = 0.0;
HH = MAX(High, length)[0];
Print("HH: "+ HH);
Divisor = HH - MIN(Low, length)[0];
Print("Divisor: "+ Divisor);
if (Divisor != 0)
PercentR.Set(100 - ( ( HH - Close[0] ) / Divisor ) * 100);
else
PercentR.Set(0.0);
Print("PercentR[0]: "+ PercentR[0].ToString());

if ((PercentR[0] >= 70) && (PercentR[1] < 70) && (Trend_flo < 1) && (Close[0] > TrStop[0]))
{
TrStop.Set(Low[0] - StDev*1.382);
Trend_flo = 1;
Print("This is a message");
Print("Trend_flo Long: "+ Trend_flo);
LastBar = CurrentBar;
}


It gets evaluated down as far as the highlighted in the red last line but I get the following error?

Error on calling the 'OnBarUpdate' method for indicator 'mfPercentRBrkout' on bar 14: Object reference not set to an instance of an object.

length1 is set to 14 so that makes sense but what does ' Object reference not set to an instance of an object." mean?

Where can one get a detailed explanation of possible Log Errors?
mefTrader is offline  
Reply With Quote
Old 12-22-2009, 06:59 AM   #5
mefTrader
Senior Member
 
Join Date: Nov 2009
Posts: 205
Thanks: 0
Thanked 0 times in 0 posts
Default

seems to be complaining about the following line

PercentR.Set(100 - ( ( HH - Close[0] ) / Divisor ) * 100);
mefTrader is offline  
Reply With Quote
Old 12-22-2009, 08:05 AM   #6
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
Default

The error message 'Object reference not set...' usually refers to accessing an object without first performing a null check (if (someObject !=null) {...}). If you post the complete code, I'll take a look and see if anything else looks out of place.
NinjaTrader_Austin is offline  
Reply With Quote
Old 12-29-2009, 07:56 PM   #7
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

mefTrader,

Is PercentR a plot, or a DataSeries you create for use internally? If you create it for internal use you have to instantiate it in the Initialize() function

Code:
PercentR = new DataSeries(this);
If its a plot, you have to add the plot at the bottom of the indicator. If you need more help please post the full code, or send me a PM if you don't want it on the forum.

mrLogik
"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"

PureLogikTrading
mrlogik is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Debugging douggreen Version 7 Beta General Questions & Bug Reports 6 10-16-2009 06:20 PM
More verbose debugging Argo1 Strategy Development 1 09-06-2009 01:00 PM
Debugging Strategy Socrato General Programming 4 07-20-2009 03:40 PM
JIT debugging issue SamuelHiggins Miscellaneous Support 5 01-15-2008 01:12 AM
Debugging using Visual Studio guym Indicator Development 2 11-28-2006 12:41 AM


All times are GMT -6. The time now is 10:07 PM.