14,239,593 members
Rate this:
See more:
I want to use double value in loop and get out put like this !! 0.00020
0.00040
0.00060

I am working on trading program in mql4 its like c , if price goes up 20points trade should buy and if price goes down 20 points trade should sell. it should be continue point to point.

```double Level1 = Open;                            // ticket1=OrderSend(Symbol(),1,Lots,Bid,3,0,Bid-TakeProfit*Point,"jems");
double i = 0.00020;
double j = 0.00020;
int ticket1;
int ticket2;
int Total = OrdersTotal();
int Prd = Period();
double Price = Bid;
double value1 = 0.00020;

if(Price > Level1 + value1 )

{

for (i = 0.00000 ;Level1 + i < Price; i++ )

{
i = i + 0.00020;
Sleep(10000);

}

}
else if (Price < Level1 - value1)
{

for (j = 0.00000 ; Level1 - j < Price ; j++)
{
j = j + 0.00020;
Sleep(10000);

}
return(0);
}

}```
Posted
Updated 15-Aug-13 21:28pm
v2
BotCar 16-Aug-13 3:42am

What's the question?

Rate this:

## Solution 2

```#include <stdio.h>

printf("value: %.5f", 0.02);```

Note: The usage of a BigDecimal-like class or a fixedpoint class (with a scale factor of 10^x) instead of double or float would be more appropriate here because with that you would be able to reduce error coming from floating point calculations to zero. Serious banking applications often make use of BigDecimal libraries.

- Depending on your problem using a fixed-point class instead of double/float may be enough: Fixed-point arithmetic[^]. If a fixed number of digits in the fraction isn't suitable for you then fixed-point isn't a good solution.

- If fixed-point arithmetic isn't enough then you should use a library that implements the decimal32, decimal64, or decimal128 floating-point types listed in a table on this page: IEEE floating point[^]. decimalXXX types are also floating point types like binary32(float) and binary(64)double but they use 10 as the base of the exponent instead of 2 so they can represent all decimal numbers accurately within a specified range. Unlike with fixed point types you can move the decimal point as you can with float/double.

- If the range of the decimal implementation isn't enough then you can choose a BigDecimal library that can store arbitrarily large decimal numbers accurately. Banking applications use this solution to make calculations with the amount of money on each account.
v3
H.Brydon 18-Aug-13 23:24pm

Exactly, float and double should not be used for any representation of currency (including stock prices).

+5
pasztorpisti 19-Aug-13 3:43am

Thank you! float and double are just one way of approximating real numbers (with hardware support!!!) but with relatively unpredictable errors. In many cases a problem is discrete (like currency) and people (including myself) are using float/double just because of convenience (float and double are available immediately in C/C++) especially when small errors are allowed even if an int/fixedpoint/decimal would be a better solution...
Rate this:

## Solution 1

redesign your solution to "event driven" concept. In that case you have a function which gets a new qoute and in that youte you make your decision.

Do NOT use Sleep!!!

conceptual Metacode

```int OnNewQoute( double qoute )
{
if( qoute > sellLimit )
{
doSellAction();
return 1;
}

return 0;
}```
Rate this:

## Solution 3

hi there,
Like pasztorpisti said, increase the resolution of your number to in order to work with sych very low floating point number,

for example: 0.00020 is the same as 0.20 / 1000,
it is like change the lens of your camera in order to focus on a very small object
i think you can return correct values this way

Good Luck,
z3ngew
pasztorpisti 16-Aug-13 11:28am

With this trick you can decrease the amount of errors but it won't be zero. The problem with the float and double types is that the base of their exponent is 2. (sign * mantissa * 2^e) Because of this it can not represent numbers like 0.1 and 0.2 accurately. With a BigNumber/BigDecimal library used for banking applications the base of exponent is 10, this way we can represent all decimal number accurately (with finite number of digits in the fraction). A fixed point solution has a limitation: There a fix scalefactor must be used as a constant (so we have to choose how many digits to use in the fraction part) but we can choose this constant to be any exponent of 10 if we want despite the fact that most performance critical fixedpoint implementations use a scalefactor that fits into 1-2 bytes, an exponent with base 256 or 2 when a bit of error in converting to decimal representation (like tostring does) isn't an issue.
Rate this:

## Solution 4

```for (i = 0.00000; Level1 + i < Price; i++ )
{
i = i + 0.00020;```

Do this:

```for (i = 0.00000; Level1 + i < Price; i += 0.00020 )
{  ```

The ++ adds 1.0 to the iterator variable, in this case "i". You are then adding .00020 to it inside the loop. The second method just adds .00020 to the iterator which is what I think you were after.

Honestly though you are going about it all wrong, I would take the approach that Karsten took, however depending on if you are really using "C" (not C# or C++), then event driven code is difficult. You should loop your price and then call a function that checks if you should buy or sell. Its very difficult from what you wrote to figure out exactly what you are trying to do.
v2