-
Notifications
You must be signed in to change notification settings - Fork 4
/
simple_trading_strategy.clj
367 lines (328 loc) · 17.4 KB
/
simple_trading_strategy.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
;; gorilla-repl.fileformat = 1
;; **
;;; ## Simple Trading Strategy Example
;; **
;; @@
; import libraries from kernel
(ns clojure-backtesting.updated_examples.simpleStrat
(: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
;; **
;; @@
;; initialise with current date and initial capital (= $10000)
(init-portfolio "1980-12-16" 10000)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-string'>"Date: 1980-12-16 Cash: $10000"</span>","value":"\"Date: 1980-12-16 Cash: $10000\""}
;; <=
;; **
;;; ### Write a strategy
;;;
;;; The following code implements a simple trading strategy:
;;;
;;; In a timespan of 10 days (inclusive of today),
;;; - Buy 50 stocks of AAPL on the first day
;;; - Sell 10 stocks of AAPL on every other day
;; **
;; **
;;; ### (1) Running the strategy
;;; - Note that you should have loaded the **extract** dataset
;; **
;; @@
;; define the "time span", i.e. to trade in the coming 10 days
(def num-of-days (atom 10))
(while (pos? @num-of-days) ;; check if num-of-days is > 0
(do
;; write your trading strategy here
(if (= 10 @num-of-days) ;; check if num-of-days == 10
(do
(order "14593" 50 :print true) ; buy 50 stocks
;; (println ((fn [date] (str "Buy 50 stocks of AAPL on " date)) (get-date)))
)
)
(if (odd? @num-of-days) ;; check if num-of-days is odd
(do
(order "14593" -10 :print true) ; sell 10 stocks
;; (println ((fn [date] (str "Sell 10 stocks of AAPL on " date)) (get-date)))
)
)
(update-eval-report) ;; update the evaluation metrics every day
(println (get-date))
; move on to the next trading day
(next-date)
; decrement counter
(swap! num-of-days dec)
)
)
;; @@
;; ->
;;; 1980-12-16
;;; Order: 1980-12-17 | 14593 | 50.000000.
;;; 1980-12-17
;;; Order: 1980-12-18 | 14593 | -10.000000.
;;; 1980-12-18
;;; 1980-12-19
;;; Order: 1980-12-22 | 14593 | -10.000000.
;;; 1980-12-22
;;; 1980-12-23
;;; Order: 1980-12-24 | 14593 | -10.000000.
;;; 1980-12-24
;;; 1980-12-26
;;; Order: 1980-12-29 | 14593 | -10.000000.
;;; 1980-12-29
;;; 1980-12-30
;;; Order: 1980-12-31 | 14593 | -10.000000.
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### Check order record
;; **
;; @@
(print-order-record)
;; @@
;; ->
;;;
;;; | :date | :permno | :price | :aprc | :quantity |
;;; |------------+---------+---------+-------+-----------|
;;; | 1980-12-17 | 14593 | 25.9375 | 25.35 | 50 |
;;; | 1980-12-18 | 14593 | 26.6875 | 25.67 | -10 |
;;; | 1980-12-22 | 14593 | 29.6875 | 26.88 | -10 |
;;; | 1980-12-24 | 14593 | 32.5625 | 27.98 | -10 |
;;; | 1980-12-29 | 14593 | 36.0625 | 29.25 | -10 |
;;; | 1980-12-31 | 14593 | 34.1875 | 28.58 | -10 |
;;;
;; <-
;; =>
;;; {"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 | 10116.11 |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
;; view portfolio value and return
; pass negative value to print-portfolio-record to get all records
(print-portfolio-record -1)
;; @@
;; ->
;;;
;;; | :date | :tot-value | :daily-ret | :tot-ret | :loan | :leverage | :margin |
;;; |------------+------------+------------+----------+-------+-----------+---------|
;;; | 1980-12-16 | $10000.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-17 | $10000.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-18 | $10015.79 | 0.00% | 0.07% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-19 | $10042.49 | 0.12% | 0.18% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-22 | $10064.41 | 0.00% | 0.28% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-23 | $10078.98 | 0.06% | 0.34% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-24 | $10097.44 | 0.00% | 0.42% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-26 | $10119.28 | 0.09% | 0.51% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-29 | $10122.82 | -0.00% | 0.53% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-30 | $10119.71 | -0.01% | 0.52% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-31 | $10116.11 | 0.00% | 0.50% | $0.00 | 0.00 | 0.00% |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### Generate evaluation report
;; **
;; @@
(print-eval-report)
;; @@
;; ->
;;;
;;; | :date | :tot-value | :vol | :r-vol | :sharpe | :r-sharpe | :pnl-pt | :max-drawdown |
;;; |------------+------------+---------+---------+----------+-----------+---------+---------------|
;;; | 1980-12-17 | $10000 | 0.0000% | 0.0000% | 0.0000% | 0.0000% | $0 | 0.0000 |
;;; | 1980-12-18 | $10015 | 0.0000% | 0.0000% | 0.0000% | 0.0000% | $7 | 0.0000 |
;;; | 1980-12-19 | $10042 | 0.0578% | 0.0578% | 3.1854% | 3.1854% | $21 | 100.0000 |
;;; | 1980-12-22 | $10064 | 0.0517% | 0.0517% | 5.3929% | 5.3929% | $21 | 100.0000 |
;;; | 1980-12-23 | $10078 | 0.0490% | 0.0490% | 6.9722% | 6.9722% | $26 | 100.0000 |
;;; | 1980-12-24 | $10097 | 0.0461% | 0.0461% | 9.1301% | 9.1301% | $24 | 100.0000 |
;;; | 1980-12-26 | $10119 | 0.0491% | 0.0491% | 10.4957% | 10.4957% | $29 | 100.0000 |
;;; | 1980-12-29 | $10122 | 0.0473% | 0.0473% | 11.2136% | 11.2136% | $24 | 100.0000 |
;;; | 1980-12-30 | $10119 | 0.0467% | 0.0467% | 11.0778% | 11.0778% | $23 | 111.5205 |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### Plot variables
;;; Below are example codes that show how to plot different variables in the portfolio record / evaluation record.
;; **
;; **
;;; ### 1. Portfolio daily return
;; **
;; @@
(def data (deref portfolio-value))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.updated_examples.simpleStrat/data</span>","value":"#'clojure-backtesting.updated_examples.simpleStrat/data"}
;; <=
;; @@
; Add legend name to series
(def data-to-plot
(map #(assoc % :plot "port-value")
data))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.updated_examples.simpleStrat/data-to-plot</span>","value":"#'clojure-backtesting.updated_examples.simpleStrat/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-16"</span>","value":"\"1980-12-16\""}],"value":"[:date \"1980-12-16\"]"},{"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'>10000</span>","value":"10000"}],"value":"[:tot-value 10000]"},{"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'>"port-value"</span>","value":"\"port-value\""}],"value":"[:plot \"port-value\"]"}],"value":"{:date \"1980-12-16\", :tot-value 10000, :daily-ret 0.0, :tot-ret 0.0, :loan 0.0, :leverage 0.0, :margin 0.0, :plot \"port-value\"}"}
;; <=
;; @@
(plot data-to-plot :plot :date :daily-ret false)
;; @@
;; ->
;;; [I 02:14:03.874 Clojupyter] oz.core:273 -- Starting up server on port 10666
;;; [I 02:14:04.159 Clojupyter] oz.server:142 -- Web server is running at `http://localhost:10666/`
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### 2. Plot volatility
;; **
;; @@
(def data (deref eval-record))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.updated_examples.simpleStrat/data</span>","value":"#'clojure-backtesting.updated_examples.simpleStrat/data"}
;; <=
;; @@
(first data)
;; @@
;; =>
;;; {"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-17"</span>","value":"\"1980-12-17\""}],"value":"[:date \"1980-12-17\"]"},{"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-double'>10000.0</span>","value":"10000.0"}],"value":"[:tot-value 10000.0]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:vol</span>","value":":vol"},{"type":"html","content":"<span class='clj-double'>0.0</span>","value":"0.0"}],"value":"[:vol 0.0]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:r-vol</span>","value":":r-vol"},{"type":"html","content":"<span class='clj-double'>0.0</span>","value":"0.0"}],"value":"[:r-vol 0.0]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:sharpe</span>","value":":sharpe"},{"type":"html","content":"<span class='clj-double'>0.0</span>","value":"0.0"}],"value":"[:sharpe 0.0]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:r-sharpe</span>","value":":r-sharpe"},{"type":"html","content":"<span class='clj-double'>0.0</span>","value":"0.0"}],"value":"[:r-sharpe 0.0]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:pnl-pt</span>","value":":pnl-pt"},{"type":"html","content":"<span class='clj-double'>0.0</span>","value":"0.0"}],"value":"[:pnl-pt 0.0]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:max-drawdown</span>","value":":max-drawdown"},{"type":"html","content":"<span class='clj-double'>0.0</span>","value":"0.0"}],"value":"[:max-drawdown 0.0]"}],"value":"{:date \"1980-12-17\", :tot-value 10000.0, :vol 0.0, :r-vol 0.0, :sharpe 0.0, :r-sharpe 0.0, :pnl-pt 0.0, :max-drawdown 0.0}"}
;; <=
;; @@
; Add legend name to series
(def data-to-plot
(map #(assoc % :plot "volatility")
data))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.updated_examples.simpleStrat/data-to-plot</span>","value":"#'clojure-backtesting.updated_examples.simpleStrat/data-to-plot"}
;; <=
;; @@
(print-eval-report)
;; @@
;; ->
;;;
;;; | :date | :tot-value | :vol | :r-vol | :sharpe | :r-sharpe | :pnl-pt | :max-drawdown |
;;; |------------+------------+---------+---------+----------+-----------+---------+---------------|
;;; | 1980-12-17 | $10000 | 0.0000% | 0.0000% | 0.0000% | 0.0000% | $0 | 0.0000 |
;;; | 1980-12-18 | $10015 | 0.0000% | 0.0000% | 0.0000% | 0.0000% | $7 | 0.0000 |
;;; | 1980-12-19 | $10042 | 0.0578% | 0.0578% | 3.1854% | 3.1854% | $21 | 100.0000 |
;;; | 1980-12-22 | $10064 | 0.0517% | 0.0517% | 5.3929% | 5.3929% | $21 | 100.0000 |
;;; | 1980-12-23 | $10078 | 0.0490% | 0.0490% | 6.9722% | 6.9722% | $26 | 100.0000 |
;;; | 1980-12-24 | $10097 | 0.0461% | 0.0461% | 9.1301% | 9.1301% | $24 | 100.0000 |
;;; | 1980-12-26 | $10119 | 0.0491% | 0.0491% | 10.4957% | 10.4957% | $29 | 100.0000 |
;;; | 1980-12-29 | $10122 | 0.0473% | 0.0473% | 11.2136% | 11.2136% | $24 | 100.0000 |
;;; | 1980-12-30 | $10119 | 0.0467% | 0.0467% | 11.0778% | 11.0778% | $23 | 111.5205 |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
(plot data-to-plot :plot :date :vol true)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### 3. Plot sharpe ratio
;; **
;; @@
(def data-to-plot
(map #(assoc % :plot "sharpe ratio")
data))
(plot data-to-plot :plot :date :sharpe true)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### 4. Plot stock price
;; **
;; @@
(def data (deref order-record))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-var'>#'clojure-backtesting.updated_examples.simpleStrat/data</span>","value":"#'clojure-backtesting.updated_examples.simpleStrat/data"}
;; <=
;; @@
(first data)
;; @@
;; =>
;;; {"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-17"</span>","value":"\"1980-12-17\""}],"value":"[:date \"1980-12-17\"]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:permno</span>","value":":permno"},{"type":"html","content":"<span class='clj-string'>"14593"</span>","value":"\"14593\""}],"value":"[:permno \"14593\"]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:price</span>","value":":price"},{"type":"html","content":"<span class='clj-double'>25.9375</span>","value":"25.9375"}],"value":"[:price 25.9375]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:aprc</span>","value":":aprc"},{"type":"html","content":"<span class='clj-string'>"25.35"</span>","value":"\"25.35\""}],"value":"[:aprc \"25.35\"]"},{"type":"list-like","open":"","close":"","separator":" ","items":[{"type":"html","content":"<span class='clj-keyword'>:quantity</span>","value":":quantity"},{"type":"html","content":"<span class='clj-long'>50</span>","value":"50"}],"value":"[:quantity 50]"}],"value":"{:date \"1980-12-17\", :permno \"14593\", :price 25.9375, :aprc \"25.35\", :quantity 50}"}
;; <=
;; @@
(def data-to-plot
(map #(assoc % :plot "price")
data))
(plot data-to-plot :plot :date :price true)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=