Currency balance and price at a specific day

Robert's Avatar

Robert

31 Jan, 2021 05:16 PM

Hi!

I would like to ask for a pointer how to best calculate the balance and price of a currency at a specific day? I have searched through the API but it seems I am not getting the big picture and thus have a hard time understanding how to go about this. I have obtained the currency in questions UUID and account, so I can call "getCurrentBalance() and it returns the correct value, but what I am looking for is "getBalance(DATE)". And similarly for the price. I have followed the code in the Toolbox form Stuart Beesley (THANKS SO MUCH FOR MAKING THAT PUBLIC!) so I can get the current price of a currency... but again... what I would like to obtain is the price at a specific day.

Any help is greatly appreciated, thanks so much!!

Robert

  1. 1 Posted by Stuart Beesley ... on 31 Jan, 2021 05:53 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    .

  2. 2 Posted by Stuart Beesley ... on 31 Jan, 2021 06:02 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    EDITED (as I re-read your message properly)

    My answers below are Python format...

    You might be better looking at my stockglance2020.py script for lots of things like this, and even extract_account_registers_csv.py.

    Anyway, roughly speaking you can do the following:

    AccountUtil.getBalanceAsOfDate​(AccountBook book, Account account, int asofDate) to get balance as of a date. So AccountUtil.getBalanceAsOfDate​( moneydance_data, acct, 20210101)

    To get a rate as of a date... object's currency(type) - i.e. acct.getCurrencyType(). Then on the currency type call . getRate​(CurrencyType relativeTo, int asOfDate) - e.g. .getRate(None, 20210101). You can then do balance / the rate (or it might be balance / (1/rate) )....

    If the currency is not the base, then you can pass the base currency type into the getRate() method as the first parameter... E.g. .getRate(moneydance_data.getCurrencies().getBaseType(), 20210101)

    PS - you don't need to use UUIDs (very often).. You normally pass the account or currency type objects as the parameter where required..

    I haven't tested the above, but it's something like this from memory...

    Hope this helps.. Let me know? If I can help further.. Shout...

    SCB

  3. 3 Posted by Robert on 31 Jan, 2021 06:05 PM

    Robert's Avatar

    This looks great, will try it right away. THANKS so much for your quick reply!

    Robert

  4. 4 Posted by Stuart Beesley ... on 31 Jan, 2021 06:49 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Attached is a demo script... I've not tested it fully, but it runs.. You will get the idea.. I remember being very confused at the start.... Once you get the hang of it, then it makes sense, but it is a voyage of self-learning discovery..!

  5. 5 Posted by Stuart Beesley ... on 31 Jan, 2021 06:50 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    attached

  6. 6 Posted by Robert on 31 Jan, 2021 07:26 PM

    Robert's Avatar

    Did not yet look at your demo script, but here is what I did, and it worked well:
    print acct.getAccountName()
    print "Shares: " + str(acct.getCurrencyType().formatFancy(AccountUtil.getBalanceAsOfDate(book, acct,20201231), '.'))
    print "Price: " + str(round(1/float(acct.getCurrencyType().getRate(None, 20201231)),8))

    Thanks again for your help with this!

    Robert

  7. 7 Posted by Stuart Beesley ... on 31 Jan, 2021 07:44 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Excellent. FYI - as you embark on your MD journey you may run into trouble when you str() things with Unicode. Especially currency names with extended characters. Best to try to avoid using str() if you can on strings...

  8. Support Staff 8 Posted by Sean Reilly on 31 Jan, 2021 09:57 PM

    Sean Reilly's Avatar

    Hi Robert,

    I'd also recommend using the static methods in the CurrencyUtil class which can give you the rate between any two currencies or securities. You can also convert a value in one currency/security into any other currency/security as of a specific date, for example with CurrencyUtil.convertValue(value1, currency1, toCurrency2, asofdate).

    Here's the documentation for it: https://infinitekind.com/dev/apidoc/com/infinitekind/moneydance/mod...

    Thanks,
    Sean

    --
    Sean Reilly
    Developer, The Infinite Kind
    https://infinitekind.com

  9. 9 Posted by Robert on 01 Feb, 2021 10:36 AM

    Robert's Avatar

    Thanks to both Stuart and Sean for your suggestions! I agree that avoiding str is a good idea, I have already run into UTF8 related error messages. ;-)

    Sean, I am sure I can dig this up myself with some trial and error... but what would I use as "toCurrency2" in your example? My "base currency" is USD, how do I plug that in there?

    Thanks again! It has been fun to automate some of my data export needs using python scripts, this has made Moneydance a lot more useful for me!!

    Robert

  10. 10 Posted by Stuart Beesley ... on 01 Feb, 2021 10:42 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Hi Robert, the demo script I sent you shows you how to get base currency:

    base = moneydance_data.getCurrencies().getBaseType()

    so:
    get the account as acct
    get the balance as balance
    acct_ct = acct.getCurrencyType()
    CurrencyUtil.convertValue(balance, acct_ct, base, 20210101)

  11. 11 Posted by Robert on 01 Feb, 2021 10:51 AM

    Robert's Avatar

    Thank you, I am all set now!

    Robert

  12. Melissa closed this discussion on 24 Feb, 2021 12:12 PM.

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