w-trade-backtest 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/.editorconfig +9 -0
  2. package/.eslintignore +3 -0
  3. package/.eslintrc.js +55 -0
  4. package/.jsdoc +25 -0
  5. package/README.md +167 -0
  6. package/SECURITY.md +5 -0
  7. package/babel.config.js +16 -0
  8. package/dist/renderOrdersTable.js +217 -0
  9. package/dist/renderOrdersTimeline.js +159 -0
  10. package/dist/renderProfitPlot.js +198 -0
  11. package/dist/renderSummary.js +194 -0
  12. package/dist/tmp.html +232 -0
  13. package/dist/w-trade-backtest.umd.js +32 -0
  14. package/dist/w-trade-backtest.umd.js.map +1 -0
  15. package/docs/calcOrders.mjs.html +278 -0
  16. package/docs/calcOrdersRatio.mjs.html +146 -0
  17. package/docs/calcOrdersSummary.mjs.html +506 -0
  18. package/docs/calcOrdersSummarySimple.mjs.html +207 -0
  19. package/docs/calcSummary.mjs.html +201 -0
  20. package/docs/closeAndSummaryOrders.mjs.html +265 -0
  21. package/docs/fonts/Montserrat/Montserrat-Bold.eot +0 -0
  22. package/docs/fonts/Montserrat/Montserrat-Bold.ttf +0 -0
  23. package/docs/fonts/Montserrat/Montserrat-Bold.woff +0 -0
  24. package/docs/fonts/Montserrat/Montserrat-Bold.woff2 +0 -0
  25. package/docs/fonts/Montserrat/Montserrat-Regular.eot +0 -0
  26. package/docs/fonts/Montserrat/Montserrat-Regular.ttf +0 -0
  27. package/docs/fonts/Montserrat/Montserrat-Regular.woff +0 -0
  28. package/docs/fonts/Montserrat/Montserrat-Regular.woff2 +0 -0
  29. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.eot +0 -0
  30. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.svg +978 -0
  31. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.ttf +0 -0
  32. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff +0 -0
  33. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff2 +0 -0
  34. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.eot +0 -0
  35. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.svg +1049 -0
  36. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.ttf +0 -0
  37. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff +0 -0
  38. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff2 +0 -0
  39. package/docs/genReport.mjs.html +196 -0
  40. package/docs/genReportCore.mjs.html +128 -0
  41. package/docs/global.html +4231 -0
  42. package/docs/index.html +84 -0
  43. package/docs/runStrategies.mjs.html +244 -0
  44. package/docs/runStrategy.mjs.html +455 -0
  45. package/docs/scripts/collapse.js +39 -0
  46. package/docs/scripts/commonNav.js +28 -0
  47. package/docs/scripts/linenumber.js +25 -0
  48. package/docs/scripts/nav.js +12 -0
  49. package/docs/scripts/polyfill.js +4 -0
  50. package/docs/scripts/prettify/Apache-License-2.0.txt +202 -0
  51. package/docs/scripts/prettify/lang-css.js +2 -0
  52. package/docs/scripts/prettify/prettify.js +28 -0
  53. package/docs/scripts/search.js +99 -0
  54. package/docs/styles/jsdoc.css +776 -0
  55. package/docs/styles/prettify.css +80 -0
  56. package/g.mjs +146 -0
  57. package/package.json +30 -0
  58. package/script.txt +19 -0
  59. package/src/WTradeBacktest.mjs +27 -0
  60. package/src/calcOrders.mjs +206 -0
  61. package/src/calcOrdersRatio.mjs +74 -0
  62. package/src/calcOrdersSummary.mjs +434 -0
  63. package/src/calcOrdersSummarySimple.mjs +135 -0
  64. package/src/calcSummary.mjs +129 -0
  65. package/src/closeAndSummaryOrders.mjs +193 -0
  66. package/src/genReport.mjs +124 -0
  67. package/src/genReportCore.mjs +56 -0
  68. package/src/ott.mjs +28 -0
  69. package/src/readJson.mjs +26 -0
  70. package/src/renderOrdersTable.js +217 -0
  71. package/src/renderOrdersTimeline.js +159 -0
  72. package/src/renderProfitPlot.js +198 -0
  73. package/src/renderSummary.js +194 -0
  74. package/src/runStrategies.mjs +172 -0
  75. package/src/runStrategy.mjs +383 -0
  76. package/src/tmp.html +232 -0
  77. package/src/writeJson.mjs +31 -0
  78. package/src/writeTxt.mjs +19 -0
  79. package/test/unit-WTradeBacktest.test.mjs +541 -0
  80. package/toolg/addVersion.mjs +4 -0
  81. package/toolg/cleanFolder.mjs +5 -0
  82. package/toolg/gDistRollup.mjs +51 -0
  83. package/toolg/gDocsExams.mjs +48 -0
  84. package/toolg/modifyReadme.mjs +4 -0
