floating point error  [SOLVED]

Questions about MultiCharts and user contributed studies.
stefanols
Posts: 51
Joined: 01 Jan 2014
Has thanked: 14 times
Been thanked: 2 times

floating point error

Postby stefanols » 14 Apr 2015

Hi,

Anyone that can help with below code?

Compile ok but get invalid floating point error.

Thanks in advance!



input: Length(21);

Value1= Sum((2*close-high-low)/(high-low)*volume,Length)/Sum(volume,Length);

Plot1( Value1, "IntraDI" ) ;
Plot2(0);

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: floating point error

Postby tony » 14 Apr 2015

In your denominator you have (high - low). That could be 0 thus the floating point zero error. Change the equation so you are not dividing by anything that could be 0.

stefanols
Posts: 51
Joined: 01 Jan 2014
Has thanked: 14 times
Been thanked: 2 times

Re: floating point error

Postby stefanols » 14 Apr 2015

ok thanks for the input:

I changed to

input: Length(21);

Value1= Sum((2*close-high-low)/(high-(low * 0.999999999999999999999999999))*volume,Length)/Sum(volume,Length);

Plot1( Value1, "IntraDI" ) ;
Plot2(0);

Or are there a more elegant way to do it?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: floating point error

Postby TJ » 14 Apr 2015

I would break down the components for easy reading and debugging.

Code: Select all


input: Length(21);

var:
IntraDI(0),
numer1(0),
denom1(0),
numer(0),
denom(0);

numer1 = (2*close-high-low) ;
denom1 = (high-low)*volume ;

if denom1 <> 0 then
numer = sum( numer1 / denom1 , Length );

denom = Sum(volume, Length );

if denom <> 0 then
IntraDI = numer / denom;

Plot1( IntraDI, "IntraDI" ) ;
Plot2(0);

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: floating point error  [SOLVED]

Postby TJ » 14 Apr 2015

ps.
[FAQ] How to Post Codes (that people can read)
viewtopic.php?f=16&t=11713


Return to “MultiCharts”