Database Reference
In-Depth Information
How to do it…
Unfortunately, Incanter doesn't convert the dates in the data ile, so we'll need to do this
ourselves. This isn't dificult to do, but it will take a few lines. Once that's done, we can
calculate the rolling averages.
1.
First, we'll write a function to parse the dates:
(def ^:dynamic *formatter* (tf/formatter "dd-MMM-yy"))
(defn parse-date [date] (tf/parse *formatter* date))
2. Now we can open the data ile, convert the appropriate rows to dates, and merge the
results back into the original dataset:
(def data
(i/add-derived-column
:date [:date-str] parse-date
(i/col-names
(incanter.io/read-dataset data-file)
[:date-str :open :high :low :close :volume])))
3.
To use this with incanter.zoo , we have to convert the dataset to a Zoo object.
When we do this, we'll tell it which column contains the time data ( :date ). From this
point on, we'll need to refer to this column with the key ( :index ):
(def data-zoo (zoo/zoo data :date))
4. Now, to compute a rolling ive-day average, we just call the incanter.zoo/roll-
mean function. This will merge the ive-day rolling average back into the dataset as
the column ( :five-day ):
(def data-roll5
(->>
(i/sel data-zoo :cols :close)
(zoo/roll-mean 5)
(i/dataset [:five-day])
(i/conj-cols data-zoo)))
 
Search WWH ::




Custom Search