/** * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import type TViewData from '../../common/TViewData'; import { type Indicator, type IndicatorTemplate, IndicatorSeries, } from '../../component/Indicator'; interface Ema { ema1?: number; } /** * EMA 指数移动平均 */ const exponentialMovingAverage: IndicatorTemplate = { name: 'EMA', shortName: 'EMA', series: IndicatorSeries.Price, calcParams: [9], precision: 2, shouldOhlc: false, figures: [{ key: 'ema', title: 'EMA: ', type: 'line' }], regenerateFigures: (params: any[]) => { return params.map((p: number, i: number) => { return { key: `ema${i + 1}`, title: `EMA${p}: `, type: 'line' }; }); }, calc: (dataList: TViewData[], indicator: Indicator) => { const { calcParams: params, figures } = indicator; let closeSum = 0; const emaValues: number[] = []; return dataList.map((TViewData: TViewData, i: number) => { const ema = {}; const close = TViewData.close; closeSum += close; params.forEach((p: number, index: number) => { if (i >= p - 1) { if (i > p - 1) { emaValues[index] = (2 * close + (p - 1) * emaValues[index]) / (p + 1); } else { emaValues[index] = closeSum / p; } ema[figures[index].key] = emaValues[index]; } }); return ema; }); }, }; export default exponentialMovingAverage;