-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuying_on_margin.clj
More file actions
317 lines (281 loc) · 9.68 KB
/
Copy pathbuying_on_margin.clj
File metadata and controls
317 lines (281 loc) · 9.68 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
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
;; gorilla-repl.fileformat = 1
;; **
;;; ## Buying on margin example
;; **
;; @@
; import libraries from kernel
(ns clojure-backtesting.examples.buy-on-margin
(: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\""}
;; <=
;; **
;;; ### Trade without leverage
;;;
;;; The trade would be allowed if you possess enough cash to pay.
;; **
;; @@
(init-portfolio "1980-12-15" 400)
(order "28636" 10 :leverage false :remaining true :print true) ;without leverage, remaining value
(next-date)
(next-date)
(next-date)
(print-order-record)
(print-portfolio)
(print-portfolio-record -1)
;; @@
;; ->
;;; Order: 1980-12-16 | 28636 | 10.000000.
;;;
;;; | :date | :permno | :price | :aprc | :quantity |
;;; |------------+---------+--------+-------+-----------|
;;; | 1980-12-16 | 28636 | 7.375 | 22.18 | 10 |
;;;
;;; | :asset | :price | :aprc | :quantity | :tot-val |
;;; |--------+--------+---------+-----------+----------|
;;; | cash | N/A | N/A | N/A | 178.23 |
;;; | 28636 | 7.125 | 21.8473 | 10 | 218.47 |
;;;
;;; | :date | :tot-value | :daily-ret | :tot-ret | :loan | :leverage | :margin |
;;; |------------+------------+------------+----------+-------+-----------+---------|
;;; | 1980-12-15 | $400.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-16 | $400.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-17 | $398.36 | -0.18% | -0.18% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-18 | $396.70 | -0.18% | -0.36% | $0.00 | 0.00 | 0.00% |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; However, the trade would not be allowed if you do not have sufficient cash.
;; **
;; @@
(init-portfolio "1980-12-15" 100)
(order "28636" 10 :leverage false :remaining true :print true) ;without leverage, remaining value
(next-date)
(next-date)
(next-date)
(print-order-record)
(print-portfolio)
(print-portfolio-record -1)
;; @@
;; ->
;;; Order request 1980-12-16 | 28636 | 10 fails.
;;; Failure reason: You do not have enough money to buy or have enough stock to sell. Try to solve by enabling leverage.
;;;
;;; | :asset | :price | :aprc | :quantity | :tot-val |
;;; |--------+--------+-------+-----------+----------|
;;; | cash | N/A | N/A | N/A | 100.00 |
;;;
;;; | :date | :tot-value | :daily-ret | :tot-ret | :loan | :leverage | :margin |
;;; |------------+------------+------------+----------+-------+-----------+---------|
;;; | 1980-12-15 | $100.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### Trade with leverage
;; **
;; @@
(init-portfolio "1980-12-15" 20)
(order "28636" 1 :print true) ;with leverage, exact value trade
(next-date)
(next-date)
(next-date)
(print-order-record)
(print-portfolio)
(print-portfolio-record -1)
;; @@
;; ->
;;; Order: 1980-12-16 | 28636 | 1.000000.
;;;
;;; | :date | :permno | :price | :aprc | :quantity |
;;; |------------+---------+--------+-------+-----------|
;;; | 1980-12-16 | 28636 | 7.375 | 22.18 | 1 |
;;;
;;; | :asset | :price | :aprc | :quantity | :tot-val |
;;; |--------+--------+---------+-----------+----------|
;;; | cash | N/A | N/A | N/A | -2.18 |
;;; | 28636 | 7.125 | 21.8473 | 1 | 21.85 |
;;;
;;; | :date | :tot-value | :daily-ret | :tot-ret | :loan | :leverage | :margin |
;;; |------------+------------+------------+----------+-------+-----------+---------|
;;; | 1980-12-15 | $20.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-16 | $20.00 | 0.00% | 0.00% | $2.18 | 0.11 | 90.18% |
;;; | 1980-12-17 | $19.84 | -0.04% | -0.04% | $2.18 | 0.11 | 90.11% |
;;; | 1980-12-18 | $19.67 | -0.04% | -0.08% | $2.18 | 0.11 | 90.04% |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### Initial margin
;; **
;; **
;;; The purchase would not be allowed if the ratio of cash to total value of assets bought on margin goes below the initial margin.
;; **
;; @@
; check variable
(println INITIAL-MARGIN)
;; @@
;; ->
;;; 0.5
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; **Example**: order failed, since cash is insufficient.
;; **
;; @@
(init-portfolio "1980-12-15" 100)
(order "28636" 10 :remaining true :print true) ;with leverage, remaining value
(next-date)
(next-date)
(next-date)
(print-order-record)
(print-portfolio)
(print-portfolio-record -1)
;; @@
;; ->
;;; Order request 1980-12-16 | 28636 | 10 fails due to initial margin requirement.
;;;
;;; | :asset | :price | :aprc | :quantity | :tot-val |
;;; |--------+--------+-------+-----------+----------|
;;; | cash | N/A | N/A | N/A | 100.00 |
;;;
;;; | :date | :tot-value | :daily-ret | :tot-ret | :loan | :leverage | :margin |
;;; |------------+------------+------------+----------+-------+-----------+---------|
;;; | 1980-12-15 | $100.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; Alternatively, you could manually update the initial margin to enable such a case. You could set it to `nil` in order to disable the initial margin requirement.
;; **
;; @@
(update-initial-margin 0.1)
(println INITIAL-MARGIN)
;; @@
;; ->
;;; 0.1
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
(init-portfolio "1980-12-15" 100)
(order "28636" 10 :remaining true :print true) ;with leverage, remaining value
(next-date)
(next-date)
(next-date)
(print-order-record)
(print-portfolio)
(print-portfolio-record -1)
;; @@
;; ->
;;; Order: 1980-12-16 | 28636 | 10.000000.
;;;
;;; | :date | :permno | :price | :aprc | :quantity |
;;; |------------+---------+--------+-------+-----------|
;;; | 1980-12-16 | 28636 | 7.375 | 22.18 | 10 |
;;;
;;; | :asset | :price | :aprc | :quantity | :tot-val |
;;; |--------+--------+---------+-----------+----------|
;;; | cash | N/A | N/A | N/A | -121.77 |
;;; | 28636 | 7.125 | 21.8473 | 10 | 218.47 |
;;;
;;; | :date | :tot-value | :daily-ret | :tot-ret | :loan | :leverage | :margin |
;;; |------------+------------+------------+----------+---------+-----------+---------|
;;; | 1980-12-15 | $100.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-16 | $100.00 | 0.00% | 0.00% | $121.77 | 1.22 | 45.09% |
;;; | 1980-12-17 | $98.36 | -0.89% | -0.89% | $121.77 | 1.24 | 44.68% |
;;; | 1980-12-18 | $96.70 | -0.93% | -1.82% | $121.77 | 1.26 | 44.26% |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
;;; ### Maintenance margin
;; **
;; **
;;; All positions will be automatically closed if the portfolio margin goes below the maintenace margin.
;; **
;; @@
(init-portfolio "1980-12-15" 100)
(order "28636" -10 :remaining true :print true) ;with leverage, remaining value
(order "25785" 20 :remaining true :print true)
(next-date)
(next-date)
(next-date)
(print-order-record)
(print-portfolio)
(print-portfolio-record -1)
;; @@
;; ->
;;; Order request 1980-12-16 | 25785 | 20 fails due to initial margin requirement.
;;; Order: 1980-12-16 | 28636 | -10.000000.
;;;
;;; | :date | :permno | :price | :aprc | :quantity |
;;; |------------+---------+--------+-------+-----------|
;;; | 1980-12-16 | 28636 | 7.375 | 22.18 | -10 |
;;;
;;; | :asset | :price | :aprc | :quantity | :tot-val |
;;; |--------+--------+---------+-----------+----------|
;;; | cash | N/A | N/A | N/A | 321.77 |
;;; | 28636 | 7.125 | 21.8473 | -10 | -218.47 |
;;;
;;; | :date | :tot-value | :daily-ret | :tot-ret | :loan | :leverage | :margin |
;;; |------------+------------+------------+----------+-------+-----------+---------|
;;; | 1980-12-15 | $100.00 | 0.00% | 0.00% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-16 | $100.00 | -0.00% | -0.00% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-17 | $101.64 | 0.71% | 0.71% | $0.00 | 0.00 | 0.00% |
;;; | 1980-12-18 | $103.30 | 0.70% | 1.41% | $0.00 | 0.00 | 0.00% |
;;;
;; <-
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
;; @@