@@ -0,0 +1,4231 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+
5
+ <meta charset="utf-8">
6
+ <title>Global - Documentation</title>
7
+
8
+
9
+ <script src="scripts/prettify/prettify.js"></script>
10
+ <script src="scripts/prettify/lang-css.js"></script>
11
+ <!--[if lt IE 9]>
12
+ <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
13
+ <![endif]-->
14
+ <link type="text/css" rel="stylesheet" href="styles/prettify.css">
15
+ <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
16
+ <script src="scripts/nav.js" defer></script>
17
+
18
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
19
+ </head>
20
+ <body>
21
+
22
+ <input type="checkbox" id="nav-trigger" class="nav-trigger" />
23
+ <label for="nav-trigger" class="navicon-button x">
24
+ <div class="navicon"></div>
25
+ </label>
26
+
27
+ <label for="nav-trigger" class="overlay"></label>
28
+
29
+ <nav >
30
+
31
+
32
+ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#calcOrders">calcOrders</a></li><li><a href="global.html#calcOrdersRatio">calcOrdersRatio</a></li><li><a href="global.html#calcOrdersSummary">calcOrdersSummary</a></li><li><a href="global.html#calcOrdersSummarySimple">calcOrdersSummarySimple</a></li><li><a href="global.html#calcSummary">calcSummary</a></li><li><a href="global.html#closeAndSummaryOrders">closeAndSummaryOrders</a></li><li><a href="global.html#genReport">genReport</a></li><li><a href="global.html#genReportCore">genReportCore</a></li><li><a href="global.html#runStrategies">runStrategies</a></li><li><a href="global.html#runStrategy">runStrategy</a></li></ul>
33
+
34
+ </nav>
35
+
36
+ <div id="main">
37
+
38
+ <h1 class="page-title">Global</h1>
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+ <section>
47
+
48
+ <header>
49
+
50
+ <h2>
51
+
52
+
53
+
54
+ </h2>
55
+
56
+
57
+ </header>
58
+
59
+ <article>
60
+
61
+ <div class="container-overview">
62
+
63
+
64
+
65
+ <dl class="details">
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
+ </dl>
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+ </div>
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+ <h3 class="subsection-title">Methods</h3>
127
+
128
+
129
+
130
+
131
+
132
+
133
+ <h4 class="name" id="calcOrders"><span class="type-signature type-signature-async">(async) </span>calcOrders<span class="signature">(arrOhlc, orders, opt<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
134
+
135
+
136
+
137
+
138
+
139
+
140
+ <dl class="details">
141
+
142
+ <dt class="tag-description">Description:</dt>
143
+ <dd class="tag-description"><ul class="dummy"><li><p>結算訂單,依K線序列判斷各下單之止盈止損觸發並計算盈虧</p>
144
+ <p>各單僅檢查time大於timeStart之K棒,long以Low&lt;=priceStopLoss先判止損、High&gt;=priceTakeProfit判止盈,short以High&gt;=priceStopLoss先判止損、Low&lt;=priceTakeProfit判止盈
145
+ long盈虧為uTrade*(priceEnd/priceStart)-uTrade-2<em>uFee,short盈虧為(priceStart-priceEnd)</em>(uTrade/priceStart)-2*uFee
146
+ 依timeStart排序逐單累計uCumuProfitOrLoss與uEquity(自uIni起算),modeResult為盈虧&lt;0時'loss'否則'profit',未觸發止盈止損者維持原單(未平倉)</p>
147
+ <p>Unit Test: <a href="https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs">Github</a></p></li></ul></dd>
148
+
149
+
150
+
151
+ <dt class="tag-source">Source:</dt>
152
+ <dd class="tag-source"><ul class="dummy"><li>
153
+ <a href="calcOrders.mjs.html">calcOrders.mjs</a>, <a href="calcOrders.mjs.html#line69">line 69</a>
154
+ </li></ul></dd>
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
+ </dl>
187
+
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+ <h5 class="h5-examples">Example</h5>
199
+
200
+ <pre class="prettyprint"><code>let arrOhlc = [
201
+ { time: '2020-01-01T00:00:00', Open: 100, High: 101, Low: 99, Close: 100 },
202
+ { time: '2020-01-01T04:00:00', Open: 100, High: 106, Low: 100, Close: 105 },
203
+ { time: '2020-01-01T08:00:00', Open: 105, High: 107, Low: 102, Close: 103 },
204
+ { time: '2020-01-01T12:00:00', Open: 103, High: 104, Low: 94, Close: 95 },
205
+ { time: '2020-01-01T16:00:00', Open: 95, High: 98, Low: 92, Close: 93 },
206
+ { time: '2020-01-01T20:00:00', Open: 93, High: 99, Low: 95, Close: 97 },
207
+ ]
208
+
209
+ let orders = [{
210
+ mode: 'long',
211
+ timeStart: '2020-01-01T00:00:00',
212
+ priceStart: 100,
213
+ uTrade: 100,
214
+ priceTakeProfit: 105,
215
+ priceStopLoss: 97,
216
+ timeEnd: '',
217
+ priceEnd: '',
218
+ modeResult: '',
219
+ uFee: 0.05,
220
+ }]
221
+
222
+ calcOrders(arrOhlc, orders, { uIni: 1000 })
223
+ .then((rs) => {
224
+ console.log(rs[0])
225
+ // => {
226
+ // mode: 'long',
227
+ // timeStart: '2020-01-01T00:00:00',
228
+ // priceStart: 100,
229
+ // uTrade: 100,
230
+ // priceTakeProfit: 105,
231
+ // priceStopLoss: 97,
232
+ // timeEnd: '2020-01-01T04:00:00',
233
+ // priceEnd: 105,
234
+ // modeResult: 'profit',
235
+ // uFee: 0.05,
236
+ // uProfitOrLoss: 4.9,
237
+ // rProfitOrLoss: 0.049,
238
+ // uCumuProfitOrLoss: 4.9,
239
+ // rCumuProfitOrLoss: '0.49%',
240
+ // uEquity: 1004.9
241
+ // }
242
+ })</code></pre>
243
+
244
+
245
+
246
+
247
+ <h5 class="h5-parameters">Parameters:</h5>
248
+
249
+
250
+ <table class="params">
251
+ <thead>
252
+ <tr>
253
+
254
+ <th>Name</th>
255
+
256
+
257
+ <th>Type</th>
258
+
259
+
260
+ <th>Attributes</th>
261
+
262
+
263
+
264
+ <th>Default</th>
265
+
266
+
267
+ <th class="last">Description</th>
268
+ </tr>
269
+ </thead>
270
+
271
+ <tbody>
272
+
273
+
274
+ <tr>
275
+
276
+ <td class="name"><code>arrOhlc</code></td>
277
+
278
+
279
+ <td class="type">
280
+
281
+
282
+ <span class="param-type">Array</span>
283
+
284
+
285
+
286
+
287
+ </td>
288
+
289
+
290
+ <td class="attributes">
291
+
292
+
293
+
294
+
295
+
296
+ </td>
297
+
298
+
299
+
300
+ <td class="default">
301
+
302
+ </td>
303
+
304
+
305
+ <td class="description last"><p>輸入K線陣列,各元素需含time、High、Low欄位</p></td>
306
+ </tr>
307
+
308
+
309
+
310
+ <tr>
311
+
312
+ <td class="name"><code>orders</code></td>
313
+
314
+
315
+ <td class="type">
316
+
317
+
318
+ <span class="param-type">Array</span>
319
+
320
+
321
+
322
+
323
+ </td>
324
+
325
+
326
+ <td class="attributes">
327
+
328
+
329
+
330
+
331
+
332
+ </td>
333
+
334
+
335
+
336
+ <td class="default">
337
+
338
+ </td>
339
+
340
+
341
+ <td class="description last"><p>輸入下單陣列,各元素需含mode('long'或'short')、timeStart、priceStart、uTrade、priceTakeProfit、priceStopLoss、uFee欄位</p></td>
342
+ </tr>
343
+
344
+
345
+
346
+ <tr>
347
+
348
+ <td class="name"><code>opt</code></td>
349
+
350
+
351
+ <td class="type">
352
+
353
+
354
+ <span class="param-type">Object</span>
355
+
356
+
357
+
358
+
359
+ </td>
360
+
361
+
362
+ <td class="attributes">
363
+
364
+ &lt;optional><br>
365
+
366
+
367
+
368
+
369
+
370
+ </td>
371
+
372
+
373
+
374
+ <td class="default">
375
+
376
+ <code>{}</code>
377
+
378
+ </td>
379
+
380
+
381
+ <td class="description last"><p>輸入設定物件,預設{}</p>
382
+ <h6>Properties</h6>
383
+
384
+
385
+ <table class="params">
386
+ <thead>
387
+ <tr>
388
+
389
+ <th>Name</th>
390
+
391
+
392
+ <th>Type</th>
393
+
394
+
395
+ <th>Attributes</th>
396
+
397
+
398
+
399
+ <th>Default</th>
400
+
401
+
402
+ <th class="last">Description</th>
403
+ </tr>
404
+ </thead>
405
+
406
+ <tbody>
407
+
408
+
409
+ <tr>
410
+
411
+ <td class="name"><code>uIni</code></td>
412
+
413
+
414
+ <td class="type">
415
+
416
+
417
+ <span class="param-type">Number</span>
418
+
419
+
420
+
421
+
422
+ </td>
423
+
424
+
425
+ <td class="attributes">
426
+
427
+ &lt;optional><br>
428
+
429
+
430
+
431
+
432
+
433
+ </td>
434
+
435
+
436
+
437
+ <td class="default">
438
+
439
+ <code>1000</code>
440
+
441
+ </td>
442
+
443
+
444
+ <td class="description last"><p>輸入初始資金數字,預設1000</p></td>
445
+ </tr>
446
+
447
+
448
+ </tbody>
449
+ </table>
450
+
451
+ </td>
452
+ </tr>
453
+
454
+
455
+ </tbody>
456
+ </table>
457
+
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+ <h5 class="h5-returns">Returns:</h5>
474
+
475
+
476
+ <div class="param-desc">
477
+ <p>回傳Promise,resolve為結算後訂單陣列</p>
478
+ </div>
479
+
480
+
481
+
482
+ <dl class="param-type">
483
+ <dt>
484
+ Type
485
+ </dt>
486
+ <dd>
487
+
488
+ <span class="param-type">Promise</span>
489
+
490
+
491
+
492
+ </dd>
493
+ </dl>
494
+
495
+
496
+
497
+
498
+
499
+
500
+
501
+
502
+
503
+
504
+ <h4 class="name" id="calcOrdersRatio"><span class="type-signature"></span>calcOrdersRatio<span class="signature">(ott, orders)</span><span class="type-signature"> &rarr; {Array}</span></h4>
505
+
506
+
507
+
508
+
509
+
510
+
511
+ <dl class="details">
512
+
513
+ <dt class="tag-description">Description:</dt>
514
+ <dd class="tag-description"><ul class="dummy"><li><p>計算各已平倉訂單之持倉天數與等效日盈虧比例</p>
515
+ <p>dayHold以timeStart與timeEnd之日期差+1計算,rProfitOrLossDay為(1+rProfitOrLoss)^(1/dayHold)-1
516
+ modeResult非有效字串(未平倉)之訂單不計算</p>
517
+ <p>Unit Test: <a href="https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs">Github</a></p></li></ul></dd>
518
+
519
+
520
+
521
+ <dt class="tag-source">Source:</dt>
522
+ <dd class="tag-source"><ul class="dummy"><li>
523
+ <a href="calcOrdersRatio.mjs.html">calcOrdersRatio.mjs</a>, <a href="calcOrdersRatio.mjs.html#line35">line 35</a>
524
+ </li></ul></dd>
525
+
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+
540
+
541
+
542
+
543
+
544
+
545
+
546
+
547
+
548
+
549
+
550
+
551
+
552
+
553
+
554
+
555
+
556
+ </dl>
557
+
558
+
559
+
560
+
561
+
562
+
563
+
564
+
565
+
566
+
567
+
568
+ <h5 class="h5-examples">Example</h5>
569
+
570
+ <pre class="prettyprint"><code>let orders = calcOrdersRatio(ott, [{
571
+ modeResult: 'profit',
572
+ timeStart: '2020-01-01T20:00:00',
573
+ timeEnd: '2020-01-03T04:00:00',
574
+ rProfitOrLoss: 0.331,
575
+ }])
576
+ console.log(orders[0])
577
+ // => {
578
+ // modeResult: 'profit',
579
+ // timeStart: '2020-01-01T20:00:00',
580
+ // timeEnd: '2020-01-03T04:00:00',
581
+ // rProfitOrLoss: 0.331,
582
+ // dayHold: 3,
583
+ // rProfitOrLossDay: 0.10000000000000009
584
+ // }</code></pre>
585
+
586
+
587
+
588
+
589
+ <h5 class="h5-parameters">Parameters:</h5>
590
+
591
+
592
+ <table class="params">
593
+ <thead>
594
+ <tr>
595
+
596
+ <th>Name</th>
597
+
598
+
599
+ <th>Type</th>
600
+
601
+
602
+
603
+
604
+
605
+ <th class="last">Description</th>
606
+ </tr>
607
+ </thead>
608
+
609
+ <tbody>
610
+
611
+
612
+ <tr>
613
+
614
+ <td class="name"><code>ott</code></td>
615
+
616
+
617
+ <td class="type">
618
+
619
+
620
+ <span class="param-type">function</span>
621
+
622
+
623
+
624
+
625
+ </td>
626
+
627
+
628
+
629
+
630
+
631
+ <td class="description last"><p>輸入時區時間函數,傳入時間字串回傳dayjs時間物件(可用src/ott.mjs或自行以dayjs包裝)</p></td>
632
+ </tr>
633
+
634
+
635
+
636
+ <tr>
637
+
638
+ <td class="name"><code>orders</code></td>
639
+
640
+
641
+ <td class="type">
642
+
643
+
644
+ <span class="param-type">Array</span>
645
+
646
+
647
+
648
+
649
+ </td>
650
+
651
+
652
+
653
+
654
+
655
+ <td class="description last"><p>輸入已結算訂單陣列,各元素需含modeResult、timeStart、timeEnd、rProfitOrLoss欄位</p></td>
656
+ </tr>
657
+
658
+
659
+ </tbody>
660
+ </table>
661
+
662
+
663
+
664
+
665
+
666
+
667
+
668
+
669
+
670
+
671
+
672
+
673
+
674
+
675
+
676
+
677
+ <h5 class="h5-returns">Returns:</h5>
678
+
679
+
680
+ <div class="param-desc">
681
+ <p>回傳附加dayHold與rProfitOrLossDay之訂單陣列</p>
682
+ </div>
683
+
684
+
685
+
686
+ <dl class="param-type">
687
+ <dt>
688
+ Type
689
+ </dt>
690
+ <dd>
691
+
692
+ <span class="param-type">Array</span>
693
+
694
+
695
+
696
+ </dd>
697
+ </dl>
698
+
699
+
700
+
701
+
702
+
703
+
704
+
705
+
706
+
707
+
708
+ <h4 class="name" id="calcOrdersSummary"><span class="type-signature"></span>calcOrdersSummary<span class="signature">(ott, uIni, ordersAll, timeOhlcStart, timeOhlcEnd)</span><span class="type-signature"> &rarr; {Object}</span></h4>
709
+
710
+
711
+
712
+
713
+
714
+
715
+ <dl class="details">
716
+
717
+ <dt class="tag-description">Description:</dt>
718
+ <dd class="tag-description"><ul class="dummy"><li><p>統計全部訂單之回測摘要</p>
719
+ <p>統計交易次數、勝率、最大回撤、最大持倉、夏普值、最終權益與各盈虧比例等欄位
720
+ 訂單需先經calcOrders結算(含uEquity)與calcOrdersRatio計算(含dayHold與rProfitOrLossDay,夏普值計算所需)
721
+ uIni非正數或timeOhlcStart、timeOhlcEnd非有效字串時throw</p>
722
+ <p>Unit Test: <a href="https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs">Github</a></p></li></ul></dd>
723
+
724
+
725
+
726
+ <dt class="tag-source">Source:</dt>
727
+ <dd class="tag-source"><ul class="dummy"><li>
728
+ <a href="calcOrdersSummary.mjs.html">calcOrdersSummary.mjs</a>, <a href="calcOrdersSummary.mjs.html#line76">line 76</a>
729
+ </li></ul></dd>
730
+
731
+
732
+
733
+
734
+
735
+
736
+
737
+
738
+
739
+
740
+
741
+
742
+
743
+
744
+
745
+
746
+
747
+
748
+
749
+
750
+
751
+
752
+
753
+
754
+
755
+
756
+
757
+
758
+
759
+
760
+
761
+ </dl>
762
+
763
+
764
+
765
+
766
+
767
+
768
+
769
+
770
+
771
+
772
+
773
+ <h5 class="h5-examples">Example</h5>
774
+
775
+ <pre class="prettyprint"><code>let arrOhlc = [
776
+ { time: '2020-01-01T00:00:00', Open: 100, High: 101, Low: 99, Close: 100 },
777
+ { time: '2020-01-01T04:00:00', Open: 100, High: 106, Low: 100, Close: 105 },
778
+ { time: '2020-01-01T08:00:00', Open: 105, High: 107, Low: 102, Close: 103 },
779
+ { time: '2020-01-01T12:00:00', Open: 103, High: 104, Low: 94, Close: 95 },
780
+ { time: '2020-01-01T16:00:00', Open: 95, High: 98, Low: 92, Close: 93 },
781
+ { time: '2020-01-01T20:00:00', Open: 93, High: 99, Low: 95, Close: 97 },
782
+ ]
783
+
784
+ //ordersSubmit, 4張下單: 前3張分別於後續K棒觸發止盈或止損, 第4張未觸發(未平倉)
785
+ let kpDef = { uTrade: 100, timeEnd: '', priceEnd: '', modeResult: '', uFee: 0.05 }
786
+ let ordersSubmit = [
787
+ { mode: 'long', timeStart: '2020-01-01T00:00:00', priceStart: 100, priceTakeProfit: 105, priceStopLoss: 97, ...kpDef },
788
+ { mode: 'long', timeStart: '2020-01-01T04:00:00', priceStart: 105, priceTakeProfit: 110.25, priceStopLoss: 101.85, ...kpDef },
789
+ { mode: 'short', timeStart: '2020-01-01T08:00:00', priceStart: 103, priceTakeProfit: 97.85, priceStopLoss: 106.09, ...kpDef },
790
+ { mode: 'long', timeStart: '2020-01-01T16:00:00', priceStart: 93, priceTakeProfit: 105, priceStopLoss: 85, ...kpDef },
791
+ ]
792
+
793
+ let orders = await calcOrders(arrOhlc, ordersSubmit, { uIni: 1000 })
794
+ orders = calcOrdersRatio(ott, orders)
795
+ let summary = calcOrdersSummary(ott, 1000, orders, '2020-01-01T00:00:00', '2020-01-01T20:00:00')
796
+ console.log(summary)
797
+ // => {
798
+ // btDays: 0,
799
+ // btYears: '0.0',
800
+ // numTrade: 4,
801
+ // numTradeFin: 3,
802
+ // numTradeUnsettled: 1,
803
+ // uTradeAllMax: 200,
804
+ // rTradeAllMax: '20.00%',
805
+ // numTradeAllMax: 2,
806
+ // uDrawdownMax: 3.1000000000000227,
807
+ // rDrawdownMax: '0.31%',
808
+ // rEquivalentDrawdownMax: '1.51%',
809
+ // rSharpe: 7.675814289051032,
810
+ // rWin: '66.67%',
811
+ // uEquityFinal: 1006.6999999999999,
812
+ // uCumuProfitOrLossFinal: 6.700000000000006,
813
+ // rCumuProfitOrLossFinal: '0.67%',
814
+ // rCumuProfitOrLossFinalNormYear: '0.00%',
815
+ // rEquivalentCumuProfitOrLossFinal: '3.35%',
816
+ // rEquivalentCumuProfitOrLossFinalNormYear: '0.00%'
817
+ // }</code></pre>
818
+
819
+
820
+
821
+
822
+ <h5 class="h5-parameters">Parameters:</h5>
823
+
824
+
825
+ <table class="params">
826
+ <thead>
827
+ <tr>
828
+
829
+ <th>Name</th>
830
+
831
+
832
+ <th>Type</th>
833
+
834
+
835
+
836
+
837
+
838
+ <th class="last">Description</th>
839
+ </tr>
840
+ </thead>
841
+
842
+ <tbody>
843
+
844
+
845
+ <tr>
846
+
847
+ <td class="name"><code>ott</code></td>
848
+
849
+
850
+ <td class="type">
851
+
852
+
853
+ <span class="param-type">function</span>
854
+
855
+
856
+
857
+
858
+ </td>
859
+
860
+
861
+
862
+
863
+
864
+ <td class="description last"><p>輸入時區時間函數,傳入時間字串回傳dayjs時間物件(可用src/ott.mjs或自行以dayjs包裝)</p></td>
865
+ </tr>
866
+
867
+
868
+
869
+ <tr>
870
+
871
+ <td class="name"><code>uIni</code></td>
872
+
873
+
874
+ <td class="type">
875
+
876
+
877
+ <span class="param-type">Number</span>
878
+
879
+
880
+
881
+
882
+ </td>
883
+
884
+
885
+
886
+
887
+
888
+ <td class="description last"><p>輸入初始資金正數</p></td>
889
+ </tr>
890
+
891
+
892
+
893
+ <tr>
894
+
895
+ <td class="name"><code>ordersAll</code></td>
896
+
897
+
898
+ <td class="type">
899
+
900
+
901
+ <span class="param-type">Array</span>
902
+
903
+
904
+
905
+
906
+ </td>
907
+
908
+
909
+
910
+
911
+
912
+ <td class="description last"><p>輸入已結算訂單陣列</p></td>
913
+ </tr>
914
+
915
+
916
+
917
+ <tr>
918
+
919
+ <td class="name"><code>timeOhlcStart</code></td>
920
+
921
+
922
+ <td class="type">
923
+
924
+
925
+ <span class="param-type">String</span>
926
+
927
+
928
+
929
+
930
+ </td>
931
+
932
+
933
+
934
+
935
+
936
+ <td class="description last"><p>輸入回測起始秒時間字串</p></td>
937
+ </tr>
938
+
939
+
940
+
941
+ <tr>
942
+
943
+ <td class="name"><code>timeOhlcEnd</code></td>
944
+
945
+
946
+ <td class="type">
947
+
948
+
949
+ <span class="param-type">String</span>
950
+
951
+
952
+
953
+
954
+ </td>
955
+
956
+
957
+
958
+
959
+
960
+ <td class="description last"><p>輸入回測結束秒時間字串</p></td>
961
+ </tr>
962
+
963
+
964
+ </tbody>
965
+ </table>
966
+
967
+
968
+
969
+
970
+
971
+
972
+
973
+
974
+
975
+
976
+
977
+
978
+
979
+
980
+
981
+
982
+ <h5 class="h5-returns">Returns:</h5>
983
+
984
+
985
+ <div class="param-desc">
986
+ <p>回傳回測摘要物件</p>
987
+ </div>
988
+
989
+
990
+
991
+ <dl class="param-type">
992
+ <dt>
993
+ Type
994
+ </dt>
995
+ <dd>
996
+
997
+ <span class="param-type">Object</span>
998
+
999
+
1000
+
1001
+ </dd>
1002
+ </dl>
1003
+
1004
+
1005
+
1006
+
1007
+
1008
+
1009
+
1010
+
1011
+
1012
+
1013
+ <h4 class="name" id="calcOrdersSummarySimple"><span class="type-signature"></span>calcOrdersSummarySimple<span class="signature">(ott, uIni, ordersAll, timeOhlcStart, timeOhlcEnd, opt<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Object}</span></h4>
1014
+
1015
+
1016
+
1017
+
1018
+
1019
+
1020
+ <dl class="details">
1021
+
1022
+ <dt class="tag-description">Description:</dt>
1023
+ <dd class="tag-description"><ul class="dummy"><li><p>統計全部訂單之精簡版回測摘要</p>
1024
+ <p>為calcOrdersSummary之精簡版,只算選股熱路徑所需欄位,純迴圈實作且不需先跑calcOrdersRatio
1025
+ 對應欄位與完整版bit-exact: numTrade、numTradeFin、rWin、uTradeAllMax、rTradeAllMax、uEquityFinal、btDays、btYears、rEquivalentCumuProfitOrLossFinalNormYear
1026
+ 不算drawdown、夏普值與累計曲線</p>
1027
+ <p>Unit Test: <a href="https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs">Github</a></p></li></ul></dd>
1028
+
1029
+
1030
+
1031
+ <dt class="tag-source">Source:</dt>
1032
+ <dd class="tag-source"><ul class="dummy"><li>
1033
+ <a href="calcOrdersSummarySimple.mjs.html">calcOrdersSummarySimple.mjs</a>, <a href="calcOrdersSummarySimple.mjs.html#line70">line 70</a>
1034
+ </li></ul></dd>
1035
+
1036
+
1037
+
1038
+
1039
+
1040
+
1041
+
1042
+
1043
+
1044
+
1045
+
1046
+
1047
+
1048
+
1049
+
1050
+
1051
+
1052
+
1053
+
1054
+
1055
+
1056
+
1057
+
1058
+
1059
+
1060
+
1061
+
1062
+
1063
+
1064
+
1065
+
1066
+ </dl>
1067
+
1068
+
1069
+
1070
+
1071
+
1072
+
1073
+
1074
+
1075
+
1076
+
1077
+
1078
+ <h5 class="h5-examples">Example</h5>
1079
+
1080
+ <pre class="prettyprint"><code>let arrOhlc = [
1081
+ { time: '2020-01-01T00:00:00', Open: 100, High: 101, Low: 99, Close: 100 },
1082
+ { time: '2020-01-01T04:00:00', Open: 100, High: 106, Low: 100, Close: 105 },
1083
+ { time: '2020-01-01T08:00:00', Open: 105, High: 107, Low: 102, Close: 103 },
1084
+ { time: '2020-01-01T12:00:00', Open: 103, High: 104, Low: 94, Close: 95 },
1085
+ { time: '2020-01-01T16:00:00', Open: 95, High: 98, Low: 92, Close: 93 },
1086
+ { time: '2020-01-01T20:00:00', Open: 93, High: 99, Low: 95, Close: 97 },
1087
+ ]
1088
+
1089
+ //ordersSubmit, 4張下單: 前3張分別於後續K棒觸發止盈或止損, 第4張未觸發(未平倉)
1090
+ let kpDef = { uTrade: 100, timeEnd: '', priceEnd: '', modeResult: '', uFee: 0.05 }
1091
+ let ordersSubmit = [
1092
+ { mode: 'long', timeStart: '2020-01-01T00:00:00', priceStart: 100, priceTakeProfit: 105, priceStopLoss: 97, ...kpDef },
1093
+ { mode: 'long', timeStart: '2020-01-01T04:00:00', priceStart: 105, priceTakeProfit: 110.25, priceStopLoss: 101.85, ...kpDef },
1094
+ { mode: 'short', timeStart: '2020-01-01T08:00:00', priceStart: 103, priceTakeProfit: 97.85, priceStopLoss: 106.09, ...kpDef },
1095
+ { mode: 'long', timeStart: '2020-01-01T16:00:00', priceStart: 93, priceTakeProfit: 105, priceStopLoss: 85, ...kpDef },
1096
+ ]
1097
+
1098
+ let orders = await calcOrders(arrOhlc, ordersSubmit, { uIni: 1000 })
1099
+ let summary = calcOrdersSummarySimple(ott, 1000, orders, '2020-01-01T00:00:00', '2020-01-01T20:00:00')
1100
+ console.log(summary)
1101
+ // => {
1102
+ // numTrade: 4,
1103
+ // numTradeFin: 3,
1104
+ // rWin: '66.67%',
1105
+ // uTradeAllMax: 200,
1106
+ // rTradeAllMax: '20.00%',
1107
+ // uEquityFinal: 1006.6999999999999,
1108
+ // btDays: 0,
1109
+ // btYears: '0.0',
1110
+ // rEquivalentCumuProfitOrLossFinalNormYear: '0.00%',
1111
+ // timeOhlcStart: '2020-01-01T00:00:00',
1112
+ // timeOhlcEnd: '2020-01-01T20:00:00'
1113
+ // }</code></pre>
1114
+
1115
+
1116
+
1117
+
1118
+ <h5 class="h5-parameters">Parameters:</h5>
1119
+
1120
+
1121
+ <table class="params">
1122
+ <thead>
1123
+ <tr>
1124
+
1125
+ <th>Name</th>
1126
+
1127
+
1128
+ <th>Type</th>
1129
+
1130
+
1131
+ <th>Attributes</th>
1132
+
1133
+
1134
+
1135
+ <th>Default</th>
1136
+
1137
+
1138
+ <th class="last">Description</th>
1139
+ </tr>
1140
+ </thead>
1141
+
1142
+ <tbody>
1143
+
1144
+
1145
+ <tr>
1146
+
1147
+ <td class="name"><code>ott</code></td>
1148
+
1149
+
1150
+ <td class="type">
1151
+
1152
+
1153
+ <span class="param-type">function</span>
1154
+
1155
+
1156
+
1157
+
1158
+ </td>
1159
+
1160
+
1161
+ <td class="attributes">
1162
+
1163
+
1164
+
1165
+
1166
+
1167
+ </td>
1168
+
1169
+
1170
+
1171
+ <td class="default">
1172
+
1173
+ </td>
1174
+
1175
+
1176
+ <td class="description last"><p>輸入時區時間函數,傳入時間字串回傳dayjs時間物件(可用src/ott.mjs或自行以dayjs包裝)</p></td>
1177
+ </tr>
1178
+
1179
+
1180
+
1181
+ <tr>
1182
+
1183
+ <td class="name"><code>uIni</code></td>
1184
+
1185
+
1186
+ <td class="type">
1187
+
1188
+
1189
+ <span class="param-type">Number</span>
1190
+
1191
+
1192
+
1193
+
1194
+ </td>
1195
+
1196
+
1197
+ <td class="attributes">
1198
+
1199
+
1200
+
1201
+
1202
+
1203
+ </td>
1204
+
1205
+
1206
+
1207
+ <td class="default">
1208
+
1209
+ </td>
1210
+
1211
+
1212
+ <td class="description last"><p>輸入初始資金正數</p></td>
1213
+ </tr>
1214
+
1215
+
1216
+
1217
+ <tr>
1218
+
1219
+ <td class="name"><code>ordersAll</code></td>
1220
+
1221
+
1222
+ <td class="type">
1223
+
1224
+
1225
+ <span class="param-type">Array</span>
1226
+
1227
+
1228
+
1229
+
1230
+ </td>
1231
+
1232
+
1233
+ <td class="attributes">
1234
+
1235
+
1236
+
1237
+
1238
+
1239
+ </td>
1240
+
1241
+
1242
+
1243
+ <td class="default">
1244
+
1245
+ </td>
1246
+
1247
+
1248
+ <td class="description last"><p>輸入已結算訂單陣列(經calcOrders設定timeStart、timeEnd、uTrade、uEquity、modeResult)</p></td>
1249
+ </tr>
1250
+
1251
+
1252
+
1253
+ <tr>
1254
+
1255
+ <td class="name"><code>timeOhlcStart</code></td>
1256
+
1257
+
1258
+ <td class="type">
1259
+
1260
+
1261
+ <span class="param-type">String</span>
1262
+
1263
+
1264
+
1265
+
1266
+ </td>
1267
+
1268
+
1269
+ <td class="attributes">
1270
+
1271
+
1272
+
1273
+
1274
+
1275
+ </td>
1276
+
1277
+
1278
+
1279
+ <td class="default">
1280
+
1281
+ </td>
1282
+
1283
+
1284
+ <td class="description last"><p>輸入回測起始秒時間字串</p></td>
1285
+ </tr>
1286
+
1287
+
1288
+
1289
+ <tr>
1290
+
1291
+ <td class="name"><code>timeOhlcEnd</code></td>
1292
+
1293
+
1294
+ <td class="type">
1295
+
1296
+
1297
+ <span class="param-type">String</span>
1298
+
1299
+
1300
+
1301
+
1302
+ </td>
1303
+
1304
+
1305
+ <td class="attributes">
1306
+
1307
+
1308
+
1309
+
1310
+
1311
+ </td>
1312
+
1313
+
1314
+
1315
+ <td class="default">
1316
+
1317
+ </td>
1318
+
1319
+
1320
+ <td class="description last"><p>輸入回測結束秒時間字串</p></td>
1321
+ </tr>
1322
+
1323
+
1324
+
1325
+ <tr>
1326
+
1327
+ <td class="name"><code>opt</code></td>
1328
+
1329
+
1330
+ <td class="type">
1331
+
1332
+
1333
+ <span class="param-type">Object</span>
1334
+
1335
+
1336
+
1337
+
1338
+ </td>
1339
+
1340
+
1341
+ <td class="attributes">
1342
+
1343
+ &lt;optional><br>
1344
+
1345
+
1346
+
1347
+
1348
+
1349
+ </td>
1350
+
1351
+
1352
+
1353
+ <td class="default">
1354
+
1355
+ <code>{}</code>
1356
+
1357
+ </td>
1358
+
1359
+
1360
+ <td class="description last"><p>輸入設定物件,預設{}</p>
1361
+ <h6>Properties</h6>
1362
+
1363
+
1364
+ <table class="params">
1365
+ <thead>
1366
+ <tr>
1367
+
1368
+ <th>Name</th>
1369
+
1370
+
1371
+ <th>Type</th>
1372
+
1373
+
1374
+ <th>Attributes</th>
1375
+
1376
+
1377
+
1378
+ <th>Default</th>
1379
+
1380
+
1381
+ <th class="last">Description</th>
1382
+ </tr>
1383
+ </thead>
1384
+
1385
+ <tbody>
1386
+
1387
+
1388
+ <tr>
1389
+
1390
+ <td class="name"><code>skipSort</code></td>
1391
+
1392
+
1393
+ <td class="type">
1394
+
1395
+
1396
+ <span class="param-type">Boolean</span>
1397
+
1398
+
1399
+
1400
+
1401
+ </td>
1402
+
1403
+
1404
+ <td class="attributes">
1405
+
1406
+ &lt;optional><br>
1407
+
1408
+
1409
+
1410
+
1411
+
1412
+ </td>
1413
+
1414
+
1415
+
1416
+ <td class="default">
1417
+
1418
+ <code>false</code>
1419
+
1420
+ </td>
1421
+
1422
+
1423
+ <td class="description last"><p>輸入呼叫端保證ordersAll已依timeStart升序時可跳過排序布林值,預設false</p></td>
1424
+ </tr>
1425
+
1426
+
1427
+ </tbody>
1428
+ </table>
1429
+
1430
+ </td>
1431
+ </tr>
1432
+
1433
+
1434
+ </tbody>
1435
+ </table>
1436
+
1437
+
1438
+
1439
+
1440
+
1441
+
1442
+
1443
+
1444
+
1445
+
1446
+
1447
+
1448
+
1449
+
1450
+
1451
+
1452
+ <h5 class="h5-returns">Returns:</h5>
1453
+
1454
+
1455
+ <div class="param-desc">
1456
+ <p>回傳精簡版回測摘要物件</p>
1457
+ </div>
1458
+
1459
+
1460
+
1461
+ <dl class="param-type">
1462
+ <dt>
1463
+ Type
1464
+ </dt>
1465
+ <dd>
1466
+
1467
+ <span class="param-type">Object</span>
1468
+
1469
+
1470
+
1471
+ </dd>
1472
+ </dl>
1473
+
1474
+
1475
+
1476
+
1477
+
1478
+
1479
+
1480
+
1481
+
1482
+
1483
+ <h4 class="name" id="calcSummary"><span class="type-signature type-signature-async">(async) </span>calcSummary<span class="signature">(ott, uIni, orders, timeOhlcStart, timeOhlcEnd, opt<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
1484
+
1485
+
1486
+
1487
+
1488
+
1489
+
1490
+ <dl class="details">
1491
+
1492
+ <dt class="tag-description">Description:</dt>
1493
+ <dd class="tag-description"><ul class="dummy"><li><p>基於全部交易單重算累積收益並統計回測摘要</p>
1494
+ <p>依timeStart排序後重算各已平倉單之uEquity、uCumuProfitOrLoss與rCumuProfitOrLoss(多策略各自結算後合併重算用)
1495
+ 再執行calcOrdersRatio與calcOrdersSummary,並附加timeTest、timeOhlcStart、timeOhlcEnd、uIni欄位
1496
+ uIni非正數或timeOhlcStart、timeOhlcEnd非有效字串時reject</p>
1497
+ <p>Unit Test: <a href="https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs">Github</a></p></li></ul></dd>
1498
+
1499
+
1500
+
1501
+ <dt class="tag-source">Source:</dt>
1502
+ <dd class="tag-source"><ul class="dummy"><li>
1503
+ <a href="calcSummary.mjs.html">calcSummary.mjs</a>, <a href="calcSummary.mjs.html#line76">line 76</a>
1504
+ </li></ul></dd>
1505
+
1506
+
1507
+
1508
+
1509
+
1510
+
1511
+
1512
+
1513
+
1514
+
1515
+
1516
+
1517
+
1518
+
1519
+
1520
+
1521
+
1522
+
1523
+
1524
+
1525
+
1526
+
1527
+
1528
+
1529
+
1530
+
1531
+
1532
+
1533
+
1534
+
1535
+
1536
+ </dl>
1537
+
1538
+
1539
+
1540
+
1541
+
1542
+
1543
+
1544
+
1545
+
1546
+
1547
+
1548
+ <h5 class="h5-examples">Example</h5>
1549
+
1550
+ <pre class="prettyprint"><code>let arrOhlc = [
1551
+ { time: '2020-01-01T00:00:00', Open: 100, High: 101, Low: 99, Close: 100 },
1552
+ { time: '2020-01-01T04:00:00', Open: 100, High: 106, Low: 100, Close: 105 },
1553
+ { time: '2020-01-01T08:00:00', Open: 105, High: 107, Low: 102, Close: 103 },
1554
+ { time: '2020-01-01T12:00:00', Open: 103, High: 104, Low: 94, Close: 95 },
1555
+ { time: '2020-01-01T16:00:00', Open: 95, High: 98, Low: 92, Close: 93 },
1556
+ { time: '2020-01-01T20:00:00', Open: 93, High: 99, Low: 95, Close: 97 },
1557
+ ]
1558
+
1559
+ //ordersSubmit, 4張下單: 前3張分別於後續K棒觸發止盈或止損, 第4張未觸發(未平倉)
1560
+ let kpDef = { uTrade: 100, timeEnd: '', priceEnd: '', modeResult: '', uFee: 0.05 }
1561
+ let ordersSubmit = [
1562
+ { mode: 'long', timeStart: '2020-01-01T00:00:00', priceStart: 100, priceTakeProfit: 105, priceStopLoss: 97, ...kpDef },
1563
+ { mode: 'long', timeStart: '2020-01-01T04:00:00', priceStart: 105, priceTakeProfit: 110.25, priceStopLoss: 101.85, ...kpDef },
1564
+ { mode: 'short', timeStart: '2020-01-01T08:00:00', priceStart: 103, priceTakeProfit: 97.85, priceStopLoss: 106.09, ...kpDef },
1565
+ { mode: 'long', timeStart: '2020-01-01T16:00:00', priceStart: 93, priceTakeProfit: 105, priceStopLoss: 85, ...kpDef },
1566
+ ]
1567
+
1568
+ let orders = await calcOrders(arrOhlc, ordersSubmit, { uIni: 1000 })
1569
+ let summary = await calcSummary(ott, 1000, orders, '2020-01-01T00:00:00', '2020-01-01T20:00:00')
1570
+ console.log(summary)
1571
+ // => {
1572
+ // timeTest: '2026-08-01T22:27:10+08:00', //執行當下時間
1573
+ // timeOhlcStart: '2020-01-01T00:00:00',
1574
+ // timeOhlcEnd: '2020-01-01T20:00:00',
1575
+ // uIni: 1000,
1576
+ // btDays: 0,
1577
+ // btYears: '0.0',
1578
+ // numTrade: 4,
1579
+ // numTradeFin: 3,
1580
+ // numTradeUnsettled: 1,
1581
+ // uTradeAllMax: 200,
1582
+ // rTradeAllMax: '20.00%',
1583
+ // numTradeAllMax: 2,
1584
+ // uDrawdownMax: 3.1000000000000227,
1585
+ // rDrawdownMax: '0.31%',
1586
+ // rEquivalentDrawdownMax: '1.51%',
1587
+ // rSharpe: 7.675814289051032,
1588
+ // rWin: '66.67%',
1589
+ // uEquityFinal: 1006.6999999999999,
1590
+ // uCumuProfitOrLossFinal: 6.700000000000006,
1591
+ // rCumuProfitOrLossFinal: '0.67%',
1592
+ // rCumuProfitOrLossFinalNormYear: '0.00%',
1593
+ // rEquivalentCumuProfitOrLossFinal: '3.35%',
1594
+ // rEquivalentCumuProfitOrLossFinalNormYear: '0.00%'
1595
+ // }</code></pre>
1596
+
1597
+
1598
+
1599
+
1600
+ <h5 class="h5-parameters">Parameters:</h5>
1601
+
1602
+
1603
+ <table class="params">
1604
+ <thead>
1605
+ <tr>
1606
+
1607
+ <th>Name</th>
1608
+
1609
+
1610
+ <th>Type</th>
1611
+
1612
+
1613
+ <th>Attributes</th>
1614
+
1615
+
1616
+
1617
+ <th>Default</th>
1618
+
1619
+
1620
+ <th class="last">Description</th>
1621
+ </tr>
1622
+ </thead>
1623
+
1624
+ <tbody>
1625
+
1626
+
1627
+ <tr>
1628
+
1629
+ <td class="name"><code>ott</code></td>
1630
+
1631
+
1632
+ <td class="type">
1633
+
1634
+
1635
+ <span class="param-type">function</span>
1636
+
1637
+
1638
+
1639
+
1640
+ </td>
1641
+
1642
+
1643
+ <td class="attributes">
1644
+
1645
+
1646
+
1647
+
1648
+
1649
+ </td>
1650
+
1651
+
1652
+
1653
+ <td class="default">
1654
+
1655
+ </td>
1656
+
1657
+
1658
+ <td class="description last"><p>輸入時區時間函數,傳入時間字串回傳dayjs時間物件(可用src/ott.mjs或自行以dayjs包裝)</p></td>
1659
+ </tr>
1660
+
1661
+
1662
+
1663
+ <tr>
1664
+
1665
+ <td class="name"><code>uIni</code></td>
1666
+
1667
+
1668
+ <td class="type">
1669
+
1670
+
1671
+ <span class="param-type">Number</span>
1672
+
1673
+
1674
+
1675
+
1676
+ </td>
1677
+
1678
+
1679
+ <td class="attributes">
1680
+
1681
+
1682
+
1683
+
1684
+
1685
+ </td>
1686
+
1687
+
1688
+
1689
+ <td class="default">
1690
+
1691
+ </td>
1692
+
1693
+
1694
+ <td class="description last"><p>輸入初始資金正數</p></td>
1695
+ </tr>
1696
+
1697
+
1698
+
1699
+ <tr>
1700
+
1701
+ <td class="name"><code>orders</code></td>
1702
+
1703
+
1704
+ <td class="type">
1705
+
1706
+
1707
+ <span class="param-type">Array</span>
1708
+
1709
+
1710
+
1711
+
1712
+ </td>
1713
+
1714
+
1715
+ <td class="attributes">
1716
+
1717
+
1718
+
1719
+
1720
+
1721
+ </td>
1722
+
1723
+
1724
+
1725
+ <td class="default">
1726
+
1727
+ </td>
1728
+
1729
+
1730
+ <td class="description last"><p>輸入已結算訂單陣列(經calcOrders)</p></td>
1731
+ </tr>
1732
+
1733
+
1734
+
1735
+ <tr>
1736
+
1737
+ <td class="name"><code>timeOhlcStart</code></td>
1738
+
1739
+
1740
+ <td class="type">
1741
+
1742
+
1743
+ <span class="param-type">String</span>
1744
+
1745
+
1746
+
1747
+
1748
+ </td>
1749
+
1750
+
1751
+ <td class="attributes">
1752
+
1753
+
1754
+
1755
+
1756
+
1757
+ </td>
1758
+
1759
+
1760
+
1761
+ <td class="default">
1762
+
1763
+ </td>
1764
+
1765
+
1766
+ <td class="description last"><p>輸入回測起始秒時間字串</p></td>
1767
+ </tr>
1768
+
1769
+
1770
+
1771
+ <tr>
1772
+
1773
+ <td class="name"><code>timeOhlcEnd</code></td>
1774
+
1775
+
1776
+ <td class="type">
1777
+
1778
+
1779
+ <span class="param-type">String</span>
1780
+
1781
+
1782
+
1783
+
1784
+ </td>
1785
+
1786
+
1787
+ <td class="attributes">
1788
+
1789
+
1790
+
1791
+
1792
+
1793
+ </td>
1794
+
1795
+
1796
+
1797
+ <td class="default">
1798
+
1799
+ </td>
1800
+
1801
+
1802
+ <td class="description last"><p>輸入回測結束秒時間字串</p></td>
1803
+ </tr>
1804
+
1805
+
1806
+
1807
+ <tr>
1808
+
1809
+ <td class="name"><code>opt</code></td>
1810
+
1811
+
1812
+ <td class="type">
1813
+
1814
+
1815
+ <span class="param-type">Object</span>
1816
+
1817
+
1818
+
1819
+
1820
+ </td>
1821
+
1822
+
1823
+ <td class="attributes">
1824
+
1825
+ &lt;optional><br>
1826
+
1827
+
1828
+
1829
+
1830
+
1831
+ </td>
1832
+
1833
+
1834
+
1835
+ <td class="default">
1836
+
1837
+ <code>{}</code>
1838
+
1839
+ </td>
1840
+
1841
+
1842
+ <td class="description last"><p>輸入設定物件,預設{}</p></td>
1843
+ </tr>
1844
+
1845
+
1846
+ </tbody>
1847
+ </table>
1848
+
1849
+
1850
+
1851
+
1852
+
1853
+
1854
+
1855
+
1856
+
1857
+
1858
+
1859
+
1860
+
1861
+
1862
+
1863
+
1864
+ <h5 class="h5-returns">Returns:</h5>
1865
+
1866
+
1867
+ <div class="param-desc">
1868
+ <p>回傳Promise,resolve為回測摘要物件</p>
1869
+ </div>
1870
+
1871
+
1872
+
1873
+ <dl class="param-type">
1874
+ <dt>
1875
+ Type
1876
+ </dt>
1877
+ <dd>
1878
+
1879
+ <span class="param-type">Promise</span>
1880
+
1881
+
1882
+
1883
+ </dd>
1884
+ </dl>
1885
+
1886
+
1887
+
1888
+
1889
+
1890
+
1891
+
1892
+
1893
+
1894
+
1895
+ <h4 class="name" id="closeAndSummaryOrders"><span class="type-signature type-signature-async">(async) </span>closeAndSummaryOrders<span class="signature">(ott, fdOhlc, fdParam, uIni, timeOhlcStart, timeOhlcEnd, keyOhlc, ordersSubmit, fdTest, opt<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
1896
+
1897
+
1898
+
1899
+
1900
+
1901
+
1902
+ <dl class="details">
1903
+
1904
+ <dt class="tag-description">Description:</dt>
1905
+ <dd class="tag-description"><ul class="dummy"><li><p>結算下單清單並輸出訂單、摘要與報告檔案</p>
1906
+ <p>以w-data-tdprovide讀取fdOhlc與fdParam資料夾數據,依timeOhlcStart至timeOhlcEnd範圍取keyOhlc之K線序列
1907
+ 經calcOrders結算ordersSubmit與calcSummary統計後,輸出orders.json、summary.json、report.html至fdTest資料夾
1908
+ fdOhlc或fdParam非資料夾、uIni非正數、timeOhlcStart或timeOhlcEnd非有效字串或非4小時整數倍區間時reject</p>
1909
+ <p>Unit Test: <a href="https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs">Github</a></p></li></ul></dd>
1910
+
1911
+
1912
+
1913
+ <dt class="tag-source">Source:</dt>
1914
+ <dd class="tag-source"><ul class="dummy"><li>
1915
+ <a href="closeAndSummaryOrders.mjs.html">closeAndSummaryOrders.mjs</a>, <a href="closeAndSummaryOrders.mjs.html#line75">line 75</a>
1916
+ </li></ul></dd>
1917
+
1918
+
1919
+
1920
+
1921
+
1922
+
1923
+
1924
+
1925
+
1926
+
1927
+
1928
+
1929
+
1930
+
1931
+
1932
+
1933
+
1934
+
1935
+
1936
+
1937
+
1938
+
1939
+
1940
+
1941
+
1942
+
1943
+
1944
+
1945
+
1946
+
1947
+
1948
+ </dl>
1949
+
1950
+
1951
+
1952
+
1953
+
1954
+
1955
+
1956
+
1957
+
1958
+
1959
+
1960
+ <h5 class="h5-examples">Example</h5>
1961
+
1962
+ <pre class="prettyprint"><code>import fs from 'fs'
1963
+
1964
+ let arrOhlc = [
1965
+ { time: '2020-01-01T00:00:00', Open: 100, High: 101, Low: 99, Close: 100 },
1966
+ { time: '2020-01-01T04:00:00', Open: 100, High: 106, Low: 100, Close: 105 },
1967
+ { time: '2020-01-01T08:00:00', Open: 105, High: 107, Low: 102, Close: 103 },
1968
+ { time: '2020-01-01T12:00:00', Open: 103, High: 104, Low: 94, Close: 95 },
1969
+ { time: '2020-01-01T16:00:00', Open: 95, High: 98, Low: 92, Close: 93 },
1970
+ { time: '2020-01-01T20:00:00', Open: 93, High: 99, Low: 95, Close: 97 },
1971
+ ]
1972
+
1973
+ //建立數據資料夾, fdOhlc放K線序列, fdParam放指標參數序列
1974
+ fs.mkdirSync('./data-ohlc', { recursive: true })
1975
+ fs.mkdirSync('./data-param', { recursive: true })
1976
+ fs.writeFileSync('./data-ohlc/btc.json', JSON.stringify(arrOhlc), 'utf8')
1977
+
1978
+ let ordersSubmit = [{
1979
+ mode: 'long',
1980
+ timeStart: '2020-01-01T00:00:00',
1981
+ priceStart: 100,
1982
+ uTrade: 100,
1983
+ priceTakeProfit: 105,
1984
+ priceStopLoss: 97,
1985
+ timeEnd: '',
1986
+ priceEnd: '',
1987
+ modeResult: '',
1988
+ uFee: 0.05,
1989
+ }]
1990
+
1991
+ await closeAndSummaryOrders(ott, './data-ohlc', './data-param', 1000, '2020-01-01T00:00:00', '2020-01-01T20:00:00', 'btc', ordersSubmit, './result')
1992
+ console.log(fs.readdirSync('./result'))
1993
+ // => [ 'orders.json', 'report.html', 'summary.json' ]</code></pre>
1994
+
1995
+
1996
+
1997
+
1998
+ <h5 class="h5-parameters">Parameters:</h5>
1999
+
2000
+
2001
+ <table class="params">
2002
+ <thead>
2003
+ <tr>
2004
+
2005
+ <th>Name</th>
2006
+
2007
+
2008
+ <th>Type</th>
2009
+
2010
+
2011
+ <th>Attributes</th>
2012
+
2013
+
2014
+
2015
+ <th>Default</th>
2016
+
2017
+
2018
+ <th class="last">Description</th>
2019
+ </tr>
2020
+ </thead>
2021
+
2022
+ <tbody>
2023
+
2024
+
2025
+ <tr>
2026
+
2027
+ <td class="name"><code>ott</code></td>
2028
+
2029
+
2030
+ <td class="type">
2031
+
2032
+
2033
+ <span class="param-type">function</span>
2034
+
2035
+
2036
+
2037
+
2038
+ </td>
2039
+
2040
+
2041
+ <td class="attributes">
2042
+
2043
+
2044
+
2045
+
2046
+
2047
+ </td>
2048
+
2049
+
2050
+
2051
+ <td class="default">
2052
+
2053
+ </td>
2054
+
2055
+
2056
+ <td class="description last"><p>輸入時區時間函數,傳入時間字串回傳dayjs時間物件(可用src/ott.mjs或自行以dayjs包裝)</p></td>
2057
+ </tr>
2058
+
2059
+
2060
+
2061
+ <tr>
2062
+
2063
+ <td class="name"><code>fdOhlc</code></td>
2064
+
2065
+
2066
+ <td class="type">
2067
+
2068
+
2069
+ <span class="param-type">String</span>
2070
+
2071
+
2072
+
2073
+
2074
+ </td>
2075
+
2076
+
2077
+ <td class="attributes">
2078
+
2079
+
2080
+
2081
+
2082
+
2083
+ </td>
2084
+
2085
+
2086
+
2087
+ <td class="default">
2088
+
2089
+ </td>
2090
+
2091
+
2092
+ <td class="description last"><p>輸入儲存K線(ohlc)序列資料夾字串,各序列以<code>${key}.json</code>儲存</p></td>
2093
+ </tr>
2094
+
2095
+
2096
+
2097
+ <tr>
2098
+
2099
+ <td class="name"><code>fdParam</code></td>
2100
+
2101
+
2102
+ <td class="type">
2103
+
2104
+
2105
+ <span class="param-type">String</span>
2106
+
2107
+
2108
+
2109
+
2110
+ </td>
2111
+
2112
+
2113
+ <td class="attributes">
2114
+
2115
+
2116
+
2117
+
2118
+
2119
+ </td>
2120
+
2121
+
2122
+
2123
+ <td class="default">
2124
+
2125
+ </td>
2126
+
2127
+
2128
+ <td class="description last"><p>輸入儲存指標參數序列資料夾字串</p></td>
2129
+ </tr>
2130
+
2131
+
2132
+
2133
+ <tr>
2134
+
2135
+ <td class="name"><code>uIni</code></td>
2136
+
2137
+
2138
+ <td class="type">
2139
+
2140
+
2141
+ <span class="param-type">Number</span>
2142
+
2143
+
2144
+
2145
+
2146
+ </td>
2147
+
2148
+
2149
+ <td class="attributes">
2150
+
2151
+
2152
+
2153
+
2154
+
2155
+ </td>
2156
+
2157
+
2158
+
2159
+ <td class="default">
2160
+
2161
+ </td>
2162
+
2163
+
2164
+ <td class="description last"><p>輸入初始資金正數</p></td>
2165
+ </tr>
2166
+
2167
+
2168
+
2169
+ <tr>
2170
+
2171
+ <td class="name"><code>timeOhlcStart</code></td>
2172
+
2173
+
2174
+ <td class="type">
2175
+
2176
+
2177
+ <span class="param-type">String</span>
2178
+
2179
+
2180
+
2181
+
2182
+ </td>
2183
+
2184
+
2185
+ <td class="attributes">
2186
+
2187
+
2188
+
2189
+
2190
+
2191
+ </td>
2192
+
2193
+
2194
+
2195
+ <td class="default">
2196
+
2197
+ </td>
2198
+
2199
+
2200
+ <td class="description last"><p>輸入回測起始秒時間字串</p></td>
2201
+ </tr>
2202
+
2203
+
2204
+
2205
+ <tr>
2206
+
2207
+ <td class="name"><code>timeOhlcEnd</code></td>
2208
+
2209
+
2210
+ <td class="type">
2211
+
2212
+
2213
+ <span class="param-type">String</span>
2214
+
2215
+
2216
+
2217
+
2218
+ </td>
2219
+
2220
+
2221
+ <td class="attributes">
2222
+
2223
+
2224
+
2225
+
2226
+
2227
+ </td>
2228
+
2229
+
2230
+
2231
+ <td class="default">
2232
+
2233
+ </td>
2234
+
2235
+
2236
+ <td class="description last"><p>輸入回測結束秒時間字串</p></td>
2237
+ </tr>
2238
+
2239
+
2240
+
2241
+ <tr>
2242
+
2243
+ <td class="name"><code>keyOhlc</code></td>
2244
+
2245
+
2246
+ <td class="type">
2247
+
2248
+
2249
+ <span class="param-type">String</span>
2250
+
2251
+
2252
+
2253
+
2254
+ </td>
2255
+
2256
+
2257
+ <td class="attributes">
2258
+
2259
+
2260
+
2261
+
2262
+
2263
+ </td>
2264
+
2265
+
2266
+
2267
+ <td class="default">
2268
+
2269
+ </td>
2270
+
2271
+
2272
+ <td class="description last"><p>輸入K線序列key字串</p></td>
2273
+ </tr>
2274
+
2275
+
2276
+
2277
+ <tr>
2278
+
2279
+ <td class="name"><code>ordersSubmit</code></td>
2280
+
2281
+
2282
+ <td class="type">
2283
+
2284
+
2285
+ <span class="param-type">Array</span>
2286
+
2287
+
2288
+
2289
+
2290
+ </td>
2291
+
2292
+
2293
+ <td class="attributes">
2294
+
2295
+
2296
+
2297
+
2298
+
2299
+ </td>
2300
+
2301
+
2302
+
2303
+ <td class="default">
2304
+
2305
+ </td>
2306
+
2307
+
2308
+ <td class="description last"><p>輸入下單陣列(格式同calcOrders之orders)</p></td>
2309
+ </tr>
2310
+
2311
+
2312
+
2313
+ <tr>
2314
+
2315
+ <td class="name"><code>fdTest</code></td>
2316
+
2317
+
2318
+ <td class="type">
2319
+
2320
+
2321
+ <span class="param-type">String</span>
2322
+
2323
+
2324
+
2325
+
2326
+ </td>
2327
+
2328
+
2329
+ <td class="attributes">
2330
+
2331
+
2332
+
2333
+
2334
+
2335
+ </td>
2336
+
2337
+
2338
+
2339
+ <td class="default">
2340
+
2341
+ </td>
2342
+
2343
+
2344
+ <td class="description last"><p>輸入輸出結果資料夾字串</p></td>
2345
+ </tr>
2346
+
2347
+
2348
+
2349
+ <tr>
2350
+
2351
+ <td class="name"><code>opt</code></td>
2352
+
2353
+
2354
+ <td class="type">
2355
+
2356
+
2357
+ <span class="param-type">Object</span>
2358
+
2359
+
2360
+
2361
+
2362
+ </td>
2363
+
2364
+
2365
+ <td class="attributes">
2366
+
2367
+ &lt;optional><br>
2368
+
2369
+
2370
+
2371
+
2372
+
2373
+ </td>
2374
+
2375
+
2376
+
2377
+ <td class="default">
2378
+
2379
+ <code>{}</code>
2380
+
2381
+ </td>
2382
+
2383
+
2384
+ <td class="description last"><p>輸入設定物件,預設{}</p>
2385
+ <h6>Properties</h6>
2386
+
2387
+
2388
+ <table class="params">
2389
+ <thead>
2390
+ <tr>
2391
+
2392
+ <th>Name</th>
2393
+
2394
+
2395
+ <th>Type</th>
2396
+
2397
+
2398
+ <th>Attributes</th>
2399
+
2400
+
2401
+
2402
+ <th>Default</th>
2403
+
2404
+
2405
+ <th class="last">Description</th>
2406
+ </tr>
2407
+ </thead>
2408
+
2409
+ <tbody>
2410
+
2411
+
2412
+ <tr>
2413
+
2414
+ <td class="name"><code>writeOrders</code></td>
2415
+
2416
+
2417
+ <td class="type">
2418
+
2419
+
2420
+ <span class="param-type">Boolean</span>
2421
+
2422
+
2423
+
2424
+
2425
+ </td>
2426
+
2427
+
2428
+ <td class="attributes">
2429
+
2430
+ &lt;optional><br>
2431
+
2432
+
2433
+
2434
+
2435
+
2436
+ </td>
2437
+
2438
+
2439
+
2440
+ <td class="default">
2441
+
2442
+ <code>true</code>
2443
+
2444
+ </td>
2445
+
2446
+
2447
+ <td class="description last"><p>輸入是否輸出orders.json布林值,預設true</p></td>
2448
+ </tr>
2449
+
2450
+
2451
+
2452
+ <tr>
2453
+
2454
+ <td class="name"><code>writeSummary</code></td>
2455
+
2456
+
2457
+ <td class="type">
2458
+
2459
+
2460
+ <span class="param-type">Boolean</span>
2461
+
2462
+
2463
+
2464
+
2465
+ </td>
2466
+
2467
+
2468
+ <td class="attributes">
2469
+
2470
+ &lt;optional><br>
2471
+
2472
+
2473
+
2474
+
2475
+
2476
+ </td>
2477
+
2478
+
2479
+
2480
+ <td class="default">
2481
+
2482
+ <code>true</code>
2483
+
2484
+ </td>
2485
+
2486
+
2487
+ <td class="description last"><p>輸入是否輸出summary.json布林值,預設true</p></td>
2488
+ </tr>
2489
+
2490
+
2491
+
2492
+ <tr>
2493
+
2494
+ <td class="name"><code>writeReport</code></td>
2495
+
2496
+
2497
+ <td class="type">
2498
+
2499
+
2500
+ <span class="param-type">Boolean</span>
2501
+
2502
+
2503
+
2504
+
2505
+ </td>
2506
+
2507
+
2508
+ <td class="attributes">
2509
+
2510
+ &lt;optional><br>
2511
+
2512
+
2513
+
2514
+
2515
+
2516
+ </td>
2517
+
2518
+
2519
+
2520
+ <td class="default">
2521
+
2522
+ <code>true</code>
2523
+
2524
+ </td>
2525
+
2526
+
2527
+ <td class="description last"><p>輸入是否輸出report.html布林值,預設true</p></td>
2528
+ </tr>
2529
+
2530
+
2531
+ </tbody>
2532
+ </table>
2533
+
2534
+ </td>
2535
+ </tr>
2536
+
2537
+
2538
+ </tbody>
2539
+ </table>
2540
+
2541
+
2542
+
2543
+
2544
+
2545
+
2546
+
2547
+
2548
+
2549
+
2550
+
2551
+
2552
+
2553
+
2554
+
2555
+
2556
+ <h5 class="h5-returns">Returns:</h5>
2557
+
2558
+
2559
+ <div class="param-desc">
2560
+ <p>回傳Promise,無resolve值,於fdTest輸出結果檔案</p>
2561
+ </div>
2562
+
2563
+
2564
+
2565
+ <dl class="param-type">
2566
+ <dt>
2567
+ Type
2568
+ </dt>
2569
+ <dd>
2570
+
2571
+ <span class="param-type">Promise</span>
2572
+
2573
+
2574
+
2575
+ </dd>
2576
+ </dl>
2577
+
2578
+
2579
+
2580
+
2581
+
2582
+
2583
+
2584
+
2585
+
2586
+
2587
+ <h4 class="name" id="genReport"><span class="type-signature type-signature-async">(async) </span>genReport<span class="signature">(ott, fpOrders, opt<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
2588
+
2589
+
2590
+
2591
+
2592
+
2593
+
2594
+ <dl class="details">
2595
+
2596
+ <dt class="tag-description">Description:</dt>
2597
+ <dd class="tag-description"><ul class="dummy"><li><p>由orders.json產製績效html報表</p>
2598
+ <p>讀取orders.json(已結算訂單陣列)後,經calcSummary統計摘要,再以genReportCore產出html報表
2599
+ 訂單須為已結算形(含uProfitOrLoss、uEquity等欄位),未結算單(timeEnd為空)仍可計算,計入summary.numTradeUnsettled
2600
+ fpOrders非有效字串或非檔案、內容非有效陣列時throw</p>
2601
+ <p>Unit Test: <a href="https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs">Github</a></p></li></ul></dd>
2602
+
2603
+
2604
+
2605
+ <dt class="tag-source">Source:</dt>
2606
+ <dd class="tag-source"><ul class="dummy"><li>
2607
+ <a href="genReport.mjs.html">genReport.mjs</a>, <a href="genReport.mjs.html#line61">line 61</a>
2608
+ </li></ul></dd>
2609
+
2610
+
2611
+
2612
+
2613
+
2614
+
2615
+
2616
+
2617
+
2618
+
2619
+
2620
+
2621
+
2622
+
2623
+
2624
+
2625
+
2626
+
2627
+
2628
+
2629
+
2630
+
2631
+
2632
+
2633
+
2634
+
2635
+
2636
+
2637
+
2638
+
2639
+
2640
+ </dl>
2641
+
2642
+
2643
+
2644
+
2645
+
2646
+
2647
+
2648
+
2649
+
2650
+
2651
+
2652
+ <h5 class="h5-examples">Example</h5>
2653
+
2654
+ <pre class="prettyprint"><code>import fs from 'fs'
2655
  mode: 'long',
1
2656
  timeStart: '2020-01-01T00:00:00',
2
2657
  priceStart: 100,
3
2658
  uTrade: 100,
4
2659
  priceTakeProfit: 105,
5
2660
  priceStopLoss: 97,
6
2661
  timeEnd: '2020-01-01T04:00:00',
7
2662
  priceEnd: 105,
8
2663
  modeResult: 'profit',
9
2664
  uFee: 0.05,
10
2665
  uProfitOrLoss: 4.9,
11
2666
  rProfitOrLoss: 0.049,
12
2667
  uCumuProfitOrLoss: 4.9,
13
2668
  uEquity: 1004.9,
2669
+
2670
+
2671
+
2672
+
2673
+ <h5 class="h5-parameters">Parameters:</h5>
2674
+
2675
+
2676
+ <table class="params">
2677
+ <thead>
2678
+ <tr>
2679
+
2680
+ <th>Name</th>
2681
+
2682
+
2683
+ <th>Type</th>
2684
+
2685
+
2686
+ <th>Attributes</th>
2687
+
2688
+
2689
+
2690
+ <th>Default</th>
2691
+
2692
+
2693
+ <th class="last">Description</th>
2694
+ </tr>
2695
+ </thead>
2696
+
2697
+ <tbody>
2698
+
2699
+
2700
+ <tr>
2701
+
2702
+ <td class="name"><code>ott</code></td>
2703
+
2704
+
2705
+ <td class="type">
2706
+
2707
+
2708
+ <span class="param-type">function</span>
2709
+
2710
+
2711
+
2712
+
2713
+ </td>
2714
+
2715
+
2716
+ <td class="attributes">
2717
+
2718
+
2719
+
2720
+
2721
+
2722
+ </td>
2723
+
2724
+
2725
+
2726
+ <td class="default">
2727
+
2728
+ </td>
2729
+
2730
+
2731
+ <td class="description last"><p>輸入時區時間函數,傳入時間字串回傳dayjs時間物件(可用src/ott.mjs或自行以dayjs包裝)</p></td>
2732
+ </tr>
2733
+
2734
+
2735
+
2736
+ <tr>
2737
+
2738
+ <td class="name"><code>fpOrders</code></td>
2739
+
2740
+
2741
+ <td class="type">
2742
+
2743
+
2744
+ <span class="param-type">String</span>
2745
+
2746
+
2747
+
2748
+
2749
+ </td>
2750
+
2751
+
2752
+ <td class="attributes">
2753
+
2754
+
2755
+
2756
+
2757
+
2758
+ </td>
2759
+
2760
+
2761
+
2762
+ <td class="default">
2763
+
2764
+ </td>
2765
+
2766
+
2767
+ <td class="description last"><p>輸入orders.json檔案路徑字串</p></td>
2768
+ </tr>
2769
+
2770
+
2771
+
2772
+ <tr>
2773
+
2774
+ <td class="name"><code>opt</code></td>
2775
+
2776
+
2777
+ <td class="type">
2778
+
2779
+
2780
+ <span class="param-type">Object</span>
2781
+
2782
+
2783
+
2784
+
2785
+ </td>
2786
+
2787
+
2788
+ <td class="attributes">
2789
+
2790
+ &lt;optional><br>
2791
+
2792
+
2793
+
2794
+
2795
+
2796
+ </td>
2797
+
2798
+
2799
+
2800
+ <td class="default">
2801
+
2802
+ <code>{}</code>
2803
+
2804
+ </td>
2805
+
2806
+
2807
+ <td class="description last"><p>輸入設定物件,預設{}</p>
2808
+ <h6>Properties</h6>
2809
+
2810
+
2811
+ <table class="params">
2812
+ <thead>
2813
+ <tr>
2814
+
2815
+ <th>Name</th>
2816
+
2817
+
2818
+ <th>Type</th>
2819
+
2820
+
2821
+ <th>Attributes</th>
2822
+
2823
+
2824
+
2825
+ <th>Default</th>
2826
+
2827
+
2828
+ <th class="last">Description</th>
2829
+ </tr>
2830
+ </thead>
2831
+
2832
+ <tbody>
2833
+
2834
+
2835
+ <tr>
2836
+
2837
+ <td class="name"><code>fpOut</code></td>
2838
+
2839
+
2840
+ <td class="type">
2841
+
2842
+
2843
+ <span class="param-type">String</span>
2844
+
2845
+
2846
+
2847
+
2848
+ </td>
2849
+
2850
+
2851
+ <td class="attributes">
2852
+
2853
+ &lt;optional><br>
2854
+
2855
+
2856
+
2857
+
2858
+
2859
+ </td>
2860
+
2861
+
2862
+
2863
+ <td class="default">
2864
+
2865
+ <code>''</code>
2866
+
2867
+ </td>
2868
+
2869
+
2870
+ <td class="description last"><p>輸入輸出html檔案路徑字串,預設為與orders.json同資料夾之report.html</p></td>
2871
+ </tr>
2872
+
2873
+
2874
+
2875
+ <tr>
2876
+
2877
+ <td class="name"><code>name</code></td>
2878
+
2879
+
2880
+ <td class="type">
2881
+
2882
+
2883
+ <span class="param-type">String</span>
2884
+
2885
+
2886
+
2887
+
2888
+ </td>
2889
+
2890
+
2891
+ <td class="attributes">
2892
+
2893
+ &lt;optional><br>
2894
+
2895
+
2896
+
2897
+
2898
+
2899
+ </td>
2900
+
2901
+
2902
+
2903
+ <td class="default">
2904
+
2905
+ <code>''</code>
2906
+
2907
+ </td>
2908
+
2909
+
2910
+ <td class="description last"><p>輸入報表標題字串,預設為orders.json所在資料夾名</p></td>
2911
+ </tr>
2912
+
2913
+
2914
+
2915
+ <tr>
2916
+
2917
+ <td class="name"><code>uIni</code></td>
2918
+
2919
+
2920
+ <td class="type">
2921
+
2922
+
2923
+ <span class="param-type">Number</span>
2924
+
2925
+
2926
+
2927
+
2928
+ </td>
2929
+
2930
+
2931
+ <td class="attributes">
2932
+
2933
+ &lt;optional><br>
2934
+
2935
+
2936
+
2937
+
2938
+
2939
+ </td>
2940
+
2941
+
2942
+
2943
+ <td class="default">
2944
+
2945
+ <code>null</code>
2946
+
2947
+ </td>
2948
+
2949
+
2950
+ <td class="description last"><p>輸入初始資金數值,預設由首筆已結算單推回(uEquity - uCumuProfitOrLoss),推不出則為1000</p></td>
2951
+ </tr>
2952
+
2953
+
2954
+
2955
+ <tr>
2956
+
2957
+ <td class="name"><code>timeOhlcStart</code></td>
2958
+
2959
+
2960
+ <td class="type">
2961
+
2962
+
2963
+ <span class="param-type">String</span>
2964
+
2965
+
2966
+
2967
+
2968
+ </td>
2969
+
2970
+
2971
+ <td class="attributes">
2972
+
2973
+ &lt;optional><br>
2974
+
2975
+
2976
+
2977
+
2978
+
2979
+ </td>
2980
+
2981
+
2982
+
2983
+ <td class="default">
2984
+
2985
+ <code>''</code>
2986
+
2987
+ </td>
2988
+
2989
+
2990
+ <td class="description last"><p>輸入摘要統計起始秒時間字串,影響btDays與年化,預設由訂單最早timeStart推得,注意訂單起訖僅為「有單的範圍」,若實際回測窗較寬(頭尾無單)年化會被高估,建議明給</p></td>
2991
+ </tr>
2992
+
2993
+
2994
+
2995
+ <tr>
2996
+
2997
+ <td class="name"><code>timeOhlcEnd</code></td>
2998
+
2999
+
3000
+ <td class="type">
3001
+
3002
+
3003
+ <span class="param-type">String</span>
3004
+
3005
+
3006
+
3007
+
3008
+ </td>
3009
+
3010
+
3011
+ <td class="attributes">
3012
+
3013
+ &lt;optional><br>
3014
+
3015
+
3016
+
3017
+
3018
+
3019
+ </td>
3020
+
3021
+
3022
+
3023
+ <td class="default">
3024
+
3025
+ <code>''</code>
3026
+
3027
+ </td>
3028
+
3029
+
3030
+ <td class="description last"><p>輸入摘要統計結束秒時間字串,預設由訂單最晚timeEnd(未平倉單取timeStart)推得</p></td>
3031
+ </tr>
3032
+
3033
+
3034
+
3035
+ <tr>
3036
+
3037
+ <td class="name"><code>withWriteSummary</code></td>
3038
+
3039
+
3040
+ <td class="type">
3041
+
3042
+
3043
+ <span class="param-type">Boolean</span>
3044
+
3045
+
3046
+
3047
+
3048
+ </td>
3049
+
3050
+
3051
+ <td class="attributes">
3052
+
3053
+ &lt;optional><br>
3054
+
3055
+
3056
+
3057
+
3058
+
3059
+ </td>
3060
+
3061
+
3062
+
3063
+ <td class="default">
3064
+
3065
+ <code>false</code>
3066
+
3067
+ </td>
3068
+
3069
+
3070
+ <td class="description last"><p>輸入是否同時輸出summary.json(與fpOut同資料夾)布林值,預設false</p></td>
3071
+ </tr>
3072
+
3073
+
3074
+ </tbody>
3075
+ </table>
3076
+
3077
+ </td>
3078
+ </tr>
3079
+
3080
+
3081
+ </tbody>
3082
+ </table>
3083
+
3084
+
3085
+
3086
+
3087
+
3088
+
3089
+
3090
+
3091
+
3092
+
3093
+
3094
+
3095
+
3096
+
3097
+
3098
+
3099
+ <h5 class="h5-returns">Returns:</h5>
3100
+
3101
+
3102
+ <div class="param-desc">
3103
+ <p>回傳Promise,resolve為物件,內含orders(訂單陣列)、summary(摘要物件)、fpOut(輸出html路徑)欄位</p>
3104
+ </div>
3105
+
3106
+
3107
+
3108
+ <dl class="param-type">
3109
+ <dt>
3110
+ Type
3111
+ </dt>
3112
+ <dd>
3113
+
3114
+ <span class="param-type">Promise</span>
3115
+
3116
+
3117
+
3118
+ </dd>
3119
+ </dl>
3120
+
3121
+
3122
+
3123
+
3124
+
3125
+
3126
+
3127
+
3128
+
3129
+
3130
+ <h4 class="name" id="genReportCore"><span class="type-signature"></span>genReportCore<span class="signature">(r, fpOut)</span><span class="type-signature"> &rarr; {undefined}</span></h4>
3131
+
3132
+
3133
+
3134
+
3135
+
3136
+
3137
+ <dl class="details">
3138
+
3139
+ <dt class="tag-description">Description:</dt>
3140
+ <dd class="tag-description"><ul class="dummy"><li><p>產出html回測報告</p>
3141
+ <p>讀取與本模組同資料夾之模板資產(tmp.html與render*.js),置換{name}、{orders}、{summary}後寫出html
3142
+ 報告內含權益曲線、訂單表格、時間軸與摘要</p>
3143
+ <p>Unit Test: <a href="https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs">Github</a></p></li></ul></dd>
3144
+
3145
+
3146
+
3147
+ <dt class="tag-source">Source:</dt>
3148
+ <dd class="tag-source"><ul class="dummy"><li>
3149
+ <a href="genReportCore.mjs.html">genReportCore.mjs</a>, <a href="genReportCore.mjs.html#line28">line 28</a>
3150
+ </li></ul></dd>
3151
+
3152
+
3153
+
3154
+
3155
+
3156
+
3157
+
3158
+
3159
+
3160
+
3161
+
3162
+
3163
+
3164
+
3165
+
3166
+
3167
+
3168
+
3169
+
3170
+
3171
+
3172
+
3173
+
3174
+
3175
+
3176
+
3177
+
3178
+
3179
+
3180
+
3181
+
3182
+ </dl>
3183
+
3184
+
3185
+
3186
+
3187
+
3188
+
3189
+
3190
+
3191
+
3192
+
3193
+
3194
+ <h5 class="h5-examples">Example</h5>
3195
+
3196
+ <pre class="prettyprint"><code>//rr為runStrategies範例之回傳{orders,summary}
3197
+ genReportCore({ name: '示範策略', orders: rr.orders, summary: rr.summary }, './report.html')
3198
+ // => 於'./report.html'寫出html報告</code></pre>
3199
+
3200
+
3201
+
3202
+
3203
+ <h5 class="h5-parameters">Parameters:</h5>
3204
+
3205
+
3206
+ <table class="params">
3207
+ <thead>
3208
+ <tr>
3209
+
3210
+ <th>Name</th>
3211
+
3212
+
3213
+ <th>Type</th>
3214
+
3215
+
3216
+
3217
+
3218
+
3219
+ <th class="last">Description</th>
3220
+ </tr>
3221
+ </thead>
3222
+
3223
+ <tbody>
3224
+
3225
+
3226
+ <tr>
3227
+
3228
+ <td class="name"><code>r</code></td>
3229
+
3230
+
3231
+ <td class="type">
3232
+
3233
+
3234
+ <span class="param-type">Object</span>
3235
+
3236
+
3237
+
3238
+
3239
+ </td>
3240
+
3241
+
3242
+
3243
+
3244
+
3245
+ <td class="description last"><p>輸入報告物件,需含name(報告名稱字串)、orders(訂單陣列)、summary(摘要物件)欄位</p></td>
3246
+ </tr>
3247
+
3248
+
3249
+
3250
+ <tr>
3251
+
3252
+ <td class="name"><code>fpOut</code></td>
3253
+
3254
+
3255
+ <td class="type">
3256
+
3257
+
3258
+ <span class="param-type">String</span>
3259
+
3260
+
3261
+
3262
+
3263
+ </td>
3264
+
3265
+
3266
+
3267
+
3268
+
3269
+ <td class="description last"><p>輸入輸出html檔案路徑字串</p></td>
3270
+ </tr>
3271
+
3272
+
3273
+ </tbody>
3274
+ </table>
3275
+
3276
+
3277
+
3278
+
3279
+
3280
+
3281
+
3282
+
3283
+
3284
+
3285
+
3286
+
3287
+
3288
+
3289
+
3290
+
3291
+ <h5 class="h5-returns">Returns:</h5>
3292
+
3293
+
3294
+ <div class="param-desc">
3295
+ <p>無回傳,於fpOut寫出html報告</p>
3296
+ </div>
3297
+
3298
+
3299
+
3300
+ <dl class="param-type">
3301
+ <dt>
3302
+ Type
3303
+ </dt>
3304
+ <dd>
3305
+
3306
+ <span class="param-type">undefined</span>
3307
+
3308
+
3309
+
3310
+ </dd>
3311
+ </dl>
3312
+
3313
+
3314
+
3315
+
3316
+
3317
+
3318
+
3319
+
3320
+
3321
+
3322
+ <h4 class="name" id="runStrategies"><span class="type-signature type-signature-async">(async) </span>runStrategies<span class="signature">(ott, strategies, funGetSeries, opt<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
3323
+
3324
+
3325
+
3326
+
3327
+
3328
+
3329
+ <dl class="details">
3330
+
3331
+ <dt class="tag-description">Description:</dt>
3332
+ <dd class="tag-description"><ul class="dummy"><li><p>執行多策略回測</p>
3333
+ <p>逐策略呼叫runStrategy(withSummary:false),各單附加策略sid後合併並依timeStart排序
3334
+ 以各策略settings.uIni總和為初始資金呼叫calcSummary統計合併摘要
3335
+ 個別策略執行失敗(如時間點無參數無法下單)時強制略過</p>
3336
+ <p>Unit Test: <a href="https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs">Github</a></p></li></ul></dd>
3337
+
3338
+
3339
+
3340
+ <dt class="tag-source">Source:</dt>
3341
+ <dd class="tag-source"><ul class="dummy"><li>
3342
+ <a href="runStrategies.mjs.html">runStrategies.mjs</a>, <a href="runStrategies.mjs.html#line79">line 79</a>
3343
+ </li></ul></dd>
3344
+
3345
+
3346
+
3347
+
3348
+
3349
+
3350
+
3351
+
3352
+
3353
+
3354
+
3355
+
3356
+
3357
+
3358
+
3359
+
3360
+
3361
+
3362
+
3363
+
3364
+
3365
+
3366
+
3367
+
3368
+
3369
+
3370
+
3371
+
3372
+
3373
+
3374
+
3375
+ </dl>
3376
+
3377
+
3378
+
3379
+
3380
+
3381
+
3382
+
3383
+
3384
+
3385
+
3386
+
3387
+ <h5 class="h5-examples">Example</h5>
3388
+
3389
+ <pre class="prettyprint"><code>let arrOhlc = [
3390
+ { time: '2020-01-01T00:00:00', Open: 100, High: 101, Low: 99, Close: 100 },
3391
+ { time: '2020-01-01T04:00:00', Open: 100, High: 106, Low: 100, Close: 105 },
3392
+ { time: '2020-01-01T08:00:00', Open: 105, High: 107, Low: 102, Close: 103 },
3393
+ { time: '2020-01-01T12:00:00', Open: 103, High: 104, Low: 94, Close: 95 },
3394
+ { time: '2020-01-01T16:00:00', Open: 95, High: 98, Low: 92, Close: 93 },
3395
+ { time: '2020-01-01T20:00:00', Open: 93, High: 99, Low: 95, Close: 97 },
3396
+ ]
3397
+
3398
+ let arrSig = [
3399
+ { time: '2020-01-01T00:00:00', param: 1 },
3400
+ { time: '2020-01-01T04:00:00', param: 0 },
3401
+ { time: '2020-01-01T08:00:00', param: 1 },
3402
+ { time: '2020-01-01T12:00:00', param: 0 },
3403
+ { time: '2020-01-01T16:00:00', param: 0 },
3404
+ { time: '2020-01-01T20:00:00', param: 0 },
3405
+ ]
3406
+
3407
+ let funGetSeries = async (key) => {
3408
+ if (key === 'btc') {
3409
+ return arrOhlc
3410
+ }
3411
+ if (key === 'sig') {
3412
+ return arrSig
3413
+ }
3414
+ throw new Error(`invalid key[${key}]`)
3415
+ }
3416
+
3417
+ let settings = { uIni: 1000, uTrade: 100, rTakeProfit: 0.05, rStopLoss: 0.03, rFee: 0.0005 }
3418
+ let strategies = [
3419
+ { sid: 's1', mode: 'long', keyOhlc: 'btc', conds: [{ key: 'sig', sym: '>', th: 0.5, opr: 'and' }], settings },
3420
+ { sid: 's2', mode: 'short', keyOhlc: 'btc', conds: [{ key: 'sig', sym: '&lt;', th: 0.5, opr: 'and' }], settings },
3421
+ ]
3422
+
3423
+ let rr = await runStrategies(ott, strategies, funGetSeries)
3424
+ console.log(rr.orders.map((o) => `${o.sid} ${o.timeStart} ${o.mode} ${o.modeResult || 'unsettled'}`))
3425
+ // => [
3426
+ // 's1 2020-01-01T00:00:00 long profit',
3427
+ // 's2 2020-01-01T04:00:00 short profit',
3428
+ // 's1 2020-01-01T08:00:00 long loss',
3429
+ // 's2 2020-01-01T12:00:00 short loss',
3430
+ // 's2 2020-01-01T16:00:00 short loss',
3431
+ // 's2 2020-01-01T20:00:00 short unsettled'
3432
+ // ]
3433
+ console.log(rr.summary.uIni, rr.summary.numTrade, rr.summary.rWin)
3434
+ // => 2000 6 40.00%</code></pre>
3435
+
3436
+
3437
+
3438
+
3439
+ <h5 class="h5-parameters">Parameters:</h5>
3440
+
3441
+
3442
+ <table class="params">
3443
+ <thead>
3444
+ <tr>
3445
+
3446
+ <th>Name</th>
3447
+
3448
+
3449
+ <th>Type</th>
3450
+
3451
+
3452
+ <th>Attributes</th>
3453
+
3454
+
3455
+
3456
+ <th>Default</th>
3457
+
3458
+
3459
+ <th class="last">Description</th>
3460
+ </tr>
3461
+ </thead>
3462
+
3463
+ <tbody>
3464
+
3465
+
3466
+ <tr>
3467
+
3468
+ <td class="name"><code>ott</code></td>
3469
+
3470
+
3471
+ <td class="type">
3472
+
3473
+
3474
+ <span class="param-type">function</span>
3475
+
3476
+
3477
+
3478
+
3479
+ </td>
3480
+
3481
+
3482
+ <td class="attributes">
3483
+
3484
+
3485
+
3486
+
3487
+
3488
+ </td>
3489
+
3490
+
3491
+
3492
+ <td class="default">
3493
+
3494
+ </td>
3495
+
3496
+
3497
+ <td class="description last"><p>輸入時區時間函數,傳入時間字串回傳dayjs時間物件(可用src/ott.mjs或自行以dayjs包裝)</p></td>
3498
+ </tr>
3499
+
3500
+
3501
+
3502
+ <tr>
3503
+
3504
+ <td class="name"><code>strategies</code></td>
3505
+
3506
+
3507
+ <td class="type">
3508
+
3509
+
3510
+ <span class="param-type">Array</span>
3511
+
3512
+
3513
+
3514
+
3515
+ </td>
3516
+
3517
+
3518
+ <td class="attributes">
3519
+
3520
+
3521
+
3522
+
3523
+
3524
+ </td>
3525
+
3526
+
3527
+
3528
+ <td class="default">
3529
+
3530
+ </td>
3531
+
3532
+
3533
+ <td class="description last"><p>輸入策略陣列,各元素需含sid與runStrategy之strategy欄位</p></td>
3534
+ </tr>
3535
+
3536
+
3537
+
3538
+ <tr>
3539
+
3540
+ <td class="name"><code>funGetSeries</code></td>
3541
+
3542
+
3543
+ <td class="type">
3544
+
3545
+
3546
+ <span class="param-type">function</span>
3547
+
3548
+
3549
+
3550
+
3551
+ </td>
3552
+
3553
+
3554
+ <td class="attributes">
3555
+
3556
+
3557
+
3558
+
3559
+
3560
+ </td>
3561
+
3562
+
3563
+
3564
+ <td class="default">
3565
+
3566
+ </td>
3567
+
3568
+
3569
+ <td class="description last"><p>輸入序列查詢async函數,依key回傳時間序列陣列</p></td>
3570
+ </tr>
3571
+
3572
+
3573
+
3574
+ <tr>
3575
+
3576
+ <td class="name"><code>opt</code></td>
3577
+
3578
+
3579
+ <td class="type">
3580
+
3581
+
3582
+ <span class="param-type">Object</span>
3583
+
3584
+
3585
+
3586
+
3587
+ </td>
3588
+
3589
+
3590
+ <td class="attributes">
3591
+
3592
+ &lt;optional><br>
3593
+
3594
+
3595
+
3596
+
3597
+
3598
+ </td>
3599
+
3600
+
3601
+
3602
+ <td class="default">
3603
+
3604
+ <code>{}</code>
3605
+
3606
+ </td>
3607
+
3608
+
3609
+ <td class="description last"><p>輸入設定物件,預設{}</p>
3610
+ <h6>Properties</h6>
3611
+
3612
+
3613
+ <table class="params">
3614
+ <thead>
3615
+ <tr>
3616
+
3617
+ <th>Name</th>
3618
+
3619
+
3620
+ <th>Type</th>
3621
+
3622
+
3623
+ <th>Attributes</th>
3624
+
3625
+
3626
+
3627
+ <th>Default</th>
3628
+
3629
+
3630
+ <th class="last">Description</th>
3631
+ </tr>
3632
+ </thead>
3633
+
3634
+ <tbody>
3635
+
3636
+
3637
+ <tr>
3638
+
3639
+ <td class="name"><code>withCalcOrderProfitOrLoss</code></td>
3640
+
3641
+
3642
+ <td class="type">
3643
+
3644
+
3645
+ <span class="param-type">Boolean</span>
3646
+
3647
+
3648
+
3649
+
3650
+ </td>
3651
+
3652
+
3653
+ <td class="attributes">
3654
+
3655
+ &lt;optional><br>
3656
+
3657
+
3658
+
3659
+
3660
+
3661
+ </td>
3662
+
3663
+
3664
+
3665
+ <td class="default">
3666
+
3667
+ <code>true</code>
3668
+
3669
+ </td>
3670
+
3671
+
3672
+ <td class="description last"><p>輸入是否經calcOrders結算訂單布林值,預設true</p></td>
3673
+ </tr>
3674
+
3675
+
3676
+
3677
+ <tr>
3678
+
3679
+ <td class="name"><code>withSummary</code></td>
3680
+
3681
+
3682
+ <td class="type">
3683
+
3684
+
3685
+ <span class="param-type">Boolean</span>
3686
+
3687
+
3688
+
3689
+
3690
+ </td>
3691
+
3692
+
3693
+ <td class="attributes">
3694
+
3695
+ &lt;optional><br>
3696
+
3697
+
3698
+
3699
+
3700
+
3701
+ </td>
3702
+
3703
+
3704
+
3705
+ <td class="default">
3706
+
3707
+ <code>true</code>
3708
+
3709
+ </td>
3710
+
3711
+
3712
+ <td class="description last"><p>輸入是否計算統計摘要布林值,預設true</p></td>
3713
+ </tr>
3714
+
3715
+
3716
+ </tbody>
3717
+ </table>
3718
+
3719
+ </td>
3720
+ </tr>
3721
+
3722
+
3723
+ </tbody>
3724
+ </table>
3725
+
3726
+
3727
+
3728
+
3729
+
3730
+
3731
+
3732
+
3733
+
3734
+
3735
+
3736
+
3737
+
3738
+
3739
+
3740
+
3741
+ <h5 class="h5-returns">Returns:</h5>
3742
+
3743
+
3744
+ <div class="param-desc">
3745
+ <p>回傳Promise,resolve為{orders,summary}物件</p>
3746
+ </div>
3747
+
3748
+
3749
+
3750
+ <dl class="param-type">
3751
+ <dt>
3752
+ Type
3753
+ </dt>
3754
+ <dd>
3755
+
3756
+ <span class="param-type">Promise</span>
3757
+
3758
+
3759
+
3760
+ </dd>
3761
+ </dl>
3762
+
3763
+
3764
+
3765
+
3766
+
3767
+
3768
+
3769
+
3770
+
3771
+
3772
+ <h4 class="name" id="runStrategy"><span class="type-signature type-signature-async">(async) </span>runStrategy<span class="signature">(ott, strategy, funGetSeries, opt<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
3773
+
3774
+
3775
+
3776
+
3777
+
3778
+
3779
+ <dl class="details">
3780
+
3781
+ <dt class="tag-description">Description:</dt>
3782
+ <dd class="tag-description"><ul class="dummy"><li><p>執行單一策略回測</p>
3783
+ <p>於各時間點以conds判斷觸發下單(各條件以sym與th比較該key序列之param值,opr為'and'須全真、'or'至少一真)
3784
+ 觸發時以該K棒Close下單,並依settings計算止盈止損價格,再經calcOrders結算與calcOrdersRatio、calcOrdersSummary統計
3785
+ 僅於全部conds序列皆有值之共同時間點判斷觸發
3786
+ strategy.mode非'long'或'short'、keyOhlc或conds無效、funGetSeries非函數時reject</p>
3787
+ <p>Unit Test: <a href="https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs">Github</a></p></li></ul></dd>
3788
+
3789
+
3790
+
3791
+ <dt class="tag-source">Source:</dt>
3792
+ <dd class="tag-source"><ul class="dummy"><li>
3793
+ <a href="runStrategy.mjs.html">runStrategy.mjs</a>, <a href="runStrategy.mjs.html#line82">line 82</a>
3794
+ </li></ul></dd>
3795
+
3796
+
3797
+
3798
+
3799
+
3800
+
3801
+
3802
+
3803
+
3804
+
3805
+
3806
+
3807
+
3808
+
3809
+
3810
+
3811
+
3812
+
3813
+
3814
+
3815
+
3816
+
3817
+
3818
+
3819
+
3820
+
3821
+
3822
+
3823
+
3824
+
3825
+
3826
+ </dl>
3827
+
3828
+
3829
+
3830
+
3831
+
3832
+
3833
+
3834
+
3835
+
3836
+
3837
+
3838
+ <h5 class="h5-examples">Example</h5>
3839
+
3840
+ <pre class="prettyprint"><code>let arrOhlc = [
3841
+ { time: '2020-01-01T00:00:00', Open: 100, High: 101, Low: 99, Close: 100 },
3842
+ { time: '2020-01-01T04:00:00', Open: 100, High: 106, Low: 100, Close: 105 },
3843
+ { time: '2020-01-01T08:00:00', Open: 105, High: 107, Low: 102, Close: 103 },
3844
+ { time: '2020-01-01T12:00:00', Open: 103, High: 104, Low: 94, Close: 95 },
3845
+ { time: '2020-01-01T16:00:00', Open: 95, High: 98, Low: 92, Close: 93 },
3846
+ { time: '2020-01-01T20:00:00', Open: 93, High: 99, Low: 95, Close: 97 },
3847
+ ]
3848
+
3849
+ let arrSig = [
3850
+ { time: '2020-01-01T00:00:00', param: 1 },
3851
+ { time: '2020-01-01T04:00:00', param: 0 },
3852
+ { time: '2020-01-01T08:00:00', param: 1 },
3853
+ { time: '2020-01-01T12:00:00', param: 0 },
3854
+ { time: '2020-01-01T16:00:00', param: 0 },
3855
+ { time: '2020-01-01T20:00:00', param: 0 },
3856
+ ]
3857
+
3858
+ let funGetSeries = async (key) => {
3859
+ if (key === 'btc') {
3860
+ return arrOhlc
3861
+ }
3862
+ if (key === 'sig') {
3863
+ return arrSig
3864
+ }
3865
+ throw new Error(`invalid key[${key}]`)
3866
+ }
3867
+
3868
+ let strategy = {
3869
+ mode: 'long',
3870
+ keyOhlc: 'btc',
3871
+ conds: [{ key: 'sig', sym: '>', th: 0.5, opr: 'and' }],
3872
+ settings: { uIni: 1000, uTrade: 100, rTakeProfit: 0.05, rStopLoss: 0.03, rFee: 0.0005 },
3873
+ }
3874
+
3875
+ let r = await runStrategy(ott, strategy, funGetSeries)
3876
+ console.log(r.orders.map((o) => `${o.timeStart} ${o.mode} ${o.priceStart}->${o.priceEnd} ${o.modeResult}`))
3877
+ // => [
3878
+ // '2020-01-01T00:00:00 long 100->105 profit',
3879
+ // '2020-01-01T08:00:00 long 103->99.91 loss'
3880
+ // ]
3881
+ console.log(r.summary.numTrade, r.summary.rWin, r.summary.uEquityFinal)
3882
+ // => 2 50.00% 1001.8</code></pre>
3883
+
3884
+
3885
+
3886
+
3887
+ <h5 class="h5-parameters">Parameters:</h5>
3888
+
3889
+
3890
+ <table class="params">
3891
+ <thead>
3892
+ <tr>
3893
+
3894
+ <th>Name</th>
3895
+
3896
+
3897
+ <th>Type</th>
3898
+
3899
+
3900
+ <th>Attributes</th>
3901
+
3902
+
3903
+
3904
+ <th>Default</th>
3905
+
3906
+
3907
+ <th class="last">Description</th>
3908
+ </tr>
3909
+ </thead>
3910
+
3911
+ <tbody>
3912
+
3913
+
3914
+ <tr>
3915
+
3916
+ <td class="name"><code>ott</code></td>
3917
+
3918
+
3919
+ <td class="type">
3920
+
3921
+
3922
+ <span class="param-type">function</span>
3923
+
3924
+
3925
+
3926
+
3927
+ </td>
3928
+
3929
+
3930
+ <td class="attributes">
3931
+
3932
+
3933
+
3934
+
3935
+
3936
+ </td>
3937
+
3938
+
3939
+
3940
+ <td class="default">
3941
+
3942
+ </td>
3943
+
3944
+
3945
+ <td class="description last"><p>輸入時區時間函數,傳入時間字串回傳dayjs時間物件(可用src/ott.mjs或自行以dayjs包裝)</p></td>
3946
+ </tr>
3947
+
3948
+
3949
+
3950
+ <tr>
3951
+
3952
+ <td class="name"><code>strategy</code></td>
3953
+
3954
+
3955
+ <td class="type">
3956
+
3957
+
3958
+ <span class="param-type">Object</span>
3959
+
3960
+
3961
+
3962
+
3963
+ </td>
3964
+
3965
+
3966
+ <td class="attributes">
3967
+
3968
+
3969
+
3970
+
3971
+
3972
+ </td>
3973
+
3974
+
3975
+
3976
+ <td class="default">
3977
+
3978
+ </td>
3979
+
3980
+
3981
+ <td class="description last"><p>輸入策略物件,需含mode('long'或'short')、keyOhlc(K線序列key字串)、conds(條件陣列,各元素為{key,sym,th,opr})、settings({uIni,uTrade,rTakeProfit,rStopLoss,rFee})欄位</p></td>
3982
+ </tr>
3983
+
3984
+
3985
+
3986
+ <tr>
3987
+
3988
+ <td class="name"><code>funGetSeries</code></td>
3989
+
3990
+
3991
+ <td class="type">
3992
+
3993
+
3994
+ <span class="param-type">function</span>
3995
+
3996
+
3997
+
3998
+
3999
+ </td>
4000
+
4001
+
4002
+ <td class="attributes">
4003
+
4004
+
4005
+
4006
+
4007
+
4008
+ </td>
4009
+
4010
+
4011
+
4012
+ <td class="default">
4013
+
4014
+ </td>
4015
+
4016
+
4017
+ <td class="description last"><p>輸入序列查詢async函數,依key回傳時間序列陣列</p></td>
4018
+ </tr>
4019
+
4020
+
4021
+
4022
+ <tr>
4023
+
4024
+ <td class="name"><code>opt</code></td>
4025
+
4026
+
4027
+ <td class="type">
4028
+
4029
+
4030
+ <span class="param-type">Object</span>
4031
+
4032
+
4033
+
4034
+
4035
+ </td>
4036
+
4037
+
4038
+ <td class="attributes">
4039
+
4040
+ &lt;optional><br>
4041
+
4042
+
4043
+
4044
+
4045
+
4046
+ </td>
4047
+
4048
+
4049
+
4050
+ <td class="default">
4051
+
4052
+ <code>{}</code>
4053
+
4054
+ </td>
4055
+
4056
+
4057
+ <td class="description last"><p>輸入設定物件,預設{}</p>
4058
+ <h6>Properties</h6>
4059
+
4060
+
4061
+ <table class="params">
4062
+ <thead>
4063
+ <tr>
4064
+
4065
+ <th>Name</th>
4066
+
4067
+
4068
+ <th>Type</th>
4069
+
4070
+
4071
+ <th>Attributes</th>
4072
+
4073
+
4074
+
4075
+ <th>Default</th>
4076
+
4077
+
4078
+ <th class="last">Description</th>
4079
+ </tr>
4080
+ </thead>
4081
+
4082
+ <tbody>
4083
+
4084
+
4085
+ <tr>
4086
+
4087
+ <td class="name"><code>withCalcOrderProfitOrLoss</code></td>
4088
+
4089
+
4090
+ <td class="type">
4091
+
4092
+
4093
+ <span class="param-type">Boolean</span>
4094
+
4095
+
4096
+
4097
+
4098
+ </td>
4099
+
4100
+
4101
+ <td class="attributes">
4102
+
4103
+ &lt;optional><br>
4104
+
4105
+
4106
+
4107
+
4108
+
4109
+ </td>
4110
+
4111
+
4112
+
4113
+ <td class="default">
4114
+
4115
+ <code>true</code>
4116
+
4117
+ </td>
4118
+
4119
+
4120
+ <td class="description last"><p>輸入是否經calcOrders結算訂單布林值,預設true</p></td>
4121
+ </tr>
4122
+
4123
+
4124
+
4125
+ <tr>
4126
+
4127
+ <td class="name"><code>withSummary</code></td>
4128
+
4129
+
4130
+ <td class="type">
4131
+
4132
+
4133
+ <span class="param-type">Boolean</span>
4134
+
4135
+
4136
+
4137
+
4138
+ </td>
4139
+
4140
+
4141
+ <td class="attributes">
4142
+
4143
+ &lt;optional><br>
4144
+
4145
+
4146
+
4147
+
4148
+
4149
+ </td>
4150
+
4151
+
4152
+
4153
+ <td class="default">
4154
+
4155
+ <code>true</code>
4156
+
4157
+ </td>
4158
+
4159
+
4160
+ <td class="description last"><p>輸入是否計算統計摘要布林值,預設true</p></td>
4161
+ </tr>
4162
+
4163
+
4164
+ </tbody>
4165
+ </table>
4166
+
4167
+ </td>
4168
+ </tr>
4169
+
4170
+
4171
+ </tbody>
4172
+ </table>
4173
+
4174
+
4175
+
4176
+
4177
+
4178
+
4179
+
4180
+
4181
+
4182
+
4183
+
4184
+
4185
+
4186
+
4187
+
4188
+
4189
+ <h5 class="h5-returns">Returns:</h5>
4190
+
4191
+
4192
+ <div class="param-desc">
4193
+ <p>回傳Promise,resolve為{orders,summary}物件</p>
4194
+ </div>
4195
+
4196
+
4197
+
4198
+ <dl class="param-type">
4199
+ <dt>
4200
+ Type
4201
+ </dt>
4202
+ <dd>
4203
+
4204
+ <span class="param-type">Promise</span>
4205
+
4206
+
4207
+
4208
+ </dd>
4209
+ </dl>
4210
+
4211
+
4212
+
4213
+
4214
+
4215
+
4216
+
4217
+
4218
+
4219
+
4220
+
4221
+ </article>
4222
+
4223
+ </section>
4224
+
4225
+
4226
+
4227
+
4228
+
4229
+
4230
+ </div>
4231
+
4232
+ <br class="clear">
4233
+
4234
+ <footer>
4235
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Sun Aug 02 2026 20:09:43 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
4236
+ </footer>
4237
+
4238
+ <script>prettyPrint();</script>
4239
+ <script src="scripts/polyfill.js"></script>
4240
+ <script src="scripts/linenumber.js"></script>
4241
+
4242
+
4243
+
4244
+ </body>
4245
+ </html>