-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrate_of_change.clj
More file actions
232 lines (209 loc) · 11.7 KB
/
Copy pathrate_of_change.clj
File metadata and controls
232 lines (209 loc) · 11.7 KB
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
;; gorilla-repl.fileformat = 1
;; **
;;; ## Rate of Change example
;; **
;; @@
(ns clojure-backtesting.examples.roc
(: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.
;;; 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 (= $10000)
(init-portfolio "1981-01-01" 1000);
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-string'>"Date: 1980-12-31 Cash: $1000"</span>","value":"\"Date: 1980-12-31 Cash: $1000\""}
;; <=
;; **
;;; ### Rate of Change (ROC) strategy
;;;
;;; ROC = (Closing price - Closing n periods ago) / Closing n periods ago * 100
;;;
;;; We'll trade according to the following rules:
;;; - **Buy signal**: when ROC crosses the lower threshold (e.g. -30)
;;; - **Sell signal**: when ROC crosses the upper threshold (e.g. +30
;; **
;; @@
(def lower-threshold -30)
(def upper-threshold 30)
(while (< (compare (get-date) "1981-12-25") 0)
(let [roc (* (ROC "14593" 10) 100)]
(if (< roc lower-threshold)
(order "14593" 1 :print false)) ; buy signal
(if (> roc upper-threshold)
(order "14593" -1 :print false))) ; sell signal
(update-eval-report)
(next-date))
(end-order)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-unkown'>true</span>","value":"true"}
;; <=
;; @@
(print-order-record 10)
;; @@
;; ->
;;;
;;; | :date | :permno | :price | :aprc | :quantity |
;;; |------------+---------+---------+-------+-----------|
;;; | 1981-01-02 | 14593 | 34.625 | 28.74 | -1 |
;;; | 1981-01-05 | 14593 | 33.8125 | 28.45 | -1 |
;;; | 1981-10-12 | 14593 | 19.3125 | 22.30 | -1 |
;;; | 1981-10-13 | 14593 | 19.375 | 22.34 | -1 |
;;; | 1981-12-28 | 14593 | 21.0 | 23.13 | 4 |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### Check portfolio record
;; **
;; @@
(print-portfolio)
;; @@
;; ->
;;;
;;; | :asset | :price | :aprc | :quantity | :tot-val |
;;; |--------+--------+-------+-----------+----------|
;;; | cash | N/A | N/A | N/A | 1009.30 |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
(print-portfolio-record 10)
;; @@
;; ->
;;;
;;; | :date | :tot-value | :daily-ret | :tot-ret | :loan | :leverage | :margin |
;;; |------------+------------+------------+----------+-------+-----------+---------|
;;; | 1980-12-31 | $1000.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;; | 1981-01-02 | $1000.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;; | 1981-01-05 | $1000.29 | -0.00% | 0.01% | $0.00 | 0.00 | 0.00% |
;;; | 1981-01-06 | $1001.41 | 0.05% | 0.06% | $0.00 | 0.00 | 0.00% |
;;; | 1981-01-07 | $1002.45 | 0.05% | 0.11% | $0.00 | 0.00 | 0.00% |
;;; | 1981-01-08 | $1002.93 | 0.02% | 0.13% | $0.00 | 0.00 | 0.00% |
;;; | 1981-01-09 | $1001.69 | -0.05% | 0.07% | $0.00 | 0.00 | 0.00% |
;;; | 1981-01-12 | $1001.83 | 0.01% | 0.08% | $0.00 | 0.00 | 0.00% |
;;; | 1981-01-13 | $1002.74 | 0.04% | 0.12% | $0.00 | 0.00 | 0.00% |
;;; | 1981-01-14 | $1002.64 | -0.00% | 0.11% | $0.00 | 0.00 | 0.00% |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### Generate evaluation report
;; **
;; @@
(print-eval-report 30)
;; @@
;; ->
;;;
;;; | :date | :tot-value | :vol | :r-vol | :sharpe | :r-sharpe | :pnl-pt | :max-drawdown |
;;; |------------+------------+---------+---------+---------------------+---------------------+---------+---------------|
;;; | 1981-01-02 | $1000 | 0.0000% | 0.0000% | 0.0000% | 0.0000% | $0 | 0.0000 |
;;; | 1981-01-05 | $1000 | 0.0000% | 0.0000% | 4599543695833.1280% | 4599543695833.1280% | $0 | 0.0000 |
;;; | 1981-01-06 | $1001 | 0.0241% | 0.0241% | 2.5316% | 2.5316% | $0 | 100.0000 |
;;; | 1981-01-07 | $1002 | 0.0256% | 0.0256% | 4.1486% | 4.1486% | $1 | 100.0000 |
;;; | 1981-01-08 | $1002 | 0.0229% | 0.0229% | 5.5475% | 5.5475% | $1 | 100.0000 |
;;; | 1981-01-09 | $1001 | 0.0346% | 0.0346% | 2.1147% | 2.1147% | $0 | 211.9380 |
;;; | 1981-01-12 | $1001 | 0.0321% | 0.0321% | 2.4749% | 2.4749% | $0 | 211.9380 |
;;; | 1981-01-13 | $1002 | 0.0317% | 0.0317% | 3.7421% | 3.7421% | $1 | 211.9380 |
;;; | 1981-01-14 | $1002 | 0.0303% | 0.0303% | 3.7759% | 3.7759% | $1 | 211.9380 |
;;; | 1981-01-15 | $1002 | 0.0305% | 0.0305% | 3.0112% | 3.0112% | $1 | 211.9380 |
;;; | 1981-01-16 | $1002 | 0.0291% | 0.0291% | 3.5129% | 3.5129% | $1 | 211.9380 |
;;; | 1981-01-19 | $1000 | 0.0337% | 0.0337% | 1.2088% | 1.2088% | $0 | 227.2370 |
;;; | 1981-01-20 | $1001 | 0.0334% | 0.0334% | 2.1923% | 2.1923% | $0 | 227.2370 |
;;; | 1981-01-21 | $1001 | 0.0329% | 0.0329% | 1.5450% | 1.5450% | $0 | 227.2370 |
;;; | 1981-01-22 | $1000 | 0.0320% | 0.0320% | 1.2109% | 1.2109% | $0 | 227.2370 |
;;; | 1981-01-23 | $1000 | 0.0310% | 0.0310% | 1.3802% | 1.3802% | $0 | 227.2370 |
;;; | 1981-01-26 | $1001 | 0.0303% | 0.0303% | 2.0115% | 2.0115% | $0 | 227.2370 |
;;; | 1981-01-27 | $1001 | 0.0295% | 0.0295% | 2.2763% | 2.2763% | $0 | 227.2370 |
;;; | 1981-01-28 | $1002 | 0.0296% | 0.0296% | 3.4513% | 3.4513% | $1 | 227.2370 |
;;; | 1981-01-29 | $1003 | 0.0297% | 0.0297% | 4.7035% | 4.7035% | $1 | 227.2370 |
;;; | 1981-01-30 | $1004 | 0.0307% | 0.0307% | 6.2963% | 6.2963% | $2 | 214.0978 |
;;; | 1981-02-02 | $1005 | 0.0319% | 0.0319% | 7.9426% | 7.9426% | $2 | 202.4048 |
;;; | 1981-02-03 | $1005 | 0.0326% | 0.0326% | 6.6785% | 6.6785% | $2 | 202.4048 |
;;; | 1981-02-04 | $1004 | 0.0331% | 0.0331% | 5.5226% | 5.5226% | $2 | 202.4048 |
;;; | 1981-02-05 | $1004 | 0.0325% | 0.0325% | 5.5614% | 5.5614% | $2 | 202.4048 |
;;; | 1981-02-06 | $1004 | 0.0319% | 0.0319% | 5.5960% | 5.5960% | $2 | 202.4048 |
;;; | 1981-02-09 | $1005 | 0.0324% | 0.0324% | 7.0664% | 7.0664% | $2 | 202.4048 |
;;; | 1981-02-10 | $1005 | 0.0318% | 0.0318% | 7.2623% | 7.2623% | $2 | 202.4048 |
;;; | 1981-02-11 | $1006 | 0.0316% | 0.0316% | 8.3162% | 8.3162% | $3 | 202.4048 |
;;; | 1981-02-12 | $1006 | 0.0310% | 0.0315% | 8.7512% | 8.6148% | $3 | 202.4048 |
;;;
;; <-
;; =>
;;; {"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.demo/data</span>","value":"#'clojure-backtesting.demo/data"}
;; <=
;; @@
(def data-to-plot
(map #(assoc % :plot "portfolio")
data))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.demo/data-to-plot</span>","value":"#'clojure-backtesting.demo/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'>"1980-12-31"</span>","value":"\"1980-12-31\""}],"value":"[:date \"1980-12-31\"]"},{"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 \"1980-12-31\", :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 false)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
;; @@