61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
/**
|
|
* HTS (High-Tech SMAs) - Combined Fast and Slow SMA Indicator
|
|
* This indicator displays both Fast SMA and Slow SMA on the same chart
|
|
*/
|
|
const HTS_INDICATOR = {
|
|
name: 'HTS',
|
|
label: 'HTS (Fast & Slow SMA)',
|
|
usesBaseData: false, // This indicator uses the chart's currently displayed data
|
|
params: [
|
|
{ name: 'fastPeriod', type: 'number', defaultValue: 33, min: 2, label: 'Fast SMA Period' },
|
|
{ name: 'slowPeriod', type: 'number', defaultValue: 133, min: 2, label: 'Slow SMA Period' },
|
|
],
|
|
calculateFull: calculateFullHTS,
|
|
};
|
|
|
|
function calculateFullHTS(data, params) {
|
|
const fastPeriod = params.fastPeriod;
|
|
const slowPeriod = params.slowPeriod;
|
|
|
|
if (!data || data.length < Math.max(fastPeriod, slowPeriod)) {
|
|
return {
|
|
fastSMA: [],
|
|
slowSMA: []
|
|
};
|
|
}
|
|
|
|
// Calculate Fast SMA
|
|
const fastSMA = calculateSMA(data, fastPeriod);
|
|
|
|
// Calculate Slow SMA
|
|
const slowSMA = calculateSMA(data, slowPeriod);
|
|
|
|
return {
|
|
fastSMA: fastSMA,
|
|
slowSMA: slowSMA
|
|
};
|
|
}
|
|
|
|
function calculateSMA(data, period) {
|
|
if (!data || data.length < period) return [];
|
|
|
|
let smaData = [];
|
|
let sum = 0;
|
|
|
|
// Calculate initial sum for the first period
|
|
for (let i = 0; i < period; i++) {
|
|
sum += data[i].close;
|
|
}
|
|
|
|
// Add the first SMA point
|
|
smaData.push({ time: data[period - 1].time, value: sum / period });
|
|
|
|
// Calculate remaining SMA points using sliding window
|
|
for (let i = period; i < data.length; i++) {
|
|
sum = sum - data[i - period].close + data[i].close;
|
|
smaData.push({ time: data[i].time, value: sum / period });
|
|
}
|
|
|
|
return smaData;
|
|
}
|