AGAIG Stop and Reverse Indicator For ThinkOrSwim

csricksdds

Trader Educator
VIP
AGAIG STOP AND REVERSE INDICATOR​

This is a new non-repainting indicator. The labels S & R are meant to STOP a Trade at that point (if one is in place) and enter a REVERSE trade at that point. The stop label is for placing hard stops should you wish to do so? I made trend arrows for the first six candles after a turn in direction.
Go here for the updated script:
https://usethinkscript.com/threads/...-indicator-for-thinkorswim.22476/#post-163005

3hJpTks.png

It includes:
» Two Bubbles — white S&R below bar on bullish flip, above bar on bearish flip​
» Three Labels​
▸ S&R (n) — candle count, shifts light at 10​
▸ Trend: LONG/SHORT | Stop: x.xx — shifts light at 10​
▸ ATR(14): x.xx — gray, volatility reference​

Every section in the code has plain English comments explaining what it does and why — handy if you ever come back to tune it later.

I think you will like this one for entries and exits.
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

I did a little code change on my STOPandREVERSE to move the labels or make them large and changed to Bubble words.
nTbKmFk.png


updated script:
Code:
# ============================================================
# AGAIG Stop & Reverse Indicator by C. Ricks 6/11/26
# Bubbles on direction change | Arrows follow trend
# Two Bubbles — white S&R below bar on bullish flip, above bar on bearish flip
# Three Labels

# S&R (n) — candle count, shifts light at 10
# Trend: LONG/SHORT | Stop: x.xx — shifts light at 10
# ATR(14): x.xx — gray, volatility reference

# ============================================================
# INPUTS
input sensitivity = 0.8; # Lower = more signals, Higher = fewer (0.1–2.0)
input atrLength = 14; # ATR period for stop distance
input atrMultiplier = 2.0; # ATR multiplier for stop distance
input showArrows = yes; # Show trend-follow arrows on every bar
input showBubbles = yes; # Show bubbles only on direction changes
input arrowOffset = 2; # Arrow distance from bar high/low (in ATR units)
input alertOnFlip = yes; # Sound alert on direction flip
input LabelSize = fontsize.medium;
input LabelLocation = location.Bottom_left;


# ============================================================
# ATR-BASED STOP CALCULATION
# ============================================================
def atr = ATR(atrLength);
def stopDist = atr * atrMultiplier * sensitivity;

# ============================================================
# STOP & REVERSE LOGIC
# Tracks a trailing stop that flips direction when price crosses it
# ============================================================
def isUpTrend;
def stopLevel;

# Seed: use close vs prior close for initial direction
def initUp = close > close[1];

# Trailing stop logic — flips when price crosses the stop
isUpTrend = if IsNaN(isUpTrend[1])
then initUp
else if isUpTrend[1] and low < stopLevel[1]
then 0 # Flip to downtrend
else if !isUpTrend[1] and high > stopLevel[1]
then 1 # Flip to uptrend
else isUpTrend[1];

stopLevel = if IsNaN(stopLevel[1])
then if isUpTrend then low - stopDist else high + stopDist
else if isUpTrend and isUpTrend[1]
then Max(stopLevel[1], low - stopDist) # Trail stop up in uptrend
else if !isUpTrend and !isUpTrend[1]
then Min(stopLevel[1], high + stopDist) # Trail stop down in downtrend
else if isUpTrend and !isUpTrend[1]
then low - stopDist # Reset on flip to uptrend
else high + stopDist; # Reset on flip to downtrend

# ============================================================
# DIRECTION CHANGE DETECTION
# ============================================================
def flippedUp = isUpTrend and !isUpTrend[1]; # Just turned bullish
def flippedDown = !isUpTrend and isUpTrend[1]; # Just turned bearish

# ============================================================
# CANDLE COUNT — bars elapsed since last direction change
# ============================================================
def candleCount;
candleCount = if flippedUp or flippedDown
then 1
else candleCount[1] + 1;

# ============================================================
# HEIKIN ASHI PRICE REFERENCES
# ============================================================
def haHigh = Fundamental(FundamentalType.HIGH, period = GetAggregationPeriod());
def haLow = Fundamental(FundamentalType.LOW, period = GetAggregationPeriod());
def haMid = (haHigh + haLow) / 2;
def arrowPad = atr * arrowOffset * 0.15;

# ============================================================
# EMBEDDED DIRECTIONAL ARROWS — Green up / Red down, size 1
# ============================================================
input markerSize = 2;

