Securities trading is basically a special case of inventory/cost of goods sold bookkeeping. It's a bit funkier than plan-vanilla flow-of-funds type bookkeeping.
You can read up on it at the website for which beancount is named: http://www.dwmbeancounter.com/BCTutorSite/Courses/Inv/lesson02-2.html In terms of that tutorial, we're essentially talking about "FIFO Perpetual Inventory" accounting... except that for securities inventory, we relax the requirement to maintain a consistent scheme for booking out inventory units. Essentially, we need to maintain a journal which is a list of (asset, datetime, units, cost [or cost per unit]). Summing for cost [or cost*cost/unit] ties out to balance sheet accounts. For trading marketable securities, a normal chart of accounts will have current asset subaccounts for every issue owned (identified by ticker, CUSIP, ISIN, or what have you), and current liability subaccounts for each issue sold short. Each of those subaccounts will have a subaccount for cost, which ties out to the journal with the constraint that sum of cost for a given asset has to equal the costs posted to the relevant balance sheet subaccount on the general ledger. Entries to the inventory journal are not double-entry accounting, and they're not necessarily money-denominated at all. Weird stuff happens to inventory in the real world (if you can call the stock market the real world). When you reduce or close a position, somebody needs to look through the list of lots, and choose which lot(s) to get rid of, based on some logic. Based on that choice, whoever's controlling the inventory will return a figure for the cost booked out, and the guy maintaining the general ledger will post a transaction decreasing the balance-sheet cost of the asset (on one side) and increasing the cost of goods sold on the income statement (on the other side). It seems to me that what you want is a Python dict of {asset: [lot, lot, lot, ...]}, where lot=(datetime, units, cost). More generally, you keep a journal like that for each brokerage account, so you'd either want to key the dict above by (account, asset), or a better way would be to use nested dicts e.g. portfolio[account][security] = [lot, lot, lot, ...]. That way you can quickly & naturally sum costs for a security in an account (via hash map lookup & list comprehension), and roll those up into total cost for a brokerage account. You can do the same for units. This corresponds to the way this stuff gets reported by brokers and the way we set up these charts of accounts. It's a little bit less ideal for reporting aggregates of securities across accounts, but still way better than an unstructured list. Unfortunately the list structure seems to be baked into the application interface of inventory.py... the inventory control logic isn't locally encapsulated, so the dumb objects here return list pointers back to where the smarts actually reside. I think this means I can't change this without completely forking the project and ripping up things nobody wants ripped up. Oh well. On Friday, 6 January 2017 09:50:54 UTC-6, Simon Michael wrote: > > Interesting discussion. > > hledger is the least sophisticated of the three when it comes to > trading, currently. Beancount seems to be leading the way here. One of > these days I hope to understand better what y'all are talking about here. > > On 1/5/17 9:18 PM, Martin Blais wrote: > > The TL;DR is that Ledger doesn't attempt to book reducing lots against > > specific positions. > > Furthermore, it doesn't distinguish between currency exchange and > positions > > with a cost basis. > > Tracking of cost basis is therefore quite limited. > > AFAIK HLedger repeats those same design choices (though I haven't looked > > for a while). > > > -- You received this message because you are subscribed to the Google Groups "Beancount" group. To unsubscribe from this group and stop receiving emails from it, send an email to beancount+unsubscr...@googlegroups.com. To post to this group, send email to beancount@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/c3a9d027-0e12-4717-9f27-0dde48a39e31%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.