# Rare code issue

**David Diez:**

I wrote some lines using __double values__:

**This way I'm getting the desired result:**

2021.07.23 15:41:41.553 ProfitLocker (NZDCAD,H4) GlobalTarget is 77.76000000000001

2021.07.23 15:41:41.553 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

---

The problem comes when I'm trying to __switch some of those values into integer__ to set the proper code:

**Now this way I'm getting a unuseful interation for positions count and GlobalTarget=0:**

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 1

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) PartialTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 1

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 2

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) PartialTarget is 15.73

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 2

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 3

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) PartialTarget is 31.45

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 2

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 4

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) PartialTarget is 31.45

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 3

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 5

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositive is 3

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositions are 6

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositive is 3

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositions are 7

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositive is 3

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositions are 8

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

---

Also this unuseful iteration __makes the PartialTarget value erratic__ but **how the GlobalTarget could be 0 when TotalPositions count is more than 0?**

Any idea on how to get this properly working?

**Lee Chee Tat -:**

TotalPositions*1.

Really? ... __0*1 is 0.__

**Could I use PositionsTotal() instead** but doesn't solve the problem cause I'm trying to filter positions by some conditions.

The major problem besides on the value types. **Double values are working fine, something happen when I set some of them into integers**.

**David Diez:**

I wrote some lines using __double values__:

**This way I'm getting the desired result:**

2021.07.23 15:41:41.553 ProfitLocker (NZDCAD,H4) GlobalTarget is 77.76000000000001

2021.07.23 15:41:41.553 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

---

The problem comes when I'm trying to __switch some of those values into integer__ to set the proper code:

**Now this way I'm getting GlobalTarget=0 through the whole iteration:**

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 1

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) PartialTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 1

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 2

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) PartialTarget is 15.73

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 2

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 3

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) PartialTarget is 31.45

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 2

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 4

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) PartialTarget is 31.45

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 3

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 5

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositive is 3

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositions are 6

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositive is 3

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositions are 7

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositive is 3

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositions are 8

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

---

**How the GlobalTarget could be 0 when TotalPositions count is more than 0?**

Any idea on how to get this properly working?

Try this...

double GlobalTarget=NormalizeDouble(((double)TotalPositions/100)*Balance,2);Print("GlobalTarget is ",GlobalTarget);

**Nikolaos Pantzos:**

Try this...

Ok, I understand what you mean but it's basically having the same.

Double is working but those values should be marked as integer for better coding practice.

**David Diez:**

Ok, I understand what you mean but it's basically having the same.

Double is working but those values should be marked as integer for better coding practice.

Just try, not need to know. Just try!

The code to show you convert variable only for division, not for hole code.

For the rest of the code it remains as integer...

**Nikolaos Pantzos:**

Just try, not need to know. Just try!

The code to show you convert variable only for division, not for hole code.

For the rest of the code it remains as integer...

Yes but I need this just to make that division, there should be an explanation.

**David Diez:**Yes but I need this just to make that division, there should be an explanation.

Please understand that dividing by 100 or dividing by 100.0 will give totally different results.

In the former, 100 is considered an Integer literal constant and so it will carry out integer division which will truncate the decimal values.

In the latter, 100.0 will be considered a floating-point literal constant and it will carry out floating-point division which will keep the decimal values.

This is the principal behind the problems you are having. So, to prevent this, either use a floating-point literal constant or typecast the variable into a (double).

Example:

Print( "90 / 100 = ", 90 / 100 ); Print( "90 / 100.0 = ", 90 / 100.0 ); Print( "90.0 / 100 = ", 90.0 / 100 ); Print( "90.0 / 100.0 = ", 90.0 / 100.0 );2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90 / 100 = 0 2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90 / 100.0 = 0.9 2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90.0 / 100 = 0.9 2021.07.23 21:26:57.060 TestCopy EURUSD,H1: 90.0 / 100.0 = 0.9

**Fernando Carreiro:**

Please understand that dividing by 100 or dividing by 100.0 will give totally different results.

In the former, 100 is considered an Integer literal constant and so it will carry out integer division which will truncate the decimal values.

In the latter, 100.0 will be considered a floating-point literal constant and it will carry out floating-point division which will keep the decimal values.

This is the principal behind the problems you are having. So, to prevent this, either use a floating-point literal constant or typecast the variable into a (double).

Example:

Ok, I understand the explanation but this is such strange issue at all.

90/100 is very clear, there is no zero divide and GlobalTarget means a double value in fact .

**David Diez:**Ok, I understand the explanation but this is such strange issue at all. 90/100 is very clear, there is no zero divide and GlobalTarget means a double value in fact .

It is not strange at all and is actually quite common in many coding languages. Why would you think of a zero divide error? That has nothing to do with it!

"GlobalTarget" may be a "double" but you have declared "TotalPositions" as an integer, so (TotalPositions/100) will give a value of "0" if "TotalPositions" is less than 100.

That is why @Nikolaos Pantzos told you to cast it as a (double).

There is no "rare" or "strange" issue. It is just that you don't understand how it works yourself.

**Fernando Carreiro:**

It is not strange at all and is actually quite common in many coding languages. Why would you think of a zero divide error? That has nothing to do with it!

"GlobalTarget" may be a "double" but you have declared "TotalPositions" as an integer, so (TotalPositions/100) will give a value of "0" if "TotalPositions" is less than 100.

That is why @Nikolaos Pantzos told you to cast it as a (double).

There is no "rare" or "strange" issue. It is just that you don't understand how it works yourself.

Yes, it's ok, maybe the Nikolaos solution is cleaner than mine's but just didn't know about this coding issue.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets

You agree to website policy and terms of use

I wrote some lines using

double values:This way I'm getting the desired result:2021.07.23 15:41:41.553 ProfitLocker (NZDCAD,H4) GlobalTarget is 77.76000000000001

2021.07.23 15:41:41.553 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

---

The problem comes when I'm trying to

switch some of those values into integerto set the proper code:Now this way I'm getting GlobalTarget=0 through the whole iteration:2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 1

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) PartialTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 1

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 2

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) PartialTarget is 15.73

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 2

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 3

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) PartialTarget is 31.45

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 2

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 4

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) PartialTarget is 31.45

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositive is 3

2021.07.23 15:56:18.014 ProfitLocker (NZDCAD,H4) TotalPositions are 5

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositive is 3

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositions are 6

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositive is 3

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositions are 7

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositive is 3

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) TotalPositions are 8

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) GlobalTarget is 0.0

2021.07.23 15:56:18.015 ProfitLocker (NZDCAD,H4) PartialTarget is 47.18

---

How the GlobalTarget could be 0 when TotalPositions count is more than 0?Any idea on how to get this properly working?