-
Notifications
You must be signed in to change notification settings - Fork 4
/
golden_cross.clj
235 lines (213 loc) · 9.95 KB
/
golden_cross.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
;; gorilla-repl.fileformat = 1
;; **
;;; ## Golden Cross Example
;; **
;; @@
; import libraries from kernel
(ns clojure-backtesting.updated_examples.goldencross
(: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]
) ;; require all libriaries from core
)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### Import dataset
;; **
;; @@
; 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\""}
;; <=
;; **
;;; ### Initialise portfolio (Go back here everytime you want to restart.)
;; **
;; @@
;; initialise with current date and initial capital (= $1000)
(init-portfolio "1981-12-15" 1000);
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-string'>"Date: 1981-12-15 Cash: $1000"</span>","value":"\"Date: 1981-12-15 Cash: $1000\""}
;; <=
;; @@
(get-date)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-string'>"1981-12-15"</span>","value":"\"1981-12-15\""}
;; <=
;; **
;;; ### Write a strategy
;;;
;;; The following code implements a trading strategy called Golden Rule:
;;;
;;; MA 15 cross above the MA 30 (golden cross)
;;;
;;; MA 15 cross below the MA 30 (death cross)
;;;
;;; So in the codes, MA15 and MA30 are compared on a daily basis, if golden cross occurs, then you set a buy order; if death cross occurs, then you set a sell order first
;;;
;;;
;; **
;; **
;;; Should increase the cache size first to reduce repeatitive File IO
;; **
;; @@
(CHANGE-CACHE-SIZE 30)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.parameters/CACHE-SIZE</span>","value":"#'clojure-backtesting.parameters/CACHE-SIZE"}
;; <=
;; @@
(while (< (compare (get-date) "1981-12-24") 0)
;; run for about a week
(do
(let [[MA15 MA30] [(moving-avg "14593" 15) (moving-avg "14593" 30)]]
(if (> MA15 MA30)
(order "14593" 0.1 :print false)
(order "14593" 0 :remaining true))))
(let [[MA15 MA30] [(moving-avg "25785" 15) (moving-avg "25785" 30)]]
(if (> MA15 MA30)
(order "25785" 1 :print false)
(order "25785" 0 :remaining true)))
(update-eval-report)
(next-date))
(end-order)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-unkown'>true</span>","value":"true"}
;; <=
;; @@
;(pprint/print-table (take 200 (deref portfolio-value)))
(print-order-record 10)
;; @@
;; ->
;;;
;;; | :date | :permno | :price | :aprc | :quantity |
;;; |------------+---------+---------+-------+-----------|
;;; | 1981-12-16 | 14593 | 19.5625 | 22.43 | 0.1 |
;;; | 1981-12-16 | 25785 | 17.25 | 55.94 | 1 |
;;; | 1981-12-17 | 14593 | 21.1875 | 23.22 | 0.1 |
;;; | 1981-12-17 | 25785 | 17.625 | 56.46 | 1 |
;;; | 1981-12-18 | 14593 | 22.9375 | 24.03 | 0.1 |
;;; | 1981-12-18 | 25785 | 18.0 | 56.98 | 1 |
;;; | 1981-12-21 | 14593 | 21.9375 | 23.57 | 0.1 |
;;; | 1981-12-21 | 25785 | 18.0 | 56.98 | 1 |
;;; | 1981-12-22 | 14593 | 22.3125 | 23.75 | 0.1 |
;;; | 1981-12-22 | 25785 | 17.5 | 56.29 | 1 |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### Check portfolio record
;; **
;; @@
;; view final portfolio
(print-portfolio)
;; @@
;; ->
;;;
;;; | :asset | :price | :aprc | :quantity | :tot-val |
;;; |--------+--------+-------+-----------+----------|
;;; | cash | N/A | N/A | N/A | 996.42 |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
;; view portfolio value and return
(print-portfolio-record 10)
;; @@
;; ->
;;;
;;; | :date | :tot-value | :daily-ret | :tot-ret | :loan | :leverage | :margin |
;;; |------------+------------+------------+----------+-------+-----------+---------|
;;; | 1981-12-15 | $1000.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;; | 1981-12-16 | $1000.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;; | 1981-12-17 | $1000.60 | -0.00% | 0.03% | $0.00 | 0.00 | 0.00% |
;;; | 1981-12-18 | $1001.80 | 0.00% | 0.08% | $0.00 | 0.00 | 0.00% |
;;; | 1981-12-21 | $1001.67 | 0.00% | 0.07% | $0.00 | 0.00 | 0.00% |
;;; | 1981-12-22 | $998.96 | -0.00% | -0.05% | $0.00 | 0.00 | 0.00% |
;;; | 1981-12-23 | $995.33 | 0.00% | -0.20% | $0.00 | 0.00 | 0.00% |
;;; | 1981-12-24 | $996.42 | 0.00% | -0.16% | $0.00 | 0.00 | 0.00% |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### Generate evaluation report
;; **
;; @@
(print-eval-report 10)
;; @@
;; ->
;;;
;;; | :date | :tot-value | :vol | :r-vol | :sharpe | :r-sharpe | :pnl-pt | :max-drawdown |
;;; |------------+------------+---------+---------+-----------------------+-----------------------+---------+---------------|
;;; | 1981-12-16 | $1000 | 0.0000% | 0.0000% | 0.0000% | 0.0000% | $0 | 0.0000 |
;;; | 1981-12-17 | $1000 | 0.0000% | 0.0000% | 9419676466259.1450% | 9419676466259.1450% | $0 | 0.0000 |
;;; | 1981-12-18 | $1001 | 0.0000% | 0.0000% | 32469602626935.1760% | 32469602626935.1760% | $0 | 0.0000 |
;;; | 1981-12-21 | $1001 | 0.0000% | 0.0000% | 33522685497906.6330% | 33522685497906.6330% | $0 | 0.0000 |
;;; | 1981-12-22 | $998 | 0.0000% | 0.0000% | -18079259030964.3000% | -18079259030964.3000% | $0 | 0.0000 |
;;; | 1981-12-23 | $995 | 0.0000% | 0.0000% | -86461715180573.6600% | -86461715180573.6600% | $0 | 0.0000 |
;;; | 1981-12-24 | $996 | 0.0000% | 0.0000% | -69689219714318.2400% | -69689219714318.2400% | $0 | 0.0000 |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### Plot variables
;; **
;; @@
(def data (deref portfolio-value))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.updated_examples.goldencross/data</span>","value":"#'clojure-backtesting.updated_examples.goldencross/data"}
;; <=
;; @@
; Add legend name to series
(def data-to-plot
(map #(assoc % :plot "portfolio")
data))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.updated_examples.goldencross/data-to-plot</span>","value":"#'clojure-backtesting.updated_examples.goldencross/data-to-plot"}
;; <=
;; @@
(first data-to-plot)
;; @@
;; =>
;;; {"type":"list-like","open":"<span class='clj-map'>{</span>","close":"<span class='clj-map'>}</span>","separator":", ","items":[{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:date</span>","value":":date"},{"type":"html","content":"<span class='clj-string'>"1981-12-15"</span>","value":"\"1981-12-15\""}],"value":"[:date \"1981-12-15\"]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:tot-value</span>","value":":tot-value"},{"type":"html","content":"<span class='clj-long'>1000</span>","value":"1000"}],"value":"[:tot-value 1000]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:daily-ret</span>","value":":daily-ret"},{"type":"html","content":"<span class='clj-double'>0.0</span>","value":"0.0"}],"value":"[:daily-ret 0.0]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:tot-ret</span>","value":":tot-ret"},{"type":"html","content":"<span class='clj-double'>0.0</span>","value":"0.0"}],"value":"[:tot-ret 0.0]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:loan</span>","value":":loan"},{"type":"html","content":"<span class='clj-double'>0.0</span>","value":"0.0"}],"value":"[:loan 0.0]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:leverage</span>","value":":leverage"},{"type":"html","content":"<span class='clj-double'>0.0</span>","value":"0.0"}],"value":"[:leverage 0.0]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:margin</span>","value":":margin"},{"type":"html","content":"<span class='clj-double'>0.0</span>","value":"0.0"}],"value":"[:margin 0.0]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:plot</span>","value":":plot"},{"type":"html","content":"<span class='clj-string'>"portfolio"</span>","value":"\"portfolio\""}],"value":"[:plot \"portfolio\"]"}],"value":"{:date \"1981-12-15\", :tot-value 1000, :daily-ret 0.0, :tot-ret 0.0, :loan 0.0, :leverage 0.0, :margin 0.0, :plot \"portfolio\"}"}
;; <=
;; @@
(plot data-to-plot :plot :date :daily-ret true)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=