Need Help on floating point error during Optimization  [SOLVED]

Questions about MultiCharts and user contributed studies.
Ming80
Posts: 24
Joined: 18 Jun 2012
Has thanked: 20 times
Been thanked: 1 time

Need Help on floating point error during Optimization

Postby Ming80 » 20 Mar 2013

Hi Guys,

I'm having trouble understanding what i'm doing wrong here. I have a simple code which
basically gives the crossover point between 2 moving averages.

This doesn't give any errors compiling or placing in a strat. However, it does give optimization errors when I try to optimize 2 of the moving averages at the same time.

The error message as follows: "Floating-point invalid operation"

Looking for help if anyone could point me in the direction on how to fix this.

Code: Select all

inputs:
Price(close),
FastAvg( 50 ),
SlowAvg( 200 ) ;

var:
CrossOverPoint(0);
//If CurrentBar > SlowAvg and CurrentBar > FastAvg then begin
CrossOverPoint = (summation(Price, SlowAvg - 1) * FastAvg
- summation(Price, FastAvg - 1)* SlowAvg)
/ (SlowAvg - FastAvg) ;

{Entries}
if MarketPosition <> 1 then
Buy next bar at CrossOverPoint Stop ;
if MarketPosition <> -1 then
SellShort next bar at CrossOverPoint Stop ;
//End; {End Strat}

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

Re: Need Help on floating point error during Optimization

Postby TJ » 20 Mar 2013

Hi Guys,
I'm having trouble understanding what i'm doing wrong here. I have a simple code which
basically gives the crossover point between 2 moving averages.
This doesn't give any errors compiling or placing in a strat. However, it does give optimization errors when I try to optimize 2 of the moving averages at the same time.
The error message as follows: "Floating-point invalid operation"
Looking for help if anyone could point me in the direction on how to fix this.

Code: Select all

inputs:
Price(close),
FastAvg( 50 ),
SlowAvg( 200 ) ;
var:
CrossOverPoint(0);
//If CurrentBar > SlowAvg and CurrentBar > FastAvg then begin
CrossOverPoint = (summation(Price, SlowAvg - 1) * FastAvg
- summation(Price, FastAvg - 1)* SlowAvg)
/ (SlowAvg - FastAvg) ;
{Entries}
if MarketPosition <> 1 then
Buy next bar at CrossOverPoint Stop ;
if MarketPosition <> -1 then
SellShort next bar at CrossOverPoint Stop ;
//End; {End Strat}
One of the reasons for the error is simply that invalid usage of the mathematical function is attempted.

Please copy the complete error message here.

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: Need Help on floating point error during Optimization

Postby sptrader » 20 Mar 2013

I think I've had a "Floating-point invalid operation" error, when dividing by zero...

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: Need Help on floating point error during Optimization  [SOLVED]

Postby ABC » 20 Mar 2013

I think I've had a "Floating-point invalid operation" error, when dividing by zero...
I second that. To make sure to avoid problems like that it's good practice to check for denominators not being zero. In your case try for example

Code: Select all

if (SlowAvg - FastAvg) <> 0 then
CrossOverPoint = (summation(Price, SlowAvg - 1) * FastAvg
- summation(Price, FastAvg - 1)* SlowAvg)
/ (SlowAvg - FastAvg)
else
//what ever value you want to use in cases of (SlowAvg - FastAvg) being 0
CrossOverPoint = 0;
Regards,
ABC

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Need Help on floating point error during Optimization

Postby Henry MultiСharts » 20 Mar 2013

You may also find our Wiki article Floating-Point Division by Zero Error Message useful.

Ming80
Posts: 24
Joined: 18 Jun 2012
Has thanked: 20 times
Been thanked: 1 time

Re: Need Help on floating point error during Optimization

Postby Ming80 » 20 Mar 2013

Thanks guys, especially ABC,

Was a bit perplexed when the code could run but unable to optimized. Indeed this was solved by checking/handling the denominators. Not sure if it was a particular parameter which caused the error but anyhow thanks!


Return to “MultiCharts”