Skip to content

Instantly share code, notes, and snippets.

@Quantisan
Created June 26, 2011 00:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Quantisan/1047101 to your computer and use it in GitHub Desktop.
Save Quantisan/1047101 to your computer and use it in GitHub Desktop.
first try using Incanter to analyse stocks data
(ns inc-sandbox.corr-demo
(:require [clojure.set :as set])
(:use (incanter core stats charts io))
(:require [clj-time.core :as time])
(:use (clj-time [format :only (formatter formatters parse)]
[coerce :only (to-long)])))
(defn sym-to-dataset
"returns a dataset read from a local CSV in './data/' given a Yahoo Finance symbol name"
[yf-symbol]
(let [+data "./data/"
+csv ".csv"
symbol (.toUpperCase yf-symbol)
filename (str +data symbol +csv)]
(-> (read-dataset
filename
:header true)
(col-names
[:Date :Open :High :Low :Close :Volume :Adj-Close]))))
(def gdx (sym-to-dataset "GDX"))
(def gld (sym-to-dataset "GLD"))
(defn same-dates?
"are two datasets covering the same time frame?"
[x y]
(let [x-dates (into #{} ($ :Date x))
y-dates (into #{} ($ :Date y))
x-y (clojure.set/difference x-dates y-dates)
y-x (clojure.set/difference y-dates x-dates)]
(and (empty? x-y) (empty? y-x))))
(same-dates? gdx gld) ; true
(def gdx-ac ($ :Adj-Close gdx))
(def gld-ac ($ :Adj-Close gld))
(defn dates-long
"returns the dates as long"
[data]
(let [ymd-formatter (formatters :year-month-day)
dates-str ($ :Date data)]
(map #(to-long (parse ymd-formatter %)) dates-str)))
;; no replace col func
(def gdx-times (dates-long gdx))
(def gld-times (dates-long gld))
(view (time-series-plot gld-times gld-ac
:x-label "Date"
:y-label "GLD"))
(view (time-series-plot gdx-times gdx-ac
:x-label "Date"
:y-label "GDX"))
(correlation gdx-ac gld-ac) ; 0.7906494552829249
(spearmans-rho gdx-ac gld-ac) ; 0.7728859703262337
;; no p-value in col, like t-test
(let [lm (linear-model gdx-ac gld-ac)]
(doto (scatter-plot gld-ac gdx-ac
:x-label "GLD"
:y-label "GDX")
(add-lines gld-ac (:fitted lm))
view))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment