I've had chatgpt adjust this and adjust that, about twenty times in an attempt to debug what it created. Anyone out there can help me with this? I know nothing about programming/thinkscript. If you need any clarification on what i'm trying to accomplish, please just ask. I'll also provide a link to a script i found here that has Change of character(CHOCH)/Break of structure(BOS) incorporated into.
https://www.tradingview.com/v/aM3PRWEM/
# Detect candle color
def isGreen = close > open;
def isRed = close < open;
# Plot line connecting swing highs/lows depending on candle color
def plotValue = if isGreen then high else if isRed then low else Double.NaN;
rec filledPlotValue = if !IsNaN(plotValue) then plotValue else filledPlotValue[1];
plot connectingLine = filledPlotValue;
connectingLine.SetDefaultColor(Color.CYAN);
connectingLine.SetLineWeight(2);
connectingLine.SetStyle(Curve.FIRM);
connectingLine.HideBubble();
connectingLine.HideTitle();
# Input: number of bars on each side for swing detection
input swingLen = 2;
# Define swing highs and swing lows
def swingHigh = high[swingLen] == Highest(high[swingLen], 2 * swingLen + 1);
def swingLow = low[swingLen] == Lowest(low[swingLen], 2 * swingLen + 1);
# Track latest swing high and low for breakout comparison
rec liveHigh = CompoundValue(1, high, if swingHigh then high[swingLen] else liveHigh[1]);
rec liveLow = CompoundValue(1, low, if swingLow then low[swingLen] else liveLow[1]);
# Determine trend direction
# 1 = uptrend (price broke above last swing high)
# -1 = downtrend (price broke below last swing low)
# 0 = no trend / initialization
rec trendDir = CompoundValue(1, 0,
if BarNumber() == 1 then 0
else if close > liveHigh[1] then 1
else if close < liveLow[1] then -1
else trendDir[1]);
# Detect Break of Structure (BOS)
def bosUp = trendDir[1] == 1 and high > liveHigh[1];
def bosDown = trendDir[1] == -1 and low < liveLow[1];
# Protected levels hold the swing low/high for potential CHoCH detection
rec protectedLow = CompoundValue(1, Double.NaN,
if BarNumber() == 1 then Double.NaN
else if bosUp and swingLow then low[swingLen]
else if trendDir[1] == 1 and close < protectedLow[1] then Double.NaN
else protectedLow[1]);
rec protectedLowBar = CompoundValue(1, 0,
if BarNumber() == 1 then 0
else if bosUp and swingLow then BarNumber()
else if trendDir[1] == 1 and close < protectedLow[1] then 0
else protectedLowBar[1]);
rec protectedHigh = CompoundValue(1, Double.NaN,
if BarNumber() == 1 then Double.NaN
else if bosDown and swingHigh then high[swingLen]
else if trendDir[1] == -1 and close > protectedHigh[1] then Double.NaN
else protectedHigh[1]);
rec protectedHighBar = CompoundValue(1, 0,
if BarNumber() == 1 then 0
else if bosDown and swingHigh then BarNumber()
else if trendDir[1] == -1 and close > protectedHigh[1] then 0
else protectedHighBar[1]);
# Detect Change of Character (CHoCH)
def chochUp = trendDir[1] == -1 and close > protectedHigh[1];
def chochDown = trendDir[1] == 1 and close < protectedLow[1];
# Track last event: 0 = none, 1 = CHoCH, 2 = BOS
rec lastEvent = CompoundValue(1, 0,
if chochUp or chochDown then 1
else if bosUp or bosDown then 2
else lastEvent[1]);
# Plot bubbles on candles responsible for BOS and CHoCH
AddChartBubble(chochUp, close, "CHoCH↑", Color.GREEN, no);
AddChartBubble(chochDown, close, "CHoCH↓", Color.RED, yes);
AddChartBubble(bosUp, close, "BOS↑", Color.CYAN, no);
AddChartBubble(bosDown, close, "BOS↓", Color.ORANGE, yes);
# Plot protected levels as dashed lines
plot PHLine = if BarNumber() >= protectedHighBar and !IsNaN(protectedHigh) and close < protectedHigh then protectedHigh else Double.NaN;
PHLine.SetDefaultColor(Color.RED);
PHLine.SetStyle(Curve.SHORT_DASH);
PHLine.SetLineWeight(2);
plot PLLine = if BarNumber() >= protectedLowBar and !IsNaN(protectedLow) and close > protectedLow then protectedLow else Double.NaN;
PLLine.SetDefaultColor(Color.GREEN);
PLLine.SetStyle(Curve.SHORT_DASH);
PLLine.SetLineWeight(2);
https://www.tradingview.com/v/aM3PRWEM/
# Detect candle color
def isGreen = close > open;
def isRed = close < open;
# Plot line connecting swing highs/lows depending on candle color
def plotValue = if isGreen then high else if isRed then low else Double.NaN;
rec filledPlotValue = if !IsNaN(plotValue) then plotValue else filledPlotValue[1];
plot connectingLine = filledPlotValue;
connectingLine.SetDefaultColor(Color.CYAN);
connectingLine.SetLineWeight(2);
connectingLine.SetStyle(Curve.FIRM);
connectingLine.HideBubble();
connectingLine.HideTitle();
# Input: number of bars on each side for swing detection
input swingLen = 2;
# Define swing highs and swing lows
def swingHigh = high[swingLen] == Highest(high[swingLen], 2 * swingLen + 1);
def swingLow = low[swingLen] == Lowest(low[swingLen], 2 * swingLen + 1);
# Track latest swing high and low for breakout comparison
rec liveHigh = CompoundValue(1, high, if swingHigh then high[swingLen] else liveHigh[1]);
rec liveLow = CompoundValue(1, low, if swingLow then low[swingLen] else liveLow[1]);
# Determine trend direction
# 1 = uptrend (price broke above last swing high)
# -1 = downtrend (price broke below last swing low)
# 0 = no trend / initialization
rec trendDir = CompoundValue(1, 0,
if BarNumber() == 1 then 0
else if close > liveHigh[1] then 1
else if close < liveLow[1] then -1
else trendDir[1]);
# Detect Break of Structure (BOS)
def bosUp = trendDir[1] == 1 and high > liveHigh[1];
def bosDown = trendDir[1] == -1 and low < liveLow[1];
# Protected levels hold the swing low/high for potential CHoCH detection
rec protectedLow = CompoundValue(1, Double.NaN,
if BarNumber() == 1 then Double.NaN
else if bosUp and swingLow then low[swingLen]
else if trendDir[1] == 1 and close < protectedLow[1] then Double.NaN
else protectedLow[1]);
rec protectedLowBar = CompoundValue(1, 0,
if BarNumber() == 1 then 0
else if bosUp and swingLow then BarNumber()
else if trendDir[1] == 1 and close < protectedLow[1] then 0
else protectedLowBar[1]);
rec protectedHigh = CompoundValue(1, Double.NaN,
if BarNumber() == 1 then Double.NaN
else if bosDown and swingHigh then high[swingLen]
else if trendDir[1] == -1 and close > protectedHigh[1] then Double.NaN
else protectedHigh[1]);
rec protectedHighBar = CompoundValue(1, 0,
if BarNumber() == 1 then 0
else if bosDown and swingHigh then BarNumber()
else if trendDir[1] == -1 and close > protectedHigh[1] then 0
else protectedHighBar[1]);
# Detect Change of Character (CHoCH)
def chochUp = trendDir[1] == -1 and close > protectedHigh[1];
def chochDown = trendDir[1] == 1 and close < protectedLow[1];
# Track last event: 0 = none, 1 = CHoCH, 2 = BOS
rec lastEvent = CompoundValue(1, 0,
if chochUp or chochDown then 1
else if bosUp or bosDown then 2
else lastEvent[1]);
# Plot bubbles on candles responsible for BOS and CHoCH
AddChartBubble(chochUp, close, "CHoCH↑", Color.GREEN, no);
AddChartBubble(chochDown, close, "CHoCH↓", Color.RED, yes);
AddChartBubble(bosUp, close, "BOS↑", Color.CYAN, no);
AddChartBubble(bosDown, close, "BOS↓", Color.ORANGE, yes);
# Plot protected levels as dashed lines
plot PHLine = if BarNumber() >= protectedHighBar and !IsNaN(protectedHigh) and close < protectedHigh then protectedHigh else Double.NaN;
PHLine.SetDefaultColor(Color.RED);
PHLine.SetStyle(Curve.SHORT_DASH);
PHLine.SetLineWeight(2);
plot PLLine = if BarNumber() >= protectedLowBar and !IsNaN(protectedLow) and close > protectedLow then protectedLow else Double.NaN;
PLLine.SetDefaultColor(Color.GREEN);
PLLine.SetStyle(Curve.SHORT_DASH);
PLLine.SetLineWeight(2);