Floating-point division by zero errors

Questions about MultiCharts and user contributed studies.
iso
Posts: 204
Joined: 08 Feb 2006
Has thanked: 1 time
Been thanked: 1 time

Floating-point division by zero errors

Postby iso » 17 Oct 2011

Hi,

I keep getting floating-point errors in my strategy code. I don't get these errors in TS. Does anyone have any ideas?

Thanks

Code: Select all

long_entry = highest(h,entry_chan) + entry_offset;
short_entry = lowest(l,entry_chan) - entry_offset;

tl_stop = tl_getvalue(1, currentdate, currenttime);

//============================= POSTION SIZING =============================
if direction = 1 then
if lowest(l,5) > tl_stop then istop = tl_stop else if lowest(l,5) <= tl_stop then istop = lowest(l,5) - entry_offset;
value1 = (risk /(long_entry - istop))/100;

if direction = -1 then
if highest(h,5) < tl_stop then istop = tl_stop else if highest(h,5) >= tl_stop then istop = highest(h,5) + entry_offset;
value1 = (risk /(istop - short_entry))/100;

//============================= ROUNDING =============================
if value1 < 1 then value2 = round(value1,2);
if value1 > 1 then value2 = round(value1,1);
if value1 > 5 then value2 = round(value1/.5,0)*.5;
if value1 > 10 then value2 = round(value1,0);

positionsize = value2 * 100;

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Floating-point division by zero errors

Postby furytrader » 17 Oct 2011

From a debugging standpoint, I would see if either of the following two lines:

Code: Select all

value1 = (risk /(long_entry - istop))/100;
value1 = (risk /(istop - short_entry))/100;
could create a division by zero error. You could rewrite these to be as follows:

Code: Select all

If long_entry <> istop then value1 = (risk /(long_entry - istop))/100;
If istop <> short_netry then value1 = (risk /(istop - short_entry))/100;
This would ensure that neither of these equations could possibly get divided by zero.

This may not solve your problem, but it's where I'd start looking.

I agree that it's weird that this code generates an error in MC but not TS.

iso
Posts: 204
Joined: 08 Feb 2006
Has thanked: 1 time
Been thanked: 1 time

Re: Floating-point division by zero errors

Postby iso » 17 Oct 2011

Thanks for the reply, that helps. It stopped with the floating-point errors and the short side works. The long side doesn't give me any errors but it just sits there not placing the entry order.

Thanks for the help, I appreciate it....

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

Re: Floating-point division by zero errors

Postby TJ » 17 Oct 2011

You can help yourself by formating the code for easy reading.

I have reformated the code for your review.
I have added 2 BEGIN/END combinations, so that you can visualize the logic.
I am sure you will find a few things that you would want to change after viewing my formating:

Code: Select all

long_entry = highest(h,entry_chan) + entry_offset;
short_entry = lowest(l,entry_chan) - entry_offset;

tl_stop = tl_getvalue(1, currentdate, currenttime);

//===== POSTION SIZING =====

if direction = 1 then
BEGIN
if lowest(l,5) > tl_stop then
istop = tl_stop
else
if lowest(l,5) <= tl_stop then
istop = lowest(l,5) - entry_offset;
END;

value1 = (risk /(long_entry - istop))/100;

if direction = -1 then
BEGIN
if highest(h,5) < tl_stop then
istop = tl_stop
else
if highest(h,5) >= tl_stop then
istop = highest(h,5) + entry_offset;
END;

value1 = (risk /(istop - short_entry))/100;

//===== ROUNDING =====

if value1 < 1 then value2 = round(value1,2);
if value1 > 1 then value2 = round(value1,1);
if value1 > 5 then value2 = round(value1/.5,0)*.5;
if value1 > 10 then value2 = round(value1,0);

positionsize = value2 * 100;

iso
Posts: 204
Joined: 08 Feb 2006
Has thanked: 1 time
Been thanked: 1 time

Re: Floating-point division by zero errors

Postby iso » 17 Oct 2011


You could rewrite these to be as follows:

Code: Select all

If long_entry <> istop then value1 = (risk /(long_entry - istop))/100;
If istop <> short_netry then value1 = (risk /(istop - short_entry))/100;
This would ensure that neither of these equations could possibly get divided by zero.
What actually is this saying? If it's not equal to zero then do the calculation? Any ideas on why it seemed to work on the short side but not the long side?

Thanks

iso
Posts: 204
Joined: 08 Feb 2006
Has thanked: 1 time
Been thanked: 1 time

Re: Floating-point division by zero errors

Postby iso » 18 Oct 2011

I simplified my code so I could debug it. See the attached snapshot. It looks like I am getting the floating-point errors because the entry (highest(h,1) + .02) is outputting an incorrect value. In the screenshot the correct value should be 116.38 but instead it is coming back with 115.58. Could this be a bug or am I looking at this wrong?

Thanks
Attachments
11-10-18-14.19.36001.png
(83.97 KiB) Downloaded 1244 times

iso
Posts: 204
Joined: 08 Feb 2006
Has thanked: 1 time
Been thanked: 1 time

Re: Floating-point division by zero errors

Postby iso » 18 Oct 2011

