NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 12-14-2009, 06:29 PM   #1
kuroro13
Senior Member
 
Join Date: May 2008
Posts: 111
Thanks: 0
Thanked 0 times in 0 posts
Default using decimals

Hi,

I have a problem with an indicator I'm working on.

Basically, my indicator formula looks like following :

Myindicator.Set(varA*varB)

where varA and varB are double types(very basic). The thing is when I'm running it , it does not plot anything. I've checked the output window to point out the problem and it appears that it comes from my varA which is always equal to 0.

I think the problem comes from the type of varA .Since the formula looks like Constant/(Period*(Period-1)*(Period+1)), it obviously returns 0.0 when Period is set to 20 for example. The correct value is 0.00000xxx.

So I tried to change the value type to decimal in order to get the precision I need but when I do it, I'm forced to change the type of varB (also to decimal) and of course my indicator won't compile since Dataseries only holds double values.

I don't know what to do anymore. Could anyone help me ?

By the way ,why can't I print the values in the output window with my variables defined as decimals ?
kuroro13 is offline  
Reply With Quote
Old 12-15-2009, 07:29 AM   #2
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

kuroro13,

You should be able to print them.

Print(var1.ToString());

Please check your variables through these prints first to see what values they are really at. Would have imagined doubles to work with your values to begin with. If you are using such a small decimal value you may consider normalizing it by removing some zeros.
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-16-2009, 04:15 AM   #3
kuroro13
Senior Member
 
Join Date: May 2008
Posts: 111
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank you Josh for your answer.

I've checked all the code through the prints and i still have the same problem.
Here is the first part of the code :

public class filtre1 : Indicator
{
#region Variables
// Wizard generated variables
private int length = 20; // Default setting for Length
// User defined variables (add any user defined variables below)
private double f18=0.00;
private double f20;
private int f48=0;
private int f38=0;
#endregion

/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Plot0"));
Add(new Line(Color.FromKnownColor(KnownColor.ControlDark), 0, "Zero"));
CalculateOnBarClose = true;
Overlay = false;
PriceTypeSupported = false;
arr0 = new double [301];
arr1 = new double [301];
arr2 = new int [301];
arr3 = new int [301];
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>

protected override void OnBarUpdate()
{




if (f38 == 0)
{
f38=1;
f40=0;
if ((Length-1)>=2)
{
f30=Length-1;
}
else {f30=2;}

f48=f30+1;

f10=Close[0];

f18=12/(f48*(f48-1)*(f48+1));
Print(f18.ToString());
double f20=(f48+1)*0.5;
Print(f20.ToString());
}


When Length is set to 20 the output window shows me :
0
10.5

my f18 is always equal to 0 . I tried to normalize it by multipying it by a 1000 factor but then it returns me 1.

I'm a little bit lost there....
kuroro13 is offline  
Reply With Quote
Old 12-16-2009, 05:09 AM   #4
Ralph
Senior Member
 
Join Date: Jul 2008
Posts: 527
Thanks: 0
Thanked 9 times in 6 posts
Default

You could try this: f18=12.0/(f48*(f48-1)*(f48+1));
Ralph is offline  
Reply With Quote
Old 12-16-2009, 05:14 AM   #5
kuroro13
Senior Member
 
Join Date: May 2008
Posts: 111
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank you Ralph !!!

It works smoothly now...but now you should give me a little explanation on why ?!!! :-)
kuroro13 is offline  
Reply With Quote
Old 12-16-2009, 05:39 AM   #6
Ralph
Senior Member
 
Join Date: Jul 2008
Posts: 527
Thanks: 0
Thanked 9 times in 6 posts
Default

Hi kuroro13,

12 is an integer and if divided by a double, the double is converted to integer obviously, so your result is: 12/7980.0 -> 0.
12.0 is a double and so is the result: 12.0/7980.0 -> 0.0015

Regards
Ralph
Ralph 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
See more decimals in SMA jmreal Charting 3 08-06-2008 07:05 AM
Use the rounded value instead of the exact value with decimals? Ninja B Automated Trading 12 07-10-2008 02:06 PM
How to not display decimals LG General Programming 4 04-18-2008 02:04 PM
FX Entry Decimals trader2be Connecting 2 12-13-2007 09:24 PM
fractions/decimals Gar Miscellaneous Support 3 05-24-2005 03:07 AM


All times are GMT -6. The time now is 09:26 PM.