1. Create a "MetaARPA" account at SDF.org. (go for it; it's useful for accessing elinks at work via Putty in any case).
2. Create a "~/bin" folder and change directories to it.
3. Using vi, create the following script, naming it "roth_rebalancer.ksh93" (or "401k_", "457b_", etc.):
Code: Select all
#!/usr/pkg/bin/ksh93
DEBUG=false
STARTIN=/sdf/arpa/<PATHTOYOURHOMEDIR>/bin
PORTFOLIO=$STARTIN/roth.portfolio
UPPER_REBALANCE_BAND=1.3
LOWER_REBALANCE_BAND=0.7
CURL=/usr/pkg/bin/curl
SRC="http://download.finance.yahoo.com/d/quotes.csv"
MUTT=/usr/pkg/bin/mutt
NOTIFICATION_EMAIL1=<YOURPHONENUMBER>@vtext.com
NOTIFICATION_EMAIL2=<YOURWORKEMAIL>
NOTIFICATION_EMAIL3=<YOURHOMEEMAIL>
function get_query {
typeset -n ticker="$1"
print -rn "$SRC?s=$(print -rn $ticker)&f=l1&e=.csv"
}
function get_quote {
typeset -n ticker="$1"
print -rn $($CURL -X GET -s $(get_query ticker))
}
body=''
# First pass, sum the portfolio value.
total=0.0
while read ticker shares; do
quote=$(get_quote ticker)
let total=$total+$quote*$shares
done <$PORTFOLIO
body=$body'--------------------------------------------
'
body=$body"Total Portfolio Value: $total
"
# Second pass, calculate whether any position is outside of balancing bands.
notify=false
while read ticker shares target; do
quote=$(get_quote ticker)
let value=$quote*$shares
let target_value=$total*$target
let lower_bound=$LOWER_REBALANCE_BAND*$target_value
let upper_bound=$UPPER_REBALANCE_BAND*$target_value
body=$body'--------------------------------------------
'
body=$body"Ticker: $ticker
"
body=$body"Value: $value
"
body=$body"Target: $target_value
"
body=$body"Lower Band: $lower_bound
"
body=$body"Upper Band: $upper_bound
"
let pct_of_port=$value/$total
body=$body"Cur Pct of Port: $pct_of_port
"
body=$body"Tgt Pct of Port: $target
"
if ((value <= lower_bound || value >= upper_bound)); then
notify=true
body=$body"Value outside of range.
"
if $DEBUG; then
print -u2 "$body"
fi
else
# TODO: Periodically collect statistics.
body=$body"Value within range.
"
if $DEBUG; then
print -u2 "$body"
fi
fi
done <$PORTFOLIO
if $notify; then
# TODO: Attach trendline report using troff's 'grap' package.
if $DEBUG; then
print -u2 "Sending e-mail notification."
fi
$MUTT -s 'Portfolio Rebalance' $NOTIFICATION_EMAIL1 <<EOF
$body
EOF
$MUTT -s 'Portfolio Rebalance' $NOTIFICATION_EMAIL2 <<EOF
$body
EOF
$MUTT -s 'Portfolio Rebalance' $NOTIFICATION_EMAIL3 <<EOF
$body
EOF
fi
4. Make the script executable with the following command: "chmod 700 roth_rebalancer.ksh93"
5. Using vi, create the following file, naming it "roth.portfolio" (or "401k." "457b.", etc.):
Code: Select all
QLENX 3920 0.5
AUENX 1467 0.25
TMF 1077 0.205
TQQQ 44 0.045
6. Run "mkcron".
7. Type "edit".
8. Enter the following:
Code: Select all
1:5,35 13-21 * * 1-5 /sdf/arpa/<PATHTOYOURHOMEDIR>/bin/roth_rebalancer.ksh93
9. Type "sync".
10. Type "quit".
Provided everything is working correctly, notification will be sent twice an hour if any of the positions are outside of their rebalance bands. Debug the script with "DEBUG=true". Force a notification by playing with the targets in the ".portfolio" file. Below is a sample of the notification body:
Update the ".portfolio" input as dividends are reinvested, purchases are made, etc. Improve the script, or, use one myriad of other languages available on sdf.org to craft your own.--------------------------------------------
Total Portfolio Value: 108890.73000000001
--------------------------------------------
Ticker: QLENX
Value: 55115.2000000000044
Target: 27222.682499999999
Lower Band: 19055.8777499999997
Upper Band: 35389.4872499999983
Cur Pct of Port: 0.506151441908783273
Tgt Pct of Port: 0.25
Value outside of range.
--------------------------------------------
Ticker: AUENX
Value: 26361.989999999998
Target: 54445.364999999998
Lower Band: 38111.7554999999993
Upper Band: 70778.9744999999966
Cur Pct of Port: 0.242095814767703388
Tgt Pct of Port: 0.50
Value outside of range.
--------------------------------------------
Ticker: TMF
Value: 22509.2999999999993
Target: 22322.5996499999965
Lower Band: 15625.8197549999968
Upper Band: 29019.3795449999961
Cur Pct of Port: 0.206714566060857541
Tgt Pct of Port: 0.205
Value within range.
--------------------------------------------
Ticker: TQQQ
Value: 4904.23999999999978
Target: 4900.08284999999978
Lower Band: 3430.05799499999966
Upper Band: 6370.10770500000035
Cur Pct of Port: 0.0450381772626558785
Tgt Pct of Port: 0.045
Value within range.
Good luck.