how to create/update an investment transaction?

russell.richter's Avatar


12 Jan, 2023 07:45 AM

I've been working on a python script to add transactions to an investment account. I've managed to add the transaction. However, the "cash balance" in the account is not updated. If I go into the MoneyDance GUI and double click on the transaction I created and then immediately hit return to save the transaction (without making any changes), the correct "cash balance" is now present.

I create the transaction by calling ParentTxn with a prameter of the root account book. I fill in the fields either directly or by getting the InvestFields and filling them in and saving them to the transaction. I then save the transactions by calling addnewTransaction from the transaction set and calling itemWasUpdated and itemWillSync on the transation.

After using inspect, there is a private field called val, which appearts (from looking at other transactions) to be the total value of the transaction. If val is valid (non-zero), the cash balnace of the account is calculated correctly. If val is zero, the cash balance is not changed. However, since val is a private field and I don't see any methods that can alter val, I don't see how I can update val.

It's obviiou that setting val is essential, but what do I need to do the transaction to make it update th value of val?



How dow I make the transaction that I created update the val field?

  1. 1 Posted by Stuart Beesley ... on 12 Jan, 2023 08:09 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    InvestFields() does make life easier.

    from com.infinitekind.moneydance.model import InvestFields
    fields = InvestFields()
    Set up the elements. 
  2. 2 Posted by Stuart Beesley ... on 12 Jan, 2023 10:34 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Here's some sample code that works.... You will have to switch in your own account / sec account / security details"

    from com.infinitekind.moneydance.model import ParentTxn, InvestFields, AbstractTxn, InvestTxnType, CurrencyUtil
    from com.infinitekind.util import DateUtil
    from com.infinitekind.util import StringUtils
    from java.lang import Math
    MD_REF = moneydance
    book = MD_REF.getCurrentAccountBook()
    invest_acct_id = "fda22df0-f5e1-451c-9fe0-8a395892b8a3"
    investAcct = MD_REF.getCurrentAccountBook().getAccountByUUID(invest_acct_id)
    sec_acct_id = "e0ba015e-96ef-4dff-8f3e-907834aeee2c"
    secAcct = MD_REF.getCurrentAccountBook().getAccountByUUID(sec_acct_id)
    sec_id = "bce6068b-b033-41f2-821d-e91c2dd1ab03"
    sec = MD_REF.getCurrentAccount().getBook().getItemForID(sec_id)
    secMult = Math.round(Math.pow(10.0, sec.getDecimalPlaces()))
    base = MD_REF.getCurrentAccount().getBook().getCurrencies().getBaseType()
    centMult = Math.round(Math.pow(10.0, base.getDecimalPlaces()))
    print investAcct
    print secAcct
    print sec
    print base
    date = DateUtil.getStrippedDateInt()
    print date
    niceCheckNum = "chkNum123"
    desc = "this_is_description"
    memo = "this_is_memo"
    pTxn = ParentTxn.makeParentTxn(
    txnType = InvestTxnType.BUY
    totalAmount = 250
    shares = 100
    price = totalAmount / shares
    fields = InvestFields()
    fields.setFieldStatus(txnType, pTxn) = date
    fields.taxDate = date
    fields.checkNum = niceCheckNum
    fields.payee = desc
    fields.memo = memo
    fields.amount = int(totalAmount * centMult)
    # fields.xfrAcct = AccountUtil.getDefaultTransferAcct(investAcct);
    # fields.fee = 0
    # fields.feeAcct = AccountUtil.getDefaultCategoryForAcct(investAcct)
    # fields.category = AccountUtil.getDefaultCategoryForAcct(investAcct) = secAcct
    fields.shares = int(shares * secMult)
    fields.price = price
    print("stored fields ", fields, " as txn ", pTxn)

    Basically, you load up InvestFields with a parent txn, change the fields, then .storeFields() back to the ParentTxn, then sync the parent.

    Let me know if this helps, if I can help further etc, any questions?

  3. 3 Posted by Stuart Beesley ... on 12 Jan, 2023 10:43 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    😂 I just re-read your original post properly....

    OK.. You should not need to call addnewTransaction, itemWasUpdated and itemWillSync - just use the method(s) shown above..

    You should not need to worry about private fields.... Where is 'val' that you referenced? I cannot see it.. What object/class..

    Basically, to update the cash balance just use InvestFields (example above) and the cash balance will be correct... Cash Balance is updated when there is a split that contains a 'pamt' parameter/value which is essentially SplitTxn.setParentAmount(). Cash balance is therefore the sum of all splitTxn's .getParentAmount().....

  4. 4 Posted by Stuart Beesley ... on 12 Jan, 2023 10:44 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    The above code generates this

  5. 5 Posted by russell.richter on 12 Jan, 2023 03:44 PM

    russell.richter's Avatar

    Stuart Beesley,

    Thank you for your email.

    What I was doing was creating a new transaction with no parameters and
    filling in the fields that are accessible (either directly in the ParentTxn
    object or through the InvestFields object which is then set to the
    transaction.) and then adding the transaction to the transaction set and
    syncing it.

    I used the inspect module to look at the objects, which also shows the
    private fields without any way to interact with them. One of those fields
    was called 'val'. 'val' was 0 for my transaction while after doing a
    no-edit edit of the transaction (open it for edit and just save it), 'val'
    was then the same as the amount field ('amt' from inspect), which I had set
    via the InvestFields interface.

    I will re-code my script to use the interface the way you specified and try
    it out.

    Also, I want to say thank you for sharing the code for your extensions. It
    has helped me significantly in coding my script.

    Thank you,

    Russell Richter

  6. 6 Posted by Stuart Beesley ... on 12 Jan, 2023 04:24 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    👍 Let me know how you get on... Any questions, shout....

    (My extensions are an evolution of knowledge... E.g. some of Toolbox will be very early / chunky code and some will be 2 years later, more advanced... 😉)

  7. System closed this discussion on 13 Apr, 2023 04:30 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

Recent Discussions

15 Apr, 2024 03:34 PM
15 Apr, 2024 03:24 PM
15 Apr, 2024 12:32 PM
15 Apr, 2024 12:20 PM
15 Apr, 2024 11:56 AM