PDA

View Full Version : Testing for null


zoltran
11-18-2007, 08:36 PM
Is there a specific way to test for a null entry in a data series?
For example .. the following is standard code to color an indicator.
How would I test if 'Above0' wasn't 'Set'


if( zz >= 0 )
Above0.Set( zz );
else
Below0.Set( zz );


TIA

KBJ
11-18-2007, 10:08 PM
Check the part of the description of a DataSeries in the help where it talks about a "null" value (see: http://www.ninjatrader-support.com/HelpGuideV6/helpguide.html?DataSeriesObject)

Also, to just test for an unassigned value, you could do this:


if (!double.IsNaN( Above0[0] ))
{
Print( "Above0[0] is defined" );
}

zoltran
11-23-2007, 06:13 AM
Thanks KBJ. I did read that part of the doc.

So, given what it says there .. I can test a 'Reset' or un-assigned dataseries for '0' .. and it should work?

NinjaTrader_Josh
11-23-2007, 08:06 PM
Correct. The code KBJ posted is a method you can use.

NinjaTrader_Ray
11-25-2007, 12:50 PM
A better approach in NT6.5 is to use the provided method "ContainsValue(int barsAgo)". It returns a true or false value.


if (Above0.ContainsValue(0) == true)
// Do something

zoltran
11-25-2007, 01:44 PM
Arg :confused: I am being particularly thick sculled.... So .. let me see if I understand now.

Assuming Above0[0] was never set....

1- Above0.ContainsValue(0) will return false ? Because it was never set

2 if (double.IsNaN( Above0[0] )) will return true .. as it is a 'Nan' at this point (unset). (removed the !not from KGB's example)

3 if (Above0[0] == 0) will return false ..as it's not really '0' stored.. and can't be tested with the == operator.


Part of my confusion comes from the discussion on Reset in the help.
To me, this is saying i can test for 0 if I use Reset() 1st.

Calling the Reset() method is unique and can be very powerful for custom indicator development. DataSeries objects can hold null values which simply means that you do not want to store a value for the current bar. Mathematically, you can correctly assign a value of zero however if the DataSeries was the primary DataSeries of an indicator whose values would be used for plotting, you may NOT want a zero value plotted. Meaning, you want a zero value for proper calculations but not a zero value for chart visualization. The Reset() method allows you to reset the current bar's DataSeries value to a zero for calculation purposes but NinjaScript would ignore this value when it plotted it on a chart.

NinjaTrader_Josh
11-25-2007, 04:57 PM
1- Above0.ContainsValue(0) will return false ? Because it was never set
If you used Reset() I believe it will return true.

2 if (double.IsNaN( Above0[0] )) will return true .. as it is a 'Nan' at this point (unset). (removed the !not from KGB's example)
It will evaluate to true.

3 if (Above0[0] == 0) will return false ..as it's not really '0' stored.. and can't be tested with the == operator.
It will evaluate as false.

zoltran
11-25-2007, 05:09 PM
thanks all !