@363045841yyt/klinechart-core 0.7.3 → 0.7.5-alpha.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.
Files changed (231) hide show
  1. package/README.md +201 -201
  2. package/README.zh-CN.md +201 -201
  3. package/dist/engine/renderers/webgl/candleSurface.js +47 -47
  4. package/dist/version.d.ts +1 -1
  5. package/dist/version.d.ts.map +1 -1
  6. package/dist/version.js +1 -2
  7. package/dist/version.js.map +1 -1
  8. package/package.json +129 -122
  9. package/src/__tests__/signal.test.ts +124 -124
  10. package/src/config/chartSettings.ts +66 -66
  11. package/src/controllers/__tests__/drawing.test.ts +214 -214
  12. package/src/controllers/__tests__/indicatorSelector.test.ts +481 -481
  13. package/src/controllers/__tests__/toolbar.test.ts +225 -225
  14. package/src/controllers/createChartController.ts +665 -665
  15. package/src/controllers/createDrawingController.ts +96 -96
  16. package/src/controllers/createIndicatorSelectorController.ts +307 -307
  17. package/src/controllers/createToolbarController.ts +146 -146
  18. package/src/controllers/index.ts +19 -19
  19. package/src/controllers/types.ts +284 -284
  20. package/src/engine/__tests__/chart.dpr.test.ts +401 -401
  21. package/src/engine/__tests__/paneRenderer.resize.test.ts +92 -92
  22. package/src/engine/chart-store.ts +121 -121
  23. package/src/engine/chart.d.ts +617 -617
  24. package/src/engine/chart.ts +2815 -2815
  25. package/src/engine/controller/__tests__/interaction.dpr.test.ts +259 -259
  26. package/src/engine/controller/interaction.ts +722 -722
  27. package/src/engine/controller/markerInteraction.ts +130 -130
  28. package/src/engine/controller/pinchTracker.ts +82 -82
  29. package/src/engine/controller/tooltipPosition.ts +48 -48
  30. package/src/engine/draw/__tests__/pixelAlign.spec.ts +176 -176
  31. package/src/engine/draw/pixelAlign.ts +259 -259
  32. package/src/engine/drawing/index.ts +655 -655
  33. package/src/engine/drawing/interaction.ts +842 -842
  34. package/src/engine/drawing/plugin.ts +343 -343
  35. package/src/engine/indicators/__tests__/__fixtures__/golden/atr.json +38 -38
  36. package/src/engine/indicators/__tests__/__fixtures__/golden/dema.json +14 -14
  37. package/src/engine/indicators/__tests__/__fixtures__/golden/hma.json +14 -14
  38. package/src/engine/indicators/__tests__/__fixtures__/golden/index.ts +55 -55
  39. package/src/engine/indicators/__tests__/__fixtures__/golden/kama.json +14 -14
  40. package/src/engine/indicators/__tests__/__fixtures__/golden/tema.json +14 -14
  41. package/src/engine/indicators/__tests__/__fixtures__/golden/wma.json +40 -40
  42. package/src/engine/indicators/__tests__/__fixtures__/synthetic.ts +65 -65
  43. package/src/engine/indicators/__tests__/_propertyAssertions.ts +76 -76
  44. package/src/engine/indicators/__tests__/atr.test.ts +153 -153
  45. package/src/engine/indicators/__tests__/calculators.test.ts +614 -614
  46. package/src/engine/indicators/__tests__/cmf-mfi.test.ts +100 -100
  47. package/src/engine/indicators/__tests__/dema.test.ts +73 -73
  48. package/src/engine/indicators/__tests__/donchian.test.ts +70 -70
  49. package/src/engine/indicators/__tests__/hma.test.ts +73 -73
  50. package/src/engine/indicators/__tests__/ichimoku.test.ts +105 -105
  51. package/src/engine/indicators/__tests__/kama.test.ts +80 -80
  52. package/src/engine/indicators/__tests__/keltner.test.ts +65 -65
  53. package/src/engine/indicators/__tests__/pivot-fib.test.ts +110 -110
  54. package/src/engine/indicators/__tests__/roc.test.ts +68 -68
  55. package/src/engine/indicators/__tests__/sar.test.ts +86 -86
  56. package/src/engine/indicators/__tests__/scheduler.test.ts +831 -831
  57. package/src/engine/indicators/__tests__/soa.test.ts +533 -533
  58. package/src/engine/indicators/__tests__/structure.test.ts +110 -110
  59. package/src/engine/indicators/__tests__/supertrend.test.ts +65 -65
  60. package/src/engine/indicators/__tests__/tema.test.ts +68 -68
  61. package/src/engine/indicators/__tests__/trix.test.ts +70 -70
  62. package/src/engine/indicators/__tests__/volatility.test.ts +117 -117
  63. package/src/engine/indicators/__tests__/volume.test.ts +115 -115
  64. package/src/engine/indicators/__tests__/volumeProfile.test.ts +74 -74
  65. package/src/engine/indicators/__tests__/vwap.test.ts +69 -69
  66. package/src/engine/indicators/__tests__/wma.test.ts +112 -112
  67. package/src/engine/indicators/__tests__/zones.test.ts +95 -95
  68. package/src/engine/indicators/atrState.ts +27 -27
  69. package/src/engine/indicators/bollState.ts +51 -51
  70. package/src/engine/indicators/calculators.ts +2593 -2593
  71. package/src/engine/indicators/cciState.ts +25 -25
  72. package/src/engine/indicators/chaikinVolState.ts +32 -32
  73. package/src/engine/indicators/cmfState.ts +27 -27
  74. package/src/engine/indicators/demaState.ts +27 -27
  75. package/src/engine/indicators/donchianState.ts +43 -43
  76. package/src/engine/indicators/eneState.ts +43 -43
  77. package/src/engine/indicators/expmaState.ts +43 -43
  78. package/src/engine/indicators/fastkState.ts +25 -25
  79. package/src/engine/indicators/fibState.ts +41 -41
  80. package/src/engine/indicators/hmaState.ts +27 -27
  81. package/src/engine/indicators/hvState.ts +28 -28
  82. package/src/engine/indicators/ichimokuState.ts +70 -70
  83. package/src/engine/indicators/indicator.worker.ts +169 -169
  84. package/src/engine/indicators/indicatorDefinitionRegistry.ts +62 -62
  85. package/src/engine/indicators/indicatorMetadata.ts +110 -110
  86. package/src/engine/indicators/indicatorRegistry.ts +106 -106
  87. package/src/engine/indicators/indicatorRuntime.ts +1548 -1548
  88. package/src/engine/indicators/kamaState.ts +34 -34
  89. package/src/engine/indicators/keltnerState.ts +49 -49
  90. package/src/engine/indicators/kstState.ts +42 -42
  91. package/src/engine/indicators/maState.ts +36 -36
  92. package/src/engine/indicators/macdState.ts +76 -76
  93. package/src/engine/indicators/mfiState.ts +27 -27
  94. package/src/engine/indicators/momState.ts +25 -25
  95. package/src/engine/indicators/obvState.ts +25 -25
  96. package/src/engine/indicators/parkinsonState.ts +28 -28
  97. package/src/engine/indicators/pivotState.ts +51 -51
  98. package/src/engine/indicators/pvtState.ts +25 -25
  99. package/src/engine/indicators/rocState.ts +27 -27
  100. package/src/engine/indicators/rsiState.ts +65 -65
  101. package/src/engine/indicators/sarState.ts +41 -41
  102. package/src/engine/indicators/scheduler.ts +1205 -1205
  103. package/src/engine/indicators/soa.ts +352 -352
  104. package/src/engine/indicators/stateComposer.ts +1262 -1262
  105. package/src/engine/indicators/stochState.ts +26 -26
  106. package/src/engine/indicators/structureState.ts +69 -69
  107. package/src/engine/indicators/supertrendState.ts +37 -37
  108. package/src/engine/indicators/temaState.ts +27 -27
  109. package/src/engine/indicators/trixState.ts +35 -35
  110. package/src/engine/indicators/vmaState.ts +27 -27
  111. package/src/engine/indicators/volumeProfileState.ts +63 -63
  112. package/src/engine/indicators/vwapState.ts +29 -29
  113. package/src/engine/indicators/wmaState.ts +27 -27
  114. package/src/engine/indicators/wmsrState.ts +25 -25
  115. package/src/engine/indicators/workerProtocol.ts +613 -613
  116. package/src/engine/indicators/zonesState.ts +47 -47
  117. package/src/engine/layout/pane.ts +161 -161
  118. package/src/engine/marker/registry.ts +265 -265
  119. package/src/engine/paneRenderer.ts +169 -169
  120. package/src/engine/renderers/Indicator/atr.ts +237 -237
  121. package/src/engine/renderers/Indicator/boll.ts +317 -317
  122. package/src/engine/renderers/Indicator/cci.ts +275 -275
  123. package/src/engine/renderers/Indicator/chaikinVol.ts +138 -138
  124. package/src/engine/renderers/Indicator/cmf.ts +137 -137
  125. package/src/engine/renderers/Indicator/dema.ts +136 -136
  126. package/src/engine/renderers/Indicator/donchian.ts +137 -137
  127. package/src/engine/renderers/Indicator/ene.ts +271 -271
  128. package/src/engine/renderers/Indicator/expma.ts +197 -197
  129. package/src/engine/renderers/Indicator/fastk.ts +316 -316
  130. package/src/engine/renderers/Indicator/fib.ts +141 -141
  131. package/src/engine/renderers/Indicator/hma.ts +136 -136
  132. package/src/engine/renderers/Indicator/hv.ts +124 -124
  133. package/src/engine/renderers/Indicator/ichimoku.ts +181 -181
  134. package/src/engine/renderers/Indicator/index.ts +241 -241
  135. package/src/engine/renderers/Indicator/indicatorData.ts +650 -650
  136. package/src/engine/renderers/Indicator/kama.ts +136 -136
  137. package/src/engine/renderers/Indicator/keltner.ts +137 -137
  138. package/src/engine/renderers/Indicator/kst.ts +302 -302
  139. package/src/engine/renderers/Indicator/ma.ts +200 -200
  140. package/src/engine/renderers/Indicator/macd.ts +477 -477
  141. package/src/engine/renderers/Indicator/macdLegend.ts +141 -141
  142. package/src/engine/renderers/Indicator/mainIndicatorLegend.ts +272 -272
  143. package/src/engine/renderers/Indicator/mfi.ts +142 -142
  144. package/src/engine/renderers/Indicator/mom.ts +311 -311
  145. package/src/engine/renderers/Indicator/obv.ts +123 -123
  146. package/src/engine/renderers/Indicator/parkinson.ts +124 -124
  147. package/src/engine/renderers/Indicator/pivot.ts +131 -131
  148. package/src/engine/renderers/Indicator/pvt.ts +123 -123
  149. package/src/engine/renderers/Indicator/roc.ts +143 -143
  150. package/src/engine/renderers/Indicator/rsi.ts +390 -390
  151. package/src/engine/renderers/Indicator/sar.ts +113 -113
  152. package/src/engine/renderers/Indicator/scale/atr_scale.ts +19 -19
  153. package/src/engine/renderers/Indicator/scale/cci_scale.ts +19 -19
  154. package/src/engine/renderers/Indicator/scale/fastk_scale.ts +19 -19
  155. package/src/engine/renderers/Indicator/scale/indicator_scale.ts +204 -204
  156. package/src/engine/renderers/Indicator/scale/kst_scale.ts +19 -19
  157. package/src/engine/renderers/Indicator/scale/macd_scale.ts +22 -22
  158. package/src/engine/renderers/Indicator/scale/mom_scale.ts +19 -19
  159. package/src/engine/renderers/Indicator/scale/rsi_scale.ts +19 -19
  160. package/src/engine/renderers/Indicator/scale/stoch_scale.ts +19 -19
  161. package/src/engine/renderers/Indicator/scale/volume_scale.ts +26 -26
  162. package/src/engine/renderers/Indicator/scale/wmsr_scale.ts +19 -19
  163. package/src/engine/renderers/Indicator/stoch.ts +359 -359
  164. package/src/engine/renderers/Indicator/structure.ts +126 -126
  165. package/src/engine/renderers/Indicator/subPaneConfig.ts +265 -265
  166. package/src/engine/renderers/Indicator/supertrend.ts +115 -115
  167. package/src/engine/renderers/Indicator/tema.ts +136 -136
  168. package/src/engine/renderers/Indicator/trix.ts +158 -158
  169. package/src/engine/renderers/Indicator/vma.ts +124 -124
  170. package/src/engine/renderers/Indicator/volumeProfile.ts +125 -125
  171. package/src/engine/renderers/Indicator/vwap.ts +123 -123
  172. package/src/engine/renderers/Indicator/wma.ts +136 -136
  173. package/src/engine/renderers/Indicator/wmsr.ts +328 -328
  174. package/src/engine/renderers/Indicator/zones.ts +104 -104
  175. package/src/engine/renderers/__tests__/boll.renderer.test.ts +314 -314
  176. package/src/engine/renderers/__tests__/ene.renderer.test.ts +305 -305
  177. package/src/engine/renderers/__tests__/expma.renderer.test.ts +279 -279
  178. package/src/engine/renderers/__tests__/ma.renderer.test.ts +426 -426
  179. package/src/engine/renderers/__tests__/mainIndicatorLegend.renderer.test.ts +502 -502
  180. package/src/engine/renderers/__tests__/yAxis.renderer.test.ts +173 -173
  181. package/src/engine/renderers/candle.ts +459 -459
  182. package/src/engine/renderers/crosshair.ts +69 -69
  183. package/src/engine/renderers/customMarkers.ts +162 -162
  184. package/src/engine/renderers/extremaMarkers.ts +246 -246
  185. package/src/engine/renderers/gridLines.ts +90 -90
  186. package/src/engine/renderers/lastPrice.ts +97 -97
  187. package/src/engine/renderers/paneTitle.ts +136 -136
  188. package/src/engine/renderers/subVolume.ts +236 -236
  189. package/src/engine/renderers/timeAxis.ts +121 -121
  190. package/src/engine/renderers/webgl/candleSurface.ts +955 -955
  191. package/src/engine/renderers/webgl/sharedWebGLSurface.ts +146 -146
  192. package/src/engine/renderers/yAxis.ts +105 -105
  193. package/src/engine/scale/__tests__/logFormula.spec.ts +148 -148
  194. package/src/engine/scale/logFormula.ts +130 -130
  195. package/src/engine/scale/price.ts +39 -39
  196. package/src/engine/scale/priceScale.ts +264 -264
  197. package/src/engine/subPaneManager.ts +427 -427
  198. package/src/engine/theme/colors.ts +642 -642
  199. package/src/engine/theme/fonts.ts +20 -20
  200. package/src/engine/utils/klineConfig.ts +49 -49
  201. package/src/engine/utils/tickCount.ts +11 -11
  202. package/src/engine/utils/tickPosition.ts +214 -214
  203. package/src/engine/utils/zoom.ts +83 -83
  204. package/src/engine/viewport/viewport.ts +67 -67
  205. package/src/index.ts +3 -3
  206. package/src/plugin/ConfigManager.ts +93 -93
  207. package/src/plugin/EventBus.ts +77 -77
  208. package/src/plugin/HookSystem.ts +106 -106
  209. package/src/plugin/PluginHost.ts +243 -243
  210. package/src/plugin/PluginRegistry.ts +92 -92
  211. package/src/plugin/StateStore.ts +73 -73
  212. package/src/plugin/index.ts +19 -19
  213. package/src/plugin/rendererPluginManager.ts +368 -368
  214. package/src/plugin/stateKeys.ts +8 -8
  215. package/src/plugin/types.ts +526 -526
  216. package/src/reactivity/index.ts +2 -2
  217. package/src/reactivity/signal.ts +119 -119
  218. package/src/semantic/controller.ts +251 -251
  219. package/src/semantic/drawShape.ts +260 -260
  220. package/src/semantic/index.ts +28 -28
  221. package/src/semantic/schema.json +256 -256
  222. package/src/semantic/types.ts +251 -251
  223. package/src/semantic/validator.ts +349 -349
  224. package/src/types/kLine.ts +13 -13
  225. package/src/types/price.ts +56 -56
  226. package/src/types/volumePrice.ts +33 -33
  227. package/src/utils/dateFormat.ts +208 -208
  228. package/src/utils/kLineDraw/axis.ts +562 -562
  229. package/src/utils/priceToY.ts +34 -34
  230. package/src/utils/volumePrice.ts +202 -202
  231. package/src/version.ts +1 -1
