Adding SellXfr transaction: balance incorrect

david.reitter's Avatar


26 Jan, 2020 07:31 PM

I'm using the function shown below to add a SellXfr transaction (InvestTxnType.SELL_XFER). I call .syncItem() and, later, refreshAccountBalances(). The transaction shows up correctly in the register, but the balances (cash, stock) are wrong. A SellXfr should not change the cash balance given that proceeds are deposited elsewhere, and it should affect the number of shares of that stock in the account. Unfortunately, the number of shares is unchanged, and the balance is increased by "amount". What should happen is that the balance is unaffected, and the number of shares is decreased by amount/share_price.

When I, in the UI, select any text field and edit it, then, after pressing enter to save the edit, the balances of stock and cash get updated and are correct. Which leads me to believe there is a problem with the API, or I need to make some other API calls.


def add_sale (txdate, security_acct, amount, share_price, target_account, desc):
    """Add Sale transaction (SellXfr) for security, depositing into target account."""
    parent_txn = ParentTxn.makeParentTxn (rb, txdate, txdate, txdate, "", unvested_acct, desc, memo, -1, ParentTxn.STATUS_UNRECONCILED)

    security_txn = SplitTxn.makeSplitTxn (parent_txn, amount, long(10000*amount/share_price),  float(share_price)/100.0, security_acct, desc, 2341, ParentTxn.STATUS_UNRECONCILED)

    xfer_txn = SplitTxn.makeSplitTxn (parent_txn, amount, amount, 0, target_account, desc, 2341, ParentTxn.STATUS_UNRECONCILED)


  1. 1 Posted by david.reitter on 26 Jan, 2020 08:49 PM

    david.reitter's Avatar

    I'll answer my own question so that others can find it.

    In the spirit of double-entry bookkeeping, the sum of the split transaction amounts apparently have to add up to 0. Thus, the "xfer_txn" portion needs to be for "- amount". Then, the cash balance is not affected.

    Apparently, MD assumes that any balance of the transactions affects the cash balance of the (parent??) account, which kind of makes sense.

    PS.: Keep adding documentation, please!!

  2. Support Staff 2 Posted by Sean Reilly on 26 Jan, 2020 10:57 PM

    Sean Reilly's Avatar

    Hi David,
    We really do need to do better with the documentation. I'd also like to say that a good way to create investment transactions and/or extract fields from existing investment transactions is using the InvestFields class ( Use setFieldStatus(transaction) to extract the investment "fields" (security, fee, fee category, description, action (buy/sell/dividend/etc), and more) or storeFields(ParentTxn) to take the information from the properties in the current fields and store them into a given transaction (usually a new txn or the one from which the fields were loaded.


    Sean Reilly
    Developer, The Infinite Kind

  3. System closed this discussion on 26 Apr, 2020 11:00 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