Dukascopy
 
 
Wiki JStore Search Login

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

    Submit JForex API bug reports in this forum only.
    Submit Converter issues in Converter Issues.
    Off topics are strictly forbidden.

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

Strange way to add Doubles
 Post subject: Strange way to add Doubles Post rating: 0   New post Posted: Mon 13 Apr, 2015, 16:53 
User avatar

User rating: 0
Joined: Fri 17 Jan, 2014, 18:13
Posts: 72
Location: NetherlandsNetherlands
On an order my nifty order manager tries to Up or Down the OpenPrice with one pip. Sometimes it works, most of the times it does not.

Why? Because of a strange way of adding or subtracting. See the images.

The code, where ding is the order and ins is the Instrument:
Image

The output. When the output shows the x.999999999, the OpenPrice is not changed.
Image


Attachments:
Schermafbeelding 2015-04-13 om 17.57.38.png [30.68 KiB]
Downloaded 310 times
Schermafbeelding 2015-04-13 om 17.51.16.png [13.97 KiB]
Downloaded 351 times
Schermafbeelding 2015-04-13 om 17.51.10.png [12.84 KiB]
Downloaded 232 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: Strange way to add Doubles Post rating: 0   New post Posted: Tue 14 Apr, 2015, 06:47 
User avatar

User rating: 0
Joined: Fri 17 Jan, 2014, 18:13
Posts: 72
Location: NetherlandsNetherlands
In addition: it only happens with JPY.

This really sucks. Whatever i do, adding the pipvalue to the price gives back strange results and only at JPY.


 
The Best Answer  Post subject: Re: Strange way to add Doubles Post rating: 0   New post Posted: Wed 15 Apr, 2015, 09:04 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Custom price may need to be rounded:
ding.setOpenPrice(round( ding.getOpenPrice() + ins.getPipValue() ));

private double round(double price, Instrument instr) {
    BigDecimal bd = new BigDecimal(price);
    bd = bd.setScale(instr.getPipScale() + 1, RoundingMode.HALF_UP);
    return bd.doubleValue();
}



 
 Post subject: Re: Strange way to add Doubles Post rating: 0   New post Posted: Wed 15 Apr, 2015, 10:24 
User avatar

User rating: 0
Joined: Fri 17 Jan, 2014, 18:13
Posts: 72
Location: NetherlandsNetherlands
Thanks. If using the BigDecimal, do not forget:

import java.math.BigDecimal;
import java.math.RoundingMode;


I already fixed the problem with a similar solution:

private double round( double d, Instrument ins ){
        double m = Math.pow( 10, ins.getPipScale() );
        return (double)( Math.round( d * m ) / m );
}


 

Jump to:  

cron
  © 1998-2024 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