@@ -1,614 +1,614 @@
1
- import { describe, it, expect } from 'vitest'
2
- import {
3
- calcMAData,
4
- calcBOLLData,
5
- calcEXPMAData,
6
- calcENEData,
7
- calcRSIData,
8
- DEFAULT_MA_PERIODS,
9
- DEFAULT_BOLL_PERIOD,
10
- DEFAULT_BOLL_MULTIPLIER,
11
- DEFAULT_EXPMA_FAST_PERIOD,
12
- DEFAULT_EXPMA_SLOW_PERIOD,
13
- DEFAULT_ENE_PERIOD,
14
- DEFAULT_ENE_DEVIATION,
15
- DEFAULT_RSI_PERIOD1,
16
- DEFAULT_RSI_PERIOD2,
17
- DEFAULT_RSI_PERIOD3,
18
- } from '../calculators'
19
- import type { KLineData } from '@/types/price'
20
-
21
- /**
22
- * 创建测试用的 K 线数据
23
- * 收盘价序列: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
24
- */
25
- function createTestData(prices: number[]): KLineData[] {
26
- return prices.map((close, index) => ({
27
- timestamp: 1000000000000 + index * 60000,
28
- open: close - 0.5,
29
- high: close + 0.5,
30
- low: close - 0.5,
31
- close,
32
- volume: 1000 + index * 100,
33
- }))
34
- }
35
-
36
- describe('calcMAData', () => {
37
- const prices = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
38
- const data = createTestData(prices)
39
-
40
- it('should return array of same length as input', () => {
41
- const result = calcMAData(data, 5)
42
- expect(result).toHaveLength(data.length)
43
- })
44
-
45
- it('should return undefined for indices before period-1', () => {
46
- const result = calcMAData(data, 5)
47
- expect(result[0]).toBeUndefined()
48
- expect(result[1]).toBeUndefined()
49
- expect(result[2]).toBeUndefined()
50
- expect(result[3]).toBeUndefined()
51
- })
52
-
53
- it('should calculate correct MA5 values using sliding window', () => {
54
- const result = calcMAData(data, 5)
55
-
56
- // MA5 at index 4: (10+11+12+13+14)/5 = 60/5 = 12
57
- expect(result[4]).toBeCloseTo(12, 2)
58
-
59
- // MA5 at index 5: (11+12+13+14+15)/5 = 65/5 = 13
60
- expect(result[5]).toBeCloseTo(13, 2)
61
-
62
- // MA5 at index 9: (15+16+17+18+19)/5 = 85/5 = 17
63
- expect(result[9]).toBeCloseTo(17, 2)
64
- })
65
-
66
- it('should calculate correct MA3 values', () => {
67
- const result = calcMAData(data, 3)
68
-
69
- // MA3 at index 2: (10+11+12)/3 = 11
70
- expect(result[2]).toBeCloseTo(11, 2)
71
-
72
- // MA3 at index 3: (11+12+13)/3 = 12
73
- expect(result[3]).toBeCloseTo(12, 2)
74
-
75
- // MA3 at index 4: (12+13+14)/3 = 13
76
- expect(result[4]).toBeCloseTo(13, 2)
77
- })
78
-
79
- it('should handle period=1 (return close price)', () => {
80
- const result = calcMAData(data, 1)
81
-
82
- for (let i = 0; i < data.length; i++) {
83
- expect(result[i]).toBeCloseTo(data[i].close, 2)
84
- }
85
- })
86
-
87
- it('should return all undefined when data length < period', () => {
88
- const shortData = createTestData([10, 11, 12])
89
- const result = calcMAData(shortData, 5)
90
-
91
- expect(result).toHaveLength(3)
92
- expect(result[0]).toBeUndefined()
93
- expect(result[1]).toBeUndefined()
94
- expect(result[2]).toBeUndefined()
95
- })
96
-
97
- it('should handle empty data', () => {
98
- const result = calcMAData([], 5)
99
- expect(result).toHaveLength(0)
100
- })
101
-
102
- it('should produce consistent results with manual calculation', () => {
103
- const result = calcMAData(data, 5)
104
-
105
- // Manual verification of sliding window
106
- for (let i = 4; i < data.length; i++) {
107
- let sum = 0
108
- for (let j = 0; j < 5; j++) {
109
- sum += data[i - j].close
110
- }
111
- const expected = sum / 5
112
- expect(result[i]).toBeCloseTo(expected, 2)
113
- }
114
- })
115
-
116
- it('should handle large datasets efficiently', () => {
117
- const largePrices = Array.from({ length: 10000 }, (_, i) => 100 + i)
118
- const largeData = createTestData(largePrices)
119
-
120
- const start = performance.now()
121
- const result = calcMAData(largeData, 60)
122
- const end = performance.now()
123
-
124
- // Should complete in reasonable time (< 100ms for 10k items)
125
- expect(end - start).toBeLessThan(100)
126
-
127
- // Verify last value is correct
128
- // Last 60 values are indices 9940-9999, prices are 100+9940=10040 to 100+9999=10099
129
- // sum = (10040 + 10099) * 60 / 2 = 604170, avg = 10069.5
130
- const lastIdx = largeData.length - 1
131
- expect(result[lastIdx]).toBeCloseTo(10069.5, 1)
132
- })
133
- })
134
-
135
- describe('DEFAULT_MA_PERIODS', () => {
136
- it('should contain standard MA periods', () => {
137
- expect(DEFAULT_MA_PERIODS).toEqual([5, 10, 20, 30, 60])
138
- })
139
-
140
- it('should be readonly array', () => {
141
- // Type check - this is more of a compile-time check
142
- // but we verify the values are as expected
143
- expect(DEFAULT_MA_PERIODS).toHaveLength(5)
144
- expect(DEFAULT_MA_PERIODS[0]).toBe(5)
145
- expect(DEFAULT_MA_PERIODS[4]).toBe(60)
146
- })
147
- })
148
-
149
- describe('calcBOLLData', () => {
150
- const prices = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
151
- const data = createTestData(prices)
152
-
153
- it('should return array of same length as input', () => {
154
- const result = calcBOLLData(data, 5, 2)
155
- expect(result).toHaveLength(data.length)
156
- })
157
-
158
- it('should return undefined for indices before period-1', () => {
159
- const result = calcBOLLData(data, 5, 2)
160
- expect(result[0]).toBeUndefined()
161
- expect(result[1]).toBeUndefined()
162
- expect(result[2]).toBeUndefined()
163
- expect(result[3]).toBeUndefined()
164
- })
165
-
166
- it('should calculate correct BOLL5 values with known data', () => {
167
- const result = calcBOLLData(data, 5, 2)
168
-
169
- // Index 4: closes [10,11,12,13,14], MA=12, variance=2, stdDev=√2
170
- const point4 = result[4]!
171
- expect(point4.middle).toBeCloseTo(12, 2)
172
- expect(point4.upper).toBeCloseTo(12 + 2 * Math.sqrt(2), 2)
173
- expect(point4.lower).toBeCloseTo(12 - 2 * Math.sqrt(2), 2)
174
-
175
- // Index 5: closes [11,12,13,14,15], MA=13, variance=2, stdDev=√2
176
- const point5 = result[5]!
177
- expect(point5.middle).toBeCloseTo(13, 2)
178
- expect(point5.upper).toBeCloseTo(13 + 2 * Math.sqrt(2), 2)
179
- expect(point5.lower).toBeCloseTo(13 - 2 * Math.sqrt(2), 2)
180
- })
181
-
182
- it('should have upper = middle + multiplier * stdDev', () => {
183
- const result = calcBOLLData(data, 5, 2)
184
- for (let i = 4; i < data.length; i++) {
185
- const p = result[i]!
186
- const stdDev = (p.upper - p.middle) / 2
187
- expect(p.lower).toBeCloseTo(p.middle - 2 * stdDev, 2)
188
- }
189
- })
190
-
191
- it('should have lower = middle - multiplier * stdDev', () => {
192
- const result = calcBOLLData(data, 5, 2)
193
- for (let i = 4; i < data.length; i++) {
194
- const p = result[i]!
195
- expect(p.lower).toBeCloseTo(p.middle - (p.upper - p.middle), 2)
196
- }
197
- })
198
-
199
- it('should return all undefined when data length < period', () => {
200
- const shortData = createTestData([10, 11, 12])
201
- const result = calcBOLLData(shortData, 5, 2)
202
-
203
- expect(result).toHaveLength(3)
204
- expect(result[0]).toBeUndefined()
205
- expect(result[1]).toBeUndefined()
206
- expect(result[2]).toBeUndefined()
207
- })
208
-
209
- it('should handle empty data', () => {
210
- const result = calcBOLLData([], 5, 2)
211
- expect(result).toHaveLength(0)
212
- })
213
-
214
- it('should produce consistent results with manual sliding window', () => {
215
- const result = calcBOLLData(data, 5, 2)
216
-
217
- for (let i = 4; i < data.length; i++) {
218
- let sum = 0
219
- for (let j = 0; j < 5; j++) {
220
- sum += data[i - j].close
221
- }
222
- const ma = sum / 5
223
-
224
- let variance = 0
225
- for (let j = 0; j < 5; j++) {
226
- variance += Math.pow(data[i - j].close - ma, 2)
227
- }
228
- const stdDev = Math.sqrt(variance / 5)
229
-
230
- expect(result[i]!.middle).toBeCloseTo(ma, 2)
231
- expect(result[i]!.upper).toBeCloseTo(ma + 2 * stdDev, 2)
232
- expect(result[i]!.lower).toBeCloseTo(ma - 2 * stdDev, 2)
233
- }
234
- })
235
-
236
- it('should handle large datasets efficiently', () => {
237
- const largePrices = Array.from({ length: 10000 }, (_, i) => 100 + i)
238
- const largeData = createTestData(largePrices)
239
-
240
- const start = performance.now()
241
- const result = calcBOLLData(largeData, 20, 2)
242
- const end = performance.now()
243
-
244
- expect(end - start).toBeLessThan(100)
245
- expect(result[19]).toBeDefined()
246
- expect(result[19]!.middle).toBeCloseTo(109.5, 1)
247
- })
248
- })
249
-
250
- describe('calcEXPMAData', () => {
251
- const prices = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
252
- const data = createTestData(prices)
253
-
254
- it('should return array of same length as input', () => {
255
- const result = calcEXPMAData(data, 12, 50)
256
- expect(result).toHaveLength(data.length)
257
- })
258
-
259
- it('should have values from index 0 (dense array)', () => {
260
- const result = calcEXPMAData(data, 12, 50)
261
- expect(result[0]).toBeDefined()
262
- expect(result[0]!.fast).toBeDefined()
263
- expect(result[0]!.slow).toBeDefined()
264
- })
265
-
266
- it('should use first close as initial EMA', () => {
267
- const result = calcEXPMAData(data, 12, 50)
268
- expect(result[0]!.fast).toBeCloseTo(10, 2)
269
- expect(result[0]!.slow).toBeCloseTo(10, 2)
270
- })
271
-
272
- it('should calculate correct EMA with K=2/(N+1)', () => {
273
- const result = calcEXPMAData(data, 12, 50)
274
- const fastK = 2 / (12 + 1)
275
- const slowK = 2 / (50 + 1)
276
-
277
- // Index 1: EMA(1) = C(1)*K + EMA(0)*(1-K)
278
- const expectedFast1 = 11 * fastK + 10 * (1 - fastK)
279
- const expectedSlow1 = 11 * slowK + 10 * (1 - slowK)
280
- expect(result[1]!.fast).toBeCloseTo(expectedFast1, 4)
281
- expect(result[1]!.slow).toBeCloseTo(expectedSlow1, 4)
282
-
283
- // Index 2: continue recursion
284
- const expectedFast2 = 12 * fastK + expectedFast1 * (1 - fastK)
285
- const expectedSlow2 = 12 * slowK + expectedSlow1 * (1 - slowK)
286
- expect(result[2]!.fast).toBeCloseTo(expectedFast2, 4)
287
- expect(result[2]!.slow).toBeCloseTo(expectedSlow2, 4)
288
- })
289
-
290
- it('should have fast line more responsive than slow line', () => {
291
- const result = calcEXPMAData(data, 12, 50)
292
-
293
- for (let i = 1; i < data.length; i++) {
294
- // Rising prices: fast EMA should be greater than slow EMA
295
- expect(result[i]!.fast).toBeGreaterThanOrEqual(result[i]!.slow)
296
- }
297
- })
298
-
299
- it('should handle empty data', () => {
300
- const result = calcEXPMAData([], 12, 50)
301
- expect(result).toHaveLength(0)
302
- })
303
-
304
- it('should handle single data point', () => {
305
- const singleData = createTestData([42])
306
- const result = calcEXPMAData(singleData, 12, 50)
307
-
308
- expect(result).toHaveLength(1)
309
- expect(result[0]!.fast).toBeCloseTo(42, 2)
310
- expect(result[0]!.slow).toBeCloseTo(42, 2)
311
- })
312
-
313
- it('should produce consistent results with manual EMA recursion', () => {
314
- const result = calcEXPMAData(data, 12, 50)
315
- const fastK = 2 / (12 + 1)
316
- const slowK = 2 / (50 + 1)
317
-
318
- let fastEMA = data[0].close
319
- let slowEMA = data[0].close
320
-
321
- for (let i = 0; i < data.length; i++) {
322
- expect(result[i]!.fast).toBeCloseTo(fastEMA, 6)
323
- expect(result[i]!.slow).toBeCloseTo(slowEMA, 6)
324
-
325
- if (i + 1 < data.length) {
326
- fastEMA = data[i + 1].close * fastK + fastEMA * (1 - fastK)
327
- slowEMA = data[i + 1].close * slowK + slowEMA * (1 - slowK)
328
- }
329
- }
330
- })
331
-
332
- it('should handle large datasets efficiently', () => {
333
- const largePrices = Array.from({ length: 10000 }, (_, i) => 100 + i)
334
- const largeData = createTestData(largePrices)
335
-
336
- const start = performance.now()
337
- const result = calcEXPMAData(largeData, 12, 50)
338
- const end = performance.now()
339
-
340
- expect(end - start).toBeLessThan(100)
341
- expect(result[0]).toBeDefined()
342
- expect(result[9999]).toBeDefined()
343
- })
344
- })
345
-
346
- describe('calcENEData', () => {
347
- const prices = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
348
- const data = createTestData(prices)
349
-
350
- it('should return array of same length as input', () => {
351
- const result = calcENEData(data, 5, 10)
352
- expect(result).toHaveLength(data.length)
353
- })
354
-
355
- it('should return undefined for indices before period-1', () => {
356
- const result = calcENEData(data, 5, 10)
357
- expect(result[0]).toBeUndefined()
358
- expect(result[1]).toBeUndefined()
359
- expect(result[2]).toBeUndefined()
360
- expect(result[3]).toBeUndefined()
361
- })
362
-
363
- it('should calculate correct ENE5 values with deviation=10', () => {
364
- const result = calcENEData(data, 5, 10)
365
-
366
- // Index 4: MA5=12, upper=12*1.1=13.2, lower=12*0.9=10.8
367
- const point4 = result[4]!
368
- expect(point4.middle).toBeCloseTo(12, 2)
369
- expect(point4.upper).toBeCloseTo(13.2, 2)
370
- expect(point4.lower).toBeCloseTo(10.8, 2)
371
-
372
- // Index 5: MA5=13, upper=13*1.1=14.3, lower=13*0.9=11.7
373
- const point5 = result[5]!
374
- expect(point5.middle).toBeCloseTo(13, 2)
375
- expect(point5.upper).toBeCloseTo(14.3, 2)
376
- expect(point5.lower).toBeCloseTo(11.7, 2)
377
- })
378
-
379
- it('should have upper = middle * (1 + deviation/100)', () => {
380
- const result = calcENEData(data, 5, 11)
381
- for (let i = 4; i < data.length; i++) {
382
- const p = result[i]!
383
- expect(p.upper).toBeCloseTo(p.middle * (1 + 11 / 100), 2)
384
- }
385
- })
386
-
387
- it('should have lower = middle * (1 - deviation/100)', () => {
388
- const result = calcENEData(data, 5, 11)
389
- for (let i = 4; i < data.length; i++) {
390
- const p = result[i]!
391
- expect(p.lower).toBeCloseTo(p.middle * (1 - 11 / 100), 2)
392
- }
393
- })
394
-
395
- it('should return all undefined when data length < period', () => {
396
- const shortData = createTestData([10, 11, 12])
397
- const result = calcENEData(shortData, 5, 10)
398
-
399
- expect(result).toHaveLength(3)
400
- expect(result[0]).toBeUndefined()
401
- expect(result[1]).toBeUndefined()
402
- expect(result[2]).toBeUndefined()
403
- })
404
-
405
- it('should handle empty data', () => {
406
- const result = calcENEData([], 5, 10)
407
- expect(result).toHaveLength(0)
408
- })
409
-
410
- it('should produce consistent results with manual calculation', () => {
411
- const result = calcENEData(data, 5, 10)
412
-
413
- for (let i = 4; i < data.length; i++) {
414
- let sum = 0
415
- for (let j = 0; j < 5; j++) {
416
- sum += data[i - j].close
417
- }
418
- const ma = sum / 5
419
-
420
- expect(result[i]!.middle).toBeCloseTo(ma, 2)
421
- expect(result[i]!.upper).toBeCloseTo(ma * (1 + 10 / 100), 2)
422
- expect(result[i]!.lower).toBeCloseTo(ma * (1 - 10 / 100), 2)
423
- }
424
- })
425
-
426
- it('should handle large datasets efficiently', () => {
427
- const largePrices = Array.from({ length: 10000 }, (_, i) => 100 + i)
428
- const largeData = createTestData(largePrices)
429
-
430
- const start = performance.now()
431
- const result = calcENEData(largeData, 10, 11)
432
- const end = performance.now()
433
-
434
- expect(end - start).toBeLessThan(100)
435
- expect(result[9]).toBeDefined()
436
- expect(result[9]!.middle).toBeCloseTo(104.5, 1)
437
- })
438
- })
439
-
440
- describe('BOLL default constants', () => {
441
- it('DEFAULT_BOLL_PERIOD should be 20', () => {
442
- expect(DEFAULT_BOLL_PERIOD).toBe(20)
443
- })
444
-
445
- it('DEFAULT_BOLL_MULTIPLIER should be 2', () => {
446
- expect(DEFAULT_BOLL_MULTIPLIER).toBe(2)
447
- })
448
- })
449
-
450
- describe('EXPMA default constants', () => {
451
- it('DEFAULT_EXPMA_FAST_PERIOD should be 12', () => {
452
- expect(DEFAULT_EXPMA_FAST_PERIOD).toBe(12)
453
- })
454
-
455
- it('DEFAULT_EXPMA_SLOW_PERIOD should be 50', () => {
456
- expect(DEFAULT_EXPMA_SLOW_PERIOD).toBe(50)
457
- })
458
- })
459
-
460
- describe('ENE default constants', () => {
461
- it('DEFAULT_ENE_PERIOD should be 10', () => {
462
- expect(DEFAULT_ENE_PERIOD).toBe(10)
463
- })
464
-
465
- it('DEFAULT_ENE_DEVIATION should be 11', () => {
466
- expect(DEFAULT_ENE_DEVIATION).toBe(11)
467
- })
468
- })
469
-
470
- describe('calcRSIData', () => {
471
- // 创建测试数据:价格连续上涨序列
472
- function createRisingPrices(count: number, start = 10): number[] {
473
- return Array.from({ length: count }, (_, i) => start + i)
474
- }
475
-
476
- // 创建测试数据:价格连续下跌序列
477
- function createFallingPrices(count: number, start = 20): number[] {
478
- return Array.from({ length: count }, (_, i) => start - i)
479
- }
480
-
481
- // 创建测试数据:价格震荡序列
482
- function createOscillatingPrices(count: number): number[] {
483
- return Array.from({ length: count }, (_, i) => 10 + (i % 2 === 0 ? 1 : -1))
484
- }
485
-
486
- it('should return array of same length as input', () => {
487
- const prices = createRisingPrices(20)
488
- const data = createTestData(prices)
489
- const result = calcRSIData(data, 6)
490
- expect(result).toHaveLength(data.length)
491
- })
492
-
493
- it('should return undefined for first period indices', () => {
494
- const prices = createRisingPrices(20)
495
- const data = createTestData(prices)
496
- const period = 6
497
- const result = calcRSIData(data, period)
498
-
499
- // 前 period 个值应为 undefined (index 0-5)
500
- for (let i = 0; i < period; i++) {
501
- expect(result[i]).toBeUndefined()
502
- }
503
- // 第 period 个值是第一个有效值 (index 6)
504
- expect(result[period]).toBeDefined()
505
- })
506
-
507
- it('should return 100 when all gains (no losses)', () => {
508
- const prices = createRisingPrices(20)
509
- const data = createTestData(prices)
510
- const period = 6
511
- const result = calcRSIData(data, period)
512
-
513
- // 第一个有效 RSI 值应为 100(全是上涨,没有下跌)
514
- expect(result[period + 1]).toBe(100)
515
- })
516
-
517
- it('should return 0 when all losses (no gains)', () => {
518
- const prices = createFallingPrices(20)
519
- const data = createTestData(prices)
520
- const period = 6
521
- const result = calcRSIData(data, period)
522
-
523
- // 第一个有效 RSI 值应为 0(全是下跌,没有上涨)
524
- expect(result[period + 1]).toBe(0)
525
- })
526
-
527
- it('should return all undefined when data length < period+1', () => {
528
- const prices = [10, 11, 12, 13, 14]
529
- const data = createTestData(prices)
530
- const period = 6
531
- const result = calcRSIData(data, period)
532
-
533
- // 数据不足时,所有值都应为 undefined
534
- expect(result).toHaveLength(5)
535
- result.forEach((v) => {
536
- expect(v).toBeUndefined()
537
- })
538
- })
539
-
540
- it('should handle empty data', () => {
541
- const result = calcRSIData([], 6)
542
- expect(result).toHaveLength(0)
543
- })
544
-
545
- it('should calculate correct RSI with known oscillating data', () => {
546
- // 震荡价格:10, 11, 10, 11, 10, 11, 10, 11, ...
547
- const prices = [10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11]
548
- const data = createTestData(prices)
549
- const period = 6
550
- const result = calcRSIData(data, period)
551
-
552
- // 第6个索引(period=6)是第一个有效值
553
- // 前6个变化:+1, -1, +1, -1, +1, -1
554
- // sumGain = 3, sumLoss = 3
555
- // avgGain = 0.5, avgLoss = 0.5
556
- // RS = 1, RSI = 100 - 100/(1+1) = 50
557
- expect(result[6]).toBeCloseTo(50, 0)
558
- })
559
-
560
- it('should handle period=1', () => {
561
- // period=1 只需要 2 条数据
562
- const prices = [10, 12]
563
- const data = createTestData(prices)
564
- const result = calcRSIData(data, 1)
565
-
566
- // 第一个变化是 +2,所以 avgGain = 2, avgLoss = 0
567
- // RSI = 100
568
- // result[0] = undefined, result[1] = 100
569
- expect(result[0]).toBeUndefined()
570
- expect(result[1]).toBe(100)
571
- })
572
-
573
- it('should produce consistent results with manual Wilder smoothing', () => {
574
- const prices = [10, 11, 12, 11, 12, 13, 12, 11, 12, 13]
575
- const data = createTestData(prices)
576
- const period = 3
577
- const result = calcRSIData(data, period)
578
-
579
- // 手动计算验证第一个有效值(index = period = 3)
580
- // 变化:+1, +1, -1, +1, +1, -1, -1, +1, +1
581
- // 前3个变化:+1, +1, -1
582
- // sumGain = 2, sumLoss = 1
583
- // avgGain = 2/3, avgLoss = 1/3
584
- // RS = 2, RSI = 100 - 100/(1+2) = 66.67
585
- expect(result[3]).toBeCloseTo(66.67, 1)
586
- })
587
-
588
- it('should handle large datasets efficiently', () => {
589
- const prices = Array.from({ length: 10000 }, (_, i) => 100 + Math.sin(i / 10) * 10)
590
- const data = createTestData(prices)
591
-
592
- const start = performance.now()
593
- const result = calcRSIData(data, 14)
594
- const end = performance.now()
595
-
596
- // 应在 100ms 内完成
597
- expect(end - start).toBeLessThan(100)
598
- expect(result[15]).toBeDefined()
599
- })
600
- })
601
-
602
- describe('RSI default constants', () => {
603
- it('DEFAULT_RSI_PERIOD1 should be 6', () => {
604
- expect(DEFAULT_RSI_PERIOD1).toBe(6)
605
- })
606
-
607
- it('DEFAULT_RSI_PERIOD2 should be 12', () => {
608
- expect(DEFAULT_RSI_PERIOD2).toBe(12)
609
- })
610
-
611
- it('DEFAULT_RSI_PERIOD3 should be 24', () => {
612
- expect(DEFAULT_RSI_PERIOD3).toBe(24)
613
- })
614
- })
1
+ import { describe, it, expect } from 'vitest'
2
+ import {
3
+ calcMAData,
4
+ calcBOLLData,
5
+ calcEXPMAData,
6
+ calcENEData,
7
+ calcRSIData,
8
+ DEFAULT_MA_PERIODS,
9
+ DEFAULT_BOLL_PERIOD,
10
+ DEFAULT_BOLL_MULTIPLIER,
11
+ DEFAULT_EXPMA_FAST_PERIOD,
12
+ DEFAULT_EXPMA_SLOW_PERIOD,
13
+ DEFAULT_ENE_PERIOD,
14
+ DEFAULT_ENE_DEVIATION,
15
+ DEFAULT_RSI_PERIOD1,
16
+ DEFAULT_RSI_PERIOD2,
17
+ DEFAULT_RSI_PERIOD3,
18
+ } from '../calculators'
19
+ import type { KLineData } from '@/types/price'
20
+
21
+ /**
22
+ * 创建测试用的 K 线数据
23
+ * 收盘价序列: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
24
+ */
25
+ function createTestData(prices: number[]): KLineData[] {
26
+ return prices.map((close, index) => ({
27
+ timestamp: 1000000000000 + index * 60000,
28
+ open: close - 0.5,
29
+ high: close + 0.5,
30
+ low: close - 0.5,
31
+ close,
32
+ volume: 1000 + index * 100,
33
+ }))
34
+ }
35
+
36
+ describe('calcMAData', () => {
37
+ const prices = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
38
+ const data = createTestData(prices)
39
+
40
+ it('should return array of same length as input', () => {
41
+ const result = calcMAData(data, 5)
42
+ expect(result).toHaveLength(data.length)
43
+ })
44
+
45
+ it('should return undefined for indices before period-1', () => {
46
+ const result = calcMAData(data, 5)
47
+ expect(result[0]).toBeUndefined()
48
+ expect(result[1]).toBeUndefined()
49
+ expect(result[2]).toBeUndefined()
50
+ expect(result[3]).toBeUndefined()
51
+ })
52
+
53
+ it('should calculate correct MA5 values using sliding window', () => {
54
+ const result = calcMAData(data, 5)
55
+
56
+ // MA5 at index 4: (10+11+12+13+14)/5 = 60/5 = 12
57
+ expect(result[4]).toBeCloseTo(12, 2)
58
+
59
+ // MA5 at index 5: (11+12+13+14+15)/5 = 65/5 = 13
60
+ expect(result[5]).toBeCloseTo(13, 2)
61
+
62
+ // MA5 at index 9: (15+16+17+18+19)/5 = 85/5 = 17
63
+ expect(result[9]).toBeCloseTo(17, 2)
64
+ })
65
+
66
+ it('should calculate correct MA3 values', () => {
67
+ const result = calcMAData(data, 3)
68
+
69
+ // MA3 at index 2: (10+11+12)/3 = 11
70
+ expect(result[2]).toBeCloseTo(11, 2)
71
+
72
+ // MA3 at index 3: (11+12+13)/3 = 12
73
+ expect(result[3]).toBeCloseTo(12, 2)
74
+
75
+ // MA3 at index 4: (12+13+14)/3 = 13
76
+ expect(result[4]).toBeCloseTo(13, 2)
77
+ })
78
+
79
+ it('should handle period=1 (return close price)', () => {
80
+ const result = calcMAData(data, 1)
81
+
82
+ for (let i = 0; i < data.length; i++) {
83
+ expect(result[i]).toBeCloseTo(data[i].close, 2)
84
+ }
85
+ })
86
+
87
+ it('should return all undefined when data length < period', () => {
88
+ const shortData = createTestData([10, 11, 12])
89
+ const result = calcMAData(shortData, 5)
90
+
91
+ expect(result).toHaveLength(3)
92
+ expect(result[0]).toBeUndefined()
93
+ expect(result[1]).toBeUndefined()
94
+ expect(result[2]).toBeUndefined()
95
+ })
96
+
97
+ it('should handle empty data', () => {
98
+ const result = calcMAData([], 5)
99
+ expect(result).toHaveLength(0)
100
+ })
101
+
102
+ it('should produce consistent results with manual calculation', () => {
103
+ const result = calcMAData(data, 5)
104
+
105
+ // Manual verification of sliding window
106
+ for (let i = 4; i < data.length; i++) {
107
+ let sum = 0
108
+ for (let j = 0; j < 5; j++) {
109
+ sum += data[i - j].close
110
+ }
111
+ const expected = sum / 5
112
+ expect(result[i]).toBeCloseTo(expected, 2)
113
+ }
114
+ })
115
+
116
+ it('should handle large datasets efficiently', () => {
117
+ const largePrices = Array.from({ length: 10000 }, (_, i) => 100 + i)
118
+ const largeData = createTestData(largePrices)
119
+
120
+ const start = performance.now()
121
+ const result = calcMAData(largeData, 60)
122
+ const end = performance.now()
123
+
124
+ // Should complete in reasonable time (< 100ms for 10k items)
125
+ expect(end - start).toBeLessThan(100)
126
+
127
+ // Verify last value is correct
128
+ // Last 60 values are indices 9940-9999, prices are 100+9940=10040 to 100+9999=10099
129
+ // sum = (10040 + 10099) * 60 / 2 = 604170, avg = 10069.5
130
+ const lastIdx = largeData.length - 1
131
+ expect(result[lastIdx]).toBeCloseTo(10069.5, 1)
132
+ })
133
+ })
134
+
135
+ describe('DEFAULT_MA_PERIODS', () => {
136
+ it('should contain standard MA periods', () => {
137
+ expect(DEFAULT_MA_PERIODS).toEqual([5, 10, 20, 30, 60])
138
+ })
139
+
140
+ it('should be readonly array', () => {
141
+ // Type check - this is more of a compile-time check
142
+ // but we verify the values are as expected
143
+ expect(DEFAULT_MA_PERIODS).toHaveLength(5)
144
+ expect(DEFAULT_MA_PERIODS[0]).toBe(5)
145
+ expect(DEFAULT_MA_PERIODS[4]).toBe(60)
146
+ })
147
+ })
148
+
149
+ describe('calcBOLLData', () => {
150
+ const prices = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
151
+ const data = createTestData(prices)
152
+
153
+ it('should return array of same length as input', () => {
154
+ const result = calcBOLLData(data, 5, 2)
155
+ expect(result).toHaveLength(data.length)
156
+ })
157
+
158
+ it('should return undefined for indices before period-1', () => {
159
+ const result = calcBOLLData(data, 5, 2)
160
+ expect(result[0]).toBeUndefined()
161
+ expect(result[1]).toBeUndefined()
162
+ expect(result[2]).toBeUndefined()
163
+ expect(result[3]).toBeUndefined()
164
+ })
165
+
166
+ it('should calculate correct BOLL5 values with known data', () => {
167
+ const result = calcBOLLData(data, 5, 2)
168
+
169
+ // Index 4: closes [10,11,12,13,14], MA=12, variance=2, stdDev=√2
170
+ const point4 = result[4]!
171
+ expect(point4.middle).toBeCloseTo(12, 2)
172
+ expect(point4.upper).toBeCloseTo(12 + 2 * Math.sqrt(2), 2)
173
+ expect(point4.lower).toBeCloseTo(12 - 2 * Math.sqrt(2), 2)
174
+
175
+ // Index 5: closes [11,12,13,14,15], MA=13, variance=2, stdDev=√2
176
+ const point5 = result[5]!
177
+ expect(point5.middle).toBeCloseTo(13, 2)
178
+ expect(point5.upper).toBeCloseTo(13 + 2 * Math.sqrt(2), 2)
179
+ expect(point5.lower).toBeCloseTo(13 - 2 * Math.sqrt(2), 2)
180
+ })
181
+
182
+ it('should have upper = middle + multiplier * stdDev', () => {
183
+ const result = calcBOLLData(data, 5, 2)
184
+ for (let i = 4; i < data.length; i++) {
185
+ const p = result[i]!
186
+ const stdDev = (p.upper - p.middle) / 2
187
+ expect(p.lower).toBeCloseTo(p.middle - 2 * stdDev, 2)
188
+ }
189
+ })
190
+
191
+ it('should have lower = middle - multiplier * stdDev', () => {
192
+ const result = calcBOLLData(data, 5, 2)
193
+ for (let i = 4; i < data.length; i++) {
194
+ const p = result[i]!
195
+ expect(p.lower).toBeCloseTo(p.middle - (p.upper - p.middle), 2)
196
+ }
197
+ })
198
+
199
+ it('should return all undefined when data length < period', () => {
200
+ const shortData = createTestData([10, 11, 12])
201
+ const result = calcBOLLData(shortData, 5, 2)
202
+
203
+ expect(result).toHaveLength(3)
204
+ expect(result[0]).toBeUndefined()
205
+ expect(result[1]).toBeUndefined()
206
+ expect(result[2]).toBeUndefined()
207
+ })
208
+
209
+ it('should handle empty data', () => {
210
+ const result = calcBOLLData([], 5, 2)
211
+ expect(result).toHaveLength(0)
212
+ })
213
+
214
+ it('should produce consistent results with manual sliding window', () => {
215
+ const result = calcBOLLData(data, 5, 2)
216
+
217
+ for (let i = 4; i < data.length; i++) {
218
+ let sum = 0
219
+ for (let j = 0; j < 5; j++) {
220
+ sum += data[i - j].close
221
+ }
222
+ const ma = sum / 5
223
+
224
+ let variance = 0
225
+ for (let j = 0; j < 5; j++) {
226
+ variance += Math.pow(data[i - j].close - ma, 2)
227
+ }
228
+ const stdDev = Math.sqrt(variance / 5)
229
+
230
+ expect(result[i]!.middle).toBeCloseTo(ma, 2)
231
+ expect(result[i]!.upper).toBeCloseTo(ma + 2 * stdDev, 2)
232
+ expect(result[i]!.lower).toBeCloseTo(ma - 2 * stdDev, 2)
233
+ }
234
+ })
235
+
236
+ it('should handle large datasets efficiently', () => {
237
+ const largePrices = Array.from({ length: 10000 }, (_, i) => 100 + i)
238
+ const largeData = createTestData(largePrices)
239
+
240
+ const start = performance.now()
241
+ const result = calcBOLLData(largeData, 20, 2)
242
+ const end = performance.now()
243
+
244
+ expect(end - start).toBeLessThan(100)
245
+ expect(result[19]).toBeDefined()
246
+ expect(result[19]!.middle).toBeCloseTo(109.5, 1)
247
+ })
248
+ })
249
+
250
+ describe('calcEXPMAData', () => {
251
+ const prices = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
252
+ const data = createTestData(prices)
253
+
254
+ it('should return array of same length as input', () => {
255
+ const result = calcEXPMAData(data, 12, 50)
256
+ expect(result).toHaveLength(data.length)
257
+ })
258
+
259
+ it('should have values from index 0 (dense array)', () => {
260
+ const result = calcEXPMAData(data, 12, 50)
261
+ expect(result[0]).toBeDefined()
262
+ expect(result[0]!.fast).toBeDefined()
263
+ expect(result[0]!.slow).toBeDefined()
264
+ })
265
+
266
+ it('should use first close as initial EMA', () => {
267
+ const result = calcEXPMAData(data, 12, 50)
268
+ expect(result[0]!.fast).toBeCloseTo(10, 2)
269
+ expect(result[0]!.slow).toBeCloseTo(10, 2)
270
+ })
271
+
272
+ it('should calculate correct EMA with K=2/(N+1)', () => {
273
+ const result = calcEXPMAData(data, 12, 50)
274
+ const fastK = 2 / (12 + 1)
275
+ const slowK = 2 / (50 + 1)
276
+
277
+ // Index 1: EMA(1) = C(1)*K + EMA(0)*(1-K)
278
+ const expectedFast1 = 11 * fastK + 10 * (1 - fastK)
279
+ const expectedSlow1 = 11 * slowK + 10 * (1 - slowK)
280
+ expect(result[1]!.fast).toBeCloseTo(expectedFast1, 4)
281
+ expect(result[1]!.slow).toBeCloseTo(expectedSlow1, 4)
282
+
283
+ // Index 2: continue recursion
284
+ const expectedFast2 = 12 * fastK + expectedFast1 * (1 - fastK)
285
+ const expectedSlow2 = 12 * slowK + expectedSlow1 * (1 - slowK)
286
+ expect(result[2]!.fast).toBeCloseTo(expectedFast2, 4)
287
+ expect(result[2]!.slow).toBeCloseTo(expectedSlow2, 4)
288
+ })
289
+
290
+ it('should have fast line more responsive than slow line', () => {
291
+ const result = calcEXPMAData(data, 12, 50)
292
+
293
+ for (let i = 1; i < data.length; i++) {
294
+ // Rising prices: fast EMA should be greater than slow EMA
295
+ expect(result[i]!.fast).toBeGreaterThanOrEqual(result[i]!.slow)
296
+ }
297
+ })
298
+
299
+ it('should handle empty data', () => {
300
+ const result = calcEXPMAData([], 12, 50)
301
+ expect(result).toHaveLength(0)
302
+ })
303
+
304
+ it('should handle single data point', () => {
305
+ const singleData = createTestData([42])
306
+ const result = calcEXPMAData(singleData, 12, 50)
307
+
308
+ expect(result).toHaveLength(1)
309
+ expect(result[0]!.fast).toBeCloseTo(42, 2)
310
+ expect(result[0]!.slow).toBeCloseTo(42, 2)
311
+ })
312
+
313
+ it('should produce consistent results with manual EMA recursion', () => {
314
+ const result = calcEXPMAData(data, 12, 50)
315
+ const fastK = 2 / (12 + 1)
316
+ const slowK = 2 / (50 + 1)
317
+
318
+ let fastEMA = data[0].close
319
+ let slowEMA = data[0].close
320
+
321
+ for (let i = 0; i < data.length; i++) {
322
+ expect(result[i]!.fast).toBeCloseTo(fastEMA, 6)
323
+ expect(result[i]!.slow).toBeCloseTo(slowEMA, 6)
324
+
325
+ if (i + 1 < data.length) {
326
+ fastEMA = data[i + 1].close * fastK + fastEMA * (1 - fastK)
327
+ slowEMA = data[i + 1].close * slowK + slowEMA * (1 - slowK)
328
+ }
329
+ }
330
+ })
331
+
332
+ it('should handle large datasets efficiently', () => {
333
+ const largePrices = Array.from({ length: 10000 }, (_, i) => 100 + i)
334
+ const largeData = createTestData(largePrices)
335
+
336
+ const start = performance.now()
337
+ const result = calcEXPMAData(largeData, 12, 50)
338
+ const end = performance.now()
339
+
340
+ expect(end - start).toBeLessThan(100)
341
+ expect(result[0]).toBeDefined()
342
+ expect(result[9999]).toBeDefined()
343
+ })
344
+ })
345
+
346
+ describe('calcENEData', () => {
347
+ const prices = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
348
+ const data = createTestData(prices)
349
+
350
+ it('should return array of same length as input', () => {
351
+ const result = calcENEData(data, 5, 10)
352
+ expect(result).toHaveLength(data.length)
353
+ })
354
+
355
+ it('should return undefined for indices before period-1', () => {
356
+ const result = calcENEData(data, 5, 10)
357
+ expect(result[0]).toBeUndefined()
358
+ expect(result[1]).toBeUndefined()
359
+ expect(result[2]).toBeUndefined()
360
+ expect(result[3]).toBeUndefined()
361
+ })
362
+
363
+ it('should calculate correct ENE5 values with deviation=10', () => {
364
+ const result = calcENEData(data, 5, 10)
365
+
366
+ // Index 4: MA5=12, upper=12*1.1=13.2, lower=12*0.9=10.8
367
+ const point4 = result[4]!
368
+ expect(point4.middle).toBeCloseTo(12, 2)
369
+ expect(point4.upper).toBeCloseTo(13.2, 2)
370
+ expect(point4.lower).toBeCloseTo(10.8, 2)
371
+
372
+ // Index 5: MA5=13, upper=13*1.1=14.3, lower=13*0.9=11.7
373
+ const point5 = result[5]!
374
+ expect(point5.middle).toBeCloseTo(13, 2)
375
+ expect(point5.upper).toBeCloseTo(14.3, 2)
376
+ expect(point5.lower).toBeCloseTo(11.7, 2)
377
+ })
378
+
379
+ it('should have upper = middle * (1 + deviation/100)', () => {
380
+ const result = calcENEData(data, 5, 11)
381
+ for (let i = 4; i < data.length; i++) {
382
+ const p = result[i]!
383
+ expect(p.upper).toBeCloseTo(p.middle * (1 + 11 / 100), 2)
384
+ }
385
+ })
386
+
387
+ it('should have lower = middle * (1 - deviation/100)', () => {
388
+ const result = calcENEData(data, 5, 11)
389
+ for (let i = 4; i < data.length; i++) {
390
+ const p = result[i]!
391
+ expect(p.lower).toBeCloseTo(p.middle * (1 - 11 / 100), 2)
392
+ }
393
+ })
394
+
395
+ it('should return all undefined when data length < period', () => {
396
+ const shortData = createTestData([10, 11, 12])
397
+ const result = calcENEData(shortData, 5, 10)
398
+
399
+ expect(result).toHaveLength(3)
400
+ expect(result[0]).toBeUndefined()
401
+ expect(result[1]).toBeUndefined()
402
+ expect(result[2]).toBeUndefined()
403
+ })
404
+
405
+ it('should handle empty data', () => {
406
+ const result = calcENEData([], 5, 10)
407
+ expect(result).toHaveLength(0)
408
+ })
409
+
410
+ it('should produce consistent results with manual calculation', () => {
411
+ const result = calcENEData(data, 5, 10)
412
+
413
+ for (let i = 4; i < data.length; i++) {
414
+ let sum = 0
415
+ for (let j = 0; j < 5; j++) {
416
+ sum += data[i - j].close
417
+ }
418
+ const ma = sum / 5
419
+
420
+ expect(result[i]!.middle).toBeCloseTo(ma, 2)
421
+ expect(result[i]!.upper).toBeCloseTo(ma * (1 + 10 / 100), 2)
422
+ expect(result[i]!.lower).toBeCloseTo(ma * (1 - 10 / 100), 2)
423
+ }
424
+ })
425
+
426
+ it('should handle large datasets efficiently', () => {
427
+ const largePrices = Array.from({ length: 10000 }, (_, i) => 100 + i)
428
+ const largeData = createTestData(largePrices)
429
+
430
+ const start = performance.now()
431
+ const result = calcENEData(largeData, 10, 11)
432
+ const end = performance.now()
433
+
434
+ expect(end - start).toBeLessThan(100)
435
+ expect(result[9]).toBeDefined()
436
+ expect(result[9]!.middle).toBeCloseTo(104.5, 1)
437
+ })
438
+ })
439
+
440
+ describe('BOLL default constants', () => {
441
+ it('DEFAULT_BOLL_PERIOD should be 20', () => {
442
+ expect(DEFAULT_BOLL_PERIOD).toBe(20)
443
+ })
444
+
445
+ it('DEFAULT_BOLL_MULTIPLIER should be 2', () => {
446
+ expect(DEFAULT_BOLL_MULTIPLIER).toBe(2)
447
+ })
448
+ })
449
+
450
+ describe('EXPMA default constants', () => {
451
+ it('DEFAULT_EXPMA_FAST_PERIOD should be 12', () => {
452
+ expect(DEFAULT_EXPMA_FAST_PERIOD).toBe(12)
453
+ })
454
+
455
+ it('DEFAULT_EXPMA_SLOW_PERIOD should be 50', () => {
456
+ expect(DEFAULT_EXPMA_SLOW_PERIOD).toBe(50)
457
+ })
458
+ })
459
+
460
+ describe('ENE default constants', () => {
461
+ it('DEFAULT_ENE_PERIOD should be 10', () => {
462
+ expect(DEFAULT_ENE_PERIOD).toBe(10)
463
+ })
464
+
465
+ it('DEFAULT_ENE_DEVIATION should be 11', () => {
466
+ expect(DEFAULT_ENE_DEVIATION).toBe(11)
467
+ })
468
+ })
469
+
470
+ describe('calcRSIData', () => {
471
+ // 创建测试数据:价格连续上涨序列
472
+ function createRisingPrices(count: number, start = 10): number[] {
473
+ return Array.from({ length: count }, (_, i) => start + i)
474
+ }
475
+
476
+ // 创建测试数据:价格连续下跌序列
477
+ function createFallingPrices(count: number, start = 20): number[] {
478
+ return Array.from({ length: count }, (_, i) => start - i)
479
+ }
480
+
481
+ // 创建测试数据:价格震荡序列
482
+ function createOscillatingPrices(count: number): number[] {
483
+ return Array.from({ length: count }, (_, i) => 10 + (i % 2 === 0 ? 1 : -1))
484
+ }
485
+
486
+ it('should return array of same length as input', () => {
487
+ const prices = createRisingPrices(20)
488
+ const data = createTestData(prices)
489
+ const result = calcRSIData(data, 6)
490
+ expect(result).toHaveLength(data.length)
491
+ })
492
+
493
+ it('should return undefined for first period indices', () => {
494
+ const prices = createRisingPrices(20)
495
+ const data = createTestData(prices)
496
+ const period = 6
497
+ const result = calcRSIData(data, period)
498
+
499
+ // 前 period 个值应为 undefined (index 0-5)
500
+ for (let i = 0; i < period; i++) {
501
+ expect(result[i]).toBeUndefined()
502
+ }
503
+ // 第 period 个值是第一个有效值 (index 6)
504
+ expect(result[period]).toBeDefined()
505
+ })
506
+
507
+ it('should return 100 when all gains (no losses)', () => {
508
+ const prices = createRisingPrices(20)
509
+ const data = createTestData(prices)
510
+ const period = 6
511
+ const result = calcRSIData(data, period)
512
+
513
+ // 第一个有效 RSI 值应为 100(全是上涨,没有下跌)
514
+ expect(result[period + 1]).toBe(100)
515
+ })
516
+
517
+ it('should return 0 when all losses (no gains)', () => {
518
+ const prices = createFallingPrices(20)
519
+ const data = createTestData(prices)
520
+ const period = 6
521
+ const result = calcRSIData(data, period)
522
+
523
+ // 第一个有效 RSI 值应为 0(全是下跌,没有上涨)
524
+ expect(result[period + 1]).toBe(0)
525
+ })
526
+
527
+ it('should return all undefined when data length < period+1', () => {
528
+ const prices = [10, 11, 12, 13, 14]
529
+ const data = createTestData(prices)
530
+ const period = 6
531
+ const result = calcRSIData(data, period)
532
+
533
+ // 数据不足时,所有值都应为 undefined
534
+ expect(result).toHaveLength(5)
535
+ result.forEach((v) => {
536
+ expect(v).toBeUndefined()
537
+ })
538
+ })
539
+
540
+ it('should handle empty data', () => {
541
+ const result = calcRSIData([], 6)
542
+ expect(result).toHaveLength(0)
543
+ })
544
+
545
+ it('should calculate correct RSI with known oscillating data', () => {
546
+ // 震荡价格:10, 11, 10, 11, 10, 11, 10, 11, ...
547
+ const prices = [10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11]
548
+ const data = createTestData(prices)
549
+ const period = 6
550
+ const result = calcRSIData(data, period)
551
+
552
+ // 第6个索引(period=6)是第一个有效值
553
+ // 前6个变化:+1, -1, +1, -1, +1, -1
554
+ // sumGain = 3, sumLoss = 3
555
+ // avgGain = 0.5, avgLoss = 0.5
556
+ // RS = 1, RSI = 100 - 100/(1+1) = 50
557
+ expect(result[6]).toBeCloseTo(50, 0)
558
+ })
559
+
560
+ it('should handle period=1', () => {
561
+ // period=1 只需要 2 条数据
562
+ const prices = [10, 12]
563
+ const data = createTestData(prices)
564
+ const result = calcRSIData(data, 1)
565
+
566
+ // 第一个变化是 +2,所以 avgGain = 2, avgLoss = 0
567
+ // RSI = 100
568
+ // result[0] = undefined, result[1] = 100
569
+ expect(result[0]).toBeUndefined()
570
+ expect(result[1]).toBe(100)
571
+ })
572
+
573
+ it('should produce consistent results with manual Wilder smoothing', () => {
574
+ const prices = [10, 11, 12, 11, 12, 13, 12, 11, 12, 13]
575
+ const data = createTestData(prices)
576
+ const period = 3
577
+ const result = calcRSIData(data, period)
578
+
579
+ // 手动计算验证第一个有效值(index = period = 3)
580
+ // 变化:+1, +1, -1, +1, +1, -1, -1, +1, +1
581
+ // 前3个变化:+1, +1, -1
582
+ // sumGain = 2, sumLoss = 1
583
+ // avgGain = 2/3, avgLoss = 1/3
584
+ // RS = 2, RSI = 100 - 100/(1+2) = 66.67
585
+ expect(result[3]).toBeCloseTo(66.67, 1)
586
+ })
587
+
588
+ it('should handle large datasets efficiently', () => {
589
+ const prices = Array.from({ length: 10000 }, (_, i) => 100 + Math.sin(i / 10) * 10)
590
+ const data = createTestData(prices)
591
+
592
+ const start = performance.now()
593
+ const result = calcRSIData(data, 14)
594
+ const end = performance.now()
595
+
596
+ // 应在 100ms 内完成
597
+ expect(end - start).toBeLessThan(100)
598
+ expect(result[15]).toBeDefined()
599
+ })
600
+ })
601
+
602
+ describe('RSI default constants', () => {
603
+ it('DEFAULT_RSI_PERIOD1 should be 6', () => {
604
+ expect(DEFAULT_RSI_PERIOD1).toBe(6)
605
+ })
606
+
607
+ it('DEFAULT_RSI_PERIOD2 should be 12', () => {
608
+ expect(DEFAULT_RSI_PERIOD2).toBe(12)
609
+ })
610
+
611
+ it('DEFAULT_RSI_PERIOD3 should be 24', () => {
612
+ expect(DEFAULT_RSI_PERIOD3).toBe(24)
613
+ })
614
+ })