@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.
- package/README.md +201 -201
- package/README.zh-CN.md +201 -201
- package/dist/engine/renderers/webgl/candleSurface.js +47 -47
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -2
- package/dist/version.js.map +1 -1
- package/package.json +129 -122
- package/src/__tests__/signal.test.ts +124 -124
- package/src/config/chartSettings.ts +66 -66
- package/src/controllers/__tests__/drawing.test.ts +214 -214
- package/src/controllers/__tests__/indicatorSelector.test.ts +481 -481
- package/src/controllers/__tests__/toolbar.test.ts +225 -225
- package/src/controllers/createChartController.ts +665 -665
- package/src/controllers/createDrawingController.ts +96 -96
- package/src/controllers/createIndicatorSelectorController.ts +307 -307
- package/src/controllers/createToolbarController.ts +146 -146
- package/src/controllers/index.ts +19 -19
- package/src/controllers/types.ts +284 -284
- package/src/engine/__tests__/chart.dpr.test.ts +401 -401
- package/src/engine/__tests__/paneRenderer.resize.test.ts +92 -92
- package/src/engine/chart-store.ts +121 -121
- package/src/engine/chart.d.ts +617 -617
- package/src/engine/chart.ts +2815 -2815
- package/src/engine/controller/__tests__/interaction.dpr.test.ts +259 -259
- package/src/engine/controller/interaction.ts +722 -722
- package/src/engine/controller/markerInteraction.ts +130 -130
- package/src/engine/controller/pinchTracker.ts +82 -82
- package/src/engine/controller/tooltipPosition.ts +48 -48
- package/src/engine/draw/__tests__/pixelAlign.spec.ts +176 -176
- package/src/engine/draw/pixelAlign.ts +259 -259
- package/src/engine/drawing/index.ts +655 -655
- package/src/engine/drawing/interaction.ts +842 -842
- package/src/engine/drawing/plugin.ts +343 -343
- package/src/engine/indicators/__tests__/__fixtures__/golden/atr.json +38 -38
- package/src/engine/indicators/__tests__/__fixtures__/golden/dema.json +14 -14
- package/src/engine/indicators/__tests__/__fixtures__/golden/hma.json +14 -14
- package/src/engine/indicators/__tests__/__fixtures__/golden/index.ts +55 -55
- package/src/engine/indicators/__tests__/__fixtures__/golden/kama.json +14 -14
- package/src/engine/indicators/__tests__/__fixtures__/golden/tema.json +14 -14
- package/src/engine/indicators/__tests__/__fixtures__/golden/wma.json +40 -40
- package/src/engine/indicators/__tests__/__fixtures__/synthetic.ts +65 -65
- package/src/engine/indicators/__tests__/_propertyAssertions.ts +76 -76
- package/src/engine/indicators/__tests__/atr.test.ts +153 -153
- package/src/engine/indicators/__tests__/calculators.test.ts +614 -614
- package/src/engine/indicators/__tests__/cmf-mfi.test.ts +100 -100
- package/src/engine/indicators/__tests__/dema.test.ts +73 -73
- package/src/engine/indicators/__tests__/donchian.test.ts +70 -70
- package/src/engine/indicators/__tests__/hma.test.ts +73 -73
- package/src/engine/indicators/__tests__/ichimoku.test.ts +105 -105
- package/src/engine/indicators/__tests__/kama.test.ts +80 -80
- package/src/engine/indicators/__tests__/keltner.test.ts +65 -65
- package/src/engine/indicators/__tests__/pivot-fib.test.ts +110 -110
- package/src/engine/indicators/__tests__/roc.test.ts +68 -68
- package/src/engine/indicators/__tests__/sar.test.ts +86 -86
- package/src/engine/indicators/__tests__/scheduler.test.ts +831 -831
- package/src/engine/indicators/__tests__/soa.test.ts +533 -533
- package/src/engine/indicators/__tests__/structure.test.ts +110 -110
- package/src/engine/indicators/__tests__/supertrend.test.ts +65 -65
- package/src/engine/indicators/__tests__/tema.test.ts +68 -68
- package/src/engine/indicators/__tests__/trix.test.ts +70 -70
- package/src/engine/indicators/__tests__/volatility.test.ts +117 -117
- package/src/engine/indicators/__tests__/volume.test.ts +115 -115
- package/src/engine/indicators/__tests__/volumeProfile.test.ts +74 -74
- package/src/engine/indicators/__tests__/vwap.test.ts +69 -69
- package/src/engine/indicators/__tests__/wma.test.ts +112 -112
- package/src/engine/indicators/__tests__/zones.test.ts +95 -95
- package/src/engine/indicators/atrState.ts +27 -27
- package/src/engine/indicators/bollState.ts +51 -51
- package/src/engine/indicators/calculators.ts +2593 -2593
- package/src/engine/indicators/cciState.ts +25 -25
- package/src/engine/indicators/chaikinVolState.ts +32 -32
- package/src/engine/indicators/cmfState.ts +27 -27
- package/src/engine/indicators/demaState.ts +27 -27
- package/src/engine/indicators/donchianState.ts +43 -43
- package/src/engine/indicators/eneState.ts +43 -43
- package/src/engine/indicators/expmaState.ts +43 -43
- package/src/engine/indicators/fastkState.ts +25 -25
- package/src/engine/indicators/fibState.ts +41 -41
- package/src/engine/indicators/hmaState.ts +27 -27
- package/src/engine/indicators/hvState.ts +28 -28
- package/src/engine/indicators/ichimokuState.ts +70 -70
- package/src/engine/indicators/indicator.worker.ts +169 -169
- package/src/engine/indicators/indicatorDefinitionRegistry.ts +62 -62
- package/src/engine/indicators/indicatorMetadata.ts +110 -110
- package/src/engine/indicators/indicatorRegistry.ts +106 -106
- package/src/engine/indicators/indicatorRuntime.ts +1548 -1548
- package/src/engine/indicators/kamaState.ts +34 -34
- package/src/engine/indicators/keltnerState.ts +49 -49
- package/src/engine/indicators/kstState.ts +42 -42
- package/src/engine/indicators/maState.ts +36 -36
- package/src/engine/indicators/macdState.ts +76 -76
- package/src/engine/indicators/mfiState.ts +27 -27
- package/src/engine/indicators/momState.ts +25 -25
- package/src/engine/indicators/obvState.ts +25 -25
- package/src/engine/indicators/parkinsonState.ts +28 -28
- package/src/engine/indicators/pivotState.ts +51 -51
- package/src/engine/indicators/pvtState.ts +25 -25
- package/src/engine/indicators/rocState.ts +27 -27
- package/src/engine/indicators/rsiState.ts +65 -65
- package/src/engine/indicators/sarState.ts +41 -41
- package/src/engine/indicators/scheduler.ts +1205 -1205
- package/src/engine/indicators/soa.ts +352 -352
- package/src/engine/indicators/stateComposer.ts +1262 -1262
- package/src/engine/indicators/stochState.ts +26 -26
- package/src/engine/indicators/structureState.ts +69 -69
- package/src/engine/indicators/supertrendState.ts +37 -37
- package/src/engine/indicators/temaState.ts +27 -27
- package/src/engine/indicators/trixState.ts +35 -35
- package/src/engine/indicators/vmaState.ts +27 -27
- package/src/engine/indicators/volumeProfileState.ts +63 -63
- package/src/engine/indicators/vwapState.ts +29 -29
- package/src/engine/indicators/wmaState.ts +27 -27
- package/src/engine/indicators/wmsrState.ts +25 -25
- package/src/engine/indicators/workerProtocol.ts +613 -613
- package/src/engine/indicators/zonesState.ts +47 -47
- package/src/engine/layout/pane.ts +161 -161
- package/src/engine/marker/registry.ts +265 -265
- package/src/engine/paneRenderer.ts +169 -169
- package/src/engine/renderers/Indicator/atr.ts +237 -237
- package/src/engine/renderers/Indicator/boll.ts +317 -317
- package/src/engine/renderers/Indicator/cci.ts +275 -275
- package/src/engine/renderers/Indicator/chaikinVol.ts +138 -138
- package/src/engine/renderers/Indicator/cmf.ts +137 -137
- package/src/engine/renderers/Indicator/dema.ts +136 -136
- package/src/engine/renderers/Indicator/donchian.ts +137 -137
- package/src/engine/renderers/Indicator/ene.ts +271 -271
- package/src/engine/renderers/Indicator/expma.ts +197 -197
- package/src/engine/renderers/Indicator/fastk.ts +316 -316
- package/src/engine/renderers/Indicator/fib.ts +141 -141
- package/src/engine/renderers/Indicator/hma.ts +136 -136
- package/src/engine/renderers/Indicator/hv.ts +124 -124
- package/src/engine/renderers/Indicator/ichimoku.ts +181 -181
- package/src/engine/renderers/Indicator/index.ts +241 -241
- package/src/engine/renderers/Indicator/indicatorData.ts +650 -650
- package/src/engine/renderers/Indicator/kama.ts +136 -136
- package/src/engine/renderers/Indicator/keltner.ts +137 -137
- package/src/engine/renderers/Indicator/kst.ts +302 -302
- package/src/engine/renderers/Indicator/ma.ts +200 -200
- package/src/engine/renderers/Indicator/macd.ts +477 -477
- package/src/engine/renderers/Indicator/macdLegend.ts +141 -141
- package/src/engine/renderers/Indicator/mainIndicatorLegend.ts +272 -272
- package/src/engine/renderers/Indicator/mfi.ts +142 -142
- package/src/engine/renderers/Indicator/mom.ts +311 -311
- package/src/engine/renderers/Indicator/obv.ts +123 -123
- package/src/engine/renderers/Indicator/parkinson.ts +124 -124
- package/src/engine/renderers/Indicator/pivot.ts +131 -131
- package/src/engine/renderers/Indicator/pvt.ts +123 -123
- package/src/engine/renderers/Indicator/roc.ts +143 -143
- package/src/engine/renderers/Indicator/rsi.ts +390 -390
- package/src/engine/renderers/Indicator/sar.ts +113 -113
- package/src/engine/renderers/Indicator/scale/atr_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/cci_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/fastk_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/indicator_scale.ts +204 -204
- package/src/engine/renderers/Indicator/scale/kst_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/macd_scale.ts +22 -22
- package/src/engine/renderers/Indicator/scale/mom_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/rsi_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/stoch_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/volume_scale.ts +26 -26
- package/src/engine/renderers/Indicator/scale/wmsr_scale.ts +19 -19
- package/src/engine/renderers/Indicator/stoch.ts +359 -359
- package/src/engine/renderers/Indicator/structure.ts +126 -126
- package/src/engine/renderers/Indicator/subPaneConfig.ts +265 -265
- package/src/engine/renderers/Indicator/supertrend.ts +115 -115
- package/src/engine/renderers/Indicator/tema.ts +136 -136
- package/src/engine/renderers/Indicator/trix.ts +158 -158
- package/src/engine/renderers/Indicator/vma.ts +124 -124
- package/src/engine/renderers/Indicator/volumeProfile.ts +125 -125
- package/src/engine/renderers/Indicator/vwap.ts +123 -123
- package/src/engine/renderers/Indicator/wma.ts +136 -136
- package/src/engine/renderers/Indicator/wmsr.ts +328 -328
- package/src/engine/renderers/Indicator/zones.ts +104 -104
- package/src/engine/renderers/__tests__/boll.renderer.test.ts +314 -314
- package/src/engine/renderers/__tests__/ene.renderer.test.ts +305 -305
- package/src/engine/renderers/__tests__/expma.renderer.test.ts +279 -279
- package/src/engine/renderers/__tests__/ma.renderer.test.ts +426 -426
- package/src/engine/renderers/__tests__/mainIndicatorLegend.renderer.test.ts +502 -502
- package/src/engine/renderers/__tests__/yAxis.renderer.test.ts +173 -173
- package/src/engine/renderers/candle.ts +459 -459
- package/src/engine/renderers/crosshair.ts +69 -69
- package/src/engine/renderers/customMarkers.ts +162 -162
- package/src/engine/renderers/extremaMarkers.ts +246 -246
- package/src/engine/renderers/gridLines.ts +90 -90
- package/src/engine/renderers/lastPrice.ts +97 -97
- package/src/engine/renderers/paneTitle.ts +136 -136
- package/src/engine/renderers/subVolume.ts +236 -236
- package/src/engine/renderers/timeAxis.ts +121 -121
- package/src/engine/renderers/webgl/candleSurface.ts +955 -955
- package/src/engine/renderers/webgl/sharedWebGLSurface.ts +146 -146
- package/src/engine/renderers/yAxis.ts +105 -105
- package/src/engine/scale/__tests__/logFormula.spec.ts +148 -148
- package/src/engine/scale/logFormula.ts +130 -130
- package/src/engine/scale/price.ts +39 -39
- package/src/engine/scale/priceScale.ts +264 -264
- package/src/engine/subPaneManager.ts +427 -427
- package/src/engine/theme/colors.ts +642 -642
- package/src/engine/theme/fonts.ts +20 -20
- package/src/engine/utils/klineConfig.ts +49 -49
- package/src/engine/utils/tickCount.ts +11 -11
- package/src/engine/utils/tickPosition.ts +214 -214
- package/src/engine/utils/zoom.ts +83 -83
- package/src/engine/viewport/viewport.ts +67 -67
- package/src/index.ts +3 -3
- package/src/plugin/ConfigManager.ts +93 -93
- package/src/plugin/EventBus.ts +77 -77
- package/src/plugin/HookSystem.ts +106 -106
- package/src/plugin/PluginHost.ts +243 -243
- package/src/plugin/PluginRegistry.ts +92 -92
- package/src/plugin/StateStore.ts +73 -73
- package/src/plugin/index.ts +19 -19
- package/src/plugin/rendererPluginManager.ts +368 -368
- package/src/plugin/stateKeys.ts +8 -8
- package/src/plugin/types.ts +526 -526
- package/src/reactivity/index.ts +2 -2
- package/src/reactivity/signal.ts +119 -119
- package/src/semantic/controller.ts +251 -251
- package/src/semantic/drawShape.ts +260 -260
- package/src/semantic/index.ts +28 -28
- package/src/semantic/schema.json +256 -256
- package/src/semantic/types.ts +251 -251
- package/src/semantic/validator.ts +349 -349
- package/src/types/kLine.ts +13 -13
- package/src/types/price.ts +56 -56
- package/src/types/volumePrice.ts +33 -33
- package/src/utils/dateFormat.ts +208 -208
- package/src/utils/kLineDraw/axis.ts +562 -562
- package/src/utils/priceToY.ts +34 -34
- package/src/utils/volumePrice.ts +202 -202
- package/src/version.ts +1 -1
|
@@ -1,477 +1,477 @@
|
|
|
1
|
-
import type { RendererPluginWithHost, RenderContext, PluginHost } from '../../../plugin'
|
|
2
|
-
import { RENDERER_PRIORITY } from '../../../plugin'
|
|
3
|
-
import type { KLineData } from '../../../types/price'
|
|
4
|
-
import { getColors } from '../../theme/colors'
|
|
5
|
-
import type { ChartTheme, ThemeColors } from '../../theme/colors'
|
|
6
|
-
import { alignToPhysicalPixelCenter } from '../../draw/pixelAlign'
|
|
7
|
-
import type { MACDRenderState } from '../../indicators/macdState'
|
|
8
|
-
import { createMACDStateKey } from '../../indicators/macdState'
|
|
9
|
-
import type { MACDPoint } from '../../indicators/calculators'
|
|
10
|
-
import { calcMACDData } from '../../indicators/calculators'
|
|
11
|
-
import { Indicator } from '../../indicators/indicatorDefinitionRegistry'
|
|
12
|
-
import { resolveStateKey } from '../../indicators/indicatorMetadata'
|
|
13
|
-
import type { IndicatorScheduler } from '../../indicators/scheduler'
|
|
14
|
-
|
|
15
|
-
type LinePoint = { x: number; y: number }
|
|
16
|
-
|
|
17
|
-
export interface MACDConfig {
|
|
18
|
-
/** 快线周期(默认 12) */
|
|
19
|
-
fastPeriod?: number
|
|
20
|
-
/** 慢线周期(默认 26) */
|
|
21
|
-
slowPeriod?: number
|
|
22
|
-
/** DEA 周期(默认 9) */
|
|
23
|
-
signalPeriod?: number
|
|
24
|
-
/** 是否显示 DIF 线 */
|
|
25
|
-
showDIF?: boolean
|
|
26
|
-
/** 是否显示 DEA 线 */
|
|
27
|
-
showDEA?: boolean
|
|
28
|
-
/** 是否显示 MACD 柱 */
|
|
29
|
-
showBAR?: boolean
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface MACDRendererOptions {
|
|
33
|
-
/** 目标 pane ID(默认 'sub') */
|
|
34
|
-
paneId?: string
|
|
35
|
-
/** 初始配置 */
|
|
36
|
-
config?: MACDConfig
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function getMACDStateKey(host: PluginHost | null, paneId: string): string | null {
|
|
40
|
-
const scheduler = host?.getService<IndicatorScheduler>('indicatorScheduler')
|
|
41
|
-
if (!scheduler) {
|
|
42
|
-
console.warn('[MACDRenderer] Scheduler not available via service locator')
|
|
43
|
-
return null
|
|
44
|
-
}
|
|
45
|
-
const meta = scheduler.getIndicatorMetadata('macd')
|
|
46
|
-
if (!meta) {
|
|
47
|
-
console.warn("[MACDRenderer] Indicator metadata for 'macd' not found, skip rendering")
|
|
48
|
-
return null
|
|
49
|
-
}
|
|
50
|
-
return resolveStateKey(meta.stateKey, paneId)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* 创建 MACD 渲染器插件
|
|
55
|
-
* 从 StateStore 读取 MACD 状态,不再内联计算
|
|
56
|
-
*/
|
|
57
|
-
export function createMACDRendererPlugin(options: MACDRendererOptions = {}): RendererPluginWithHost {
|
|
58
|
-
const { paneId = 'sub', config: initialConfig = {} } = options
|
|
59
|
-
|
|
60
|
-
let pluginHost: PluginHost | null = null
|
|
61
|
-
|
|
62
|
-
function resolveKey(): string | null {
|
|
63
|
-
return getMACDStateKey(pluginHost, paneId)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const config: Required<MACDConfig> = {
|
|
67
|
-
fastPeriod: 12,
|
|
68
|
-
slowPeriod: 26,
|
|
69
|
-
signalPeriod: 9,
|
|
70
|
-
showDIF: true,
|
|
71
|
-
showDEA: true,
|
|
72
|
-
showBAR: true,
|
|
73
|
-
...initialConfig,
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// 线条点缓存(用于 WebGL/Canvas2D 渲染)
|
|
77
|
-
let cachedLineKey = ''
|
|
78
|
-
let cachedDifPoints: LinePoint[] = []
|
|
79
|
-
let cachedDeaPoints: LinePoint[] = []
|
|
80
|
-
|
|
81
|
-
function clearLineCache() {
|
|
82
|
-
cachedLineKey = ''
|
|
83
|
-
cachedDifPoints = []
|
|
84
|
-
cachedDeaPoints = []
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// 构建线条缓存 key
|
|
88
|
-
function buildLineCacheKey(
|
|
89
|
-
range: { start: number; end: number },
|
|
90
|
-
kLineCenters: number[],
|
|
91
|
-
pane: RenderContext['pane'],
|
|
92
|
-
displayMin: number,
|
|
93
|
-
displayMax: number,
|
|
94
|
-
stateTimestamp: number
|
|
95
|
-
): string {
|
|
96
|
-
const dr = pane.yAxis.getDisplayRange()
|
|
97
|
-
return [
|
|
98
|
-
stateTimestamp,
|
|
99
|
-
range.start,
|
|
100
|
-
range.end,
|
|
101
|
-
kLineCenters.length,
|
|
102
|
-
kLineCenters[0]?.toFixed(2) ?? 'n',
|
|
103
|
-
kLineCenters[kLineCenters.length - 1]?.toFixed(2) ?? 'n',
|
|
104
|
-
displayMax.toFixed(6),
|
|
105
|
-
displayMin.toFixed(6),
|
|
106
|
-
pane.yAxis.getPriceOffset().toFixed(6),
|
|
107
|
-
pane.yAxis.getScaleType(),
|
|
108
|
-
pane.height.toFixed(2),
|
|
109
|
-
config.showDIF,
|
|
110
|
-
config.showDEA,
|
|
111
|
-
].join('|')
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return {
|
|
115
|
-
name: `macd_${paneId}`,
|
|
116
|
-
version: '1.0.0',
|
|
117
|
-
description: 'MACD 指标渲染器',
|
|
118
|
-
debugName: 'MACD',
|
|
119
|
-
paneId: paneId,
|
|
120
|
-
priority: RENDERER_PRIORITY.INDICATOR,
|
|
121
|
-
|
|
122
|
-
onInstall(host: PluginHost) {
|
|
123
|
-
pluginHost = host
|
|
124
|
-
},
|
|
125
|
-
|
|
126
|
-
getDeclaredNamespaces() {
|
|
127
|
-
const key = resolveKey()
|
|
128
|
-
return key ? [key] : []
|
|
129
|
-
},
|
|
130
|
-
|
|
131
|
-
draw(context: RenderContext) {
|
|
132
|
-
const { ctx, pane, data, range, scrollLeft, dpr, kLineCenters, lineWebGLSurface } = context
|
|
133
|
-
const klineData = data as KLineData[]
|
|
134
|
-
const colors = getColors(context.theme)
|
|
135
|
-
|
|
136
|
-
// 从 StateStore 读取 MACD 状态
|
|
137
|
-
const stateKey = resolveKey()
|
|
138
|
-
if (!stateKey) return
|
|
139
|
-
const state = pluginHost?.getSharedState<MACDRenderState>(stateKey)
|
|
140
|
-
if (!state || state.visibleMin > state.visibleMax) return
|
|
141
|
-
if (klineData.length < config.slowPeriod) return
|
|
142
|
-
|
|
143
|
-
const macdData = state.series
|
|
144
|
-
if (!macdData || macdData.length === 0) return
|
|
145
|
-
|
|
146
|
-
// 使用 state 中的极值,或回退到计算
|
|
147
|
-
let valueMin = state.visibleMin
|
|
148
|
-
let valueMax = state.visibleMax
|
|
149
|
-
|
|
150
|
-
// 添加 padding
|
|
151
|
-
const padding = Math.max(0.05, (valueMax - valueMin) * 0.1)
|
|
152
|
-
valueMin = valueMin - padding
|
|
153
|
-
valueMax = valueMax + padding
|
|
154
|
-
const valueRange = valueMax - valueMin || 1
|
|
155
|
-
|
|
156
|
-
const displayRange = pane.yAxis.getDisplayRange({ minPrice: valueMin, maxPrice: valueMax })
|
|
157
|
-
const displayMin = displayRange.minPrice
|
|
158
|
-
const displayMax = displayRange.maxPrice
|
|
159
|
-
const displayValueRange = displayMax - displayMin || 1
|
|
160
|
-
|
|
161
|
-
// 零轴位置
|
|
162
|
-
const zeroY = pane.height - (0 - displayMin) / displayValueRange * pane.height
|
|
163
|
-
|
|
164
|
-
const drawStart = Math.max(range.start, config.slowPeriod - 1)
|
|
165
|
-
const drawEnd = Math.min(range.end, klineData.length)
|
|
166
|
-
|
|
167
|
-
// 绘制 MACD 柱状图(WebGL 优先)
|
|
168
|
-
if (config.showBAR) {
|
|
169
|
-
const alignedZeroY = Math.round(zeroY * dpr) / dpr
|
|
170
|
-
|
|
171
|
-
const maxBars = Math.max(1, drawEnd - drawStart)
|
|
172
|
-
const barUpBuf = new Float32Array(maxBars * 4)
|
|
173
|
-
const barUpLightBuf = new Float32Array(maxBars * 4)
|
|
174
|
-
const barDownBuf = new Float32Array(maxBars * 4)
|
|
175
|
-
const barDownLightBuf = new Float32Array(maxBars * 4)
|
|
176
|
-
let barUpCount = 0, barUpLightCount = 0, barDownCount = 0, barDownLightCount = 0
|
|
177
|
-
|
|
178
|
-
for (let i = drawStart; i < drawEnd; i++) {
|
|
179
|
-
const point = macdData[i]
|
|
180
|
-
if (!point) continue
|
|
181
|
-
|
|
182
|
-
const barRect = context.kBarRects[i - range.start]
|
|
183
|
-
if (!barRect) continue
|
|
184
|
-
|
|
185
|
-
const barY = pane.height - (point.macd - displayMin) / displayValueRange * pane.height
|
|
186
|
-
const isPositive = point.macd >= 0
|
|
187
|
-
|
|
188
|
-
const prevPoint = i > 0 ? macdData[i - 1] : null
|
|
189
|
-
const isRising = prevPoint ? point.macd >= prevPoint.macd : true
|
|
190
|
-
|
|
191
|
-
const alignedBarY = Math.round(barY * dpr) / dpr
|
|
192
|
-
const minBarHPx = 1 / dpr
|
|
193
|
-
|
|
194
|
-
let buf: Float32Array
|
|
195
|
-
let idx: number
|
|
196
|
-
|
|
197
|
-
if (isPositive) {
|
|
198
|
-
const rawH = alignedZeroY - alignedBarY
|
|
199
|
-
const finalH = rawH <= 0 ? minBarHPx : Math.max(rawH, minBarHPx)
|
|
200
|
-
const finalBarY = rawH <= 0 ? alignedZeroY - minBarHPx : alignedZeroY - finalH
|
|
201
|
-
if (isRising) { buf = barUpBuf; idx = barUpCount++ }
|
|
202
|
-
else { buf = barUpLightBuf; idx = barUpLightCount++ }
|
|
203
|
-
const off = idx * 4
|
|
204
|
-
buf[off] = barRect.x; buf[off + 1] = finalBarY; buf[off + 2] = barRect.width; buf[off + 3] = finalH
|
|
205
|
-
} else {
|
|
206
|
-
const rawH = alignedBarY - alignedZeroY
|
|
207
|
-
const finalH = rawH <= 0 ? minBarHPx : Math.max(rawH, minBarHPx)
|
|
208
|
-
if (isRising) { buf = barDownLightBuf; idx = barDownLightCount++ }
|
|
209
|
-
else { buf = barDownBuf; idx = barDownCount++ }
|
|
210
|
-
const off = idx * 4
|
|
211
|
-
buf[off] = barRect.x; buf[off + 1] = alignedZeroY; buf[off + 2] = barRect.width; buf[off + 3] = finalH
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const usedWebGL = drawMacdBarsWithWebGL(context, barUpBuf, barUpCount, barUpLightBuf, barUpLightCount, barDownBuf, barDownCount, barDownLightBuf, barDownLightCount)
|
|
216
|
-
if (!usedWebGL) {
|
|
217
|
-
drawMacdBarsWithCanvas2D(ctx, scrollLeft, colors, barUpBuf, barUpCount, barUpLightBuf, barUpLightCount, barDownBuf, barDownCount, barDownLightBuf, barDownLightCount)
|
|
218
|
-
} else {
|
|
219
|
-
compositeMacdWebGL(ctx, context)
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// 更新线条点缓存
|
|
224
|
-
const lineCacheKey = buildLineCacheKey(range, kLineCenters, pane, displayMin, displayMax, state.timestamp)
|
|
225
|
-
if (cachedLineKey !== lineCacheKey) {
|
|
226
|
-
cachedLineKey = lineCacheKey
|
|
227
|
-
cachedDifPoints = []
|
|
228
|
-
cachedDeaPoints = []
|
|
229
|
-
|
|
230
|
-
if (config.showDIF) {
|
|
231
|
-
for (let i = drawStart; i < drawEnd; i++) {
|
|
232
|
-
const point = macdData[i]
|
|
233
|
-
if (!point) continue
|
|
234
|
-
const centerX = kLineCenters[i - range.start]
|
|
235
|
-
if (centerX === undefined) continue
|
|
236
|
-
const logicY = pane.height - (point.dif - displayMin) / displayValueRange * pane.height
|
|
237
|
-
cachedDifPoints.push({ x: centerX, y: logicY })
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (config.showDEA) {
|
|
242
|
-
for (let i = drawStart; i < drawEnd; i++) {
|
|
243
|
-
const point = macdData[i]
|
|
244
|
-
if (!point) continue
|
|
245
|
-
const centerX = kLineCenters[i - range.start]
|
|
246
|
-
if (centerX === undefined) continue
|
|
247
|
-
const logicY = pane.height - (point.dea - displayMin) / displayValueRange * pane.height
|
|
248
|
-
cachedDeaPoints.push({ x: centerX, y: logicY })
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// 绘制 DIF/DEA 线(WebGL 优先,Canvas2D 回退)
|
|
254
|
-
const enableWebGL = context.settings?.enableWebGLRendering !== false
|
|
255
|
-
let usedWebGLForLines = false
|
|
256
|
-
if (enableWebGL && lineWebGLSurface?.isAvailable()) {
|
|
257
|
-
const lines: Array<{ points: LinePoint[]; width: number; color: string }> = []
|
|
258
|
-
if (config.showDIF && cachedDifPoints.length >= 2) {
|
|
259
|
-
lines.push({ points: cachedDifPoints, width: 1, color: colors.MACD.DIF })
|
|
260
|
-
}
|
|
261
|
-
if (config.showDEA && cachedDeaPoints.length >= 2) {
|
|
262
|
-
lines.push({ points: cachedDeaPoints, width: 1, color: colors.MACD.DEA })
|
|
263
|
-
}
|
|
264
|
-
const allOk = lines.length > 0 && lineWebGLSurface.drawLineStrips(lines, scrollLeft)
|
|
265
|
-
if (allOk) {
|
|
266
|
-
usedWebGLForLines = true
|
|
267
|
-
lineWebGLSurface.compositeTo(ctx, { imageSmoothingEnabled: false })
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (!usedWebGLForLines) {
|
|
272
|
-
drawMacdLinesWithCanvas2D(ctx, scrollLeft, colors, cachedDifPoints, cachedDeaPoints, config)
|
|
273
|
-
}
|
|
274
|
-
},
|
|
275
|
-
|
|
276
|
-
onDataUpdate() {
|
|
277
|
-
clearLineCache()
|
|
278
|
-
},
|
|
279
|
-
|
|
280
|
-
getConfig() {
|
|
281
|
-
return { ...config }
|
|
282
|
-
},
|
|
283
|
-
|
|
284
|
-
setConfig(newConfig: Record<string, unknown>) {
|
|
285
|
-
let needClearLineCache = false
|
|
286
|
-
if ('fastPeriod' in newConfig && newConfig.fastPeriod !== config.fastPeriod) {
|
|
287
|
-
clearLineCache()
|
|
288
|
-
}
|
|
289
|
-
if ('slowPeriod' in newConfig && newConfig.slowPeriod !== config.slowPeriod) {
|
|
290
|
-
clearLineCache()
|
|
291
|
-
}
|
|
292
|
-
if ('signalPeriod' in newConfig && newConfig.signalPeriod !== config.signalPeriod) {
|
|
293
|
-
clearLineCache()
|
|
294
|
-
}
|
|
295
|
-
if ('showDIF' in newConfig && newConfig.showDIF !== config.showDIF) {
|
|
296
|
-
needClearLineCache = true
|
|
297
|
-
}
|
|
298
|
-
if ('showDEA' in newConfig && newConfig.showDEA !== config.showDEA) {
|
|
299
|
-
needClearLineCache = true
|
|
300
|
-
}
|
|
301
|
-
Object.assign(config, newConfig)
|
|
302
|
-
if (needClearLineCache) {
|
|
303
|
-
clearLineCache()
|
|
304
|
-
}
|
|
305
|
-
},
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
function drawMacdBarsWithWebGL(
|
|
310
|
-
context: RenderContext,
|
|
311
|
-
barUpBuf: Float32Array, barUpCount: number,
|
|
312
|
-
barUpLightBuf: Float32Array, barUpLightCount: number,
|
|
313
|
-
barDownBuf: Float32Array, barDownCount: number,
|
|
314
|
-
barDownLightBuf: Float32Array, barDownLightCount: number
|
|
315
|
-
): boolean {
|
|
316
|
-
const colors = getColors(context.theme)
|
|
317
|
-
if (context.settings?.enableWebGLRendering === false) return false
|
|
318
|
-
const surface = context.candleWebGLSurface
|
|
319
|
-
if (!surface || !surface.isAvailable()) return false
|
|
320
|
-
|
|
321
|
-
surface.clear()
|
|
322
|
-
|
|
323
|
-
const ok1 = barUpCount === 0 || surface.drawRectBuffer(barUpBuf.subarray(0, barUpCount * 4), barUpCount, colors.MACD.BAR_UP, context.scrollLeft)
|
|
324
|
-
const ok2 = barUpLightCount === 0 || surface.drawRectBuffer(barUpLightBuf.subarray(0, barUpLightCount * 4), barUpLightCount, colors.MACD.BAR_UP_LIGHT, context.scrollLeft)
|
|
325
|
-
const ok3 = barDownCount === 0 || surface.drawRectBuffer(barDownBuf.subarray(0, barDownCount * 4), barDownCount, colors.MACD.BAR_DOWN, context.scrollLeft)
|
|
326
|
-
const ok4 = barDownLightCount === 0 || surface.drawRectBuffer(barDownLightBuf.subarray(0, barDownLightCount * 4), barDownLightCount, colors.MACD.BAR_DOWN_LIGHT, context.scrollLeft)
|
|
327
|
-
|
|
328
|
-
return ok1 && ok2 && ok3 && ok4
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
function drawMacdBarsWithCanvas2D(
|
|
332
|
-
ctx: CanvasRenderingContext2D,
|
|
333
|
-
scrollLeft: number,
|
|
334
|
-
colors: { MACD: { BAR_UP: string; BAR_UP_LIGHT: string; BAR_DOWN: string; BAR_DOWN_LIGHT: string } },
|
|
335
|
-
barUpBuf: Float32Array, barUpCount: number,
|
|
336
|
-
barUpLightBuf: Float32Array, barUpLightCount: number,
|
|
337
|
-
barDownBuf: Float32Array, barDownCount: number,
|
|
338
|
-
barDownLightBuf: Float32Array, barDownLightCount: number
|
|
339
|
-
): void {
|
|
340
|
-
ctx.save()
|
|
341
|
-
ctx.translate(-scrollLeft, 0)
|
|
342
|
-
|
|
343
|
-
ctx.fillStyle = colors.MACD.BAR_UP
|
|
344
|
-
for (let i = 0; i < barUpCount; i++) {
|
|
345
|
-
const off = i * 4
|
|
346
|
-
ctx.fillRect(barUpBuf[off], barUpBuf[off + 1], barUpBuf[off + 2], barUpBuf[off + 3])
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
ctx.fillStyle = colors.MACD.BAR_UP_LIGHT
|
|
350
|
-
for (let i = 0; i < barUpLightCount; i++) {
|
|
351
|
-
const off = i * 4
|
|
352
|
-
ctx.fillRect(barUpLightBuf[off], barUpLightBuf[off + 1], barUpLightBuf[off + 2], barUpLightBuf[off + 3])
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
ctx.fillStyle = colors.MACD.BAR_DOWN
|
|
356
|
-
for (let i = 0; i < barDownCount; i++) {
|
|
357
|
-
const off = i * 4
|
|
358
|
-
ctx.fillRect(barDownBuf[off], barDownBuf[off + 1], barDownBuf[off + 2], barDownBuf[off + 3])
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
ctx.fillStyle = colors.MACD.BAR_DOWN_LIGHT
|
|
362
|
-
for (let i = 0; i < barDownLightCount; i++) {
|
|
363
|
-
const off = i * 4
|
|
364
|
-
ctx.fillRect(barDownLightBuf[off], barDownLightBuf[off + 1], barDownLightBuf[off + 2], barDownLightBuf[off + 3])
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
ctx.restore()
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
function drawMacdLinesWithCanvas2D(
|
|
371
|
-
ctx: CanvasRenderingContext2D,
|
|
372
|
-
scrollLeft: number,
|
|
373
|
-
colors: ThemeColors,
|
|
374
|
-
difPoints: LinePoint[],
|
|
375
|
-
deaPoints: LinePoint[],
|
|
376
|
-
config: { showDIF: boolean; showDEA: boolean }
|
|
377
|
-
): void {
|
|
378
|
-
ctx.save()
|
|
379
|
-
ctx.translate(-scrollLeft, 0)
|
|
380
|
-
ctx.lineWidth = 1
|
|
381
|
-
ctx.lineJoin = 'round'
|
|
382
|
-
ctx.lineCap = 'round'
|
|
383
|
-
|
|
384
|
-
if (config.showDIF && difPoints.length >= 2) {
|
|
385
|
-
ctx.strokeStyle = colors.MACD.DIF
|
|
386
|
-
ctx.beginPath()
|
|
387
|
-
ctx.moveTo(difPoints[0]!.x, difPoints[0]!.y)
|
|
388
|
-
for (let i = 1; i < difPoints.length; i++) {
|
|
389
|
-
const point = difPoints[i]!
|
|
390
|
-
ctx.lineTo(point.x, point.y)
|
|
391
|
-
}
|
|
392
|
-
ctx.stroke()
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
if (config.showDEA && deaPoints.length >= 2) {
|
|
396
|
-
ctx.strokeStyle = colors.MACD.DEA
|
|
397
|
-
ctx.beginPath()
|
|
398
|
-
ctx.moveTo(deaPoints[0]!.x, deaPoints[0]!.y)
|
|
399
|
-
for (let i = 1; i < deaPoints.length; i++) {
|
|
400
|
-
const point = deaPoints[i]!
|
|
401
|
-
ctx.lineTo(point.x, point.y)
|
|
402
|
-
}
|
|
403
|
-
ctx.stroke()
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
ctx.restore()
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
function compositeMacdWebGL(ctx: CanvasRenderingContext2D, context: RenderContext): void {
|
|
410
|
-
const surface = context.candleWebGLSurface
|
|
411
|
-
if (!surface) return
|
|
412
|
-
|
|
413
|
-
surface.compositeTo(ctx)
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
/**
|
|
417
|
-
* 计算指定索引处的 MACD 值(供图例使用)
|
|
418
|
-
* 使用 calculators.ts 中的计算函数
|
|
419
|
-
*/
|
|
420
|
-
export function calcMACDAtIndex(
|
|
421
|
-
data: KLineData[],
|
|
422
|
-
index: number,
|
|
423
|
-
fastPeriod: number = 12,
|
|
424
|
-
slowPeriod: number = 26,
|
|
425
|
-
signalPeriod: number = 9
|
|
426
|
-
): { dif: number; dea: number; macd: number } | null {
|
|
427
|
-
if (index < slowPeriod || index >= data.length) return null
|
|
428
|
-
|
|
429
|
-
const macdData = calcMACDData(data, fastPeriod, slowPeriod, signalPeriod)
|
|
430
|
-
return macdData[index] ?? null
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
/**
|
|
434
|
-
* 获取 MACD 标题信息(供 paneTitle 使用)
|
|
435
|
-
* 从 pluginHost 获取已计算好的数据,避免重复计算
|
|
436
|
-
*/
|
|
437
|
-
export function getMACDTitleInfo(
|
|
438
|
-
index: number,
|
|
439
|
-
fastPeriod: number,
|
|
440
|
-
slowPeriod: number,
|
|
441
|
-
signalPeriod: number,
|
|
442
|
-
pluginHost: PluginHost,
|
|
443
|
-
paneId: string = 'sub_MACD',
|
|
444
|
-
theme: ChartTheme = 'light'
|
|
445
|
-
): { name: string; params: number[]; values: Array<{ label: string; value: number; color: string }> } | null {
|
|
446
|
-
const colors = getColors(theme)
|
|
447
|
-
const state = pluginHost.getSharedState<MACDRenderState>(createMACDStateKey(paneId))
|
|
448
|
-
if (!state) return null
|
|
449
|
-
|
|
450
|
-
const point = state.series[index]
|
|
451
|
-
if (!point) return null
|
|
452
|
-
|
|
453
|
-
return {
|
|
454
|
-
name: 'MACD',
|
|
455
|
-
params: [fastPeriod, slowPeriod, signalPeriod],
|
|
456
|
-
values: [
|
|
457
|
-
{ label: 'DIF', value: point.dif, color: colors.MACD.DIF },
|
|
458
|
-
{ label: 'DEA', value: point.dea, color: colors.MACD.DEA },
|
|
459
|
-
{ label: 'MACD', value: point.macd, color: point.macd >= 0 ? colors.MACD.BAR_UP : colors.MACD.BAR_DOWN },
|
|
460
|
-
],
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
@Indicator({
|
|
465
|
-
name: 'macd',
|
|
466
|
-
displayName: 'MACD',
|
|
467
|
-
category: 'oscillator',
|
|
468
|
-
stateKey: createMACDStateKey,
|
|
469
|
-
defaultPaneId: 'sub_MACD',
|
|
470
|
-
paneIdField: 'macdPaneId',
|
|
471
|
-
applyResult: (host, state, paneId) => {
|
|
472
|
-
host.setSharedState(createMACDStateKey(paneId), state as any, 'indicator_scheduler')
|
|
473
|
-
},
|
|
474
|
-
})
|
|
475
|
-
class MACDIndicatorDefinition {
|
|
476
|
-
static rendererFactory = createMACDRendererPlugin
|
|
477
|
-
}
|
|
1
|
+
import type { RendererPluginWithHost, RenderContext, PluginHost } from '../../../plugin'
|
|
2
|
+
import { RENDERER_PRIORITY } from '../../../plugin'
|
|
3
|
+
import type { KLineData } from '../../../types/price'
|
|
4
|
+
import { getColors } from '../../theme/colors'
|
|
5
|
+
import type { ChartTheme, ThemeColors } from '../../theme/colors'
|
|
6
|
+
import { alignToPhysicalPixelCenter } from '../../draw/pixelAlign'
|
|
7
|
+
import type { MACDRenderState } from '../../indicators/macdState'
|
|
8
|
+
import { createMACDStateKey } from '../../indicators/macdState'
|
|
9
|
+
import type { MACDPoint } from '../../indicators/calculators'
|
|
10
|
+
import { calcMACDData } from '../../indicators/calculators'
|
|
11
|
+
import { Indicator } from '../../indicators/indicatorDefinitionRegistry'
|
|
12
|
+
import { resolveStateKey } from '../../indicators/indicatorMetadata'
|
|
13
|
+
import type { IndicatorScheduler } from '../../indicators/scheduler'
|
|
14
|
+
|
|
15
|
+
type LinePoint = { x: number; y: number }
|
|
16
|
+
|
|
17
|
+
export interface MACDConfig {
|
|
18
|
+
/** 快线周期(默认 12) */
|
|
19
|
+
fastPeriod?: number
|
|
20
|
+
/** 慢线周期(默认 26) */
|
|
21
|
+
slowPeriod?: number
|
|
22
|
+
/** DEA 周期(默认 9) */
|
|
23
|
+
signalPeriod?: number
|
|
24
|
+
/** 是否显示 DIF 线 */
|
|
25
|
+
showDIF?: boolean
|
|
26
|
+
/** 是否显示 DEA 线 */
|
|
27
|
+
showDEA?: boolean
|
|
28
|
+
/** 是否显示 MACD 柱 */
|
|
29
|
+
showBAR?: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface MACDRendererOptions {
|
|
33
|
+
/** 目标 pane ID(默认 'sub') */
|
|
34
|
+
paneId?: string
|
|
35
|
+
/** 初始配置 */
|
|
36
|
+
config?: MACDConfig
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function getMACDStateKey(host: PluginHost | null, paneId: string): string | null {
|
|
40
|
+
const scheduler = host?.getService<IndicatorScheduler>('indicatorScheduler')
|
|
41
|
+
if (!scheduler) {
|
|
42
|
+
console.warn('[MACDRenderer] Scheduler not available via service locator')
|
|
43
|
+
return null
|
|
44
|
+
}
|
|
45
|
+
const meta = scheduler.getIndicatorMetadata('macd')
|
|
46
|
+
if (!meta) {
|
|
47
|
+
console.warn("[MACDRenderer] Indicator metadata for 'macd' not found, skip rendering")
|
|
48
|
+
return null
|
|
49
|
+
}
|
|
50
|
+
return resolveStateKey(meta.stateKey, paneId)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 创建 MACD 渲染器插件
|
|
55
|
+
* 从 StateStore 读取 MACD 状态,不再内联计算
|
|
56
|
+
*/
|
|
57
|
+
export function createMACDRendererPlugin(options: MACDRendererOptions = {}): RendererPluginWithHost {
|
|
58
|
+
const { paneId = 'sub', config: initialConfig = {} } = options
|
|
59
|
+
|
|
60
|
+
let pluginHost: PluginHost | null = null
|
|
61
|
+
|
|
62
|
+
function resolveKey(): string | null {
|
|
63
|
+
return getMACDStateKey(pluginHost, paneId)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const config: Required<MACDConfig> = {
|
|
67
|
+
fastPeriod: 12,
|
|
68
|
+
slowPeriod: 26,
|
|
69
|
+
signalPeriod: 9,
|
|
70
|
+
showDIF: true,
|
|
71
|
+
showDEA: true,
|
|
72
|
+
showBAR: true,
|
|
73
|
+
...initialConfig,
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 线条点缓存(用于 WebGL/Canvas2D 渲染)
|
|
77
|
+
let cachedLineKey = ''
|
|
78
|
+
let cachedDifPoints: LinePoint[] = []
|
|
79
|
+
let cachedDeaPoints: LinePoint[] = []
|
|
80
|
+
|
|
81
|
+
function clearLineCache() {
|
|
82
|
+
cachedLineKey = ''
|
|
83
|
+
cachedDifPoints = []
|
|
84
|
+
cachedDeaPoints = []
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 构建线条缓存 key
|
|
88
|
+
function buildLineCacheKey(
|
|
89
|
+
range: { start: number; end: number },
|
|
90
|
+
kLineCenters: number[],
|
|
91
|
+
pane: RenderContext['pane'],
|
|
92
|
+
displayMin: number,
|
|
93
|
+
displayMax: number,
|
|
94
|
+
stateTimestamp: number
|
|
95
|
+
): string {
|
|
96
|
+
const dr = pane.yAxis.getDisplayRange()
|
|
97
|
+
return [
|
|
98
|
+
stateTimestamp,
|
|
99
|
+
range.start,
|
|
100
|
+
range.end,
|
|
101
|
+
kLineCenters.length,
|
|
102
|
+
kLineCenters[0]?.toFixed(2) ?? 'n',
|
|
103
|
+
kLineCenters[kLineCenters.length - 1]?.toFixed(2) ?? 'n',
|
|
104
|
+
displayMax.toFixed(6),
|
|
105
|
+
displayMin.toFixed(6),
|
|
106
|
+
pane.yAxis.getPriceOffset().toFixed(6),
|
|
107
|
+
pane.yAxis.getScaleType(),
|
|
108
|
+
pane.height.toFixed(2),
|
|
109
|
+
config.showDIF,
|
|
110
|
+
config.showDEA,
|
|
111
|
+
].join('|')
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
name: `macd_${paneId}`,
|
|
116
|
+
version: '1.0.0',
|
|
117
|
+
description: 'MACD 指标渲染器',
|
|
118
|
+
debugName: 'MACD',
|
|
119
|
+
paneId: paneId,
|
|
120
|
+
priority: RENDERER_PRIORITY.INDICATOR,
|
|
121
|
+
|
|
122
|
+
onInstall(host: PluginHost) {
|
|
123
|
+
pluginHost = host
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
getDeclaredNamespaces() {
|
|
127
|
+
const key = resolveKey()
|
|
128
|
+
return key ? [key] : []
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
draw(context: RenderContext) {
|
|
132
|
+
const { ctx, pane, data, range, scrollLeft, dpr, kLineCenters, lineWebGLSurface } = context
|
|
133
|
+
const klineData = data as KLineData[]
|
|
134
|
+
const colors = getColors(context.theme)
|
|
135
|
+
|
|
136
|
+
// 从 StateStore 读取 MACD 状态
|
|
137
|
+
const stateKey = resolveKey()
|
|
138
|
+
if (!stateKey) return
|
|
139
|
+
const state = pluginHost?.getSharedState<MACDRenderState>(stateKey)
|
|
140
|
+
if (!state || state.visibleMin > state.visibleMax) return
|
|
141
|
+
if (klineData.length < config.slowPeriod) return
|
|
142
|
+
|
|
143
|
+
const macdData = state.series
|
|
144
|
+
if (!macdData || macdData.length === 0) return
|
|
145
|
+
|
|
146
|
+
// 使用 state 中的极值,或回退到计算
|
|
147
|
+
let valueMin = state.visibleMin
|
|
148
|
+
let valueMax = state.visibleMax
|
|
149
|
+
|
|
150
|
+
// 添加 padding
|
|
151
|
+
const padding = Math.max(0.05, (valueMax - valueMin) * 0.1)
|
|
152
|
+
valueMin = valueMin - padding
|
|
153
|
+
valueMax = valueMax + padding
|
|
154
|
+
const valueRange = valueMax - valueMin || 1
|
|
155
|
+
|
|
156
|
+
const displayRange = pane.yAxis.getDisplayRange({ minPrice: valueMin, maxPrice: valueMax })
|
|
157
|
+
const displayMin = displayRange.minPrice
|
|
158
|
+
const displayMax = displayRange.maxPrice
|
|
159
|
+
const displayValueRange = displayMax - displayMin || 1
|
|
160
|
+
|
|
161
|
+
// 零轴位置
|
|
162
|
+
const zeroY = pane.height - (0 - displayMin) / displayValueRange * pane.height
|
|
163
|
+
|
|
164
|
+
const drawStart = Math.max(range.start, config.slowPeriod - 1)
|
|
165
|
+
const drawEnd = Math.min(range.end, klineData.length)
|
|
166
|
+
|
|
167
|
+
// 绘制 MACD 柱状图(WebGL 优先)
|
|
168
|
+
if (config.showBAR) {
|
|
169
|
+
const alignedZeroY = Math.round(zeroY * dpr) / dpr
|
|
170
|
+
|
|
171
|
+
const maxBars = Math.max(1, drawEnd - drawStart)
|
|
172
|
+
const barUpBuf = new Float32Array(maxBars * 4)
|
|
173
|
+
const barUpLightBuf = new Float32Array(maxBars * 4)
|
|
174
|
+
const barDownBuf = new Float32Array(maxBars * 4)
|
|
175
|
+
const barDownLightBuf = new Float32Array(maxBars * 4)
|
|
176
|
+
let barUpCount = 0, barUpLightCount = 0, barDownCount = 0, barDownLightCount = 0
|
|
177
|
+
|
|
178
|
+
for (let i = drawStart; i < drawEnd; i++) {
|
|
179
|
+
const point = macdData[i]
|
|
180
|
+
if (!point) continue
|
|
181
|
+
|
|
182
|
+
const barRect = context.kBarRects[i - range.start]
|
|
183
|
+
if (!barRect) continue
|
|
184
|
+
|
|
185
|
+
const barY = pane.height - (point.macd - displayMin) / displayValueRange * pane.height
|
|
186
|
+
const isPositive = point.macd >= 0
|
|
187
|
+
|
|
188
|
+
const prevPoint = i > 0 ? macdData[i - 1] : null
|
|
189
|
+
const isRising = prevPoint ? point.macd >= prevPoint.macd : true
|
|
190
|
+
|
|
191
|
+
const alignedBarY = Math.round(barY * dpr) / dpr
|
|
192
|
+
const minBarHPx = 1 / dpr
|
|
193
|
+
|
|
194
|
+
let buf: Float32Array
|
|
195
|
+
let idx: number
|
|
196
|
+
|
|
197
|
+
if (isPositive) {
|
|
198
|
+
const rawH = alignedZeroY - alignedBarY
|
|
199
|
+
const finalH = rawH <= 0 ? minBarHPx : Math.max(rawH, minBarHPx)
|
|
200
|
+
const finalBarY = rawH <= 0 ? alignedZeroY - minBarHPx : alignedZeroY - finalH
|
|
201
|
+
if (isRising) { buf = barUpBuf; idx = barUpCount++ }
|
|
202
|
+
else { buf = barUpLightBuf; idx = barUpLightCount++ }
|
|
203
|
+
const off = idx * 4
|
|
204
|
+
buf[off] = barRect.x; buf[off + 1] = finalBarY; buf[off + 2] = barRect.width; buf[off + 3] = finalH
|
|
205
|
+
} else {
|
|
206
|
+
const rawH = alignedBarY - alignedZeroY
|
|
207
|
+
const finalH = rawH <= 0 ? minBarHPx : Math.max(rawH, minBarHPx)
|
|
208
|
+
if (isRising) { buf = barDownLightBuf; idx = barDownLightCount++ }
|
|
209
|
+
else { buf = barDownBuf; idx = barDownCount++ }
|
|
210
|
+
const off = idx * 4
|
|
211
|
+
buf[off] = barRect.x; buf[off + 1] = alignedZeroY; buf[off + 2] = barRect.width; buf[off + 3] = finalH
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const usedWebGL = drawMacdBarsWithWebGL(context, barUpBuf, barUpCount, barUpLightBuf, barUpLightCount, barDownBuf, barDownCount, barDownLightBuf, barDownLightCount)
|
|
216
|
+
if (!usedWebGL) {
|
|
217
|
+
drawMacdBarsWithCanvas2D(ctx, scrollLeft, colors, barUpBuf, barUpCount, barUpLightBuf, barUpLightCount, barDownBuf, barDownCount, barDownLightBuf, barDownLightCount)
|
|
218
|
+
} else {
|
|
219
|
+
compositeMacdWebGL(ctx, context)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// 更新线条点缓存
|
|
224
|
+
const lineCacheKey = buildLineCacheKey(range, kLineCenters, pane, displayMin, displayMax, state.timestamp)
|
|
225
|
+
if (cachedLineKey !== lineCacheKey) {
|
|
226
|
+
cachedLineKey = lineCacheKey
|
|
227
|
+
cachedDifPoints = []
|
|
228
|
+
cachedDeaPoints = []
|
|
229
|
+
|
|
230
|
+
if (config.showDIF) {
|
|
231
|
+
for (let i = drawStart; i < drawEnd; i++) {
|
|
232
|
+
const point = macdData[i]
|
|
233
|
+
if (!point) continue
|
|
234
|
+
const centerX = kLineCenters[i - range.start]
|
|
235
|
+
if (centerX === undefined) continue
|
|
236
|
+
const logicY = pane.height - (point.dif - displayMin) / displayValueRange * pane.height
|
|
237
|
+
cachedDifPoints.push({ x: centerX, y: logicY })
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (config.showDEA) {
|
|
242
|
+
for (let i = drawStart; i < drawEnd; i++) {
|
|
243
|
+
const point = macdData[i]
|
|
244
|
+
if (!point) continue
|
|
245
|
+
const centerX = kLineCenters[i - range.start]
|
|
246
|
+
if (centerX === undefined) continue
|
|
247
|
+
const logicY = pane.height - (point.dea - displayMin) / displayValueRange * pane.height
|
|
248
|
+
cachedDeaPoints.push({ x: centerX, y: logicY })
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// 绘制 DIF/DEA 线(WebGL 优先,Canvas2D 回退)
|
|
254
|
+
const enableWebGL = context.settings?.enableWebGLRendering !== false
|
|
255
|
+
let usedWebGLForLines = false
|
|
256
|
+
if (enableWebGL && lineWebGLSurface?.isAvailable()) {
|
|
257
|
+
const lines: Array<{ points: LinePoint[]; width: number; color: string }> = []
|
|
258
|
+
if (config.showDIF && cachedDifPoints.length >= 2) {
|
|
259
|
+
lines.push({ points: cachedDifPoints, width: 1, color: colors.MACD.DIF })
|
|
260
|
+
}
|
|
261
|
+
if (config.showDEA && cachedDeaPoints.length >= 2) {
|
|
262
|
+
lines.push({ points: cachedDeaPoints, width: 1, color: colors.MACD.DEA })
|
|
263
|
+
}
|
|
264
|
+
const allOk = lines.length > 0 && lineWebGLSurface.drawLineStrips(lines, scrollLeft)
|
|
265
|
+
if (allOk) {
|
|
266
|
+
usedWebGLForLines = true
|
|
267
|
+
lineWebGLSurface.compositeTo(ctx, { imageSmoothingEnabled: false })
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (!usedWebGLForLines) {
|
|
272
|
+
drawMacdLinesWithCanvas2D(ctx, scrollLeft, colors, cachedDifPoints, cachedDeaPoints, config)
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
|
|
276
|
+
onDataUpdate() {
|
|
277
|
+
clearLineCache()
|
|
278
|
+
},
|
|
279
|
+
|
|
280
|
+
getConfig() {
|
|
281
|
+
return { ...config }
|
|
282
|
+
},
|
|
283
|
+
|
|
284
|
+
setConfig(newConfig: Record<string, unknown>) {
|
|
285
|
+
let needClearLineCache = false
|
|
286
|
+
if ('fastPeriod' in newConfig && newConfig.fastPeriod !== config.fastPeriod) {
|
|
287
|
+
clearLineCache()
|
|
288
|
+
}
|
|
289
|
+
if ('slowPeriod' in newConfig && newConfig.slowPeriod !== config.slowPeriod) {
|
|
290
|
+
clearLineCache()
|
|
291
|
+
}
|
|
292
|
+
if ('signalPeriod' in newConfig && newConfig.signalPeriod !== config.signalPeriod) {
|
|
293
|
+
clearLineCache()
|
|
294
|
+
}
|
|
295
|
+
if ('showDIF' in newConfig && newConfig.showDIF !== config.showDIF) {
|
|
296
|
+
needClearLineCache = true
|
|
297
|
+
}
|
|
298
|
+
if ('showDEA' in newConfig && newConfig.showDEA !== config.showDEA) {
|
|
299
|
+
needClearLineCache = true
|
|
300
|
+
}
|
|
301
|
+
Object.assign(config, newConfig)
|
|
302
|
+
if (needClearLineCache) {
|
|
303
|
+
clearLineCache()
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function drawMacdBarsWithWebGL(
|
|
310
|
+
context: RenderContext,
|
|
311
|
+
barUpBuf: Float32Array, barUpCount: number,
|
|
312
|
+
barUpLightBuf: Float32Array, barUpLightCount: number,
|
|
313
|
+
barDownBuf: Float32Array, barDownCount: number,
|
|
314
|
+
barDownLightBuf: Float32Array, barDownLightCount: number
|
|
315
|
+
): boolean {
|
|
316
|
+
const colors = getColors(context.theme)
|
|
317
|
+
if (context.settings?.enableWebGLRendering === false) return false
|
|
318
|
+
const surface = context.candleWebGLSurface
|
|
319
|
+
if (!surface || !surface.isAvailable()) return false
|
|
320
|
+
|
|
321
|
+
surface.clear()
|
|
322
|
+
|
|
323
|
+
const ok1 = barUpCount === 0 || surface.drawRectBuffer(barUpBuf.subarray(0, barUpCount * 4), barUpCount, colors.MACD.BAR_UP, context.scrollLeft)
|
|
324
|
+
const ok2 = barUpLightCount === 0 || surface.drawRectBuffer(barUpLightBuf.subarray(0, barUpLightCount * 4), barUpLightCount, colors.MACD.BAR_UP_LIGHT, context.scrollLeft)
|
|
325
|
+
const ok3 = barDownCount === 0 || surface.drawRectBuffer(barDownBuf.subarray(0, barDownCount * 4), barDownCount, colors.MACD.BAR_DOWN, context.scrollLeft)
|
|
326
|
+
const ok4 = barDownLightCount === 0 || surface.drawRectBuffer(barDownLightBuf.subarray(0, barDownLightCount * 4), barDownLightCount, colors.MACD.BAR_DOWN_LIGHT, context.scrollLeft)
|
|
327
|
+
|
|
328
|
+
return ok1 && ok2 && ok3 && ok4
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function drawMacdBarsWithCanvas2D(
|
|
332
|
+
ctx: CanvasRenderingContext2D,
|
|
333
|
+
scrollLeft: number,
|
|
334
|
+
colors: { MACD: { BAR_UP: string; BAR_UP_LIGHT: string; BAR_DOWN: string; BAR_DOWN_LIGHT: string } },
|
|
335
|
+
barUpBuf: Float32Array, barUpCount: number,
|
|
336
|
+
barUpLightBuf: Float32Array, barUpLightCount: number,
|
|
337
|
+
barDownBuf: Float32Array, barDownCount: number,
|
|
338
|
+
barDownLightBuf: Float32Array, barDownLightCount: number
|
|
339
|
+
): void {
|
|
340
|
+
ctx.save()
|
|
341
|
+
ctx.translate(-scrollLeft, 0)
|
|
342
|
+
|
|
343
|
+
ctx.fillStyle = colors.MACD.BAR_UP
|
|
344
|
+
for (let i = 0; i < barUpCount; i++) {
|
|
345
|
+
const off = i * 4
|
|
346
|
+
ctx.fillRect(barUpBuf[off], barUpBuf[off + 1], barUpBuf[off + 2], barUpBuf[off + 3])
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
ctx.fillStyle = colors.MACD.BAR_UP_LIGHT
|
|
350
|
+
for (let i = 0; i < barUpLightCount; i++) {
|
|
351
|
+
const off = i * 4
|
|
352
|
+
ctx.fillRect(barUpLightBuf[off], barUpLightBuf[off + 1], barUpLightBuf[off + 2], barUpLightBuf[off + 3])
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
ctx.fillStyle = colors.MACD.BAR_DOWN
|
|
356
|
+
for (let i = 0; i < barDownCount; i++) {
|
|
357
|
+
const off = i * 4
|
|
358
|
+
ctx.fillRect(barDownBuf[off], barDownBuf[off + 1], barDownBuf[off + 2], barDownBuf[off + 3])
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
ctx.fillStyle = colors.MACD.BAR_DOWN_LIGHT
|
|
362
|
+
for (let i = 0; i < barDownLightCount; i++) {
|
|
363
|
+
const off = i * 4
|
|
364
|
+
ctx.fillRect(barDownLightBuf[off], barDownLightBuf[off + 1], barDownLightBuf[off + 2], barDownLightBuf[off + 3])
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
ctx.restore()
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function drawMacdLinesWithCanvas2D(
|
|
371
|
+
ctx: CanvasRenderingContext2D,
|
|
372
|
+
scrollLeft: number,
|
|
373
|
+
colors: ThemeColors,
|
|
374
|
+
difPoints: LinePoint[],
|
|
375
|
+
deaPoints: LinePoint[],
|
|
376
|
+
config: { showDIF: boolean; showDEA: boolean }
|
|
377
|
+
): void {
|
|
378
|
+
ctx.save()
|
|
379
|
+
ctx.translate(-scrollLeft, 0)
|
|
380
|
+
ctx.lineWidth = 1
|
|
381
|
+
ctx.lineJoin = 'round'
|
|
382
|
+
ctx.lineCap = 'round'
|
|
383
|
+
|
|
384
|
+
if (config.showDIF && difPoints.length >= 2) {
|
|
385
|
+
ctx.strokeStyle = colors.MACD.DIF
|
|
386
|
+
ctx.beginPath()
|
|
387
|
+
ctx.moveTo(difPoints[0]!.x, difPoints[0]!.y)
|
|
388
|
+
for (let i = 1; i < difPoints.length; i++) {
|
|
389
|
+
const point = difPoints[i]!
|
|
390
|
+
ctx.lineTo(point.x, point.y)
|
|
391
|
+
}
|
|
392
|
+
ctx.stroke()
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (config.showDEA && deaPoints.length >= 2) {
|
|
396
|
+
ctx.strokeStyle = colors.MACD.DEA
|
|
397
|
+
ctx.beginPath()
|
|
398
|
+
ctx.moveTo(deaPoints[0]!.x, deaPoints[0]!.y)
|
|
399
|
+
for (let i = 1; i < deaPoints.length; i++) {
|
|
400
|
+
const point = deaPoints[i]!
|
|
401
|
+
ctx.lineTo(point.x, point.y)
|
|
402
|
+
}
|
|
403
|
+
ctx.stroke()
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
ctx.restore()
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function compositeMacdWebGL(ctx: CanvasRenderingContext2D, context: RenderContext): void {
|
|
410
|
+
const surface = context.candleWebGLSurface
|
|
411
|
+
if (!surface) return
|
|
412
|
+
|
|
413
|
+
surface.compositeTo(ctx)
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* 计算指定索引处的 MACD 值(供图例使用)
|
|
418
|
+
* 使用 calculators.ts 中的计算函数
|
|
419
|
+
*/
|
|
420
|
+
export function calcMACDAtIndex(
|
|
421
|
+
data: KLineData[],
|
|
422
|
+
index: number,
|
|
423
|
+
fastPeriod: number = 12,
|
|
424
|
+
slowPeriod: number = 26,
|
|
425
|
+
signalPeriod: number = 9
|
|
426
|
+
): { dif: number; dea: number; macd: number } | null {
|
|
427
|
+
if (index < slowPeriod || index >= data.length) return null
|
|
428
|
+
|
|
429
|
+
const macdData = calcMACDData(data, fastPeriod, slowPeriod, signalPeriod)
|
|
430
|
+
return macdData[index] ?? null
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* 获取 MACD 标题信息(供 paneTitle 使用)
|
|
435
|
+
* 从 pluginHost 获取已计算好的数据,避免重复计算
|
|
436
|
+
*/
|
|
437
|
+
export function getMACDTitleInfo(
|
|
438
|
+
index: number,
|
|
439
|
+
fastPeriod: number,
|
|
440
|
+
slowPeriod: number,
|
|
441
|
+
signalPeriod: number,
|
|
442
|
+
pluginHost: PluginHost,
|
|
443
|
+
paneId: string = 'sub_MACD',
|
|
444
|
+
theme: ChartTheme = 'light'
|
|
445
|
+
): { name: string; params: number[]; values: Array<{ label: string; value: number; color: string }> } | null {
|
|
446
|
+
const colors = getColors(theme)
|
|
447
|
+
const state = pluginHost.getSharedState<MACDRenderState>(createMACDStateKey(paneId))
|
|
448
|
+
if (!state) return null
|
|
449
|
+
|
|
450
|
+
const point = state.series[index]
|
|
451
|
+
if (!point) return null
|
|
452
|
+
|
|
453
|
+
return {
|
|
454
|
+
name: 'MACD',
|
|
455
|
+
params: [fastPeriod, slowPeriod, signalPeriod],
|
|
456
|
+
values: [
|
|
457
|
+
{ label: 'DIF', value: point.dif, color: colors.MACD.DIF },
|
|
458
|
+
{ label: 'DEA', value: point.dea, color: colors.MACD.DEA },
|
|
459
|
+
{ label: 'MACD', value: point.macd, color: point.macd >= 0 ? colors.MACD.BAR_UP : colors.MACD.BAR_DOWN },
|
|
460
|
+
],
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
@Indicator({
|
|
465
|
+
name: 'macd',
|
|
466
|
+
displayName: 'MACD',
|
|
467
|
+
category: 'oscillator',
|
|
468
|
+
stateKey: createMACDStateKey,
|
|
469
|
+
defaultPaneId: 'sub_MACD',
|
|
470
|
+
paneIdField: 'macdPaneId',
|
|
471
|
+
applyResult: (host, state, paneId) => {
|
|
472
|
+
host.setSharedState(createMACDStateKey(paneId), state as any, 'indicator_scheduler')
|
|
473
|
+
},
|
|
474
|
+
})
|
|
475
|
+
class MACDIndicatorDefinition {
|
|
476
|
+
static rendererFactory = createMACDRendererPlugin
|
|
477
|
+
}
|