input arrowCutoff = 6; # Stop showing trend arrows after this many candles

plot BullMarker = if showArrows and isUpTrend and candleCount <= arrowCutoff then haMid else Double.NaN;
BullMarker.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BullMarker.SetDefaultColor(Color.GREEN);
BullMarker.SetLineWeight(markerSize);

plot BearMarker = if showArrows and !isUpTrend and candleCount <= arrowCutoff then haMid else Double.NaN;
BearMarker.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BearMarker.SetDefaultColor(Color.RED);
BearMarker.SetLineWeight(markerSize);

# ============================================================
# DIRECTION CHANGE BUBBLES — white background
# ============================================================
AddChartBubble(
showBubbles and flippedUp,
haLow - arrowPad * 2,
"S&R Long",
Color.WHITE,
no
);

AddChartBubble(
showBubbles and flippedDown,
haHigh + arrowPad * 2,
"S&R Short",
Color.WHITE,
yes
);

# ============================================================
# CANDLE COLORING (optional paintbar)
# ============================================================
AssignPriceColor(
if isUpTrend
then Color.GREEN
else Color.RED
);

# ============================================================
# ALERTS
# ============================================================
Alert(alertOnFlip and flippedUp, "Trend Spark SAR — Flipped BULLISH", Alert.BAR, Sound.Chimes);
Alert(alertOnFlip and flippedDown, "Trend Spark SAR — Flipped BEARISH", Alert.BAR, Sound.Bell);

# ============================================================
# EXTENDED TREND THRESHOLD
# ============================================================
input extendedThreshold = 10; # Candles before trend is considered extended

def isExtended = candleCount >= extendedThreshold;

# Color logic: bright green/red = active, light = extended

# ============================================================
# LABELS (status bar)
# S&R label with candle count | Trend & Stop | ATR
# ============================================================
AddLabel(yes,
"S & R(" + candleCount + ")",
if isUpTrend
then (if isExtended then Color.LIGHT_GREEN else Color.GREEN)
else (if isExtended then Color.PINK else Color.RED), LabelLocation, LabelSize
);

AddLabel(yes,
if isUpTrend
then "Trend: LONG | Stop: " + Round(stopLevel, 2)
else "Trend: SHORT | Stop: " + Round(stopLevel, 2),
if isUpTrend
then (if isExtended then Color.LIGHT_GREEN else Color.GREEN)
else (if isExtended then Color.PINK else Color.RED), LabelLocation, LabelSize
);

AddLabel(yes,
"ATR(" + atrLength + "): " + Round(atr, 2),
Color.GRAY, LabelLocation, LabelSize
);
 
Last edited by a moderator:
I did a little code change on my STOPandREVERSE to move the labels or make them large and changed to Bubble words.
nTbKmFk.png


updated script:
Code:
# ============================================================
# AGAIG Stop & Reverse Indicator by C. Ricks 6/11/26
# Bubbles on direction change | Arrows follow trend
# Two Bubbles — white S&R below bar on bullish flip, above bar on bearish flip
# Three Labels

# S&R (n) — candle count, shifts light at 10
# Trend: LONG/SHORT | Stop: x.xx — shifts light at 10
# ATR(14): x.xx — gray, volatility reference

# ============================================================
# INPUTS
input sensitivity = 0.8; # Lower = more signals, Higher = fewer (0.1–2.0)
input atrLength = 14; # ATR period for stop distance
input atrMultiplier = 2.0; # ATR multiplier for stop distance
input showArrows = yes; # Show trend-follow arrows on every bar
input showBubbles = yes; # Show bubbles only on direction changes
input arrowOffset = 2; # Arrow distance from bar high/low (in ATR units)
input alertOnFlip = yes; # Sound alert on direction flip
input LabelSize = fontsize.medium;
input LabelLocation = location.Bottom_left;


# ============================================================
# ATR-BASED STOP CALCULATION
# ============================================================
def atr = ATR(atrLength);
def stopDist = atr * atrMultiplier * sensitivity;

# ============================================================
# STOP & REVERSE LOGIC
# Tracks a trailing stop that flips direction when price crosses it
# ============================================================
def isUpTrend;
def stopLevel;

# Seed: use close vs prior close for initial direction
def initUp = close > close[1];

# Trailing stop logic — flips when price crosses the stop
isUpTrend = if IsNaN(isUpTrend[1])
then initUp
else if isUpTrend[1] and low < stopLevel[1]
then 0 # Flip to downtrend
else if !isUpTrend[1] and high > stopLevel[1]
then 1 # Flip to uptrend
else isUpTrend[1];

