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/genReport.mjs.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
|
|
|
@@ -46,12 +46,19 @@
|
|
|
46
46
|
<section>
|
|
47
47
|
<article>
|
|
48
48
|
<pre class="prettyprint source linenums"><code>import path from 'path'
|
|
49
|
-
import
|
|
50
|
-
import
|
|
49
|
+
import find from 'lodash-es/find.js'
|
|
50
|
+
import get from 'lodash-es/get.js'
|
|
51
|
+
import map from 'lodash-es/map.js'
|
|
52
|
+
import max from 'lodash-es/max.js'
|
|
53
|
+
import min from 'lodash-es/min.js'
|
|
54
|
+
import isearr from 'wsemi/src/isearr.mjs'
|
|
55
|
+
import isestr from 'wsemi/src/isestr.mjs'
|
|
56
|
+
import isnum from 'wsemi/src/isnum.mjs'
|
|
57
|
+
import fsIsFile from 'wsemi/src/fsIsFile.mjs'
|
|
58
|
+
import fsReadJson from 'wsemi/src/fsReadJson.mjs'
|
|
59
|
+
import fsWriteJson from 'wsemi/src/fsWriteJson.mjs'
|
|
51
60
|
import calcSummary from './calcSummary.mjs'
|
|
52
61
|
import genReportCore from './genReportCore.mjs'
|
|
53
|
-
import readJson from './readJson.mjs'
|
|
54
|
-
import writeJson from './writeJson.mjs'
|
|
55
62
|
|
|
56
63
|
|
|
57
64
|
/**
|
|
@@ -108,57 +115,57 @@ import writeJson from './writeJson.mjs'
|
|
|
108
115
|
let genReport = async (ott, fpOrders, opt = {}) => {
|
|
109
116
|
|
|
110
117
|
//check
|
|
111
|
-
if (!
|
|
118
|
+
if (!isestr(fpOrders)) {
|
|
112
119
|
throw new Error(`invalid fpOrders`)
|
|
113
120
|
}
|
|
114
|
-
if (!
|
|
121
|
+
if (!fsIsFile(fpOrders)) {
|
|
115
122
|
throw new Error(`fpOrders[${fpOrders}] is not a file`)
|
|
116
123
|
}
|
|
117
124
|
|
|
118
|
-
//orders
|
|
119
|
-
let orders =
|
|
120
|
-
if (!
|
|
125
|
+
//orders, fsReadJson回傳{success}或{error}, 讀取或解析失敗時取不到success
|
|
126
|
+
let orders = get(fsReadJson(fpOrders), 'success', null)
|
|
127
|
+
if (!isearr(orders)) {
|
|
121
128
|
throw new Error(`orders in fpOrders[${fpOrders}] is not an effective array`)
|
|
122
129
|
}
|
|
123
130
|
|
|
124
131
|
//fpOut
|
|
125
|
-
let fpOut =
|
|
126
|
-
if (!
|
|
132
|
+
let fpOut = get(opt, 'fpOut', '')
|
|
133
|
+
if (!isestr(fpOut)) {
|
|
127
134
|
fpOut = path.resolve(path.dirname(fpOrders), 'report.html')
|
|
128
135
|
}
|
|
129
136
|
|
|
130
137
|
//name
|
|
131
|
-
let name =
|
|
132
|
-
if (!
|
|
138
|
+
let name = get(opt, 'name', '')
|
|
139
|
+
if (!isestr(name)) {
|
|
133
140
|
name = path.basename(path.dirname(path.resolve(fpOrders)))
|
|
134
141
|
}
|
|
135
142
|
|
|
136
143
|
//uIni, 未給則由首筆已結算單推回(calcOrders 定義 uEquity = uIni + uCumuProfitOrLoss)
|
|
137
|
-
let uIni =
|
|
138
|
-
if (!
|
|
139
|
-
let o =
|
|
144
|
+
let uIni = get(opt, 'uIni', null)
|
|
145
|
+
if (!isnum(uIni)) {
|
|
146
|
+
let o = find(orders, (o) => isnum(o.uEquity) && isnum(o.uCumuProfitOrLoss))
|
|
140
147
|
uIni = o ? o.uEquity - o.uCumuProfitOrLoss : 1000
|
|
141
148
|
}
|
|
142
149
|
|
|
143
150
|
//timeOhlcStart, timeOhlcEnd, 未給則由訂單起訖推得
|
|
144
|
-
let timeOhlcStart =
|
|
145
|
-
if (!
|
|
146
|
-
timeOhlcStart =
|
|
151
|
+
let timeOhlcStart = get(opt, 'timeOhlcStart', '')
|
|
152
|
+
if (!isestr(timeOhlcStart)) {
|
|
153
|
+
timeOhlcStart = min(map(orders, 'timeStart'))
|
|
147
154
|
}
|
|
148
|
-
let timeOhlcEnd =
|
|
149
|
-
if (!
|
|
150
|
-
timeOhlcEnd =
|
|
155
|
+
let timeOhlcEnd = get(opt, 'timeOhlcEnd', '')
|
|
156
|
+
if (!isestr(timeOhlcEnd)) {
|
|
157
|
+
timeOhlcEnd = max(map(orders, (o) => isestr(o.timeEnd) ? o.timeEnd : o.timeStart))
|
|
151
158
|
}
|
|
152
159
|
|
|
153
160
|
//withWriteSummary
|
|
154
|
-
let withWriteSummary =
|
|
161
|
+
let withWriteSummary = get(opt, 'withWriteSummary', false)
|
|
155
162
|
|
|
156
163
|
//calcSummary
|
|
157
164
|
let summary = await calcSummary(ott, uIni, orders, timeOhlcStart, timeOhlcEnd)
|
|
158
165
|
|
|
159
|
-
//
|
|
166
|
+
//fsWriteJson
|
|
160
167
|
if (withWriteSummary) {
|
|
161
|
-
|
|
168
|
+
fsWriteJson(path.resolve(path.dirname(fpOut), 'summary.json'), { name, summary }, { useFormat: true })
|
|
162
169
|
}
|
|
163
170
|
|
|
164
171
|
//genReportCore
|
|
@@ -183,7 +190,7 @@ export default genReport
|
|
|
183
190
|
<br class="clear">
|
|
184
191
|
|
|
185
192
|
<footer>
|
|
186
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on
|
|
193
|
+
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.
|
|
187
194
|
</footer>
|
|
188
195
|
|
|
189
196
|
<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
|
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
<pre class="prettyprint source linenums"><code>import path from 'path'
|
|
49
49
|
import fs from 'fs'
|
|
50
50
|
import { fileURLToPath } from 'url'
|
|
51
|
-
import
|
|
51
|
+
import fsWriteText from 'wsemi/src/fsWriteText.mjs'
|
|
52
52
|
|
|
53
53
|
|
|
54
54
|
//模板資產(tmp.html, render*.js)與本模組同資料夾, 以模組位置解析, 不依賴 cwd
|
|
@@ -94,8 +94,8 @@ let genReportCore = (r, fpOut) => {
|
|
|
94
94
|
h = h.replace(`{orders}`, JSON.stringify(r.orders))
|
|
95
95
|
h = h.replace(`{summary}`, JSON.stringify(r.summary))
|
|
96
96
|
|
|
97
|
-
//
|
|
98
|
-
|
|
97
|
+
//fsWriteText
|
|
98
|
+
fsWriteText(fpOut, h)
|
|
99
99
|
|
|
100
100
|
}
|
|
101
101
|
|
|
@@ -115,7 +115,7 @@ export default genReportCore
|
|
|
115
115
|
<br class="clear">
|
|
116
116
|
|
|
117
117
|
<footer>
|
|
118
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on
|
|
118
|
+
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.
|
|
119
119
|
</footer>
|
|
120
120
|
|
|
121
121
|
<script>prettyPrint();</script>
|