⚠️Advanced Support & Resistance
init = () => {
input.int("Lookback Period", 20, "lookback");
input.int("Touches", 2, "touches");
input.bool("Show Support", true, "showSupport");
input.bool("Show Resistance", true, "showResistance");
input.color("Support Color", color.green, "supportColor");
input.color("Resistance Color", color.red, "resistanceColor");
};
onTick = (length, _moment, _, ta, inputs) => {
const {
lookback,
touches,
showSupport,
showResistance,
supportColor,
resistanceColor,
} = inputs;
if (length < lookback) return;
const lows = Array.from({ length: lookback }, (_, i) => low(i)).sort(
(a, b) => a - b
);
const highs = Array.from({ length: lookback }, (_, i) => high(i)).sort(
(a, b) => b - a
);
const uniqueLows = [...new Set(lows)];
const uniqueHighs = [...new Set(highs)];
let support = null;
let resistance = null;
for (let lvl of uniqueLows) {
const count = lows.filter((l) => Math.abs(l - lvl) < 1e-8).length;
const broken = lows.some((_, i) => closeC(i) < lvl - 1e-8);
if (count >= touches && !broken) {
support = lvl;
break;
}
}
for (let lvl of uniqueHighs) {
const count = highs.filter((h) => Math.abs(h - lvl) < 1e-8).length;
const broken = highs.some((_, i) => closeC(i) > lvl + 1e-8);
if (count >= touches && !broken) {
resistance = lvl;
break;
}
}
if (showSupport && support !== null) {
horizontalLine(
time(0),
support,
{ linecolor: supportColor, linewidth: 2 },
"Support"
);
}
if (showResistance && resistance !== null) {
horizontalLine(
time(0),
resistance,
{ linecolor: resistanceColor, linewidth: 2 },
"Resistance"
);
}
};Last updated