stopLevel = if IsNaN(stopLevel[1])
then if isUpTrend then low - stopDist else high + stopDist
else if isUpTrend and isUpTrend[1]
then Max(stopLevel[1], low - stopDist) # Trail stop up in uptrend
else if !isUpTrend and !isUpTrend[1]
then Min(stopLevel[1], high + stopDist) # Trail stop down in downtrend
else if isUpTrend and !isUpTrend[1]
then low - stopDist # Reset on flip to uptrend
else high + stopDist; # Reset on flip to downtrend

# ============================================================
# DIRECTION CHANGE DETECTION
# ============================================================
def flippedUp = isUpTrend and !isUpTrend[1]; # Just turned bullish
def flippedDown = !isUpTrend and isUpTrend[1]; # Just turned bearish

# ============================================================
# CANDLE COUNT — bars elapsed since last direction change
# ============================================================
def candleCount;
candleCount = if flippedUp or flippedDown
then 1
else candleCount[1] + 1;

# ============================================================
# HEIKIN ASHI PRICE REFERENCES
# ============================================================
def haHigh = Fundamental(FundamentalType.HIGH, period = GetAggregationPeriod());
def haLow = Fundamental(FundamentalType.LOW, period = GetAggregationPeriod());
def haMid = (haHigh + haLow) / 2;
def arrowPad = atr * arrowOffset * 0.15;

# ============================================================
# EMBEDDED DIRECTIONAL ARROWS — Green up / Red down, size 1
# ============================================================
input markerSize = 2;

input arrowCutoff = 6; # Stop showing trend arrows after this many candles

plot BullMarker = if showArrows and isUpTrend and candleCount <= arrowCutoff then haMid else Double.NaN;
BullMarker.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BullMarker.SetDefaultColor(Color.GREEN);
BullMarker.SetLineWeight(markerSize);

plot BearMarker = if showArrows and !isUpTrend and candleCount <= arrowCutoff then haMid else Double.NaN;
BearMarker.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BearMarker.SetDefaultColor(Color.RED);
BearMarker.SetLineWeight(markerSize);

# ============================================================
# DIRECTION CHANGE BUBBLES — white background
# ============================================================
AddChartBubble(
showBubbles and flippedUp,
haLow - arrowPad * 2,
"S&R Long",
Color.WHITE,
no
);

AddChartBubble(
showBubbles and flippedDown,
haHigh + arrowPad * 2,
"S&R Short",
Color.WHITE,
yes
);

# ============================================================
# CANDLE COLORING (optional paintbar)
# ============================================================
AssignPriceColor(
if isUpTrend
then Color.GREEN
else Color.RED
);

# ============================================================
# ALERTS
# ============================================================
Alert(alertOnFlip and flippedUp, "Trend Spark SAR — Flipped BULLISH", Alert.BAR, Sound.Chimes);
Alert(alertOnFlip and flippedDown, "Trend Spark SAR — Flipped BEARISH", Alert.BAR, Sound.Bell);

# ============================================================
# EXTENDED TREND THRESHOLD
# ============================================================
input extendedThreshold = 10; # Candles before trend is considered extended

def isExtended = candleCount >= extendedThreshold;

# Color logic: bright green/red = active, light = extended

# ============================================================
# LABELS (status bar)
# S&R label with candle count | Trend & Stop | ATR
# ============================================================
AddLabel(yes,
"S & R(" + candleCount + ")",
if isUpTrend
then (if isExtended then Color.LIGHT_GREEN else Color.GREEN)
else (if isExtended then Color.PINK else Color.RED), LabelLocation, LabelSize
);

AddLabel(yes,
if isUpTrend
then "Trend: LONG | Stop: " + Round(stopLevel, 2)
else "Trend: SHORT | Stop: " + Round(stopLevel, 2),
if isUpTrend
then (if isExtended then Color.LIGHT_GREEN else Color.GREEN)
else (if isExtended then Color.PINK else Color.RED), LabelLocation, LabelSize
);

AddLabel(yes,
"ATR(" + atrLength + "): " + Round(atr, 2),
Color.GRAY, LabelLocation, LabelSize
);
I guess I'm a little dense this morning and not sure where you mean to put my updated version? It looks like it will be one of the best I have come up with (although I'm working on a version that give some A.I. projections for expected ranges?). Making the labels larger is great for older people like me and I though it is a good upgrade...I could do it in tutoring maybe?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
1269 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