w-trade-solve 1.0.5 → 2.0.2
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/README.md +3 -2
- package/dist/w-trade-solve.umd.js +4 -4
- package/dist/w-trade-solve.umd.js.map +1 -1
- package/docs/calcFitness.mjs.html +1 -1
- package/docs/calcLevelNumTrade.mjs.html +1 -1
- package/docs/cont.mjs.html +2 -2
- package/docs/estimComps.mjs.html +1 -1
- package/docs/estimFinder.mjs.html +1 -1
- package/docs/estimKeys.mjs.html +145 -77
- package/docs/estimPicks.mjs.html +1 -1
- package/docs/genSid.mjs.html +1 -1
- package/docs/genStrategyFileName.mjs.html +14 -10
- package/docs/genTid.mjs.html +1 -1
- package/docs/global.html +192 -22
- package/docs/index.html +1 -1
- package/docs/ott.mjs.html +1 -1
- package/docs/p2r.mjs.html +1 -1
- package/docs/readStrategies.mjs.html +127 -43
- package/g.mjs +3 -2
- package/package.json +2 -2
- package/src/cont.mjs +1 -1
- package/src/estimKeys.mjs +144 -76
- package/src/genStrategyFileName.mjs +12 -8
- package/src/readStrategies.mjs +126 -42
- package/test/unit-estimKeys.test.mjs +222 -5
- package/test/unit-genStrategyFileName.test.mjs +56 -17
- package/test/unit-readStrategies.test.mjs +189 -53
package/src/estimKeys.mjs
CHANGED
|
@@ -7,13 +7,15 @@ import size from 'lodash-es/size.js'
|
|
|
7
7
|
import cint from 'wsemi/src/cint.mjs'
|
|
8
8
|
import dig from 'wsemi/src/dig.mjs'
|
|
9
9
|
import fsCreateFolder from 'wsemi/src/fsCreateFolder.mjs'
|
|
10
|
-
import
|
|
10
|
+
import fsIsFile from 'wsemi/src/fsIsFile.mjs'
|
|
11
11
|
import fsIsFolder from 'wsemi/src/fsIsFolder.mjs'
|
|
12
|
+
import fsReadJson from 'wsemi/src/fsReadJson.mjs'
|
|
12
13
|
import fsWriteJson from 'wsemi/src/fsWriteJson.mjs'
|
|
13
|
-
import haskey from 'wsemi/src/haskey.mjs'
|
|
14
14
|
import isearr from 'wsemi/src/isearr.mjs'
|
|
15
15
|
import isestr from 'wsemi/src/isestr.mjs'
|
|
16
16
|
import isnum from 'wsemi/src/isnum.mjs'
|
|
17
|
+
import isbol from 'wsemi/src/isbol.mjs'
|
|
18
|
+
import isfun from 'wsemi/src/isfun.mjs'
|
|
17
19
|
import rang from 'wsemi/src/rang.mjs'
|
|
18
20
|
import wround from 'wsemi/src/round.mjs'
|
|
19
21
|
import omlRGA from 'w-optimization/src/omlRGA.mjs'
|
|
@@ -23,7 +25,6 @@ import omlPSO from 'w-optimization/src/omlPSO.mjs'
|
|
|
23
25
|
import omlACO from 'w-optimization/src/omlACO.mjs'
|
|
24
26
|
import dataProvide from 'w-data-tdprovide'
|
|
25
27
|
import runStrategy from 'w-trade-backtest/src/runStrategy.mjs'
|
|
26
|
-
import readStrategies from './readStrategies.mjs'
|
|
27
28
|
import genTid from './genTid.mjs'
|
|
28
29
|
import genSid from './genSid.mjs'
|
|
29
30
|
import p2r from './p2r.mjs'
|
|
@@ -37,8 +38,9 @@ import genStrategyFileName from './genStrategyFileName.mjs'
|
|
|
37
38
|
*
|
|
38
39
|
* 設計變數為[止損比例, 止盈比例, ...各key之門檻],止損與止盈由opt.thsSl與opt.thsTp給予之百分比清單離散取值
|
|
39
40
|
* 各key門檻取值範圍為[-thLim,thLim]切為thLim*20段,並各自平移+vdir與-vdir(vdir=thLim*5)為兩組,使解值恆大於0代表'>'條件、恆小於0代表'<'條件,還原時再扣除平移值,故單一設計變數即同時涵蓋大於與小於兩種條件
|
|
40
|
-
* 以w-optimization之最佳化函數求解,由opt.methodOml指定演算法,各次計算皆以w-trade-backtest之runStrategy回測並經calcFitness
|
|
41
|
-
* 各次計算結果若同時滿足交易次數、勝率與最終等效年化盈虧三門檻,則以genStrategyFileName產生檔名存入fdData
|
|
41
|
+
* 以w-optimization之最佳化函數求解,由opt.methodOml指定演算法,各次計算皆以w-trade-backtest之runStrategy回測並經calcFitness計算適應值;亦可由opt.funRunStrategyCustom注入自訂評估核心加速求解(如以w-trade-backtest之buildStrategyFastSession預算first-touch表與因子對位序列),注入僅回傳精簡欄位之核心時可配合opt.useRunStrategyBeforeSave於存檔前重跑完整回測,使落地summary維持完整欄位口徑;存檔前重跑後檔內numTrade、levelNumTrade、tkid、fitness與檔名皆由重跑之summary重新衍生,確保檔名與檔案內容同源一致;重算檔名與原檔名不同(評估核心與重跑結果跨級距)時,對新檔名重做存在檢查、身分驗證與優劣比較,新檔名已有更佳者則放棄本次寫入
|
|
42
|
+
* 各次計算結果若同時滿足交易次數、勝率與最終等效年化盈虧三門檻,則以genStrategyFileName產生檔名存入fdData;檔名為tkid(tid+交易次數級距)之純函數,故直接組出檔名查詢既有策略(O(1),不列舉全庫),同tkid已有策略時僅於最終等效年化盈虧更佳時原地覆寫,故同tkid恆僅一檔;覆寫前驗證舊檔內tkid、name與interval與本次一致,不一致代表雜湊碰撞,直接throw以避免無聲覆寫
|
|
43
|
+
* 策略檔內容除策略本體(tid、sid、name、symbol、interval、keyOhlc、mode、conds、settings)與summary、fitness外,另落地tkid、numTrade、levelNumTrade、keys與solve區塊(timeStart、timeEnd、methodOml、thNumTrade、thRWin、thREquivalentCumuProfitOrLossFinalNormYear、thsTp、thsSl、timeSolved),使識別與求解條件欄位自足,不依賴檔名
|
|
42
44
|
* opt除下列各欄位外,其餘欄位皆原樣傳遞至所選之最佳化函數,兩者欄位名稱無重疊,故可一併給予求解設定
|
|
43
45
|
* 各演算法共通之設定為NContiguous(最佳解連續未更新次數上限,預設100)、NRepeat、NCore、ModeOutLimit、UseRepeat、UseImmigration、LocalSearchMethod與funGetBetter、funGenerationBefore、funGenerationAfter三接口;族群數與迴圈數之欄位名稱各演算法不同,RGA與DE為Np與Ng、PSO為Np與Nl、HS為Ns與Ni、ACO為Na與Nl;另各演算法有其專屬超參數,如PSO之psoC1Start、RGA之rgaCrossover、DE之deF、HS之hsHMCR與ACO之acoAlphaStart等
|
|
44
46
|
* 預設之求解量甚大(單一key約需1萬7千次回測),僅需快速試算時可調小族群數與NContiguous
|
|
@@ -46,7 +48,7 @@ import genStrategyFileName from './genStrategyFileName.mjs'
|
|
|
46
48
|
*
|
|
47
49
|
* Unit Test: {@link https://github.com/yuda-lyu/w-trade-solve/blob/master/test/unit-estimKeys.test.mjs Github}
|
|
48
50
|
* @function
|
|
49
|
-
* @param {Function} ott 輸入時區時間函數,傳入時間字串回傳dayjs
|
|
51
|
+
* @param {Function} ott 輸入時區時間函數,傳入時間字串回傳dayjs時間物件,未傳入時回傳當下時間(供solve.timeSolved記錄),可用src/ott.mjs
|
|
50
52
|
* @param {String} name 輸入幣種名稱字串,例如'btc',用於組出K線序列key(`${name}_price_${interval}`)與縮寫tid
|
|
51
53
|
* @param {String} symbol 輸入交易對名稱字串,例如'BTCUSDT',僅儲存至策略內供辨識
|
|
52
54
|
* @param {String} interval 輸入K線週期字串,例如'4hr'
|
|
@@ -64,6 +66,9 @@ import genStrategyFileName from './genStrategyFileName.mjs'
|
|
|
64
66
|
* @param {Number} [opt.thNumTrade=1] 輸入策略須儲存之最少交易次數數值,預設1
|
|
65
67
|
* @param {Number} [opt.thRWin=0.5] 輸入策略須儲存之最低勝率比例數值,預設0.5
|
|
66
68
|
* @param {Number} [opt.thREquivalentCumuProfitOrLossFinalNormYear=0.15] 輸入策略須儲存之最低最終等效年化盈虧比例數值,預設0.15
|
|
69
|
+
* @param {Function} [opt.funRunStrategyCustom=null] 輸入自訂回測評估async函數,傳入{ott,strategy,funGetSeries}物件,須回傳{summary}且summary至少含numTrade、rWin、uEquityFinal與rEquivalentCumuProfitOrLossFinalNormYear四欄,各欄語義須與runStrategy等值,回傳之準確性由注入端負責;未給時採runStrategy,預設null
|
|
70
|
+
* @param {Boolean} [opt.useRunStrategyBeforeSave=false] 輸入存檔前是否重跑一次回測並以其summary重新衍生檔案內容與檔名布林值,注入僅回傳精簡欄位之評估核心時應設true以維持策略檔欄位口徑,求解搜索仍全程使用自訂函數不受影響,預設false
|
|
71
|
+
* @param {Function} [opt.funRunStrategyCustomBeforeSave=null] 輸入存檔前重跑用之自訂回測async函數,介面同opt.funRunStrategyCustom,僅於opt.useRunStrategyBeforeSave為true時生效,未給時採runStrategy,預設null
|
|
67
72
|
* @param {Boolean} [opt.useShowLog=false] 輸入是否顯示求解過程log布林值,預設false
|
|
68
73
|
* @returns {Promise} 回傳Promise,resolve為求解結果物件,含bestSolution(最佳解,內有ps設計變數陣列與fitness適應值)、stopMode(觸發停止之機制字串)、stopIteration(停止時之迴圈數)與stopExecutions(停止時之核心分析次數)等欄位
|
|
69
74
|
* @example
|
|
@@ -129,8 +134,8 @@ let estimKeys = async (ott, name, symbol, interval, fdOhlc, fdParam, timeStart,
|
|
|
129
134
|
}
|
|
130
135
|
|
|
131
136
|
let methodOml = get(opt, 'methodOml', '')
|
|
132
|
-
if (
|
|
133
|
-
methodOml = 'PSO'
|
|
137
|
+
if (methodOml !== 'RGA' && methodOml !== 'DE' && methodOml !== 'HS' && methodOml !== 'PSO' && methodOml !== 'ACO') {
|
|
138
|
+
methodOml = 'PSO' //非五者之一時採PSO, 使solve.methodOml記錄實際採用之演算法
|
|
134
139
|
}
|
|
135
140
|
let thsTp = get(opt, 'thsTp', null)
|
|
136
141
|
if (!isearr(thsTp)) {
|
|
@@ -152,6 +157,12 @@ let estimKeys = async (ott, name, symbol, interval, fdOhlc, fdParam, timeStart,
|
|
|
152
157
|
if (!isnum(thREquivalentCumuProfitOrLossFinalNormYear)) {
|
|
153
158
|
thREquivalentCumuProfitOrLossFinalNormYear = 0.15
|
|
154
159
|
}
|
|
160
|
+
let funRunStrategyCustom = get(opt, 'funRunStrategyCustom', null)
|
|
161
|
+
let useRunStrategyBeforeSave = get(opt, 'useRunStrategyBeforeSave', null)
|
|
162
|
+
if (!isbol(useRunStrategyBeforeSave)) {
|
|
163
|
+
useRunStrategyBeforeSave = false
|
|
164
|
+
}
|
|
165
|
+
let funRunStrategyCustomBeforeSave = get(opt, 'funRunStrategyCustomBeforeSave', null)
|
|
155
166
|
let useShowLog = get(opt, 'useShowLog', null)
|
|
156
167
|
|
|
157
168
|
//provideData
|
|
@@ -254,6 +265,50 @@ let estimKeys = async (ott, name, symbol, interval, fdOhlc, fdParam, timeStart,
|
|
|
254
265
|
|
|
255
266
|
}
|
|
256
267
|
|
|
268
|
+
//checkSavable, 檢查指定檔名處是否可寫入策略: 無檔案或舊檔非合法json回傳true(直接寫入);
|
|
269
|
+
// 舊檔身分(tkid/name/interval)與本次不一致代表雜湊碰撞, 直接throw以避免無聲覆寫;
|
|
270
|
+
// 新等效年化盈虧較佳時回傳true(原地覆寫), 否則回傳false(保留既有較佳者)
|
|
271
|
+
let checkSavable = (fpStrategy, fnStrategy, tkid, rEquivalentCumuProfitOrLossFinalNormYearNew) => {
|
|
272
|
+
|
|
273
|
+
//check, 無同檔名策略, 新出現須儲存
|
|
274
|
+
if (!fsIsFile(fpStrategy)) {
|
|
275
|
+
return true
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
//dataOld
|
|
279
|
+
let dOld = fsReadJson(fpStrategy) //回傳{success}或{error}, 讀取或解析失敗時為null
|
|
280
|
+
let dataOld = get(dOld, 'success', null)
|
|
281
|
+
|
|
282
|
+
//check, 舊檔非合法json, 直接覆寫
|
|
283
|
+
if (dataOld === null) {
|
|
284
|
+
return true
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
//覆寫前驗證舊檔身分與本次一致, 不一致代表雜湊碰撞, 直接throw以避免無聲覆寫
|
|
288
|
+
let tkidOld = get(dataOld, 'tkid', '')
|
|
289
|
+
let nameOld = get(dataOld, 'name', '')
|
|
290
|
+
let intervalOld = get(dataOld, 'interval', '')
|
|
291
|
+
if ((isestr(tkidOld) && tkidOld !== tkid) || (isestr(nameOld) && nameOld !== name) || (isestr(intervalOld) && intervalOld !== interval)) {
|
|
292
|
+
throw new Error(`hash collision: fnStrategy[${fnStrategy}], tkidOld[${tkidOld}] vs tkid[${tkid}], nameOld[${nameOld}] vs name[${name}], intervalOld[${intervalOld}] vs interval[${interval}]`)
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
//rEquivalentCumuProfitOrLossFinalNormYearOld
|
|
296
|
+
let rEquivalentCumuProfitOrLossFinalNormYearOld = p2r(get(dataOld, 'summary.rEquivalentCumuProfitOrLossFinalNormYear', ''))
|
|
297
|
+
|
|
298
|
+
//check, 出現更好的等效盈虧比例, 原地覆寫
|
|
299
|
+
if (rEquivalentCumuProfitOrLossFinalNormYearNew > rEquivalentCumuProfitOrLossFinalNormYearOld) {
|
|
300
|
+
|
|
301
|
+
if (useShowLog) {
|
|
302
|
+
console.log(`覆寫較差策略...`, `${rEquivalentCumuProfitOrLossFinalNormYearNew} > ${rEquivalentCumuProfitOrLossFinalNormYearOld}`, fpStrategy)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return true
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
//新等效盈虧未較佳, 不寫入
|
|
309
|
+
return false
|
|
310
|
+
}
|
|
311
|
+
|
|
257
312
|
//ifun, fitnessMin
|
|
258
313
|
let ifun = 0
|
|
259
314
|
let fitnessMin = 1e20
|
|
@@ -342,41 +397,69 @@ let estimKeys = async (ott, name, symbol, interval, fdOhlc, fdParam, timeStart,
|
|
|
342
397
|
}
|
|
343
398
|
|
|
344
399
|
//runStrategy
|
|
345
|
-
let r =
|
|
400
|
+
let r = null
|
|
401
|
+
if (isfun(funRunStrategyCustom)) {
|
|
402
|
+
r = await funRunStrategyCustom({ ott, strategy, funGetSeries })
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
r = await runStrategy(ott, strategy, funGetSeries)
|
|
406
|
+
}
|
|
346
407
|
// console.log(ifun, 'summary', r.summary)
|
|
347
408
|
|
|
348
|
-
//
|
|
349
|
-
let
|
|
409
|
+
//rEquivalentCumuProfitOrLossFinalNormYear
|
|
410
|
+
let rEquivalentCumuProfitOrLossFinalNormYear = p2r(r.summary.rEquivalentCumuProfitOrLossFinalNormYear)
|
|
350
411
|
|
|
351
412
|
//rWin
|
|
352
413
|
let rWin = p2r(r.summary.rWin)
|
|
353
414
|
|
|
354
|
-
//
|
|
355
|
-
|
|
415
|
+
//genRes
|
|
416
|
+
let genRes = (r) => {
|
|
356
417
|
|
|
357
|
-
|
|
358
|
-
|
|
418
|
+
//fitness
|
|
419
|
+
let fitness = calcFitness(strategy.settings, r.summary)
|
|
359
420
|
|
|
360
|
-
|
|
361
|
-
|
|
421
|
+
//numTrade
|
|
422
|
+
let numTrade = cint(r.summary.numTrade)
|
|
362
423
|
|
|
363
|
-
|
|
364
|
-
|
|
424
|
+
//levelNumTrade
|
|
425
|
+
let levelNumTrade = calcLevelNumTrade(numTrade)
|
|
365
426
|
|
|
366
|
-
|
|
367
|
-
|
|
427
|
+
//tkid
|
|
428
|
+
let tkid = `${tid}:${levelNumTrade}`
|
|
368
429
|
|
|
369
|
-
|
|
370
|
-
|
|
430
|
+
return {
|
|
431
|
+
|
|
432
|
+
...strategy,
|
|
371
433
|
|
|
372
|
-
|
|
434
|
+
tkid,
|
|
435
|
+
numTrade,
|
|
436
|
+
levelNumTrade,
|
|
437
|
+
keys,
|
|
373
438
|
|
|
374
|
-
|
|
439
|
+
summary: r.summary,
|
|
375
440
|
|
|
376
|
-
|
|
441
|
+
fitness,
|
|
377
442
|
|
|
443
|
+
solve: {
|
|
444
|
+
timeStart,
|
|
445
|
+
timeEnd,
|
|
446
|
+
methodOml,
|
|
447
|
+
thNumTrade,
|
|
448
|
+
thRWin,
|
|
449
|
+
thREquivalentCumuProfitOrLossFinalNormYear,
|
|
450
|
+
thsTp,
|
|
451
|
+
thsSl,
|
|
452
|
+
timeSolved: ott().format('YYYY-MM-DDTHH:mm:ss'),
|
|
453
|
+
},
|
|
454
|
+
|
|
455
|
+
}
|
|
378
456
|
}
|
|
457
|
+
|
|
458
|
+
//res, 除策略本體與summary、fitness外, 落地tkid、levelNumTrade、keys與solve使欄位自足, 識別與求解條件不依賴檔名
|
|
459
|
+
let res = genRes(r)
|
|
379
460
|
// console.log(ifun, res)
|
|
461
|
+
|
|
462
|
+
//resBrief
|
|
380
463
|
let resBrief = {
|
|
381
464
|
|
|
382
465
|
'方向': res.mode,
|
|
@@ -399,20 +482,20 @@ let estimKeys = async (ott, name, symbol, interval, fdOhlc, fdParam, timeStart,
|
|
|
399
482
|
// console.log(ifun, resBrief)
|
|
400
483
|
|
|
401
484
|
//update fitnessMin
|
|
402
|
-
if (fitnessMin > fitness) {
|
|
485
|
+
if (fitnessMin > res.fitness) {
|
|
403
486
|
|
|
404
487
|
if (useShowLog) {
|
|
405
488
|
console.log(ifun, '更新最佳解', resBrief)
|
|
406
489
|
}
|
|
407
490
|
|
|
408
491
|
//update
|
|
409
|
-
fitnessMin = fitness
|
|
492
|
+
fitnessMin = res.fitness
|
|
410
493
|
|
|
411
494
|
}
|
|
412
495
|
|
|
413
496
|
//儲存可用參數組
|
|
414
497
|
if (
|
|
415
|
-
numTrade >= thNumTrade &&
|
|
498
|
+
res.numTrade >= thNumTrade &&
|
|
416
499
|
rWin >= thRWin &&
|
|
417
500
|
// rCumuProfitOrLossFinal > 0 &&
|
|
418
501
|
// rTradeAllMax <= 0.8 &&
|
|
@@ -425,73 +508,58 @@ let estimKeys = async (ott, name, symbol, interval, fdOhlc, fdParam, timeStart,
|
|
|
425
508
|
console.log(ifun, '可用參數組', resBrief)
|
|
426
509
|
}
|
|
427
510
|
|
|
428
|
-
//
|
|
429
|
-
let
|
|
430
|
-
if (true) {
|
|
431
|
-
let ss = readStrategies(fdData, { readContent: false })
|
|
432
|
-
each(ss, (s) => {
|
|
433
|
-
kpTkid[s.tkid] = s
|
|
434
|
-
})
|
|
435
|
-
}
|
|
436
|
-
// console.log('kpTkid', kpTkid)
|
|
437
|
-
|
|
438
|
-
//levelNumTrade
|
|
439
|
-
let levelNumTrade = calcLevelNumTrade(numTrade)
|
|
440
|
-
|
|
441
|
-
//tkid
|
|
442
|
-
let tkid = `${tid}:${levelNumTrade}`
|
|
443
|
-
|
|
444
|
-
//b
|
|
445
|
-
let b = false
|
|
511
|
+
//fnStrategy, 檔名為tkid之純函數, 故可直接組出檔名查詢既有策略, 不需列舉全庫
|
|
512
|
+
let fnStrategy = genStrategyFileName(name, interval, mode, keys, strategy.settings, r.summary)
|
|
446
513
|
|
|
447
|
-
//
|
|
448
|
-
let
|
|
514
|
+
//fpStrategy
|
|
515
|
+
let fpStrategy = path.resolve(fdData, fnStrategy)
|
|
449
516
|
|
|
450
|
-
|
|
451
|
-
|
|
517
|
+
//b, 檢查是否已有同tkid策略, 無檔案或舊檔非合法json即寫入, 已有者僅於新等效年化盈虧更佳時覆寫
|
|
518
|
+
let b = checkSavable(fpStrategy, fnStrategy, res.tkid, rEquivalentCumuProfitOrLossFinalNormYear)
|
|
452
519
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
//check
|
|
457
|
-
if (rEquivalentCumuProfitOrLossFinalNormYearNew > rEquivalentCumuProfitOrLossFinalNormYearOld) {
|
|
520
|
+
//save
|
|
521
|
+
if (b) {
|
|
458
522
|
|
|
459
|
-
|
|
460
|
-
|
|
523
|
+
//useRunStrategyBeforeSave
|
|
524
|
+
if (useRunStrategyBeforeSave) {
|
|
461
525
|
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
526
|
+
if (isfun(funRunStrategyCustomBeforeSave)) {
|
|
527
|
+
r = await funRunStrategyCustomBeforeSave({ ott, strategy, funGetSeries })
|
|
528
|
+
}
|
|
529
|
+
else {
|
|
530
|
+
r = await runStrategy(ott, strategy, funGetSeries)
|
|
465
531
|
}
|
|
466
|
-
fsDeleteFile(kpTkid[tkid].path)
|
|
467
532
|
|
|
468
|
-
|
|
533
|
+
//res, 檔內全部衍生欄位由重跑之summary重新衍生, 確保檔案內容同源一致
|
|
534
|
+
res = genRes(r)
|
|
469
535
|
|
|
470
|
-
|
|
471
|
-
|
|
536
|
+
//fnStrategyRe, 以重跑之summary重算檔名, 使檔名級距與檔內同源
|
|
537
|
+
let fnStrategyRe = genStrategyFileName(name, interval, mode, keys, strategy.settings, r.summary)
|
|
472
538
|
|
|
473
|
-
|
|
474
|
-
|
|
539
|
+
//check, 檔名有變代表評估核心與重跑結果跨級距, 新檔名須重做存在檢查/身分驗證/優劣比較, 不可沿用原檔名之通過結果
|
|
540
|
+
if (fnStrategyRe !== fnStrategy) {
|
|
475
541
|
|
|
476
|
-
|
|
542
|
+
//fnStrategy, fpStrategy
|
|
543
|
+
fnStrategy = fnStrategyRe
|
|
544
|
+
fpStrategy = path.resolve(fdData, fnStrategy)
|
|
477
545
|
|
|
478
|
-
|
|
479
|
-
|
|
546
|
+
//b, 以重跑之等效年化盈虧對新檔名重檢
|
|
547
|
+
b = checkSavable(fpStrategy, fnStrategy, res.tkid, p2r(get(r, 'summary.rEquivalentCumuProfitOrLossFinalNormYear', '')))
|
|
480
548
|
|
|
481
|
-
|
|
482
|
-
let fnStrategy = genStrategyFileName(name, interval, mode, keys, strategy.settings, r.summary)
|
|
549
|
+
}
|
|
483
550
|
|
|
484
|
-
|
|
485
|
-
let fpStrategy = path.resolve(fdData, fnStrategy)
|
|
551
|
+
}
|
|
486
552
|
|
|
487
553
|
//fsWriteJson
|
|
488
|
-
|
|
554
|
+
if (b) {
|
|
555
|
+
fsWriteJson(fpStrategy, res, { useFormat: true })
|
|
556
|
+
}
|
|
489
557
|
|
|
490
558
|
}
|
|
491
559
|
|
|
492
560
|
}
|
|
493
561
|
|
|
494
|
-
return fitness
|
|
562
|
+
return res.fitness
|
|
495
563
|
}
|
|
496
564
|
|
|
497
565
|
//oml
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import crypto from 'crypto'
|
|
1
2
|
import cint from 'wsemi/src/cint.mjs'
|
|
2
|
-
import cont from './cont.mjs'
|
|
3
3
|
import genTid from './genTid.mjs'
|
|
4
4
|
import calcLevelNumTrade from './calcLevelNumTrade.mjs'
|
|
5
5
|
|
|
@@ -7,9 +7,12 @@ import calcLevelNumTrade from './calcLevelNumTrade.mjs'
|
|
|
7
7
|
/**
|
|
8
8
|
* 產生策略儲存檔名
|
|
9
9
|
*
|
|
10
|
-
* 檔名為`${
|
|
10
|
+
* 檔名為`${mode}_${levelNumTrade}_${hash}.json`,hash為md5(`${coin}:${interval}:${tid}:${levelNumTrade}`)取前16碼(64bits)
|
|
11
11
|
* tid由genTid以coin、interval、mode與keys產生,levelNumTrade由calcLevelNumTrade以summary.numTrade產生
|
|
12
|
-
*
|
|
12
|
+
* 檔名為(coin,interval,tid,levelNumTrade)之純函數且與求解結果(等效年化盈虧)無關,故同tkid恆對應同一檔名,
|
|
13
|
+
* 存檔端可直接組出檔名以fsIsFile查詢既有策略(O(1)),不需列舉全庫,且更新為原地覆寫,結構上不會出現同tkid多檔
|
|
14
|
+
* 因genTid會剔除`${coin}_`與`${interval}_`樣式,不同幣種或週期之同組keys會得到相同tid,故hash輸入需含coin與interval以避免跨幣種/跨週期碰撞
|
|
15
|
+
* 採定長雜湊係因NTFS單一檔名段上限為255字元(UTF-16計,LongPathsEnabled無法放寬),keys個數多時舊式`${tid} ⊙ ...`檔名會超限
|
|
13
16
|
* settings目前未參與檔名組成
|
|
14
17
|
*
|
|
15
18
|
* Unit Test: {@link https://github.com/yuda-lyu/w-trade-solve/blob/master/test/unit-genStrategyFileName.test.mjs Github}
|
|
@@ -19,7 +22,7 @@ import calcLevelNumTrade from './calcLevelNumTrade.mjs'
|
|
|
19
22
|
* @param {String} mode 輸入交易方向字串,可選'long'或'short'
|
|
20
23
|
* @param {Array} keys 輸入指標key字串陣列
|
|
21
24
|
* @param {Object} settings 輸入策略設定物件
|
|
22
|
-
* @param {Object} summary 輸入回測統計摘要物件,需含numTrade(交易次數)
|
|
25
|
+
* @param {Object} summary 輸入回測統計摘要物件,需含numTrade(交易次數)欄位
|
|
23
26
|
* @returns {String} 回傳策略儲存檔名字串
|
|
24
27
|
* @example
|
|
25
28
|
*
|
|
@@ -31,7 +34,8 @@ import calcLevelNumTrade from './calcLevelNumTrade.mjs'
|
|
|
31
34
|
*
|
|
32
35
|
* let fn = genStrategyFileName('btc', '4hr', 'long', keys, settings, summary)
|
|
33
36
|
* console.log(fn)
|
|
34
|
-
* // => '
|
|
37
|
+
* // => 'long_12_9fce6961e8f56493.json'
|
|
38
|
+
* //tid為'long ║ p_ma_1day', 級距為12, hash為md5('btc:4hr:long ║ p_ma_1day:12')前16碼
|
|
35
39
|
*
|
|
36
40
|
*/
|
|
37
41
|
let genStrategyFileName = (coin, interval, mode, keys, settings, summary) => {
|
|
@@ -46,11 +50,11 @@ let genStrategyFileName = (coin, interval, mode, keys, settings, summary) => {
|
|
|
46
50
|
//levelNumTrade
|
|
47
51
|
let levelNumTrade = calcLevelNumTrade(numTrade)
|
|
48
52
|
|
|
49
|
-
//
|
|
50
|
-
let
|
|
53
|
+
//hash, 含coin與interval避免跨幣種/跨週期之同名tid碰撞
|
|
54
|
+
let hash = crypto.createHash('md5').update(`${coin}:${interval}:${tid}:${levelNumTrade}`, 'utf8').digest('hex').slice(0, 16)
|
|
51
55
|
|
|
52
56
|
//fnStrategy
|
|
53
|
-
let fnStrategy = `${
|
|
57
|
+
let fnStrategy = `${mode}_${levelNumTrade}_${hash}.json`
|
|
54
58
|
|
|
55
59
|
return fnStrategy
|
|
56
60
|
}
|
package/src/readStrategies.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import each from 'lodash-es/each.js'
|
|
|
3
3
|
import filter from 'lodash-es/filter.js'
|
|
4
4
|
import get from 'lodash-es/get.js'
|
|
5
5
|
import size from 'lodash-es/size.js'
|
|
6
|
+
import cstr from 'wsemi/src/cstr.mjs'
|
|
6
7
|
import fsIsFolder from 'wsemi/src/fsIsFolder.mjs'
|
|
7
8
|
import fsReadJson from 'wsemi/src/fsReadJson.mjs'
|
|
8
9
|
import fsTreeFolder from 'wsemi/src/fsTreeFolder.mjs'
|
|
@@ -12,37 +13,47 @@ import sep from 'wsemi/src/sep.mjs'
|
|
|
12
13
|
import cont from './cont.mjs'
|
|
13
14
|
|
|
14
15
|
|
|
16
|
+
//ptnFnHash, genStrategyFileName所產生之雜湊檔名樣式`${mode}_${levelNumTrade}_${hash16}.json`
|
|
17
|
+
let ptnFnHash = /^(long|short)_\d+_[0-9a-f]{16}\.json$/
|
|
18
|
+
|
|
19
|
+
|
|
15
20
|
/**
|
|
16
21
|
* 讀取儲存策略資料夾內各策略檔資訊
|
|
17
22
|
*
|
|
18
23
|
* 僅列舉fd第1層之檔案,資料夾不列入,fd非資料夾或無任何檔案時回傳空陣列
|
|
19
|
-
*
|
|
20
|
-
*
|
|
24
|
+
* 各檔依opt.fromContent決定解析來源:
|
|
25
|
+
* 'filename'為舊式檔名反解: 以主檔名(剔除'.json')依cont.dlmPkgs切段,依序為tid、levelNumTrade與rEquivalentCumuProfitOrLossFinalNormYear,切不出tid者予以略過
|
|
26
|
+
* 'content'為讀取檔內json之tid、tkid、levelNumTrade與summary等欄位(即estimKeys所寫出之策略內容),非合法json或無tid欄位者予以略過,因需讀檔故較'filename'慢
|
|
27
|
+
* 'auto'(預設)為逐檔判別: 檔名符合genStrategyFileName雜湊格式(`${mode}_${levelNumTrade}_${hash16}.json`)者走'content',其餘走'filename',故新舊格式混存時皆可列舉
|
|
28
|
+
* tid依cont.dlmSeps切段,首段為mode,其餘為各key所組成之ps
|
|
21
29
|
* tkid為`${tid}:${levelNumTrade}`,與estimKeys內用於判斷同組keys與同交易次數級距之組合方式一致
|
|
22
30
|
*
|
|
23
31
|
* Unit Test: {@link https://github.com/yuda-lyu/w-trade-solve/blob/master/test/unit-readStrategies.test.mjs Github}
|
|
24
32
|
* @function
|
|
25
33
|
* @param {String} fd 輸入儲存策略資料夾字串
|
|
26
34
|
* @param {Object} [opt={}] 輸入設定物件,預設{}
|
|
27
|
-
* @param {Boolean} [opt.readContent=false]
|
|
35
|
+
* @param {Boolean} [opt.readContent=false] 輸入是否額外提供策略檔內容布林值,為true時各元素額外提供data欄位,內容非合法json時data為null,預設false
|
|
36
|
+
* @param {String} [opt.fromContent='auto'] 輸入解析來源字串,可選'filename'(由檔名反解)、'content'(讀檔內json欄位)與'auto'(依檔名格式逐檔判別),給予其他值時亦採'auto',預設'auto'
|
|
28
37
|
* @returns {Array} 回傳策略資訊陣列,各元素為{path,name,tkid,tid,levelNumTrade,rEquivalentCumuProfitOrLossFinalNormYear,mode,ps},opt.readContent為true時額外提供data
|
|
29
38
|
* @example
|
|
30
39
|
*
|
|
31
40
|
* import fs from 'fs'
|
|
32
|
-
* import
|
|
41
|
+
* import genStrategyFileName from './src/genStrategyFileName.mjs'
|
|
33
42
|
*
|
|
34
43
|
* let fd = './tmp/data-strategy'
|
|
35
44
|
* fs.mkdirSync(fd, { recursive: true })
|
|
36
45
|
*
|
|
37
|
-
* //以genStrategyFileName
|
|
38
|
-
* let
|
|
39
|
-
*
|
|
46
|
+
* //以genStrategyFileName之檔名規則儲存策略檔, 檔名為雜湊格式, 識別資訊由檔內json承載
|
|
47
|
+
* let summary = { numTrade: 30, rEquivalentCumuProfitOrLossFinalNormYear: '31.25%' }
|
|
48
|
+
* let fn = genStrategyFileName('btc', '4hr', 'long', ['btc_4hr_ma_1day'], {}, summary)
|
|
49
|
+
* let data = { tid: 'long ║ ma_1day', tkid: 'long ║ ma_1day:12', levelNumTrade: 12, mode: 'long', summary }
|
|
50
|
+
* fs.writeFileSync(`${fd}/${fn}`, JSON.stringify(data), 'utf8')
|
|
40
51
|
*
|
|
41
52
|
* console.log(readStrategies(fd))
|
|
42
53
|
* // => [
|
|
43
54
|
* // {
|
|
44
|
-
* // path: './tmp/data-strategy/
|
|
45
|
-
* // name: '
|
|
55
|
+
* // path: './tmp/data-strategy/long_12_a7578526a1fc57d8.json',
|
|
56
|
+
* // name: 'long_12_a7578526a1fc57d8.json',
|
|
46
57
|
* // tkid: 'long ║ ma_1day:12',
|
|
47
58
|
* // tid: 'long ║ ma_1day',
|
|
48
59
|
* // levelNumTrade: '12',
|
|
@@ -58,6 +69,12 @@ let readStrategies = (fd, opt = {}) => {
|
|
|
58
69
|
//readContent
|
|
59
70
|
let readContent = get(opt, 'readContent', false)
|
|
60
71
|
|
|
72
|
+
//fromContent
|
|
73
|
+
let fromContent = get(opt, 'fromContent', '')
|
|
74
|
+
if (fromContent !== 'filename' && fromContent !== 'content') {
|
|
75
|
+
fromContent = 'auto'
|
|
76
|
+
}
|
|
77
|
+
|
|
61
78
|
//check
|
|
62
79
|
if (!fsIsFolder(fd)) {
|
|
63
80
|
console.log(`invalid fd[${fd}]`)
|
|
@@ -79,44 +96,111 @@ let readStrategies = (fd, opt = {}) => {
|
|
|
79
96
|
let ss = []
|
|
80
97
|
each(vfps, (v) => {
|
|
81
98
|
|
|
82
|
-
//
|
|
83
|
-
let
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
//tid
|
|
87
|
-
let tid = get(qs, 0, '')
|
|
88
|
-
|
|
89
|
-
//check
|
|
90
|
-
if (!isestr(tid)) {
|
|
91
|
-
return true //跳出換下一個
|
|
99
|
+
//useContent, 'auto'依檔名是否符合雜湊格式逐檔判別
|
|
100
|
+
let useContent = fromContent === 'content'
|
|
101
|
+
if (fromContent === 'auto') {
|
|
102
|
+
useContent = ptnFnHash.test(v.name)
|
|
92
103
|
}
|
|
93
104
|
|
|
94
|
-
//levelNumTrade, rEquivalentCumuProfitOrLossFinalNormYear, ps, mode
|
|
95
|
-
let levelNumTrade = get(qs, 1, '')
|
|
96
|
-
let rEquivalentCumuProfitOrLossFinalNormYear = get(qs, 2, '')
|
|
97
|
-
let ps = sep(tid, cont.dlmSeps)
|
|
98
|
-
let mode = get(ps, 0, '')
|
|
99
|
-
ps = drop(ps)
|
|
100
|
-
|
|
101
|
-
//tkid
|
|
102
|
-
let tkid = `${tid}:${levelNumTrade}`
|
|
103
|
-
|
|
104
105
|
//s
|
|
105
|
-
let s =
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
106
|
+
let s = null
|
|
107
|
+
|
|
108
|
+
if (useContent) {
|
|
109
|
+
|
|
110
|
+
//data, 由檔內json取各欄位
|
|
111
|
+
let d = fsReadJson(v.path) //回傳{success}或{error}, 讀取或解析失敗時為null
|
|
112
|
+
let data = get(d, 'success', null)
|
|
113
|
+
|
|
114
|
+
//check
|
|
115
|
+
if (data === null) {
|
|
116
|
+
return true //跳出換下一個
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
//tid
|
|
120
|
+
let tid = get(data, 'tid', '')
|
|
121
|
+
|
|
122
|
+
//check
|
|
123
|
+
if (!isestr(tid)) {
|
|
124
|
+
return true //跳出換下一個
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
//levelNumTrade, 為與檔名反解之回傳格式一致, 轉為字串
|
|
128
|
+
let levelNumTrade = cstr(get(data, 'levelNumTrade', ''))
|
|
129
|
+
|
|
130
|
+
//rEquivalentCumuProfitOrLossFinalNormYear
|
|
131
|
+
let rEquivalentCumuProfitOrLossFinalNormYear = get(data, 'summary.rEquivalentCumuProfitOrLossFinalNormYear', '')
|
|
132
|
+
|
|
133
|
+
//ps, mode
|
|
134
|
+
let ps = sep(tid, cont.dlmSeps)
|
|
135
|
+
let mode = get(ps, 0, '')
|
|
136
|
+
ps = drop(ps)
|
|
137
|
+
|
|
138
|
+
//tkid, 舊內容無tkid欄位時由tid與levelNumTrade組出
|
|
139
|
+
let tkid = get(data, 'tkid', '')
|
|
140
|
+
if (!isestr(tkid)) {
|
|
141
|
+
tkid = `${tid}:${levelNumTrade}`
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
//s
|
|
145
|
+
s = {
|
|
146
|
+
path: v.path,
|
|
147
|
+
name: v.name,
|
|
148
|
+
tkid,
|
|
149
|
+
tid,
|
|
150
|
+
levelNumTrade,
|
|
151
|
+
rEquivalentCumuProfitOrLossFinalNormYear,
|
|
152
|
+
mode,
|
|
153
|
+
ps,
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
//readContent
|
|
157
|
+
if (readContent) {
|
|
158
|
+
s.data = data
|
|
159
|
+
}
|
|
160
|
+
|
|
114
161
|
}
|
|
162
|
+
else {
|
|
163
|
+
|
|
164
|
+
//qs
|
|
165
|
+
let name = replace(v.name, '.json', '')
|
|
166
|
+
let qs = sep(name, cont.dlmPkgs)
|
|
167
|
+
|
|
168
|
+
//tid
|
|
169
|
+
let tid = get(qs, 0, '')
|
|
170
|
+
|
|
171
|
+
//check
|
|
172
|
+
if (!isestr(tid)) {
|
|
173
|
+
return true //跳出換下一個
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
//levelNumTrade, rEquivalentCumuProfitOrLossFinalNormYear, ps, mode
|
|
177
|
+
let levelNumTrade = get(qs, 1, '')
|
|
178
|
+
let rEquivalentCumuProfitOrLossFinalNormYear = get(qs, 2, '')
|
|
179
|
+
let ps = sep(tid, cont.dlmSeps)
|
|
180
|
+
let mode = get(ps, 0, '')
|
|
181
|
+
ps = drop(ps)
|
|
182
|
+
|
|
183
|
+
//tkid
|
|
184
|
+
let tkid = `${tid}:${levelNumTrade}`
|
|
185
|
+
|
|
186
|
+
//s
|
|
187
|
+
s = {
|
|
188
|
+
path: v.path,
|
|
189
|
+
name: v.name,
|
|
190
|
+
tkid,
|
|
191
|
+
tid,
|
|
192
|
+
levelNumTrade,
|
|
193
|
+
rEquivalentCumuProfitOrLossFinalNormYear,
|
|
194
|
+
mode,
|
|
195
|
+
ps,
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
//readContent
|
|
199
|
+
if (readContent) {
|
|
200
|
+
let d = fsReadJson(v.path) //回傳{success}或{error}, 讀取或解析失敗時data為null
|
|
201
|
+
s.data = get(d, 'success', null)
|
|
202
|
+
}
|
|
115
203
|
|
|
116
|
-
//readContent
|
|
117
|
-
if (readContent) {
|
|
118
|
-
let d = fsReadJson(v.path) //回傳{success}或{error}, 讀取或解析失敗時data為null
|
|
119
|
-
s.data = get(d, 'success', null)
|
|
120
204
|
}
|
|
121
205
|
|
|
122
206
|
//push
|