tag:infinitekind.tenderapp.com,2009-01-14:/discussions/moneydance-development/7742-investfieldsprice-how-to-convertInfinite Kind: Discussion 2023-05-22T20:50:19Ztag:infinitekind.tenderapp.com,2009-01-14:Comment/579339122023-02-13T21:54:34Z2023-02-14T11:09:18ZInvestFields.Price - how to convert<div><p>Usually for all MD values (stored as Long), it’s something like:</p>
<p>theCurrency.getDoubleValue(originalLongValue)</p>
<p>Where theCurrency will probably be either the security account’s security (currency) or the account’s currency.</p>
<p>And to display long values nicely use currency.formatSemiFancy(longVal, ‘.’) or c.formatFancy() or c.format()</p>
<p>To convert a double back to long for MD, use currency.getLongValue(doubleValue).</p>
<p>I'll take a look at InvestFields.price and come back to you.....:</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/579339122023-02-13T21:56:04Z2023-02-14T11:09:19ZInvestFields.Price - how to convert<div><p>.</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/579339122023-02-13T22:00:23Z2023-02-14T11:08:01ZInvestFields.Price - how to convert<div><p>.</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/579339122023-02-13T22:03:11Z2023-02-14T11:08:08ZInvestFields.Price - how to convert<div><p>.</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/579339122023-02-13T22:07:06Z2023-02-14T11:08:14ZInvestFields.Price - how to convert<div><p>.</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/579339122023-02-14T11:07:48Z2023-02-14T11:07:48ZInvestFields.Price - how to convert<div><p>OK, I just tested this myself and yup - the expected price's decimal place is different from what you might expect....</p>
<p>I checked the <code>InvestFields()</code> code. The field <code>price</code> is simply based on method <code>SplitTxn.getRate()</code> and that is:</p>
<pre>
<code>-(split.splitAmount / split.parentAmount) - which is -(shares / parent amount/value)</code>
</pre>
<p>but... both the above amounts are Long (hence held without any decimal place - so 1234.5678 at 4dpc is held at 12345678 - example only)...</p>
<p>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.</p>
<p>One of the internal MD solutions used is this..:</p>
<pre>
<code>from com.infinitekind.moneydance.model import CurrencyUtil
price = 1.0 / fields.price
txnPrice = CurrencyUtil.getUserRate(fields.secCurr, fields.curr, price)</code>
</pre>
<p>This is essentially doing the calculation to convert from the security's dpc to the txn's account's currency.</p>
<p>Behind the scenes the maths is this:<br></p>
<pre>
<code>rawRate * Math.pow(10.0D, (fromCurrency.getDecimalPlaces() - toCurrency.getDecimalPlaces()));</code>
</pre>
(of course, in all the above you will have to handle any divide by zero issues...)
<p>Hope this helps? Let me know?</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/579339122023-02-20T20:36:24Z2023-02-20T20:36:24ZInvestFields.Price - how to convert<div><p>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).</p>
<p>I adjust the amount by adding/subtracting the fee if necessary to get the actual price paid/received for the shares).</p>
<p>Easy-ish and accurate.</p>
<p>Thanks for your help.</p></div>Bob Btag:infinitekind.tenderapp.com,2009-01-14:Comment/579339122023-02-20T20:45:02Z2023-02-20T20:45:02ZInvestFields.Price - how to convert<div><p>👍</p></div>Stuart Beesley (Mr Toolbox)