• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

MathFormula not working as expected

 
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to calculate de profit of an arbitragetrade, but for some reasson, the program dosn't get the result I expect.
These are the variables of the formula:
moneyToSpend = 0.1 btc (which is give or take depending on the current rate 100$)
askPrice = the lowest askingprice for a coin across a list of exchanges in btc
bidPrice = the highest bidprice for a coin across a list of exchanges in btc
amount (the amount to be bought) = moneyToSpend / askPrice
takerFee (needs to be payed when buying the coin on exchange a) = takerFeePercentage (is kept in a constant for each exhange) * amount
WithdrawalFee (needs to be payed when transfering the coin from exchange a to b) = a fixed amount based on the coin and exchange kept in an enum
makerFee (needs to be payed when selling on exchange b) = makerFeePercentage(is kept in a constant for each exhange) * (amount - takerFee - withdrawalFee)
profit = (bidPrice - askPrice) * (amount- takerFee - withdrawalFee - makerFee)

In code I translated it to:


The formula seems logic to me, am I that stupid or does java not follow simple mathrules anymore, like calculate first what's between parentheses?
 
Master Rancher
Posts: 4700
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you printed out what each of those values are at each stage?
Just to ensure they are what you think they are.
That includes values returned by a method call (eg bid.getBid() and ask.getAsk()).
 
Saloon Keeper
Posts: 12428
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pieter Vandevelde wrote:does java not follow simple mathrules anymore, like calculate first what's between parentheses?


This question reveals a fundamental misunderstanding of arithmetic in general and how Java performs arithmetic specifically.

"Simple math rules" never specified that what's between parentheses should be calculated first. The only purpose of parentheses is to make it explicit which operands go with which operators.

Here's an example: The expression "(a - b) * cᵈ" means that b belongs to the subtraction operation and not to the multiplication operation. It does NOT mean that you subtract b from a before you raise c to the power d.

In practice it doesn't matter because arithmetic operators don't have side-effects. Regardless of the order you evaluate them in, the result will always be the same. Most humans use this fact to first evaluate operators with higher precedence (such as parentheses) before they evaluate operators with lower precedence. Java is different. Java ALWAYS evaluates from left to right.

So what does this have to do with your problem? Absolutely nothing. As I said, unless your operators have side-effects, order of evaluation is inconsequential. There are three possible reasons for your issue:

1) Your method calls have side-effects that you don't take into account.
2) You're expecting an exact result but you're getting an imprecise result. This is because you're using floating point arithmetic.
3) Your math is off.

We can't help you further unless you tell us which result is not what you were expecting, what it is you were expecting and what the actual result was.
 
Pieter Vandevelde
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here's an example: The expression "(a - b) * cᵈ" means that b belongs to the subtraction operation and not to the multiplication operation. It does NOT mean that you subtract b from a before you raise c to the power d.  


No, but it does mean you substract b from a and raise c to the power of d before multyplying the two, if you write  a - b * cᵈ, then you have a whole other result, cause according my understanding you then first raise c to the power of d, multiply it with b and substract the result from a

This is an example of the results and what I get when I use the same formula in a calculator:


 
Marshal
Posts: 70647
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two of those figures are out by a factor of 10, and makerFee is out by ???
 
Pieter Vandevelde
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
makerfee I already figured out, that was a simple typo, why the amount is way of is a mystery to me, the taker is of because it get's multiplied by the amount
 
Dave Tolls
Master Rancher
Posts: 4700
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you've made a code change can you post that?
 
Pieter Vandevelde
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formula is unchanged
 
Saloon Keeper
Posts: 22656
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's often easiest if you move all the data-fetching operations away from the actual calculations.

But I do find some of the basis premises here confusing.

On the surface, it looks like you're dealing with a trade of financial instruments, where apparently both buyer and seller can implement a service fee/commission. So far, so good. That's basically this:
where sellFee and buyFee are per-unit amounts. Whose actual values might adjust (discount) in cases of large-quantity trades.

Where it breaks down is that I could swear that at least one of those calculations was doing something like this:

It's like selling one share but calculating on a purchase of 20 shares.

And Campbell pointed out I just did the exact same thing! So I've corrected it.

So I'm confused.
 
Sheriff
Posts: 15936
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This stands out to me:

Pieter Vandevelde wrote:
moneyToSpend = 0.1 btc
...


0.1 is != to 0.01
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic