Primer on Jython and Moneydance Methods

Welcome to Moneydance Jython

This page has notes on using Jython and introduces the important Moneydance methods. While the documentation is pretty thin, it tries to present things in the order that most people will use and concentrates on the methods used to produce reports. Use this page with the programmatically generated documentation. That page links to minimal definitions of all methods, this page lists brief descriptions of common methods.

Many of the Moneydance classes and methods can be accessed with just a few commands on the Python console, and you can cut and paste many of the examples below to check out the methods in your environment. The methods below were choosen for their utility in writing report generators. They will be useful in most other programming too, such as those that modify Moneydance data. Once you understand the methods here, many others will be simple to learn.

Jython quirks

Once you have the Jython extension installed, you can run programs through two mechanisms, the Jython console or from the command line. Until you have a program that works, use the console. The command line interface will fail silently on syntax errors.

When Moneydance reads a Jython program it feeds it through code that handles interactive input and that doesn't quite support normal Python syntax. For example, this program fails, first by complaining about a syntax error caused by a comment, and later by the last line having a syntax error:

def sq(i):
    # Comment that will cause a syntax error
    return i*i

for i in range(3):
    print i, sq(i)
print 'done'

The comment may be replaced by a blank line and that terminates the definition. Interactive mode needs a blank line to end the for statement, and that unindented print statement causes another syntax error. If you're loading a longish program, you can get a bizarre mix of errors and code execution that simply shouldn't execute. Don't use comment lines in indented code and terminate outermost multi-line statements with a blank line, e.g.:

def sq(i):
    pass # Comment that will pass Moneydance's Jython
    return i*i

for i in range(3):
    print i, sq(i)

print 'done'

Class rootAccount

Pretty much any Moneydance Jython program will start with something like:
ra = moneydance.getRootAccount()

The methods special to rootAccount are not very useful for writing reports, the methods for the generic account class are the useful ones. However, there are a couple:

Class Account

Reports generally involve accounts or transactions, and transactions involve accounts, so the account class is important. You can get access to an account in the Python console:
>>> ra = moneydance.getRootAccount()
>>> acct = ra.getAccountByName('checking')
>>> print repr(acct)
Checking
>>> acct
Checking

Useful methods:

Transactions: class ParentTxn and SplitTxn

Transactions involve at least two accounts, the account you entered the transaction into and the account it referenced. If you used a split, then it will reference several accounts.

To get all the transactions associated with an account, you need to have the rootAccount search for them. Consider this checking account with a short register:

Date       Chk  Desc    Account         Payment   Deposit   Balance 
1/01/2005  Dep  Bossco  Bonus                    1,000.00  1,000.00
1/03/2005       Banke   Savings          100.00              900.00
1/07/2005  1    CG      Groceries         20.00              880.00
1/15/2005  2    Sitgo   - 2 splits -      30.00              850.00
 Split 1:       Lunch   Personal:Dining   10.00
 Split 2:       Gas     Automotive:Fuel   20.00
1/31/2005  ATM  Cash    ATM Withdrawal   100.00              750.00
>>> txns = ra.getTransactionSet().getTransactionsForAccount(acct).getAllTxns()
>>> txns
com.moneydance.apps.md.model.TxnSet$TransactionEnumerator@1c9f8fb
>>> for txn in txns:
...     print repr(txn)
... 
[ParentTxn(12) desc=Cash; val=-10000; stat= ; #splits=1; chk=ATM; ...
[ParentTxn(10) desc=Bank; val=-10000; stat= ; #splits=1; chk=; ...
[ParentTxn(8) desc=CG; val=-2000; stat= ; #splits=1; chk=1; ...
[ParentTxn(6) desc=Sitgo; val=-3000; stat= ; #splits=2; chk=2; ...
[ParentTxn(3) desc=Bossco; val=100000; stat= ; #splits=1; chk=Dep; ...

The list of attributes displayed isn't complete and Moneydance doesn't let you access them directly, e.g. txn.val won't work. The attributes are accessed by various methods. Here are all the fields displayed above for the transactions with 2 splits with the method a program can use to get the values or a close relative:

Other useful methods include the following. Methods prefixed with txn. apply to the parent transaction (class ParentTxn) and sxn. apply to the first split transaction (class SplitTxn).

Class InvestmentAccount

There isn't much difference between banking accounts and investment accounts within Moneydance. For that matter, there isn't much difference between foreign currency holdings and security holdings in that both are held in units with a value that fluctuates over time.

Suppose we have an investment account called Elemental Holdings, which has two stocks from these transactions:

  Security: Itanium Mining
   Date     Dir     Shares   $/sh     Value    Balance
2005/04/01  Buy    10.0000  40.00      $400.00    10.0000

  Security: Radium Arc Lamps
   Date     Dir     Shares   $/sh     Value    Balance
2005/03/14  Buy   100.0000   3.14      $314.00   100.0000
   Commission:                           $0.15
If we look at these via Mondydance:
>>> acct = ra.getAccountByName('elemental holdings')
>>> acct.getBalance()
0L
>>> txns = ra.getTransactionSet().getTransactionsForAccount(acct).getAllTxns() 
>>> for txn in txns:
...      print repr(txn)
... 
[ParentTxn(21) desc=Buy Shares; val=-40000; #splits=1; chk=; date=20050401; ...
[SplitTxn(18) desc=Elemental Holdings; val=40000; rate=1.0; amt=-40000; val=40000;  ]
[SplitTxn(16) desc=Elemental Holdings; val=31415; rate=1.0; amt=-31415; val=31415;  ]
[ParentTxn(15) desc=Buy Shares; val=-31415; #splits=2; chk=; date=20050314; ...
The last transaction has two splits, let's take a closer look at it as we look at some important methods.
>>> ts = ra.getTransactionSet()
>>> txn =ts.getTxnByID(15)
>>> txn.getSplitCount()
2
>>> sxn0 = txn.getSplit(0)
>>> sxn1 = txn.getSplit(1)

Class CurrencyType

Moneydance tracks currencies and share values in CurrencyType classes. As implied above, these holding sizes are tracked as multiples of some fraction of a share. The fraction size, currency name, and many other attributes are tracked within this class.

You can get to an account's currency type from the Python console:

>>> acct = ra.getAccountByName('elemental holdings:itanium mining')
>>> ct = acct.getCurrencyType()
>>> chk = ra.getAccountByName('checking')
>>> cct = chk.getCurrencyType()


Contact Ric Werme or visit his home page.

Last updated 2005 November 1.