Skip to content

Instantly share code, notes, and snippets.

@guilespi
Created October 29, 2012 02:25
Show Gist options
  • Save guilespi/3971076 to your computer and use it in GitHub Desktop.
Save guilespi/3971076 to your computer and use it in GitHub Desktop.
Build a matrix from stock datasets
(defn select-value
"Given a dataset indexed by date, returns the value corresponding to a specified column
if existent for a specific date"
[ds column date]
(let [row (ds {:Date date})]
(when-not (nil? row) (incanter.core/$ 0 column row))))
(defn get-data
"Given a list of `symbols`, its data and a list of specific `timestamps`, builds a matrix(sequence)
with each column corresponding to a stock and the value extracted using `column`
:Date AAPL GOOG WFC
2012-02-01 54.1 33.1 25.0
2012-02-02 56.3 33.4 22.9"
[timestamps symbols column symbols-data time-of-day]
(let [grouped-data (reduce (fn [m, s] (assoc m (s 0) (incanter.core/$group-by :Date (s 1)))) {} symbols-data)]
(for [t timestamps]
(let [unparsed-date (unparse (formatters :year-month-day) t)]
(reduce #(merge %1 {(%2 0) (select-value (%2 1) column unparsed-date)}) {:Date (to-long t)} grouped-data)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment