Skip to content

Instantly share code, notes, and snippets.

@davidvhill
Last active February 20, 2020 18:12
Show Gist options
  • Save davidvhill/43ccf6d38a6d43f04e6bed8a365d82a1 to your computer and use it in GitHub Desktop.
Save davidvhill/43ccf6d38a6d43f04e6bed8a365d82a1 to your computer and use it in GitHub Desktop.
Clojure yield-on-cost report
(ns compound.core
(:gen-class))
;; items is :label, dividend rate, dividend growth rate
(def items [[:abbv 0.0504 0.209]
[:mo 0.07 0.10]
[:pru 0.0472 0.13]
[:abc 0.0178 0.10]
[:amp 0.0219 0.11]
[:cm 0.0521 0.036]
[:cmi 0.0316 0.118]
[:cvs 0.0278 0.127]
[:dal 0.0274 0.38]
[:epd 0.068 0.042]
[:oxy 0.0760 0.024]
[:spg 0.06 0.107]
[:wfc 0.0434 0.073]])
(defn total
[capital rate growth years]
(loop [r rate y years total capital]
(if (= y 0)
total
(recur (+ r (* r growth))
(- y 1)
(+ total (* total r))))))
(defn yield-on-cost
[rate growth years]
(loop [r rate y years]
(if (= y 0)
r
(recur (+ r (* r growth)) (- y 1)))))
;; nice format to stdout: (pprint/print-table (sort-by :yield-on-cost (report 15)))
(defn report
[years]
(map (fn [i] {:ticker (nth i 0) :yield-on-cost (yield-on-cost (nth i 1) (nth i 2) years)})
items))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment