-
Notifications
You must be signed in to change notification settings - Fork 4
/
Plotting_frame.clj
201 lines (168 loc) · 13.9 KB
/
Plotting_frame.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
;; gorilla-repl.fileformat = 1
;; **
;;; # Plotting Frame
;; **
;; @@
(ns clojure-backtesting.examples.plottingFrame
(:require [clojure-backtesting.data :refer :all]
[clojure-backtesting.data-management :refer :all]
[clojure-backtesting.portfolio :refer :all]
[clojure-backtesting.order :refer :all]
[clojure-backtesting.evaluate :refer :all]
[clojure-backtesting.plot :refer :all]
[clojure-backtesting.counter :refer :all]
[clojure-backtesting.automation :refer :all]
[clojure-backtesting.parameters :refer :all]
[clojure-backtesting.indicators :refer :all]
[clojure-backtesting.direct :refer :all]
[clojure.string :as str]
[clojure.java.io :as io]
[clojure.pprint :as pprint]
[clojure.edn :as edn]
[java-time :as t]
) ;; require all libriaries from core
)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
; path to dataset = "/Volumes/T7/CRSP"
; change it to the relative path to your own dataset
;
(load-dataset "./Volumes/T7/CRSP" "main" add-aprc)
;; @@
;; ->
;;; The dataset is already furnished by add-aprc. No more modification is needed.
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-string'>"Date range: 1972-01-03 ~ 2017-02-10"</span>","value":"\"Date range: 1972-01-03 ~ 2017-02-10\""}
;; <=
;; @@
; path to dataset = "/Volumes/T7/CRSP"
; change it to the relative path to your own dataset
;
(load-dataset "./Volumes/T7/Compustat" "compustat")
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-string'>"Date range: 1962-01-31 ~ 2017-12-31"</span>","value":"\"Date range: 1962-01-31 ~ 2017-12-31\""}
;; <=
;; @@
(init-portfolio "2016-12-30" 100000)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-string'>"Date: 2016-12-30 Cash: $100000"</span>","value":"\"Date: 2016-12-30 Cash: $100000\""}
;; <=
;; @@
(def data (get-permno-prev-n-days "14593" 100))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.demo/data</span>","value":"#'clojure-backtesting.demo/data"}
;; <=
;; @@
(defn get-return [data tic]
(loop [remaining (rest data)
firstline (first data)
result-set []]
(if (empty? remaining)
result-set
(let [nextline (first remaining)
next-remaining (rest remaining)
PRC1 (get firstline :PRC)
PRC2 (get nextline :PRC)
return (/ (- PRC1 PRC2) PRC2)]
(recur next-remaining nextline (conj result-set {:PRC (get firstline :PRC) :tic tic :DATE (get firstline :DATE) :RETURN return}))))))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.demo/get-return</span>","value":"#'clojure-backtesting.demo/get-return"}
;; <=
;; @@
(def singlestock (get-return data "AAPL"))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.demo/singlestock</span>","value":"#'clojure-backtesting.demo/singlestock"}
;; <=
;; @@
(plot singlestock :tic :DATE :RETURN true)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
;; @@
;; =>
;;; {"type":"list-like","open":"<span class='clj-lazy-seq'>(</span>","close":"<span class='clj-lazy-seq'>)</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-string'>"AAPL"</span>","value":"\"AAPL\""},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"},{"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}],"value":"(\"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" \"AAPL\" nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil)"}
;; <=
;; @@
(def data2 (get-permno-prev-n-days (first (tic-permno "F")) 100))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.demo/data2</span>","value":"#'clojure-backtesting.demo/data2"}
;; <=
;; @@
(def data2mul (get-return data2 "F"))
;; @@
;; @@
(plot data2mul :tic :DATE :RETURN true)
;; @@
;; @@
(def data3 (get-permno-prev-n-days (first (tic-permno "IBM")) 100))
;; @@
;; @@
(def data3mul (get-return data3 "IBM"))
;; @@
;; @@
(plot data3mul :tic :DATE :RETURN true)
;; @@
;; @@
(def data4 (get-permno-prev-n-days (first (tic-permno "TSLA")) 100))
;; @@
;; @@
(def data4mul (get-return data4 "TSLA"))
;; @@
;; @@
(plot data4mul :tic :DATE :RETURN true)
;; @@
;; @@
(plot (concat singlestock data4mul data2mul data3mul) :tic :DATE :RETURN true)
;; @@
;; @@
(plot (concat singlestock data4mul data2mul data3mul) :tic :DATE :PRC true)
;; @@
;; @@
(plot singlestock :tic :DATE :PRC true)
;; @@
;; @@
(plot singlestock :tic :DATE :PRC :RETURN true)
;; @@
;; @@
(plot (concat singlestock data4mul data2mul data3mul) :tic :DATE :PRC :RETURN true)
;; @@
;; @@
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.demo/multistocks</span>","value":"#'clojure-backtesting.demo/multistocks"}
;; <=
;; @@
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
(plot singlestock :tic :date :price :return true)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
(plot multistocks :tic :date :price :return true)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=