tag:infinitekind.tenderapp.com,2009-01-14:/discussions/moneydance-development/2666-adding-sellxfr-transaction-balance-incorrectInfinite Kind: Discussion 2020-04-26T23:00:25Ztag:infinitekind.tenderapp.com,2009-01-14:Comment/480413572020-01-26T19:31:17Z2020-01-26T19:31:17ZAdding SellXfr transaction: balance incorrect<div><p>Hi,<br>
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.</p>
<p>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.</p>
<p>Thanks,<br>
David</p>
<pre>
<code>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)
TxnUtil.setSecurityPart(security_txn)
parent_txn.addSplit(security_txn)
xfer_txn = SplitTxn.makeSplitTxn (parent_txn, amount, amount, 0, target_account, desc, 2341, ParentTxn.STATUS_UNRECONCILED)
TxnUtil.setXfrPart(xfer_txn)
parent_txn.addSplit(xfer_txn)
parent_txn.setInvestTxnType(InvestTxnType.SELL_XFER)
rb.getTransactionSet().addNewTxn(parent_txn)
parent_txn.syncItem()</code>
</pre></div>david.reittertag:infinitekind.tenderapp.com,2009-01-14:Comment/480413572020-01-26T20:49:57Z2020-01-26T20:49:57ZAdding SellXfr transaction: balance incorrect<div><p>I'll answer my own question so that others can find it.</p>
<p>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.</p>
<p>Apparently, MD assumes that any balance of the transactions affects the cash balance of the (parent??) account, which kind of makes sense.</p>
<p>PS.: Keep adding documentation, please!!</p></div>david.reittertag:infinitekind.tenderapp.com,2009-01-14:Comment/480413572020-01-26T22:57:31Z2020-01-26T22:57:31ZAdding SellXfr transaction: balance incorrect<div><p>Hi David,<br>
We <em>really</em> 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 (<a href="https://infinitekind.com/dev/apidoc/com/infinitekind/moneydance/model/InvestFields.html">https://infinitekind.com/dev/apidoc/com/infinitekind/moneydance/mod...</a>). 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.</p>
<p>Thanks!<br>
Sean</p>
<p>--<br>
Sean Reilly<br>
Developer, The Infinite Kind<br>
<a href="https://infinitekind.com">https://infinitekind.com</a></p></div>Sean Reilly