InvestFields.Price - how to convert
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.
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | 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
1 Posted by Stuart Beesley ... on 13 Feb, 2023 09:54 PM
Usually for all MD values (stored as Long), it’s something like:
theCurrency.getDoubleValue(originalLongValue)
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 Posted by Stuart Beesley ... on 13 Feb, 2023 09:56 PM
.
3 Posted by Stuart Beesley ... on 13 Feb, 2023 10:00 PM
.
4 Posted by Stuart Beesley ... on 13 Feb, 2023 10:03 PM
.
5 Posted by Stuart Beesley ... on 13 Feb, 2023 10:07 PM
.
6 Posted by Stuart Beesley ... on 14 Feb, 2023 11:07 AM
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 fieldprice
is simply based on methodSplitTxn.getRate()
and that is: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..:
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:
(of course, in all the above you will have to handle any divide by zero issues...)Hope this helps? Let me know?
7 Posted by Bob B on 20 Feb, 2023 08:36 PM
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 Posted by Stuart Beesley ... on 20 Feb, 2023 08:45 PM
👍
System closed this discussion on 22 May, 2023 08:50 PM.