Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

getRequestedAmount - getAmount Problem
 Post subject: getRequestedAmount - getAmount Problem Post rating: 0   New post Posted: Tue 31 Jan, 2012, 16:07 

User rating: 0
Joined: Mon 23 Jan, 2012, 20:42
Posts: 5
Location: SwitzerlandSwitzerland
Hi,

I have a problem that I cannot solve. I make an order in EUR/USD for 1 lot,
in the onMessage function I call order.getRequestedAmount once and order.getAmount once.
The order state is ORDER_FILLED.
getRequestAmount returns me 0.9999999
getAmount return me 0.999999
So the is one more nine in getRequestAmount.
So when I compare this two amounts to check if order has been filled totally (which is the case), result do not match.
The same happens for lot size of: 0.1,0.2 etc...

Do you have a solution ?

        if(messageOrder.getState() == IOrder.State.FILLED) {
                double o = messageOrder.getRequestedAmount();
                double a = messageOrder.getAmount();
                DecimalFormat df = new DecimalFormat("0.00000000000000000000");
                console.getOut().println(key + " : order amount: "+df.format(o));
                console.getOut().println(key + " : order requested amount: "+df.format(a));
                int result = Double.compare(o, a);
                if(result != 0) {
                    console.getOut().println(key + " : Order amounts do not match !");
                    messageOrder.setRequestedAmount(0);
                }
        }

order requested amount outputs: 0.99999990000000000000
order mount outputs: 0.99999900000000000000

Of course every time I get the print: Order amounts do not match !

Many thanks in advance for any answer.


 
 Post subject: Re: getRequestedAmount - getAmount Problem Post rating: 0   New post Posted: Tue 31 Jan, 2012, 16:37 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
See:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Quote:
double data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead.

For double comparison use:
https://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html#compare(double,%20double)


 
 Post subject: Re: getRequestedAmount - getAmount Problem Post rating: 0   New post Posted: Tue 31 Jan, 2012, 17:06 

User rating: 0
Joined: Mon 23 Jan, 2012, 20:42
Posts: 5
Location: SwitzerlandSwitzerland
Hi,

Thanks for your reply.
I just copied the sample code from the docs. All the samples do not use BigDecimal.
My new code is:

   IOrder messageOrder = message.getOrder();
       if(messageOrder.getState() == IOrder.State.FILLED) {
       BigDecimal o = new BigDecimal(originalOrder.getRequestedAmount());
       BigDecimal a = new BigDecimal(originalOrder.getAmount());
       if(!o.equals(a)) {
          console.getOut().println("Amounts do not match !");
          messageOrder.setRequestedAmount(0);
       }
    }


I still get the difference.

Quote:
double data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead.

getRequestedAmount and getAmount do return double data type.
By the way all methods from API do return double.

How can I proceed ?

P.S. I enter my order as follows:
   IOrder order = engine.submitOrder(label, instrument, oCommand, 1, price, 2, stopLoss, 0);


 
 Post subject: Re: getRequestedAmount - getAmount Problem Post rating: 0   New post Posted: Tue 31 Jan, 2012, 17:27 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
cricri wrote:
I still get the difference.
You are mixing here two things - o.equals(a) compares two objects, not their values, for BigDecimal comparison use:
https://docs.oracle.com/javase/1.4.2/docs/api/java/math/BigDecimal.html#compareTo(java.math.BigDecimal)
There is less hustle if you use the method that we suggested before:
https://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html#compare(double,%20double)
Please read its documentation and find usage examples.
cricri wrote:
By the way all methods from API do return double.
Because they are precise enough for the purpose they are used, you only need to take care when:
  1. Comparing to another value.
  2. Printing the values, see examples: https://www.dukascopy.com/wiki/#IConsole/Logging_values.


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com