Brokerage

Functionality

The Brokerage app allows a user to invest in the stock market and monitor the performance of the portfolio. Security is of utmost importance. The portfolio can be browsed while offline. Some non-critical data (e.g., stock watchlist) may be shared in read-only mode with other apps. There is no sharing between user accounts.

Key Behaviors

Schema

Data Types

// The IDs are store keys.
type SecurityID string
type BatchID string
type TxID string
type TxState int
type TxType int

const Cash = SecurityID(“Cash”)
const NoBatch = BatchID(“”)

const (
  Open TxState = iota
  Submitted
  Accepted
  Done
  Rejected
  Cancel
  Cancelled
)

const
  Buy TxType = iota
  Sell
  Dividend
  ShortTermCapGain
  LongTermCapGain
  Interest
  // other complicated transaction types...
)

type AssetBatch struct {
  ID BatchID
  Security SecurityID
  NumShares int32
  Price float32
  Date int64
}

type Transaction struct {
  ID TxID
  State TxState
  Type TxType
  Security SecurityID
  NumShares int32
  FromBatch BatchID // when selling
  DateOpened int64
  DateAccepted int64
  DateClosed int64  // done, rejected, cancelled
}

type SecurityMeta struct {
  Security SecurityID
  Name string
  // other quasi-static info...
}

type SecurityPrice struct {
  Price float32
  Date int64
}

type WatchlistEntry struct {
  Security SecurityID
  Name string
  Price float32
  Date int64
}

Organization

Collection <deckId>
Brokerage/
  <user>/              // database
    Portfolio/         // collection (user: R, server: RW)
      Assets/
        <security-ID>/
          <batch-ID>   // type AssetBatch
      Completed-Transactions/
        <tx-ID>        // type Transaction
      Securities/
        <security-ID>/
          metadata     // type SecurityMeta
          price        // type SecurityPrice
    Transactions/      // collection (user: RW, server: RW)
      <tx-ID>          // type Transaction
    Shared/            // collection (user: RW)
      Watchlist/
        <security-ID>  // type WatchlistEntry

There is one app database (“Brokerage/”) with 3 collections each with a syncgroup:

In the “Transactions” collection, the user writes or updates objects in these cases:

Some transaction types cannot be entered by the user, they exist for the server to report automatically created transactions (e.g. dividends).

During app-to-app synchronization, the peers accept each other’s data based on the default last-timestamp-wins policy.

Conflicts

During app-to-server synchronization, the server code handles its own conflict resolution, after decrypting the objects, applying business rules, and validating changes transactionally with the firm’s databases. It rejects invalid transactions, decides whether a “cancel” arrived before the transaction was done, and updates the objects under the “Assets” directory. The server also updates the objects under the “Securities” directory, filling the security metadata if needed, and updating the security prices.