70 lines
3.4 KiB
JavaScript
70 lines
3.4 KiB
JavaScript
// Namespace setup
|
|
window.App = window.App || {};
|
|
window.App.Charts = (function() {
|
|
// 依赖 Indicators
|
|
const Indicators = (window.App && window.App.Indicators) || {};
|
|
|
|
function addMovingAveragesToChart(candleData) {
|
|
if (!window.tvWidget || !tvWidget.mainChart || !candleData || candleData.length === 0) return;
|
|
if (!window.movingAverages) return;
|
|
if (!tvWidget.series) tvWidget.series = {};
|
|
|
|
if (tvWidget.series.maSeries && tvWidget.series.maSeries.length > 0) {
|
|
tvWidget.series.maSeries.forEach(series => {
|
|
try { tvWidget.mainChart.removeSeries(series); } catch(e) {}
|
|
});
|
|
}
|
|
tvWidget.series.maSeries = [];
|
|
|
|
window.movingAverages.forEach(maConfig => {
|
|
if (!maConfig.visible) return;
|
|
try {
|
|
const maData = Indicators.calculateMA(candleData, maConfig.type, maConfig.length, maConfig.source);
|
|
const smoothedData = maConfig.smoothType !== 'none' ? (window.applySmoothToMA ? window.applySmoothToMA(maData, maConfig.smoothType, maConfig.smoothLength) : maData) : maData;
|
|
const maSeries = tvWidget.mainChart.addLineSeries({
|
|
color: maConfig.color,
|
|
lineWidth: maConfig.lineWidth || 2,
|
|
lineStyle: maConfig.lineStyle || 0,
|
|
title: `${maConfig.type}(${maConfig.length})`,
|
|
lastValueVisible: false,
|
|
priceLineVisible: false,
|
|
crosshairMarkerVisible: true,
|
|
});
|
|
maSeries.setData(smoothedData);
|
|
maConfig.data = smoothedData;
|
|
tvWidget.series.maSeries.push(maSeries);
|
|
} catch(e) {}
|
|
});
|
|
}
|
|
|
|
function addBollingerBandsToChart(candleData) {
|
|
if (!window.tvWidget || !tvWidget.mainChart || !candleData || candleData.length === 0) return;
|
|
if (!window.bollingerBands) return;
|
|
if (!tvWidget.series) tvWidget.series = {};
|
|
|
|
if (tvWidget.series.bbSeries && tvWidget.series.bbSeries.length > 0) {
|
|
tvWidget.series.bbSeries.forEach(series => { try { tvWidget.mainChart.removeSeries(series); } catch(e) {} });
|
|
}
|
|
tvWidget.series.bbSeries = [];
|
|
|
|
window.bollingerBands.forEach(bbConfig => {
|
|
if (!bbConfig.visible) return;
|
|
try {
|
|
const bbData = Indicators.calculateBB(candleData, bbConfig.length, bbConfig.upperMultiplier, bbConfig.lowerMultiplier, bbConfig.source);
|
|
const upperSeries = tvWidget.mainChart.addLineSeries({ color: bbConfig.upperColor, lineWidth: bbConfig.lineWidth || 2, lineStyle: bbConfig.lineStyle || 0, lastValueVisible: false, priceLineVisible: false, crosshairMarkerVisible: true });
|
|
const middleSeries = tvWidget.mainChart.addLineSeries({ color: bbConfig.middleColor, lineWidth: bbConfig.lineWidth || 2, lineStyle: bbConfig.lineStyle || 0, lastValueVisible: false, priceLineVisible: false, crosshairMarkerVisible: true });
|
|
const lowerSeries = tvWidget.mainChart.addLineSeries({ color: bbConfig.lowerColor, lineWidth: bbConfig.lineWidth || 2, lineStyle: bbConfig.lineStyle || 0, lastValueVisible: false, priceLineVisible: false, crosshairMarkerVisible: true });
|
|
upperSeries.setData(bbData.map(item => ({ time: item.time, value: item.upper })));
|
|
middleSeries.setData(bbData.map(item => ({ time: item.time, value: item.middle })));
|
|
lowerSeries.setData(bbData.map(item => ({ time: item.time, value: item.lower })));
|
|
bbConfig.data = bbData;
|
|
tvWidget.series.bbSeries.push(upperSeries, middleSeries, lowerSeries);
|
|
} catch(e) {}
|
|
});
|
|
}
|
|
|
|
return { addMovingAveragesToChart, addBollingerBandsToChart };
|
|
})();
|
|
|
|
|