w-trade-backtest 1.0.4 → 1.0.6
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.
- package/dist/w-trade-backtest.umd.js +2 -27
- package/dist/w-trade-backtest.umd.js.map +1 -1
- package/docs/buildKLinesTpSl.mjs.html +218 -0
- package/docs/calcOrders.mjs.html +2 -2
- package/docs/calcOrdersRatio.mjs.html +2 -2
- package/docs/calcOrdersSummary.mjs.html +2 -2
- package/docs/calcOrdersSummarySimple.mjs.html +2 -2
- package/docs/calcSummary.mjs.html +2 -2
- package/docs/closeAndSummaryOrders.mjs.html +7 -7
- package/docs/genKLinesTpSl.mjs.html +154 -0
- package/docs/genReport.mjs.html +34 -27
- package/docs/genReportCore.mjs.html +5 -5
- package/docs/global.html +1274 -165
- package/docs/index.html +2 -2
- package/docs/loadKLinesTpSl.mjs.html +189 -0
- package/docs/runStrategies.mjs.html +2 -2
- package/docs/runStrategiesAndBacktest.mjs.html +7 -7
- package/docs/runStrategy.mjs.html +2 -2
- package/package.json +2 -3
- package/src/WTradeBacktest.mjs +6 -0
- package/src/buildKLinesTpSl.mjs +146 -0
- package/src/closeAndSummaryOrders.mjs +5 -5
- package/src/genKLinesTpSl.mjs +82 -0
- package/src/genReport.mjs +32 -25
- package/src/genReportCore.mjs +3 -3
- package/src/loadKLinesTpSl.mjs +117 -0
- package/src/runStrategiesAndBacktest.mjs +5 -5
- package/test/unit-WTradeBacktest.test.mjs +23 -643
- package/test/unit-buildKLinesTpSl.test.mjs +98 -0
- package/test/unit-calcOrders.test.mjs +60 -0
- package/test/unit-calcOrdersRatio.test.mjs +39 -0
- package/test/unit-calcOrdersSummary.test.mjs +71 -0
- package/test/unit-calcOrdersSummarySimple.test.mjs +30 -0
- package/test/unit-calcSummary.test.mjs +30 -0
- package/test/unit-closeAndSummaryOrders.test.mjs +53 -0
- package/test/unit-genKLinesTpSl.test.mjs +46 -0
- package/test/unit-genReport.test.mjs +113 -0
- package/test/unit-genReportCore.test.mjs +39 -0
- package/test/unit-loadKLinesTpSl.test.mjs +158 -0
- package/test/unit-ott.test.mjs +20 -0
- package/test/unit-runStrategies.test.mjs +30 -0
- package/test/unit-runStrategiesAndBacktest.test.mjs +114 -0
- package/test/unit-runStrategy.test.mjs +53 -0
- package/test/unit-setup.mjs +120 -0
- package/src/readJson.mjs +0 -26
- package/src/writeJson.mjs +0 -31
- package/src/writeTxt.mjs +0 -19
package/docs/index.html
CHANGED
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
<nav >
|
|
30
30
|
|
|
31
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#runStrategiesAndBacktest">runStrategiesAndBacktest</a></li><li><a href="global.html#runStrategy">runStrategy</a></li></ul>
|
|
32
|
+
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#buildKLinesTpSl">buildKLinesTpSl</a></li><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#genKLinesTpSl">genKLinesTpSl</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#loadKLinesTpSl">loadKLinesTpSl</a></li><li><a href="global.html#runStrategies">runStrategies</a></li><li><a href="global.html#runStrategiesAndBacktest">runStrategiesAndBacktest</a></li><li><a href="global.html#runStrategy">runStrategy</a></li></ul>
|
|
33
33
|
|
|
34
34
|
</nav>
|
|
35
35
|
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
<br class="clear">
|
|
72
72
|
|
|
73
73
|
<footer>
|
|
74
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on
|
|
74
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Tue Aug 04 2026 23:16:05 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
75
75
|
</footer>
|
|
76
76
|
|
|
77
77
|
<script>prettyPrint();</script>
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
|
|
5
|
+
<meta charset="utf-8">
|
|
6
|
+
<title>loadKLinesTpSl.mjs - 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#buildKLinesTpSl">buildKLinesTpSl</a></li><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#genKLinesTpSl">genKLinesTpSl</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#loadKLinesTpSl">loadKLinesTpSl</a></li><li><a href="global.html#runStrategies">runStrategies</a></li><li><a href="global.html#runStrategiesAndBacktest">runStrategiesAndBacktest</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">loadKLinesTpSl.mjs</h1>
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
<section>
|
|
47
|
+
<article>
|
|
48
|
+
<pre class="prettyprint source linenums"><code>import path from 'path'
|
|
49
|
+
import fs from 'fs'
|
|
50
|
+
import dig from 'wsemi/src/dig.mjs'
|
|
51
|
+
import isestr from 'wsemi/src/isestr.mjs'
|
|
52
|
+
import fsIsFile from 'wsemi/src/fsIsFile.mjs'
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 載入止盈止損先觸結果快取檔並提供訂單重建
|
|
57
|
+
*
|
|
58
|
+
* 讀取buildKLinesTpSl產出之快取json,提供ordersFromBars依指定mode與tpsl組合與進場時間陣列重建訂單陣列
|
|
59
|
+
* 重建訂單之盈虧與累計欄位公式逐欄對齊calcOrders,可直接餵入calcOrdersSummary或calcOrdersSummarySimple
|
|
60
|
+
* fpJson非有效字串或非檔案時throw
|
|
61
|
+
*
|
|
62
|
+
* Unit Test: {@link https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs Github}
|
|
63
|
+
* @function
|
|
64
|
+
* @param {String} fpJson 輸入buildKLinesTpSl產出之快取json檔案路徑字串
|
|
65
|
+
* @returns {Object} 回傳物件,內含meta(快取設定物件)、bars(以進場時間為key之結果物件)與ordersFromBars(mode, tpsl, entryTimes, opt)訂單重建函數,函數之opt可給uTrade(每單金額,預設1)、uIni(初始資金,預設1000)、rFee(手續費率,預設0.0005)、maxExitTime(出場時間大於等於此值之單視為未平倉,預設null)與skipSort(呼叫端保證entryTimes已升序時可省排序,預設false),回傳訂單陣列,快取內查無之進場時間會被濾除,未結單各盈虧欄位留空
|
|
66
|
+
* @example
|
|
67
|
+
*
|
|
68
|
+
* //fpJson為buildKLinesTpSl產出之快取檔
|
|
69
|
+
* let r = loadKLinesTpSl('./cache/kltpsl.json')
|
|
70
|
+
* console.log(r.meta.nBars)
|
|
71
|
+
* // => 6
|
|
72
|
+
*
|
|
73
|
+
* let orders = r.ordersFromBars('long', 5, ['2020-01-01T00:00:00', '2020-01-01T12:00:00'], { uTrade: 100, uIni: 1000 })
|
|
74
|
+
* console.log(orders.map((o) => `${o.timeStart} ${o.timeEnd || 'unsettled'} ${o.modeResult || '-'}`))
|
|
75
|
+
* // => [
|
|
76
|
+
* // '2020-01-01T00:00:00 2020-01-01T04:00:00 profit',
|
|
77
|
+
* // '2020-01-01T12:00:00 unsettled -'
|
|
78
|
+
* // ]
|
|
79
|
+
* console.log(orders[0].priceStart, orders[0].priceEnd, orders[0].uProfitOrLoss, orders[0].uEquity)
|
|
80
|
+
* // => 100 105 4.9 1004.9
|
|
81
|
+
*
|
|
82
|
+
*/
|
|
83
|
+
let loadKLinesTpSl = (fpJson) => {
|
|
84
|
+
|
|
85
|
+
//check
|
|
86
|
+
if (!isestr(fpJson)) {
|
|
87
|
+
throw new Error(`invalid fpJson`)
|
|
88
|
+
}
|
|
89
|
+
if (!fsIsFile(fpJson)) {
|
|
90
|
+
throw new Error(`fpJson[${fpJson}] is not a file`)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let obj = JSON.parse(fs.readFileSync(path.resolve(fpJson), 'utf8'))
|
|
94
|
+
let meta = obj.meta
|
|
95
|
+
let bars = obj.bars
|
|
96
|
+
|
|
97
|
+
let ordersFromBars = (mode, tpsl, entryTimes, opt = {}) => {
|
|
98
|
+
let { uTrade = 1, uIni = 1000, rFee = 0.0005, maxExitTime = null, skipSort = false } = opt
|
|
99
|
+
let k = `${mode}_${tpsl}`
|
|
100
|
+
let tpr = tpsl / 100
|
|
101
|
+
let uFee = rFee * uTrade
|
|
102
|
+
|
|
103
|
+
//先建全部 order 物件(未結者欄位留空, 對齊 calcOrders 對未平倉單的處理)。
|
|
104
|
+
// skipSort: 呼叫端保證 entryTimes 已升序時省 [...].sort()(熱路徑省數千筆複製+排序); 預設 false 維持原行為。
|
|
105
|
+
let ets = skipSort ? entryTimes : [...entryTimes].sort()
|
|
106
|
+
let orders = ets.filter((t) => bars[t]).map((t) => {
|
|
107
|
+
let c0 = bars[t].close
|
|
108
|
+
return {
|
|
109
|
+
mode,
|
|
110
|
+
timeStart: t,
|
|
111
|
+
priceStart: c0,
|
|
112
|
+
uTrade,
|
|
113
|
+
rTakeProfit: tpr,
|
|
114
|
+
priceTakeProfit: mode === 'long' ? (1 + tpr) * c0 : (1 - tpr) * c0,
|
|
115
|
+
rStopLoss: tpr,
|
|
116
|
+
priceStopLoss: mode === 'long' ? (1 - tpr) * c0 : (1 + tpr) * c0,
|
|
117
|
+
timeEnd: '',
|
|
118
|
+
priceEnd: '',
|
|
119
|
+
modeResult: '',
|
|
120
|
+
rFee,
|
|
121
|
+
uFee,
|
|
122
|
+
uProfitOrLoss: '',
|
|
123
|
+
rProfitOrLoss: '',
|
|
124
|
+
uCumuProfitOrLoss: '',
|
|
125
|
+
rCumuProfitOrLoss: '',
|
|
126
|
+
uEquity: '',
|
|
127
|
+
uTradeAll: '',
|
|
128
|
+
rTradeAll: '',
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
//填已結單(公式對齊 d04 calcOrders)
|
|
133
|
+
let uEquity = uIni
|
|
134
|
+
let uCumu = 0
|
|
135
|
+
for (let o of orders) {
|
|
136
|
+
let r = bars[o.timeStart][k]
|
|
137
|
+
if (!r || r.win < 0 || !r.timeEnd) {
|
|
138
|
+
continue //未結 → 留空(計入 numTrade, 不計入已結績效)
|
|
139
|
+
}
|
|
140
|
+
if (maxExitTime && r.timeEnd >= maxExitTime) {
|
|
141
|
+
continue //區段內尚未觸 → 留未平倉
|
|
142
|
+
}
|
|
143
|
+
o.timeEnd = r.timeEnd
|
|
144
|
+
o.priceEnd = r.win === 1 ? o.priceTakeProfit : o.priceStopLoss
|
|
145
|
+
o.uProfitOrLoss = o.mode === 'long'
|
|
146
|
+
? o.uTrade * (o.priceEnd / o.priceStart) - o.uTrade - 2 * o.uFee
|
|
147
|
+
: (o.priceStart - o.priceEnd) * (o.uTrade / o.priceStart) - 2 * o.uFee
|
|
148
|
+
o.rProfitOrLoss = o.uProfitOrLoss / o.uTrade
|
|
149
|
+
uCumu += o.uProfitOrLoss
|
|
150
|
+
o.uCumuProfitOrLoss = uCumu
|
|
151
|
+
o.rCumuProfitOrLoss = dig((o.uCumuProfitOrLoss / uIni) * 100, 2) + '%'
|
|
152
|
+
uEquity += o.uProfitOrLoss
|
|
153
|
+
o.uEquity = uEquity
|
|
154
|
+
o.modeResult = o.uProfitOrLoss < 0 ? 'loss' : 'profit'
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return orders
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return { meta, bars, ordersFromBars }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
export default loadKLinesTpSl
|
|
165
|
+
</code></pre>
|
|
166
|
+
</article>
|
|
167
|
+
</section>
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
</div>
|
|
175
|
+
|
|
176
|
+
<br class="clear">
|
|
177
|
+
|
|
178
|
+
<footer>
|
|
179
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Tue Aug 04 2026 23:16:05 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
180
|
+
</footer>
|
|
181
|
+
|
|
182
|
+
<script>prettyPrint();</script>
|
|
183
|
+
<script src="scripts/polyfill.js"></script>
|
|
184
|
+
<script src="scripts/linenumber.js"></script>
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
</body>
|
|
189
|
+
</html>
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
<nav >
|
|
30
30
|
|
|
31
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#runStrategiesAndBacktest">runStrategiesAndBacktest</a></li><li><a href="global.html#runStrategy">runStrategy</a></li></ul>
|
|
32
|
+
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#buildKLinesTpSl">buildKLinesTpSl</a></li><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#genKLinesTpSl">genKLinesTpSl</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#loadKLinesTpSl">loadKLinesTpSl</a></li><li><a href="global.html#runStrategies">runStrategies</a></li><li><a href="global.html#runStrategiesAndBacktest">runStrategiesAndBacktest</a></li><li><a href="global.html#runStrategy">runStrategy</a></li></ul>
|
|
33
33
|
|
|
34
34
|
</nav>
|
|
35
35
|
|
|
@@ -231,7 +231,7 @@ export default runStrategies
|
|
|
231
231
|
<br class="clear">
|
|
232
232
|
|
|
233
233
|
<footer>
|
|
234
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on
|
|
234
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Tue Aug 04 2026 23:16:05 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
235
235
|
</footer>
|
|
236
236
|
|
|
237
237
|
<script>prettyPrint();</script>
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
<nav >
|
|
30
30
|
|
|
31
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#runStrategiesAndBacktest">runStrategiesAndBacktest</a></li><li><a href="global.html#runStrategy">runStrategy</a></li></ul>
|
|
32
|
+
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#buildKLinesTpSl">buildKLinesTpSl</a></li><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#genKLinesTpSl">genKLinesTpSl</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#loadKLinesTpSl">loadKLinesTpSl</a></li><li><a href="global.html#runStrategies">runStrategies</a></li><li><a href="global.html#runStrategiesAndBacktest">runStrategiesAndBacktest</a></li><li><a href="global.html#runStrategy">runStrategy</a></li></ul>
|
|
33
33
|
|
|
34
34
|
</nav>
|
|
35
35
|
|
|
@@ -50,10 +50,10 @@ import get from 'lodash-es/get.js'
|
|
|
50
50
|
import size from 'lodash-es/size.js'
|
|
51
51
|
import isearr from 'wsemi/src/isearr.mjs'
|
|
52
52
|
import fsIsFolder from 'wsemi/src/fsIsFolder.mjs'
|
|
53
|
+
import fsWriteJson from 'wsemi/src/fsWriteJson.mjs'
|
|
53
54
|
import WDataTdprovide from 'w-data-tdprovide/src/WDataTdprovide.mjs'
|
|
54
55
|
import runStrategies from './runStrategies.mjs'
|
|
55
56
|
import genReportCore from './genReportCore.mjs'
|
|
56
|
-
import writeJson from './writeJson.mjs'
|
|
57
57
|
|
|
58
58
|
|
|
59
59
|
/**
|
|
@@ -153,8 +153,8 @@ let runStrategiesAndBacktest = async (ott, fdOhlc, fdParam, strategies, timeStar
|
|
|
153
153
|
//fpOrders
|
|
154
154
|
let fpOrders = path.resolve(fdTest, fnOrders)
|
|
155
155
|
|
|
156
|
-
//
|
|
157
|
-
|
|
156
|
+
//fsWriteJson
|
|
157
|
+
fsWriteJson(fpOrders, r.orders, { useFormat: true })
|
|
158
158
|
|
|
159
159
|
}
|
|
160
160
|
|
|
@@ -173,8 +173,8 @@ let runStrategiesAndBacktest = async (ott, fdOhlc, fdParam, strategies, timeStar
|
|
|
173
173
|
}
|
|
174
174
|
// console.log('stsm', stsm)
|
|
175
175
|
|
|
176
|
-
//
|
|
177
|
-
|
|
176
|
+
//fsWriteJson
|
|
177
|
+
fsWriteJson(fpSummary, stsm, { useFormat: true })
|
|
178
178
|
|
|
179
179
|
console.log('summary', r.summary, size(r.orders))
|
|
180
180
|
}
|
|
@@ -211,7 +211,7 @@ export default runStrategiesAndBacktest
|
|
|
211
211
|
<br class="clear">
|
|
212
212
|
|
|
213
213
|
<footer>
|
|
214
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on
|
|
214
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Tue Aug 04 2026 23:16:05 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
215
215
|
</footer>
|
|
216
216
|
|
|
217
217
|
<script>prettyPrint();</script>
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
<nav >
|
|
30
30
|
|
|
31
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#runStrategiesAndBacktest">runStrategiesAndBacktest</a></li><li><a href="global.html#runStrategy">runStrategy</a></li></ul>
|
|
32
|
+
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#buildKLinesTpSl">buildKLinesTpSl</a></li><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#genKLinesTpSl">genKLinesTpSl</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#loadKLinesTpSl">loadKLinesTpSl</a></li><li><a href="global.html#runStrategies">runStrategies</a></li><li><a href="global.html#runStrategiesAndBacktest">runStrategiesAndBacktest</a></li><li><a href="global.html#runStrategy">runStrategy</a></li></ul>
|
|
33
33
|
|
|
34
34
|
</nav>
|
|
35
35
|
|
|
@@ -442,7 +442,7 @@ export default runStrategy
|
|
|
442
442
|
<br class="clear">
|
|
443
443
|
|
|
444
444
|
<footer>
|
|
445
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on
|
|
445
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Tue Aug 04 2026 23:16:05 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
446
446
|
</footer>
|
|
447
447
|
|
|
448
448
|
<script>prettyPrint();</script>
|
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "w-trade-backtest",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"main": "dist/w-trade-backtest.umd.js",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"dayjs": "^1.11.21",
|
|
7
6
|
"lodash-es": "^4.18.1",
|
|
8
|
-
"w-data-tdprovide": "^1.0.
|
|
7
|
+
"w-data-tdprovide": "^1.0.4",
|
|
9
8
|
"wsemi": "^1.8.67"
|
|
10
9
|
},
|
|
11
10
|
"devDependencies": {
|
package/src/WTradeBacktest.mjs
CHANGED
|
@@ -9,6 +9,9 @@ import calcSummary from './calcSummary.mjs'
|
|
|
9
9
|
import closeAndSummaryOrders from './closeAndSummaryOrders.mjs'
|
|
10
10
|
import genReport from './genReport.mjs'
|
|
11
11
|
import genReportCore from './genReportCore.mjs'
|
|
12
|
+
import genKLinesTpSl from './genKLinesTpSl.mjs'
|
|
13
|
+
import buildKLinesTpSl from './buildKLinesTpSl.mjs'
|
|
14
|
+
import loadKLinesTpSl from './loadKLinesTpSl.mjs'
|
|
12
15
|
|
|
13
16
|
|
|
14
17
|
let r = {
|
|
@@ -23,6 +26,9 @@ let r = {
|
|
|
23
26
|
closeAndSummaryOrders,
|
|
24
27
|
genReport,
|
|
25
28
|
genReportCore,
|
|
29
|
+
genKLinesTpSl,
|
|
30
|
+
buildKLinesTpSl,
|
|
31
|
+
loadKLinesTpSl,
|
|
26
32
|
}
|
|
27
33
|
|
|
28
34
|
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
import isestr from 'wsemi/src/isestr.mjs'
|
|
4
|
+
import istime from 'wsemi/src/istime.mjs'
|
|
5
|
+
import fsIsFolder from 'wsemi/src/fsIsFolder.mjs'
|
|
6
|
+
import fsCreateFolder from 'wsemi/src/fsCreateFolder.mjs'
|
|
7
|
+
import WDataTdprovide from 'w-data-tdprovide/src/WDataTdprovide.mjs'
|
|
8
|
+
import genKLinesTpSl from './genKLinesTpSl.mjs'
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 建置各K線與各止盈止損組合之先觸結果快取檔
|
|
13
|
+
*
|
|
14
|
+
* 以w-data-tdprovide讀取fdOhlc與fdParam資料夾內keyOhlc序列,取ts至te範圍K線後,逐一組合modes與tps呼叫genKLinesTpSl
|
|
15
|
+
* 各組合以`${mode}_${tpsl}`為key存入各根K線,win為1(止盈先觸)、0(止損先觸)或-1(至區間末仍未結)
|
|
16
|
+
* timeEnd於建置時由hold位移換算為絕對時間,故下游查表即得而不需位置索引,未結者為null
|
|
17
|
+
* 僅存win、timeEnd與各根close,盈虧由下游loadKLinesTpSl之ordersFromBars以close與手續費重算
|
|
18
|
+
* fdOhlc或fdParam非資料夾、fpJson或opt.keyOhlc非有效字串、opt.ts或opt.te非時間字串時throw
|
|
19
|
+
*
|
|
20
|
+
* Unit Test: {@link https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs Github}
|
|
21
|
+
* @function
|
|
22
|
+
* @param {String} fdOhlc 輸入儲存K線(ohlc)序列資料夾字串,各序列以`${key}.json`儲存
|
|
23
|
+
* @param {String} fdParam 輸入儲存指標參數序列資料夾字串
|
|
24
|
+
* @param {String} fpJson 輸入輸出快取json檔案路徑字串,資料夾不存在時自動建立
|
|
25
|
+
* @param {Object} [opt={}] 輸入設定物件,預設{}
|
|
26
|
+
* @param {String} opt.keyOhlc 輸入K線序列key字串
|
|
27
|
+
* @param {String} opt.ts 輸入起始秒時間字串
|
|
28
|
+
* @param {String} opt.te 輸入結束秒時間字串
|
|
29
|
+
* @param {Array} [opt.tps=[4,5,6,7,8,9,10]] 輸入止盈止損百分比數值陣列,如5代表止盈與止損皆5%,預設[4,5,6,7,8,9,10]
|
|
30
|
+
* @param {Array} [opt.modes=['long','short']] 輸入交易方向字串陣列,預設['long','short']
|
|
31
|
+
* @returns {Promise} 回傳Promise,resolve為{meta,bars}物件,meta內含keyOhlc、ts、te、tps、modes與nBars(K線根數),bars以進場時間為key,各值內含close與各`${mode}_${tpsl}`之{win,timeEnd},並同步寫出至fpJson
|
|
32
|
+
* @example
|
|
33
|
+
*
|
|
34
|
+
* import fs from 'fs'
|
|
35
|
+
*
|
|
36
|
+
* let arrOhlc = [
|
|
37
|
+
* { time: '2020-01-01T00:00:00', Open: 100, High: 101, Low: 99, Close: 100 },
|
|
38
|
+
* { time: '2020-01-01T04:00:00', Open: 100, High: 106, Low: 100, Close: 105 },
|
|
39
|
+
* { time: '2020-01-01T08:00:00', Open: 105, High: 107, Low: 102, Close: 103 },
|
|
40
|
+
* { time: '2020-01-01T12:00:00', Open: 103, High: 104, Low: 94, Close: 95 },
|
|
41
|
+
* { time: '2020-01-01T16:00:00', Open: 95, High: 98, Low: 92, Close: 93 },
|
|
42
|
+
* { time: '2020-01-01T20:00:00', Open: 93, High: 99, Low: 95, Close: 97 },
|
|
43
|
+
* ]
|
|
44
|
+
*
|
|
45
|
+
* //建立數據資料夾, fdOhlc放K線序列, fdParam放指標參數序列
|
|
46
|
+
* fs.mkdirSync('./data-ohlc', { recursive: true })
|
|
47
|
+
* fs.mkdirSync('./data-param', { recursive: true })
|
|
48
|
+
* fs.writeFileSync('./data-ohlc/btc.json', JSON.stringify(arrOhlc), 'utf8')
|
|
49
|
+
*
|
|
50
|
+
* let r = await buildKLinesTpSl('./data-ohlc', './data-param', './cache/kltpsl.json', {
|
|
51
|
+
* keyOhlc: 'btc',
|
|
52
|
+
* ts: '2020-01-01T00:00:00',
|
|
53
|
+
* te: '2020-01-01T20:00:00',
|
|
54
|
+
* tps: [5],
|
|
55
|
+
* modes: ['long'],
|
|
56
|
+
* })
|
|
57
|
+
* console.log(r.meta)
|
|
58
|
+
* // => {
|
|
59
|
+
* // keyOhlc: 'btc',
|
|
60
|
+
* // ts: '2020-01-01T00:00:00',
|
|
61
|
+
* // te: '2020-01-01T20:00:00',
|
|
62
|
+
* // tps: [ 5 ],
|
|
63
|
+
* // modes: [ 'long' ],
|
|
64
|
+
* // nBars: 6
|
|
65
|
+
* // }
|
|
66
|
+
* console.log(r.bars['2020-01-01T00:00:00'])
|
|
67
|
+
* // => { close: 100, long_5: { win: 1, timeEnd: '2020-01-01T04:00:00' } }
|
|
68
|
+
* console.log(r.bars['2020-01-01T12:00:00'])
|
|
69
|
+
* // => { close: 95, long_5: { win: -1, timeEnd: null } }
|
|
70
|
+
*
|
|
71
|
+
*/
|
|
72
|
+
let buildKLinesTpSl = async (fdOhlc, fdParam, fpJson, opt = {}) => {
|
|
73
|
+
//一次性全建(訓練區間固定, 不需增量); 若日後要增量延長: 已結entry不動、未結entry用新OHLC重掃、新棒加key即可
|
|
74
|
+
|
|
75
|
+
//check
|
|
76
|
+
if (!fsIsFolder(fdOhlc)) {
|
|
77
|
+
throw new Error(`fdOhlc[${fdOhlc}] is not a folder`)
|
|
78
|
+
}
|
|
79
|
+
if (!fsIsFolder(fdParam)) {
|
|
80
|
+
throw new Error(`fdParam[${fdParam}] is not a folder`)
|
|
81
|
+
}
|
|
82
|
+
if (!isestr(fpJson)) {
|
|
83
|
+
throw new Error(`invalid fpJson`)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let provideData = WDataTdprovide(fdOhlc, fdParam) //數據資料夾由呼叫端顯式指定
|
|
87
|
+
|
|
88
|
+
let {
|
|
89
|
+
keyOhlc,
|
|
90
|
+
ts,
|
|
91
|
+
te,
|
|
92
|
+
tps = [4, 5, 6, 7, 8, 9, 10],
|
|
93
|
+
modes = ['long', 'short'],
|
|
94
|
+
} = opt
|
|
95
|
+
|
|
96
|
+
//check
|
|
97
|
+
if (!isestr(keyOhlc)) {
|
|
98
|
+
throw new Error(`invalid opt.keyOhlc`)
|
|
99
|
+
}
|
|
100
|
+
if (!istime(ts)) {
|
|
101
|
+
throw new Error(`invalid opt.ts`)
|
|
102
|
+
}
|
|
103
|
+
if (!istime(te)) {
|
|
104
|
+
throw new Error(`invalid opt.te`)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//OHLC(訓練區間)
|
|
108
|
+
let arrOhlc = await provideData.getTimeSeriesByTimeRange(keyOhlc, ts, te)
|
|
109
|
+
let n = arrOhlc.length
|
|
110
|
+
let time = arrOhlc.map((r) => r.time)
|
|
111
|
+
|
|
112
|
+
//每根 close(各 mode_tpsl 共用)
|
|
113
|
+
let bars = {}
|
|
114
|
+
for (let i = 0; i < n; i++) {
|
|
115
|
+
bars[time[i]] = { close: arrOhlc[i].Close }
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
//各 (mode, tpsl) 跑 first-touch → 填 { win, timeEnd }
|
|
119
|
+
for (let mode of modes) {
|
|
120
|
+
for (let tpsl of tps) {
|
|
121
|
+
let { win, hold } = genKLinesTpSl(arrOhlc, { tp: tpsl / 100, sl: tpsl / 100, mode })
|
|
122
|
+
let k = `${mode}_${tpsl}`
|
|
123
|
+
for (let i = 0; i < n; i++) {
|
|
124
|
+
let wn = win[i]
|
|
125
|
+
let timeEnd = (wn >= 0 && Number.isFinite(hold[i])) ? time[i + hold[i]] : null
|
|
126
|
+
bars[time[i]][k] = { win: wn, timeEnd }
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
//out
|
|
132
|
+
let out = { meta: { keyOhlc, ts, te, tps, modes, nBars: n }, bars }
|
|
133
|
+
|
|
134
|
+
//寫檔
|
|
135
|
+
let jp = path.resolve(fpJson)
|
|
136
|
+
let fd = path.dirname(jp)
|
|
137
|
+
if (!fsIsFolder(fd)) {
|
|
138
|
+
fsCreateFolder(fd)
|
|
139
|
+
}
|
|
140
|
+
fs.writeFileSync(jp, JSON.stringify(out))
|
|
141
|
+
|
|
142
|
+
return out
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
export default buildKLinesTpSl
|
|
@@ -6,8 +6,8 @@ import isestr from 'wsemi/src/isestr.mjs'
|
|
|
6
6
|
import isarr from 'wsemi/src/isarr.mjs'
|
|
7
7
|
import cint from 'wsemi/src/cint.mjs'
|
|
8
8
|
import fsIsFolder from 'wsemi/src/fsIsFolder.mjs'
|
|
9
|
+
import fsWriteJson from 'wsemi/src/fsWriteJson.mjs'
|
|
9
10
|
import WDataTdprovide from 'w-data-tdprovide/src/WDataTdprovide.mjs'
|
|
10
|
-
import writeJson from './writeJson.mjs'
|
|
11
11
|
import calcOrders from './calcOrders.mjs'
|
|
12
12
|
import calcSummary from './calcSummary.mjs'
|
|
13
13
|
import genReportCore from './genReportCore.mjs'
|
|
@@ -148,8 +148,8 @@ let closeAndSummaryOrders = async (ott, fdOhlc, fdParam, uIni, timeOhlcStart, ti
|
|
|
148
148
|
//fpOrders
|
|
149
149
|
let fpOrders = path.resolve(fdTest, fnOrders)
|
|
150
150
|
|
|
151
|
-
//
|
|
152
|
-
|
|
151
|
+
//fsWriteJson
|
|
152
|
+
fsWriteJson(fpOrders, r.orders, { useFormat: true })
|
|
153
153
|
|
|
154
154
|
}
|
|
155
155
|
|
|
@@ -168,8 +168,8 @@ let closeAndSummaryOrders = async (ott, fdOhlc, fdParam, uIni, timeOhlcStart, ti
|
|
|
168
168
|
}
|
|
169
169
|
// console.log('stsm', stsm)
|
|
170
170
|
|
|
171
|
-
//
|
|
172
|
-
|
|
171
|
+
//fsWriteJson
|
|
172
|
+
fsWriteJson(fpSummary, stsm, { useFormat: true })
|
|
173
173
|
|
|
174
174
|
console.log('summary', r.summary, size(r.orders))
|
|
175
175
|
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 計算各K線進場後之止盈止損先觸結果
|
|
3
|
+
*
|
|
4
|
+
* 以各根K線收盤價為進場價,往後逐根K線掃描first-touch,long以Low<=進場價*(1-sl)判止損、High>=進場價*(1+tp)判止盈
|
|
5
|
+
* short以High>=進場價*(1+sl)判止損、Low<=進場價*(1-tp)判止盈,同根同時觸發時皆先判止損
|
|
6
|
+
* 末根K線與掃描至序列末仍未觸發者視為未結,win為-1且pnl與hold為NaN
|
|
7
|
+
*
|
|
8
|
+
* Unit Test: {@link https://github.com/yuda-lyu/w-trade-backtest/blob/master/test/unit-WTradeBacktest.test.mjs Github}
|
|
9
|
+
* @function
|
|
10
|
+
* @param {Array} arrOhlc 輸入K線陣列,各元素需含time(秒時間字串)、Close、High、Low欄位,且依時間由小至大排序
|
|
11
|
+
* @param {Object} [opt={}] 輸入設定物件,預設{}
|
|
12
|
+
* @param {Number} opt.tp 輸入止盈比例正數,如0.05代表5%
|
|
13
|
+
* @param {Number} opt.sl 輸入止損比例正數,如0.05代表5%
|
|
14
|
+
* @param {String} [opt.mode='long'] 輸入交易方向字串,可為'long'或'short',預設'long'
|
|
15
|
+
* @returns {Object} 回傳物件,內含time(時間字串陣列)、pnl(Float64Array,止盈為tp、止損為-sl、未結為NaN)、win(Int8Array,1為止盈先觸、0為止損先觸、-1為未結)、hold(Float64Array,出場K線與進場K線之位移根數,未結為NaN)欄位,各陣列長度同arrOhlc且index對應
|
|
16
|
+
* @example
|
|
17
|
+
*
|
|
18
|
+
* let arrOhlc = [
|
|
19
|
+
* { time: '2020-01-01T00:00:00', Open: 100, High: 101, Low: 99, Close: 100 },
|
|
20
|
+
* { time: '2020-01-01T04:00:00', Open: 100, High: 106, Low: 100, Close: 105 },
|
|
21
|
+
* { time: '2020-01-01T08:00:00', Open: 105, High: 107, Low: 102, Close: 103 },
|
|
22
|
+
* { time: '2020-01-01T12:00:00', Open: 103, High: 104, Low: 94, Close: 95 },
|
|
23
|
+
* { time: '2020-01-01T16:00:00', Open: 95, High: 98, Low: 92, Close: 93 },
|
|
24
|
+
* { time: '2020-01-01T20:00:00', Open: 93, High: 99, Low: 95, Close: 97 },
|
|
25
|
+
* ]
|
|
26
|
+
*
|
|
27
|
+
* let r = genKLinesTpSl(arrOhlc, { tp: 0.05, sl: 0.05, mode: 'long' })
|
|
28
|
+
* console.log(Array.from(r.win))
|
|
29
|
+
* // => [ 1, 0, 0, -1, 1, -1 ]
|
|
30
|
+
* console.log(Array.from(r.hold))
|
|
31
|
+
* // => [ 1, 2, 1, NaN, 1, NaN ]
|
|
32
|
+
* console.log(Array.from(r.pnl))
|
|
33
|
+
* // => [ 0.05, -0.05, -0.05, NaN, 0.05, NaN ]
|
|
34
|
+
*
|
|
35
|
+
*/
|
|
36
|
+
let genKLinesTpSl = (arrOhlc, opt = {}) => {
|
|
37
|
+
|
|
38
|
+
let tp = opt.tp; let sl = opt.sl; let mode = opt.mode || 'long'
|
|
39
|
+
if (!(tp > 0) || !(sl > 0)) throw new Error(`genKLinesTpSl: invalid tp[${tp}] / sl[${sl}]`)
|
|
40
|
+
|
|
41
|
+
let n = arrOhlc.length
|
|
42
|
+
let time = new Array(n)
|
|
43
|
+
let C = new Float64Array(n); let H = new Float64Array(n); let L = new Float64Array(n)
|
|
44
|
+
for (let i = 0; i < n; i++) {
|
|
45
|
+
let r = arrOhlc[i]; time[i] = r.time; C[i] = r.Close; H[i] = r.High; L[i] = r.Low
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let pnl = new Float64Array(n).fill(NaN)
|
|
49
|
+
let win = new Int8Array(n).fill(-1)
|
|
50
|
+
let hold = new Float64Array(n).fill(NaN)
|
|
51
|
+
|
|
52
|
+
for (let i = 0; i < n - 1; i++) {
|
|
53
|
+
let e = C[i]; if (!(e > 0)) continue
|
|
54
|
+
let up = e * (1 + tp); let dn = e * (1 - sl) //長: up=止盈線, dn=止損線
|
|
55
|
+
for (let j = i + 1; j < n; j++) {
|
|
56
|
+
if (mode === 'long') {
|
|
57
|
+
//長: 跌破 dn (SL) 或 漲到 up (TP); 同根同觸 → 先判 SL
|
|
58
|
+
if (L[j] <= dn) {
|
|
59
|
+
pnl[i] = -sl; win[i] = 0; hold[i] = j - i; break
|
|
60
|
+
}
|
|
61
|
+
if (H[j] >= up) {
|
|
62
|
+
pnl[i] = tp; win[i] = 1; hold[i] = j - i; break
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
//空: 漲到 e*(1+sl) (SL) 或 跌到 e*(1-tp) (TP); 同根同觸 → 先判 SL
|
|
67
|
+
let upS = e * (1 + sl); let dnS = e * (1 - tp)
|
|
68
|
+
if (H[j] >= upS) {
|
|
69
|
+
pnl[i] = -sl; win[i] = 0; hold[i] = j - i; break
|
|
70
|
+
}
|
|
71
|
+
if (L[j] <= dnS) {
|
|
72
|
+
pnl[i] = tp; win[i] = 1; hold[i] = j - i; break
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return { time, pnl, win, hold }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
export default genKLinesTpSl
|