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 02-15-2012, 09:03 AM   #1
ShruggedAtlas
Member
 
Join Date: Jun 2011
Posts: 58
Thanks: 23
Thanked 0 times in 0 posts
Default Value outside of range - divide by zero issue

I've got a simple indicator that compares the relative changes in price to changes in another oscillator. I've printed the resulting data to the output window and the values vary from negative to positive and are in a relatively tight range except when there is no change in price and I end up with a spike to infinity (which prints to the output window) this seems to occurr because when price doesn't change there is a divide by zero incident.

Is there a way to check for divide by zero and not plot that? or is there another approach I should take to compare the relative changes of two sets of data?

Here is my code:

Code:
			double RsiDelta = (RSI(2,3)[0] + RSI(2,3)[1] + RSI(2,3)[2]) - 
								(RSI(2,3)[1] + RSI(2,3)[2] + RSI(2,3)[3]);
			
			double PriceDelta = (Close[0] - Close[1]) * 100;
			
			Print("RsiDelta is " + RsiDelta);
			Print("PriceDelta is " + PriceDelta);
			Print("RsiDelta divided by PriceDelta is " + RsiDelta / PriceDelta);

            PTR.Set(RsiDelta / PriceDelta);
ShruggedAtlas is offline  
Reply With Quote
Old 02-15-2012, 09:08 AM   #2
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

ShruggedAtlas,

double PriceDelta = (Close[0] - Close[1]) * 100;

and

PTR.Set(RsiDelta / PriceDelta);

Is the culprit here. This may be zero, as such you need to check :

if ( PriceDelta < Double.Epsilon )
{
//do something
}

You may want to use the prior PriceDelta that wasn't zero, i.e. keep track of the previous PriceDelta and only update the previous one if PriceDelta isn't zero.

Please let me know if I may assist further.
Last edited by NinjaTrader_AdamP; 02-15-2012 at 09:23 AM.
NinjaTrader_AdamP is offline  
Reply With Quote
The following 2 users say thank you to NinjaTrader_AdamP for this post:
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
Range bar plotting issue ikeaboy Charting 13 08-14-2011 05:12 PM
formula // multiply // divide values in strategy wizard resist Strategy Development 1 10-04-2010 01:28 PM
B18 - issue with drawing a ray - range bar chart toddaclark Version 7 Beta General Questions & Bug Reports 1 07-01-2010 04:13 AM
Beta 16: issue and crash with range bars dfumagalli Version 7 Beta General Questions & Bug Reports 3 06-01-2010 11:54 AM
Divide By Zero tb2000 General Programming 2 07-01-2008 11:21 PM


All times are GMT -6. The time now is 01:59 AM.