Experilous

Last week I was able to finish my preliminary work in porting my Lua scripts to JavaScript, and get a bonus by integrating them with jqPlot to get nice line graphs just like I had in Excel.  The syntax and features are ever-evolving, but take a look at what I’ve got so far.

First of all, I can get easy summaries of both my overall funds, as well as any stock positions that I have.  (Note that all the data I’ve used is entirely faked.  I’m not financially secure, regrettably.)

summary table of budget funds

summary of all funds

active stock position information

active stock position information

Further, with the aid of jqPlot, I can get nice graphs.  Ever since December when I switched from Excel to Lua, I had nonetheless been keeping my Excel data (just for my stocks) up-to-date so that I could get the graphs.  Now I can drop Excel completely, and get graphs not only for my stocks, but for all my money.

fund and stock graphs over time

funds, stocks, recent stocks

As for code, it has followed many of the same organizing principles that I used in the Lua code, separating out the general utilities from the actual data.  But to represent the data, instead of using object notation to represent the raw structure of the data, I instead created a few helper objects that depend heavily upon chaining.  A BudgetActivities object, for example, has methods on it such as Income() and Purchase() that will add an item of activity to its internal list, and will then return itself so that another call to Income() or Purchase can be made.  It can also forward function calls to the last activity item pushed onto its list.  If the last thing added was a Purchase, then a call to the methods Tags(), Item(), or Withdrawal() will be forward to the Purchase in order to update its details (and then the BudgetActivity instance will again be returned).  The resulting script looks something like this:

new BudgetActivities(activities)
    .FundValueUpdate("2013-01-03 12:15 CST", funds.savings, "119,370.14 USD")
    .FundValueUpdate("2013-01-03 12:17 CST", funds.checking, " 14,938.22 USD")
    .FundValueUpdate("2013-01-03 12:28 CST", funds.ret401k, "338,525.75 USD")
    .FundValueUpdate("2013-01-03 12:32 CST", funds.cash, " 185.00 USD")
    .Income("2013-01-04 03:30 CST", "Employer", "biweekly paycheck")
        .Tags("recurring", "biweekly", "paycheck")
        .Deposit(directDeposit, "2,880.00 USD")
    .Income("2013-01-18 03:30 CST", "Employer", "biweekly paycheck")
        .Tags("recurring", "biweekly", "paycheck")
        .Deposit(directDeposit, "2,880.00 USD")
    .Purchase("2013-02-01 09:25 CST", "Landlord", "rent")
        .Tags("recurring", "monthly", "rent")
        .Withdrawal(giveCheck, "2,500.00 USD")
    .Income("2013-02-01 03:30 CST", "Employer", "biweekly paycheck")
        .Tags("recurring", "biweekly", "paycheck")
        .Deposit(directDeposit, "2,880.00 USD")
    .Purchase("2013-02-04 21:01 CST", "Amazon", "new computer parts", "computer", "hardware")
        .Tags("electronics")
        .Item("case", "89.99 USD")
        .Item("motherboard", "159.99 USD")
        .Item("CPU", "329.99 USD")
        .Item("memory", "329.99 USD")
        .Item("graphics card", "549.99 USD")
        .Item("network card", "54.99 USD")
        .Item("power supply", "74.99 USD")
        .Item("router", "179.99 USD")
        .Item("monitor", "299.99 USD")
        .Item("monitor", "299.99 USD")
        .Item("monitor", "299.99 USD")
        .Item("Blu-Ray burner", "99.99 USD")
        .Item("SSD", "499.99 USD")
        .Item("HDD", "199.99 USD")
        .Item("keyboard", "59.99 USD")
        .Item("mouse", "79.99 USD")
        .Fee("120.00 USD", "shipping")
        .Withdrawal(debitCard)
;

Stock activity is similar:

new StockActivities(activities, funds.invest)
    .Deposit   ("2013-02-16 CST", " 50,000.00 USD", funds.savings)
    .Buy       ("2013-02-21 CST", " AAPL", " 445.7700 USD", 100).Commission("1.00 USD")
    .Prices    ("2013-03-01 CST").AtClose(" AAPL", " 430.4700 USD")
    .Prices    ("2013-04-01 CST").AtClose(" AAPL", " 428.9100 USD")
    .Prices    ("2013-05-01 CST").AtClose(" AAPL", " 439.2900 USD")
    .Dividend  ("2013-05-16 CST", " AAPL", " 305.00 USD")
    .Prices    ("2013-06-03 CST").AtClose(" AAPL", " 450.7200 USD")
    .Prices    ("2013-06-07 CST").AtClose(" AAPL", " 441.8100 USD")
    .Prices    ("2013-07-01 CST").AtClose(" AAPL", " 409.2200 USD")
    .Deposit   ("2013-07-08 CST", " 20,000.00 USD", funds.savings)
    .Buy       ("2013-07-22 CST", " MSFT", " 31.8700 USD", 600).Commission("3.00 USD")
    .Prices    ("2013-08-01 CST").AtClose(" AAPL", " 456.6800 USD").AtClose(" MSFT", " 31.6700 USD")
    .Prices    ("2013-08-21 CST").AtClose(" AAPL", " 502.3600 USD").AtClose(" MSFT", " 31.6100 USD")
    .Dividend  ("2013-08-08 CST", " AAPL", " 305.00 USD")
    .Prices    ("2013-09-03 CST").AtClose(" AAPL", " 488.5800 USD").AtClose(" MSFT", " 31.8800 USD")
    .Dividend  ("2013-09-12 CST", " MSFT", " 138.00 USD")
;

You can look at a live version of the sample here.  I also have all of the scripts wrapped up in a zip, if anyone is ambitious enough to take a peek:  budget-scripts.zip

(Edit 2014-01-27 11:15 CST – Fixed Value.toString() in finance.js to properly display fractional portions with a leading zero, e.g., “5.05 USD” instead of “5.5 USD”.)

No Comments

Leave a comment