Change of character/Break of structure help needed...

johnwood34

Member
VIP
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);
 

Attachments

  • Screenshot 2025-08-08 234042.png
    Screenshot 2025-08-08 234042.png
    113.1 KB · Views: 9

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
367 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top