-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsimulator.rkt
308 lines (279 loc) · 17.7 KB
/
simulator.rkt
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
#lang racket/base
(require gregor
math/statistics
racket/class
racket/list
racket/gui/base
"chart.rkt"
"db-queries.rkt"
"strategy/ascending-triangle.rkt"
"strategy/bear-rally.rkt"
"strategy/bull-pullback.rkt"
"strategy/descending-triangle.rkt"
"strategy/high-base.rkt"
"strategy/low-base.rkt"
"strategy/range-pullback.rkt"
"strategy/range-rally.rkt"
"structs.rkt")
(provide show-simulator)
(struct trade-with-exit (symbol date price amount exit-date exit-price test)
#:transparent)
(struct test-with-symbol (symbol date entry stop target)
#:transparent)
(define (trade-with-exit-history symbol trade-history)
(if (or (empty? trade-history)
(= 1 (length trade-history)))
(list)
(let ([first (first trade-history)]
[second (second trade-history)])
(append (list (trade-with-exit symbol
(trade-date first)
(trade-price first)
(trade-amount first)
(trade-date second)
(trade-price second)
(trade-test first)))
(trade-with-exit-history symbol (list-tail trade-history 2))))))
(define (trade-with-exit-history->ratios tweh)
(map (λ (e) (real->decimal-string (/ (- (test-target (trade-with-exit-test e)) (trade-with-exit-price e))
(- (test-entry (trade-with-exit-test e)) (test-stop (trade-with-exit-test e))))))
tweh))
(define (trade-with-exit-history->risks tweh)
(map (λ (e) (real->decimal-string (* (- (test-entry (trade-with-exit-test e)) (test-stop (trade-with-exit-test e)))
(trade-with-exit-amount e))))
tweh))
(define (trade-with-exit-history->rewards tweh)
(map (λ (e) (real->decimal-string (* (- (test-target (trade-with-exit-test e)) (test-entry (trade-with-exit-test e)))
(trade-with-exit-amount e))))
tweh))
(define (trade-with-exit-history->pcts tweh)
(map (λ (e) (* (/ (- (trade-with-exit-exit-price e) (trade-with-exit-price e))
(trade-with-exit-price e))
(trade-with-exit-amount e) 100))
tweh))
(define (trade-with-exit-history->pcts-str tweh)
(map (λ (e) (real->decimal-string e))
(trade-with-exit-history->pcts tweh)))
(define simulator-frame (new frame% [label "Simulator"] [width 1000] [height 1000]))
(define simulator-input-pane (new horizontal-pane%
[parent simulator-frame]
[stretchable-height #f]))
(define start-date-field
(new text-field%
[parent simulator-input-pane]
[label "Start Date"]
[init-value "2000-01-01"]))
(define end-date-field
(new text-field%
[parent simulator-input-pane]
[label "End Date"]
[init-value "2018-01-01"]))
(define random-above-25-str "From 2000 above $25")
(define random-sp-500-str "From current S&P 500")
(define symbol-source-choice
(new choice%
[parent simulator-input-pane]
[label "Symbol Source"]
[choices (list random-above-25-str random-sp-500-str)]))
(define strategy-hash
(hash "Bull Pullback" bull-pullback-execution
"Bear Rally" bear-rally-execution
"High Base" high-base-execution
"Low Base" low-base-execution
"Ascending Triangle" ascending-triangle-execution
"Descending Triangle" descending-triangle-execution
"Range Pullback" range-pullback-execution
"Range Rally" range-rally-execution))
(define strategy-choice
(new choice%
[parent simulator-input-pane]
[label "Strategy"]
[choices (sort (hash-keys strategy-hash) string<?)]))
(define simulator-get-1-button
(new button%
[parent simulator-input-pane]
[label "Get 1"]
[callback (λ (c e)
(send c enable #f)
(let* ([symbol (first (cond
[(equal? (send symbol-source-choice get-string-selection)
random-above-25-str)
(get-random-symbols-over-price 25.0 1)]
[(equal? (send symbol-source-choice get-string-selection)
random-sp-500-str)
(get-random-sp-500-symbols 1)]))]
[exec ((hash-ref strategy-hash (send strategy-choice get-string-selection))
(get-date-ohlc symbol (send start-date-field get-value)
(send end-date-field get-value)))]
[tweh (trade-with-exit-history symbol (history-trade exec))]
[winners (filter (λ (t) (<= 0 (* (- (trade-with-exit-exit-price t)
(trade-with-exit-price t))
(trade-with-exit-amount t)))) tweh)]
[win-pct (if (= 0 (length tweh)) 0
(* (/ (length winners) (length tweh)) 100))]
[win-pct-avg (if (= 0 (length winners)) 0
(mean (trade-with-exit-history->pcts winners)))]
[losers (remove* winners tweh)]
[lose-pct (if (= 0 (length tweh)) 0
(* (/ (length losers) (length tweh)) 100))]
[lose-pct-avg (if (= 0 (length losers)) 0
(mean (trade-with-exit-history->pcts losers)))]
[reward-ratio (if (= 0 lose-pct-avg) 0 (/ win-pct-avg (abs lose-pct-avg)))]
[return (+ (* win-pct win-pct-avg) (* lose-pct lose-pct-avg))])
; (display-low-base-execution symbol lbe)
; (displayln tweh)
(send simulator-trades-box set
(map (λ (e) (trade-with-exit-symbol e)) tweh)
(map (λ (e) (~t (posix->datetime (trade-with-exit-date e)) "yyyy-MM-dd")) tweh)
(map (λ (e) (real->decimal-string (trade-with-exit-price e))) tweh)
(map (λ (e) (real->decimal-string (test-entry (trade-with-exit-test e)))) tweh)
(map (λ (e) (real->decimal-string (test-stop (trade-with-exit-test e)))) tweh)
(map (λ (e) (real->decimal-string (test-target (trade-with-exit-test e)))) tweh)
(map (λ (e) (~t (posix->datetime (trade-with-exit-exit-date e)) "yyyy-MM-dd")) tweh)
(map (λ (e) (real->decimal-string (trade-with-exit-exit-price e))) tweh)
(trade-with-exit-history->ratios tweh)
(trade-with-exit-history->risks tweh)
(trade-with-exit-history->rewards tweh)
(trade-with-exit-history->pcts-str tweh))
(map (λ (t i) (send simulator-trades-box set-data i
(list symbol (trade-with-exit-date t))))
tweh (range (length tweh)))
(send simulator-trades-box set-label
(string-append "Trades History - Reward Ratio: "
(real->decimal-string reward-ratio)
" Win Pct: " (real->decimal-string win-pct)
" Lose Pct: " (real->decimal-string lose-pct)
" Win Pct Avg: " (real->decimal-string win-pct-avg)
" Lose Pct Avg: " (real->decimal-string lose-pct-avg)
" Return: " (real->decimal-string return)))
(send simulator-test-box set
(map (λ (e) symbol) (history-test exec))
(map (λ (e) (~t (posix->datetime (dv-date e)) "yyyy-MM-dd")) (history-test exec))
(map (λ (e) (real->decimal-string (test-entry (dv-value e)))) (history-test exec))
(map (λ (e) (real->decimal-string (test-stop (dv-value e)))) (history-test exec))
(map (λ (e) (real->decimal-string (test-target (dv-value e)))) (history-test exec)))
(map (λ (t i) (send simulator-test-box set-data i
(list symbol (dv-date t))))
(history-test exec) (range (length (history-test exec)))))
(send c enable #t))]))
(define simulator-get-40-button
(new button%
[parent simulator-input-pane]
[label "Get 40"]
[callback (λ (c e)
(send c enable #f)
(let* ([symbols (cond
[(equal? (send symbol-source-choice get-string-selection)
random-above-25-str)
(get-random-symbols-over-price 25.0 40)]
[(equal? (send symbol-source-choice get-string-selection)
random-sp-500-str)
(get-random-sp-500-symbols 40)])]
[execs (map (λ (s) (list s ((hash-ref strategy-hash (send strategy-choice get-string-selection))
(get-date-ohlc s (send start-date-field get-value)
(send end-date-field get-value))))) symbols)]
[tweh (flatten (map (λ (lbe) (trade-with-exit-history
(first lbe)
(history-trade (second lbe))))
execs))]
[tws (flatten (map (λ (lbe) (map (λ (t) (test-with-symbol (first lbe)
(dv-date t)
(test-entry (dv-value t))
(test-stop (dv-value t))
(test-target (dv-value t))))
(history-test (second lbe))))
execs))]
[winners (filter (λ (t) (<= 0 (* (- (trade-with-exit-exit-price t)
(trade-with-exit-price t))
(trade-with-exit-amount t)))) tweh)]
[win-pct (if (= 0 (length tweh)) 0
(* (/ (length winners) (length tweh)) 100))]
[win-pct-avg (if (= 0 (length winners)) 0
(mean (trade-with-exit-history->pcts winners)))]
[losers (remove* winners tweh)]
[lose-pct (if (= 0 (length tweh)) 0
(* (/ (length losers) (length tweh)) 100))]
[lose-pct-avg (if (= 0 (length losers)) 0
(mean (trade-with-exit-history->pcts losers)))]
[reward-ratio (if (= 0 lose-pct-avg) 0 (/ win-pct-avg (abs lose-pct-avg)))]
[return (+ (* win-pct win-pct-avg) (* lose-pct lose-pct-avg))])
(send simulator-trades-box set
(map (λ (e) (trade-with-exit-symbol e)) tweh)
(map (λ (e) (~t (posix->datetime (trade-with-exit-date e)) "yyyy-MM-dd")) tweh)
(map (λ (e) (real->decimal-string (trade-with-exit-price e))) tweh)
(map (λ (e) (real->decimal-string (test-entry (trade-with-exit-test e)))) tweh)
(map (λ (e) (real->decimal-string (test-stop (trade-with-exit-test e)))) tweh)
(map (λ (e) (real->decimal-string (test-target (trade-with-exit-test e)))) tweh)
(map (λ (e) (~t (posix->datetime (trade-with-exit-exit-date e)) "yyyy-MM-dd")) tweh)
(map (λ (e) (real->decimal-string (trade-with-exit-exit-price e))) tweh)
(trade-with-exit-history->ratios tweh)
(trade-with-exit-history->risks tweh)
(trade-with-exit-history->rewards tweh)
(trade-with-exit-history->pcts-str tweh))
(map (λ (t i) (send simulator-trades-box set-data i
(list (trade-with-exit-symbol t) (trade-with-exit-date t))))
tweh (range (length tweh)))
(send simulator-trades-box set-label
(string-append "Trades History - Reward Ratio: " (real->decimal-string reward-ratio)
" Win Pct: " (real->decimal-string win-pct)
" Lose Pct: " (real->decimal-string lose-pct)
" Win Pct Avg: " (real->decimal-string win-pct-avg)
" Lose Pct Avg: " (real->decimal-string lose-pct-avg)
" Return: " (real->decimal-string return)))
(send simulator-test-box set
(map (λ (t) (test-with-symbol-symbol t)) tws)
(map (λ (t) (~t (posix->datetime (test-with-symbol-date t)) "yyyy-MM-dd")) tws)
(map (λ (t) (real->decimal-string (test-with-symbol-entry t))) tws)
(map (λ (t) (real->decimal-string (test-with-symbol-stop t))) tws)
(map (λ (t) (real->decimal-string (test-with-symbol-target t))) tws))
(map (λ (t i) (send simulator-test-box set-data i
(list (test-with-symbol-symbol t) (test-with-symbol-date t))))
tws (range (length tws))))
(send c enable #t))]))
(define simulator-table-pane (new vertical-pane%
[parent simulator-frame]))
(define (add-months d n)
(~t (+months (posix->datetime d) n) "yyyy-MM-dd"))
(define (subtract-months d n)
(~t (-months (posix->datetime d) n) "yyyy-MM-dd"))
(define simulator-trades-box-columns (list "Symbol" "Date" "Price" "Entry Price" "Stop Price" "Target Price" "Exit Date"
"Exit Price" "Ratio" "Risk" "Reward" "Pct Gain"))
(define simulator-trades-box (new list-box%
[label "Trade History"]
[parent simulator-table-pane]
[callback (λ (b e)
(let ([symbol (first (send b get-data (first (send b get-selections))))]
[date (second (send b get-data (first (send b get-selections))))])
(refresh-chart symbol
(subtract-months date 5)
(add-months date 3))))]
[style (list 'single 'column-headers 'vertical-label)]
[columns simulator-trades-box-columns]
[choices (list "")]))
(define simulator-test-box-columns (list "Symbol" "Date" "Entry Price" "Stop Price" "Target Price"))
(define simulator-test-box (new list-box%
[label "TEST History"]
[parent simulator-table-pane]
[callback (λ (b e)
(let ([symbol (first (send b get-data (first (send b get-selections))))]
[date (second (send b get-data (first (send b get-selections))))])
(refresh-chart symbol
(subtract-months date 5)
(add-months date 3))))]
[style (list 'single 'column-headers 'vertical-label)]
[columns simulator-test-box-columns]
[choices (list "")]))
(define (show-simulator)
(send simulator-frame show #t)
(let ([box-width (send simulator-trades-box get-width)]
[num-cols (length simulator-trades-box-columns)])
(for-each (λ (i) (send simulator-trades-box set-column-width i
100
100
100)) (range num-cols)))
(let ([box-width (send simulator-test-box get-width)]
[num-cols (length simulator-test-box-columns)])
(for-each (λ (i) (send simulator-test-box set-column-width i
250
250
250)) (range num-cols))))