also when I run this on a forex instrument I am not getting all of the decimals in the print log output. Is this just not displaying all of the values or is it actually rounding it off?
Attachments
11-10-18-14.30.07001.png
(85.94 KiB) Downloaded 1249 times

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Floating-point division by zero errors

Postby furytrader » 19 Oct 2011

It's difficult to say why you are getting that errant value when you try to calculate the long entry. One thing I always do when I am debugging something is that I always include the date and time of the price bar that is generating the printed values - so I can make sure that I am looking at the right data points.

As for outputting the correct number of decimals, you can use the NumToStr(value,decimal places) command which should help with data output.

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

Re: Floating-point division by zero errors

Postby TJ » 19 Oct 2011


You could rewrite these to be as follows:

Code: Select all

If long_entry <> istop then value1 = (risk /(long_entry - istop))/100;
If istop <> short_netry then value1 = (risk /(istop - short_entry))/100;
This would ensure that neither of these equations could possibly get divided by zero.
What actually is this saying? If it's not equal to zero then do the calculation? Any ideas on why it seemed to work on the short side but not the long side?

Thanks
Have you looked at the reformatted code I posted above?
Do you understand it? or do you think that is irrelevant?

iso
Posts: 204
Joined: 08 Feb 2006
Has thanked: 1 time
Been thanked: 1 time

Re: Floating-point division by zero errors

Postby iso » 19 Oct 2011

Hi,

I used your reformatted code and I am sure it is cleaner but it didn't do anything for the issue's that I am running into. This seems to be some kind of bug as I can run this in TS with no problems. Something is causing the price channel to come back with an incorrect value. Here is another screenshot from right now. As you can see it is trying to get the value for the high of one bar ago but for some reason it is pulling a value from where price hasn't even been and it is also below the value of the stop loss, which is causing the division errors.
Attachments
11-10-19-14.26.55001.png
(112.47 KiB) Downloaded 1248 times

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Floating-point division by zero errors

Postby furytrader » 19 Oct 2011

Weird, because I ran a simplified version of your code on a futures chart and it came up with the right answer when it comes to calculating the variable Long_Entry. This is the code I used:

Code: Select all

Input: Risk(100), Entry_Chan(1), Offset(2);
Vars: Long_Entry(0), TL_STOP(0), Dollar_Risk(0), PositionSize(0);

Long_Entry = highest(High,entry_chan) + offset point;
TL_Stop = TL_GetEndVal(1);
Dollar_Risk = Long_Entry - TL_STOP;
PositionSize = risk / dollar_risk;

If LastBarOnChart = TRUE Then Begin
Print("Entry - ", Long_Entry);
ENd;
The reason I used "LastBarOnChart" is to make it easy for me to confirm which bar price levels the most recent calculation is referring to (this is why I mentioned earlier to always include the bar date and bar time whenever you use a print statement). You wouldn't necessarily want to use that in your original code.

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

Re: Floating-point division by zero errors

Postby TJ » 19 Oct 2011

Hi,

I used your reformatted code and I am sure it is cleaner but it didn't do anything for the issue's that I am running into. This seems to be some kind of bug as I can run this in TS with no problems. Something is causing the price channel to come back with an incorrect value. Here is another screenshot from right now. As you can see it is trying to get the value for the high of one bar ago but for some reason it is pulling a value from where price hasn't even been and it is also below the value of the stop loss, which is causing the division errors.
My reformatted code is NOT supposed to change the operation of your code.
The purpose of the reformatting, if you read my post again, is so that you can see the logic better.

Can you see the logic better? Or it reads the same to you?

Look at the code again... specifically the value1 calculation.
Can you see the long side of value1 is ALWAYS overwritten by the short side?

Now go back to your original code... can you see any logic flow in that spaghetti?



ps. I would avoid using generic variable names (ie value1, condition1, etc.).
Variable names are free, they do not cost money. A specific name will help you debug when you have to trace your code.

iso
Posts: 204
Joined: 08 Feb 2006
Has thanked: 1 time
Been thanked: 1 time

Re: Floating-point division by zero errors

Postby iso » 19 Oct 2011

I see what you are saying. The code I posted originally was modified from the original as I was adding and deleting stuff to try to get it to work. The original is...

if direction = 1 then
value1 = risk /(long_entry - istop);

if direction = -1 then
value1 = risk /(istop - short_entry);


I will add "LastBarOnChart" to see if that helps what's being returned by highest(h,1)


Thanks for the input

iso
Posts: 204
Joined: 08 Feb 2006
Has thanked: 1 time
Been thanked: 1 time

Re: Floating-point division by zero errors

Postby iso » 19 Oct 2011

For fx symbols, shouldn't it pull the decimal places from the instruments price scale?

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Floating-point division by zero errors

Postby furytrader » 19 Oct 2011

The decimal points that it pulls from the scale, for calculating purposes, and how it prints it out in the print log are two separate things - just because it doesn't show all the decimals when you print it out (unless your force it to, by using NumToStr() ) doesn't mean that it's not pulling the information out when you assign the variable. EasyLanguage is weird like that.

iso
Posts: 204
Joined: 08 Feb 2006
Has thanked: 1 time
Been thanked: 1 time

Re: Floating-point division by zero errors

Postby iso » 19 Oct 2011

interesting, thanks


Return to “MultiCharts”