Technical Indicator KLineChart
Technical Indicator KLineChart
com/en-US/guide/indicator
KLineChart
Technical indicator
This document introduces the built-in technical indicators in the chart and how to customize a technical
indicator.
MA 85, 10, 30, 60? BIAS 86, 12, 24? VR 824, 30?
then add it globally through klinecharts.registerIndicator , add it to the chart and use it like the
built-in technical indicator.
Attribute description
typescript
{
// indicator name
name: string
// The short name of the indicator, used for display, the name will be displayed by default
shortName?: string
// precision, default is 4
precision?: number
// calculation parameter
calcParams?: any[]
// Do you need ohlc
shouldOhlc?: boolean
// Do you need to format large data values, starting from 1000, for example, do you need to fo
shouldFormatBigNumber?: boolean
// Do you need visible
visible?: boolean
// z level
zLevel?: number
// extended data
extendData?: any
// series, default is 'normal'
series?: 'normal' | 'price' | 'volume'
// Data information
figures?: Array<{
// Used to get the median value of the calculation result
key: string
// for tooltip display
title?: string
// graphic type
type?: string
// Base value, if given, it will be drawn up and down with this value, generally used when t
baseValue?: number
// is a method
attrs?: ({
coordinate: IndicatorFigureAttrsCallbackCoordinate
bounding: Bounding
barSpace: BarSpace
xAxis: XAxis
yAxis: YAxis
}) => IndicatorFigureAttrs
// is a method
styles?: (
data: {
// The data of the previous graph
prev: {
// k-line data, type refer to [data source]
kLineData?: KLineData,
// technical indicator data
indicatorData?: any
},
// data of the current graph
current: {
kLineData?: KLineData
indicatorData?: any
},
// Data for the next graph
next: {
kLineData?: KLineData,
indicatorData?: any
}
},
// technical chart example
indicator: Indicator
// The default technical indicator style, that is, the technical indicator style set globa
defaultStyles: IndicatorStyle
) => IndicatorFigureStyle
}>,
// The specified minimum value, default null
minValue?: number
// The specified maximum value, default null
maxValue?: number
// style, support increment, default is null, type refer to indicator in [style]
styles?: IndicatorStyle
// calculation method, can be a promise
calc: (
// data source, see [data source] for type
dataList: KLineData[],
// technical indicator example
indicator: indicator
) => Promise<Array<any>> | Array<any>
// Regenerate figure graphic configuration method, which will be triggered after the calculati
regenerateFigures?: (
// calculation parameter
calcParms: any[]
) => Array<IndicatorFigure<D>>
// Create a custom hint text
createTooltipDataSource?: (params: {
// data source, see [data source] for type
kLineDataList: KLineData[]
// technical indicator example
indicator: Indicator
// Visible area information
visibleRange: {
// start data index
from: number
// end data index
to: number
// real start data index
realFrom: number
// real end data index
realTo: number
},
// window size information
bounding: {
// width
width: number
// high
height: number
// distance to the left
left: number
Step.1
First determine the calculation parameters (calcParams) and configuration items (figures). The 'MA'
technical indicator needs to display the line connecting the average closing price of the two periods,
one is 'ma1' and the other is 'ma2'. So figures are configured as:
javascript
{
// The calculation parameters are 2, one calculates the mean value of 5 cycle times, namely '
calcParams: [5, 10],
figures: [
// first line 'ma5'
{ key: 'ma1', title: 'MA5: ', type: 'line' },
// second line 'ma10'
Step.2
javascript
{
name: 'MA',
shortName: 'MA',
calcParams: [5, 10],
figures: [
{ key: 'ma1', title: 'MA5: ', type: 'line' },
{ key: 'ma2', title: 'MA10: ', type: 'line' }
],
// When the calculation parameters are changed, it is hoped that the prompt is the same as th
regenerateFigures: (params) => {
return params. map((p, i) => {
return { key: `ma${i + 1}`, title: `MA${p}: `, type: 'line' }
})
},
// Calculation results
calc: (kLineDataList, { calcParams, figures }) => {
// Note: The number of returned data needs to be consistent with the number of data in kLin
// It is best to take the callback parameter calcParams as the calculation parameter. If no
const closeSums = []
return kLineDataList. map((kLineData, i) => {
const ma = {}
const close = kLineData. close
calcParams.forEach((param, j) => {
closeSums[j] = (closeSums[j] || 0) + close
if (i >= param - 1) {
ma[figures[j].key] = closeSums[j] / param
closeSums[j] -= kLineDataList[i - (param - 1)].close
}
})
// If there is a value, the data format of each item here should be { ma1: xxx, ma2: xxx
// Each key needs to be consistent with the value corresponding to the subkey in figures
return ma
})
}
}