InvestFields.Price - how to convert

Bob B's Avatar

Bob B

13 Feb, 2023 09:27 PM

I'm struggling with how to convert the double values in the InvestFields price field. I thought I should just take the inverse (1/price) but that gives me a value that is off by a number of decimal places. The number of places is usually 2 but sometimes only 1, that is, a price from InvestFields might be 0 .6946 which gives me an inverse of 1.4395... but the actual value displayed in the UI is 143.95.

So I could multiple by 100 and get the right value but sometimes I only need to multiple by 10.

How do I determine the correct multiplier? I've tried looking for a method that would return the correct value (like I can with amounts in long) but I haven't found one that takes double and the input. I'm sure I'm missing something simple.

Thanks for any help.

  1. 1 Posted by Stuart Beesley ... on 13 Feb, 2023 09:54 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Usually for all MD values (stored as Long), it’s something like:


    Where theCurrency will probably be either the security account’s security (currency) or the account’s currency.

    And to display long values nicely use currency.formatSemiFancy(longVal, ‘.’) or c.formatFancy() or c.format()

    To convert a double back to long for MD, use currency.getLongValue(doubleValue).

    I'll take a look at InvestFields.price and come back to you.....:

  2. 2 Posted by Stuart Beesley ... on 13 Feb, 2023 09:56 PM

    Stuart Beesley (Mr Toolbox)'s Avatar


  3. 3 Posted by Stuart Beesley ... on 13 Feb, 2023 10:00 PM

    Stuart Beesley (Mr Toolbox)'s Avatar


  4. 4 Posted by Stuart Beesley ... on 13 Feb, 2023 10:03 PM

    Stuart Beesley (Mr Toolbox)'s Avatar


  5. 5 Posted by Stuart Beesley ... on 13 Feb, 2023 10:07 PM

    Stuart Beesley (Mr Toolbox)'s Avatar


  6. 6 Posted by Stuart Beesley ... on 14 Feb, 2023 11:07 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    OK, I just tested this myself and yup - the expected price's decimal place is different from what you might expect....

    I checked the InvestFields() code. The field price is simply based on method SplitTxn.getRate() and that is:

    -(split.splitAmount / split.parentAmount) - which is -(shares / parent amount/value)

    but... both the above amounts are Long (hence held without any decimal place - so 1234.5678 at 4dpc is held at 12345678 - example only)...

    thus, if your security is 4dpc and your value currency is 2dpc, then the result is 2 dpc (for example) different to what you might expect. I don't know if this is a bug or as expected... But MD seems to 'handle it'. I think therefore that MD is assuming that this field is stored loosely with reference to the Security's(currency) and its dpc.

    One of the internal MD solutions used is this..:

    from com.infinitekind.moneydance.model import CurrencyUtil
    price = 1.0 / fields.price
    txnPrice = CurrencyUtil.getUserRate(fields.secCurr, fields.curr, price)

    This is essentially doing the calculation to convert from the security's dpc to the txn's account's currency.

    Behind the scenes the maths is this:

    rawRate * Math.pow(10.0D, (fromCurrency.getDecimalPlaces() - toCurrency.getDecimalPlaces()));
    (of course, in all the above you will have to handle any divide by zero issues...)

    Hope this helps? Let me know?

  7. 7 Posted by Bob B on 20 Feb, 2023 08:36 PM

    Bob B's Avatar

    Thanks so much. What should be so simple, always seems complicated! What I decided to do is calculate the price from the double values for amount and shares (amount/shares).

    I adjust the amount by adding/subtracting the fee if necessary to get the actual price paid/received for the shares).

    Easy-ish and accurate.

    Thanks for your help.

  8. 8 Posted by Stuart Beesley ... on 20 Feb, 2023 08:45 PM

    Stuart Beesley (Mr Toolbox)'s Avatar


  9. System closed this discussion on 22 May, 2023 08:50 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac