Need help in identifying all transactions

shriram natarajan's Avatar

shriram natarajan

05 May, 2024 03:57 PM

Hello folks,

I'd like to write a python extension/script to collate all transactions pertaining to a specific stock (say TSLA) across my accounts.

Is there a clean way to do this? My code (below) does not seem to capture all of the historic transactions.

Thanks!

        for txn in txnSet.iterableTxns():
            desc = str(txn.getDescription()).lower()
            if (str(txn.getAccount().getAccountType()) == "INVESTMENT"):
                try:
                    memo = str(txn.getMemo()).lower()
                    # print("investment %s %s" % (desc, category))
                    if (("tesla" in desc) or ("tsla" in desc) or ('tesla' in memo) or ('tsla' in memo)):
                          print(txn)
                except AttributeError, e:
                    pass

  1. 1 Posted by Stuart Beesley ... on 05 May, 2024 06:58 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Here's some code to help you out...:

    from com.infinitekind.moneydance.model import CurrencyType, ParentTxn, Account, InvestFields
    book = moneydance.getCurrentAccountBook()
    dec = moneydance.getPreferences().getDecimalChar()
    allSecurities = [sec for sec in moneydance.getCurrentAccountBook().getCurrencies().getAllCurrencies() if sec.getCurrencyType() == CurrencyType.Type.SECURITY]
    allSecurities = sorted(allSecurities, key=lambda x: (x.getCurrencyType(), x.getName().lower()))
    
    wantTicker = "GB00X2W09562"
    
    selectedSecurity = None
    for sec in allSecurities:
     if sec.getTickerSymbol() == wantTicker:
      selectedSecurity = sec
      break
    
    if selectedSecurity is None: raise Exception("Error: Security for ticker: '%s' NOT found!?" %(wantTicker))
    print("Found Security: '%s' for ticker: '%s'" %(selectedSecurity, wantTicker))
    print "---"
    
    fields = InvestFields()  # Use this to handle/process investment txns
    
    countFound = 0
    tset = book.getTransactionSet()
    for txn in tset:
     if type(txn) != ParentTxn: continue                                               # You only want ParentTxns! Splits in an Investment account will be cash transfers in/out
     if txn.getAccount().getAccountType() != Account.AccountType.INVESTMENT: continue  # if it's not an Investment account, then you don't want it!
     investAcct = txn.getAccount()
     investCurr = investAcct.getCurrencyType()
     fields.setFieldStatus(txn)
     if not fields.hasSecurity or fields.security is None: continue
     secAcct = fields.security  # This is an Account with a currency of the security
     secCurr = secAcct.getCurrencyType()
     if secCurr is None or secCurr is not selectedSecurity: continue
     print "Found txn. Investment Account: '%s', Security Account: '%s', Security(Currency): '%s'" %(investAcct, secAcct, secCurr)
     #print txn.toMultilineString()
     print "Desc:", txn.getDescription()
     print "Memo:", txn.getMemo()
     print fields.toString()
     print "Shares:", secCurr.formatSemiFancy(fields.shares, dec)
     print "Price:", fields.price
     print "Amount:", investCurr.formatSemiFancy(fields.amount, dec)
     countFound += 1
     if countFound > 100: break
    

    change wantTicker to the exact string of the ticker (tools/securities/edit) that you want...

    InvestFields() is your friend on this one!

    Hope this helps you? Any questions, shout...

  2. 2 Posted by Stuart Beesley ... on 05 May, 2024 07:09 PM

    Stuart Beesley (Mr Toolbox)'s Avatar
  3. 3 Posted by Shriram Nataraj... on 06 May, 2024 05:09 AM

    Shriram Natarajan's Avatar

    Dear Mr. Toolbox/Stuart,

    This code hit the mark. My prices are printing out weirdly/are probably not
    set right. However the "shares" and "Amount"s are coming through clearly
    across all accounts. I did not realize getCurrency included Securities.
    Thanks for the helpful links and the working code!!

    Appreciate it,
    Shriram

  4. 4 Posted by Stuart Beesley ... on 06 May, 2024 05:12 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    All rates have to be decoded by doing

    1.0 / rate
    1.0 / price

    Did that work?

    Yes Currencies and Securities are all/both Currencies.

  5. 5 Posted by Shriram Nataraj... on 06 May, 2024 05:43 AM

    Shriram Natarajan's Avatar

    Oh neat -- yeah the prices line up once I use that decoder ring!

    Thanks,
    Shriram

  6. 6 Posted by Stuart Beesley ... on 06 May, 2024 07:22 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    👍

  7. System closed this discussion on 05 Aug, 2024 07:30 AM.

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