GHLA SuperTrend CCI HighLow Activator For ThinkOrSwim

After you add the STDEV zones then add this to the bottom of the script for labels
Code:
# === Oscillator Range Analysis ===

def isOverbought2 = GannOsc >= UpperLine2;
def isOverbought1 = GannOsc >= UpperLine1;
def isOversold2   = GannOsc <= LowerLine2;
def isOversold1   = GannOsc <= LowerLine1;

def oscZone =
    if isOverbought2 then 2
    else if isOverbought1 then 1
    else if isOversold2 then -2
    else if isOversold1 then -1
    else 0;

def forecastSignal =
    if upSignal and oscZone <= -1 then 1  # Strong Buy
    else if downSignal and oscZone >= 1 then -1  # Strong Sell
    else if upSignal then 2  # Buy Opportunity
    else if downSignal then -2  # Sell Opportunity
    else 0;

# === AddLabel Displays ===

AddLabel(yes,
    if upSignal then "TREND: Bullish (UP)"
    else if downSignal then "TREND: Bearish (DOWN)"
    else "TREND: Neutral/Sideways",
    if upSignal then Color.GREEN
    else if downSignal then Color.RED
    else Color.GRAY
);

AddLabel(yes,
    "GANN STATE: " +
    (if lastState == 1 then "Bull Zone"
     else if lastState == -1 then "Bear Zone"
     else "Neutral"),
    if lastState == 1 then CreateColor(0, 255, 0)
    else if lastState == -1 then CreateColor(255, 165, 0)
    else Color.GRAY
);

AddLabel(yes,
    "OSC Range: " +
    (if oscZone == 2 then "Overbought (2σ)"
     else if oscZone == 1 then "Overbought (1.5σ)"
     else if oscZone == -1 then "Oversold (1.5σ)"
     else if oscZone == -2 then "Oversold (2σ)"
     else "In Range"),
    if oscZone >= 1 then Color.RED
    else if oscZone <= -1 then Color.GREEN
    else Color.LIGHT_GRAY
);

AddLabel(yes,
    "FORECAST: " +
    (if forecastSignal == 1 then "Strong BUY Zone"
     else if forecastSignal == -1 then "Strong SELL Zone"
     else if forecastSignal == 2 then "Buying Opportunity"
     else if forecastSignal == -2 then "Selling Opportunity"
     else "Wait / Watch"),
    if forecastSignal == 1 or forecastSignal == 2 then Color.GREEN
    else if forecastSignal == -1 or forecastSignal == -2 then Color.RED
    else Color.YELLOW
);
 
Here the script with the 2 additional parts by @chewie76 and @antwerks ,thank you.
uFSlaN7.png

Code:
# GHLA_SuperTrend_CCI_High_Low_Activator_Oscillator
#By CANDO13579
#STDEV zones by Chewie76
#Oscillator Range Analysis Labels by Antwerks

declare lower;
declare real_size;
input ghlaPeriod = 10;
input stPeriod = 14;
input stShiftTicks = 20.0;
input useFilter = yes;

def minTick = TickSize();
def shift = stShiftTicks * minTick;

# -----------------------------
# GHLA Logic
# -----------------------------
def ghlaAvgH = Average(high, ghlaPeriod);
def ghlaAvgL = Average(low, ghlaPeriod);

def sw = if close > ghlaAvgH then 1
else if close < ghlaAvgL then -1
else 0;

def ghlaDir = CompoundValue(1, if sw != 0 then sw else ghlaDir[1], 0);
def ghlaLevel = if ghlaDir < 0 then ghlaAvgH else ghlaAvgL;

# -----------------------------
# Custom CCI using Typical Price
# -----------------------------
def tp = (high + low + close) / 3;
def tpSMA = Average(tp, stPeriod);
def meanDev = Average(AbsValue(tp - tpSMA), stPeriod);
def cciValue = if meanDev == 0 then 0 else (tp - tpSMA) / (0.015 * meanDev);

# -----------------------------
# SuperTrend State Machine
# -----------------------------

# stFlag: 1=long, -1=short
rec stFlag = if IsNaN(stFlag[1]) then if cciValue >= 0 then 1 else -1
else if cciValue > 0 and stFlag[1] <= 0 then 1
else if cciValue < 0 and stFlag[1] >= 0 then -1
else stFlag[1];

rec st = if IsNaN(st[1]) then if cciValue >= 0 then low - shift else high + shift
else
if cciValue > 0 and stFlag[1] <= 0 then low - shift
else if cciValue < 0 and stFlag[1] >= 0 then high + shift
else
if stFlag > 0 and (low - shift) > st[1] then low - shift
else if stFlag < 0 and (high + shift) < st[1] then high + shift
else
if useFilter and stFlag > 0 and st[1] < st[1] and (close < open or high < high[1]) then st[1]
else if useFilter and stFlag < 0 and st[1] > st[1] and (close > open or low > low[1]) then st[1]
else st[1];

# -----------------------------
# signals
# -----------------------------
def notNaST = !IsNaN(st);
def upSignal = notNaST and close > st and close > ghlaLevel;
def downSignal = notNaST and close < st and close < ghlaLevel;
def neutralSignal = notNaST and !upSignal and !downSignal;

# -----------------------------
# Output (centered)
# -----------------------------
def signalCenter = 0;
plot Sig = signalCenter;
Sig.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Sig.SetLineWeight(4);

Sig.AssignValueColor(
if upSignal then Color.GREEN
else if downSignal then Color.RED
else Color.LIGHT_GRAY
);

# Boundary reference lines
# -----------------------------
plot UpperLine = 1;
UpperLine.SetDefaultColor(Color.DARK_GRAY);
UpperLine.SetStyle(Curve.SHORT_DASH);

plot LowerLine = -1;
LowerLine.SetDefaultColor(Color.DARK_GRAY);
LowerLine.SetStyle(Curve.SHORT_DASH);

# Gann High/Low Activator Oscillator

input activatorPeriod = 14;

# Calculate simple moving averages
def avgHigh = Average(high, activatorPeriod);
def avgLow = Average(low, activatorPeriod);

# Determine state
def bullState = close > avgHigh;
def bearState = close < avgLow;

# Track the last valid state
def lastState = CompoundValue(1,
if bullState then 1
else if bearState then -1
else lastState[1],
0);

def avgRange = avgHigh - avgLow;
def oscillator =
if avgRange != 0 then
if lastState == 1 then (close - avgLow) / avgRange
else if lastState == -1 then (close - avgHigh) / avgRange
else 0.5
else 0.5;

# Plot oscillator
plot GannOsc = oscillator;
GannOsc.SetLineWeight(2);
GannOsc.AssignValueColor(
if lastState == 1 then CreateColor(0, 255, 0) # Green
else if lastState == -1 then CreateColor(255, 165, 0) # Orange
else Color.GRAY
);

# Plot midline
plot MidLine = 0.5;
MidLine.SetDefaultColor(Color.LIGHT_GRAY);
MidLine.SetStyle(Curve.SHORT_DASH);
# 2 Standard Deviation Full

input deviations = 2.0;
input deviations2 = 2.5;
input fullRange = yes;
input length = 300;

def regression;
def stdDeviation;
if (fullRange) {
regression = InertiaAll(GannOsc);
stdDeviation = StDevAll(GannOsc);
} else {
regression = InertiaAll(GannOsc, length);
stdDeviation = StDevAll(GannOsc, length);
}

plot UpperLine1 = regression + deviations * stdDeviation;
#plot MiddleLine = regression;
plot LowerLine1 = regression - deviations * stdDeviation;

UpperLine1.SetDefaultColor(Color.RED);
LowerLine1.SetDefaultColor(Color.GREEN);
UpperLine1.SetLineWeight(1);
LowerLine1.SetLineWeight(1);
UpperLine1.HideBubble();
UpperLine1.HideTitle();
LowerLine1.HideBubble();
LowerLine1.HideTitle();


plot UpperLine2 = regression + deviations2 * stdDeviation;
plot LowerLine2 = regression - deviations2 * stdDeviation;

UpperLine2.SetDefaultColor(Color.RED);
LowerLine2.SetDefaultColor(Color.GREEN);
UpperLine2.SetLineWeight(1);
LowerLine2.SetLineWeight(1);
UpperLine2.HideBubble();
UpperLine2.HideTitle();
LowerLine2.HideBubble();
LowerLine2.HideTitle();

AddCloud(UpperLine2, UpperLine1, Color.RED, Color.CURRENT);
AddCloud(LowerLine1, LowerLine2, Color.GREEN, Color.CURRENT);
# === Oscillator Range Analysis ===

def isOverbought2 = GannOsc >= UpperLine2;
def isOverbought1 = GannOsc >= UpperLine1;
def isOversold2 = GannOsc <= LowerLine2;
def isOversold1 = GannOsc <= LowerLine1;

def oscZone =
if isOverbought2 then 2
else if isOverbought1 then 1
else if isOversold2 then -2
else if isOversold1 then -1
else 0;

def forecastSignal =
if upSignal and oscZone <= -1 then 1 # Strong Buy
else if downSignal and oscZone >= 1 then -1 # Strong Sell
else if upSignal then 2 # Buy Opportunity
else if downSignal then -2 # Sell Opportunity
else 0;

# === AddLabel Displays ===

AddLabel(yes,
if upSignal then "TREND: Bullish (UP)"
else if downSignal then "TREND: Bearish (DOWN)"
else "TREND: Neutral/Sideways",
if upSignal then Color.GREEN
else if downSignal then Color.RED
else Color.GRAY
);

AddLabel(yes,
"GANN STATE: " +
(if lastState == 1 then "Bull Zone"
else if lastState == -1 then "Bear Zone"
else "Neutral"),
if lastState == 1 then CreateColor(0, 255, 0)
else if lastState == -1 then CreateColor(255, 165, 0)
else Color.GRAY
);

AddLabel(yes,
"OSC Range: " +
(if oscZone == 2 then "Overbought (2σ)"
else if oscZone == 1 then "Overbought (1.5σ)"
else if oscZone == -1 then "Oversold (1.5σ)"
else if oscZone == -2 then "Oversold (2σ)"
else "In Range"),
if oscZone >= 1 then Color.RED
else if oscZone <= -1 then Color.GREEN
else Color.LIGHT_GRAY
);

AddLabel(yes,
"FORECAST: " +
(if forecastSignal == 1 then "Strong BUY Zone"
else if forecastSignal == -1 then "Strong SELL Zone"
else if forecastSignal == 2 then "Buying Opportunity"
else if forecastSignal == -2 then "Selling Opportunity"
else "Wait / Watch"),
if forecastSignal == 1 or forecastSignal == 2 then Color.GREEN
else if forecastSignal == -1 or forecastSignal == -2 then Color.RED
else Color.YELLOW
);

Here:
 

Attachments

  • GHLAV3.png
    GHLAV3.png
    70.2 KB · Views: 109
Last edited:
Adding an average line is also helpful. Adjust to your liking. Try 8/9 for shorter, or 20 for longer.

input linelength = 20;
plot line = MovingAverage( AverageType.EXPONENTIAL, GannOsc, linelength);
line.setdefaultColor(color.white);
 
Having a histogram plotted can also help you as it shows each candle change. Give this a go.
1766232062998.png


plot GannOscH = oscillator;
GannOscH.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

GannOscH.AssignValueColor(if GannOscH > GannOscH[1] and GannOscH > 0
then Color.yellow
else if GannOscH > 0 and GannOscH < GannOscH[1]
then color.red
else if GannOscH < 0 and GannOscH < GannOscH[1]
then Color.cyan
else createColor(51,102,255));
 
Having a histogram plotted can also help you as it shows each candle change. Give this a go.
View attachment 26607

plot GannOscH = oscillator;
GannOscH.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

GannOscH.AssignValueColor(if GannOscH > GannOscH[1] and GannOscH > 0
then Color.yellow
else if GannOscH > 0 and GannOscH < GannOscH[1]
then color.red
else if GannOscH < 0 and GannOscH < GannOscH[1]
then Color.cyan
else createColor(51,102,255));
unless the colors are well defined then there may be confusion and definitly competing visual away from the signal red/green or green/red event... I would include a toggle to turn on or off
Code:
# === Optional Histogram Layer for GannOscillator ===
input showHistogram = yes;

plot GannOscH = if showHistogram then oscillator else Double.NaN;
GannOscH.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
GannOscH.SetLineWeight(2);

GannOscH.AssignValueColor(
    if GannOscH > GannOscH[1] and GannOscH > 0 then Color.YELLOW
    else if GannOscH > 0 and GannOscH < GannOscH[1] then Color.RED
    else if GannOscH < 0 and GannOscH < GannOscH[1] then Color.CYAN
    else CreateColor(51, 102, 255)
);
 
This is terrific, thanks. Can anyone create a scan for buy or sell crossovers?
Here is a super bullish scan for the GHLA
What This Scan Looks For

ConditionMeaning
upSignal = truePrice is above SuperTrend and GHLA
oscillator <= LowerLineMomentum oscillator is oversold
ResultStrong buy or early trend entry
Code:
# === Bullish Scan for GHLA_SuperTrend_CCI_High_Low_Activator_Oscillator ===

input ghlaPeriod = 10;
input stPeriod = 14;
input stShiftTicks = 20.0;
input useFilter = yes;
input activatorPeriod = 14;

def minTick = TickSize();
def shift = stShiftTicks * minTick;

# GHLA
def ghlaAvgH = Average(high, ghlaPeriod);
def ghlaAvgL = Average(low, ghlaPeriod);
def sw = if close > ghlaAvgH then 1 else if close < ghlaAvgL then -1 else 0;
def ghlaDir = CompoundValue(1, if sw != 0 then sw else ghlaDir[1], 0);
def ghlaLevel = if ghlaDir < 0 then ghlaAvgH else ghlaAvgL;

# SuperTrend (based on CCI)
def tp = (high + low + close) / 3;
def tpSMA = Average(tp, stPeriod);
def meanDev = Average(AbsValue(tp - tpSMA), stPeriod);
def cciValue = if meanDev == 0 then 0 else (tp - tpSMA) / (0.015 * meanDev);

rec stFlag = if IsNaN(stFlag[1]) then if cciValue >= 0 then 1 else -1
else if cciValue > 0 and stFlag[1] <= 0 then 1
else if cciValue < 0 and stFlag[1] >= 0 then -1
else stFlag[1];

rec st = if IsNaN(st[1]) then if cciValue >= 0 then low - shift else high + shift
else
if cciValue > 0 and stFlag[1] <= 0 then low - shift
else if cciValue < 0 and stFlag[1] >= 0 then high + shift
else
if stFlag > 0 and (low - shift) > st[1] then low - shift
else if stFlag < 0 and (high + shift) < st[1] then high + shift
else st[1];

def notNaST = !IsNaN(st);
def upSignal = notNaST and close > st and close > ghlaLevel;

# Gann High/Low Activator
def avgHigh = Average(high, activatorPeriod);
def avgLow = Average(low, activatorPeriod);
def lastState = CompoundValue(1,
    if close > avgHigh then 1
    else if close < avgLow then -1
    else lastState[1], 0);

def avgRange = avgHigh - avgLow;
def oscillator =
if avgRange != 0 then
    if lastState == 1 then (close - avgLow) / avgRange
    else if lastState == -1 then (close - avgHigh) / avgRange
    else 0.5
else 0.5;

# Oscillator Zones
input deviations = 2.0;
input deviations2 = 2.5;
input fullRange = yes;
input length = 300;

def regression = if fullRange then InertiaAll(oscillator) else InertiaAll(oscillator, length);
def stdDeviation = if fullRange then StDevAll(oscillator) else StDevAll(oscillator, length);

def UpperLine1 = regression + deviations * stdDeviation;
def LowerLine1 = regression - deviations * stdDeviation;
def UpperLine2 = regression + deviations2 * stdDeviation;
def LowerLine2 = regression - deviations2 * stdDeviation;

def isOversold1 = oscillator <= LowerLine1;
def isOversold2 = oscillator <= LowerLine2;

# === Bullish Conditions ===
# Strong Buy if upSignal and oversold (1.5σ or 2σ)
# Buy Opportunity if upSignal but not oversold

def strongBuy = upSignal and (isOversold1 or isOversold2);
def buyOpportunity = upSignal and !(isOversold1 or isOversold2);

plot scan = strongBuy or buyOpportunity;

https://tos.mx/!Zlyetirk

 
One written that resembles the GHLA very closely. Features:
  • Breakouts: Cross outside ±2 standard deviation Bollinger Bands
  • Reversals: Cross back inside the bands (from outside)
  • Trend Context: Band expansion/contraction before breakout
  • Metrics Displayed:
    • 20-day SMA
    • Distance from SMA (%)
    • BB% (Relative Band Position)
To Use:
  • Works as both a lower study (visual) or a scanner column (easily adaptable).
  • Add optional market cap filter manually in TOS Stock Hacker → Fundamental Filter.

https://tos.mx/!FJgOy9yp
Code:
# === Bollinger Breakouts & Reversals for Large Caps ===
# === ANTWERKS 12/22/2025
declare lower;

input length = 20;               # Bollinger SMA length
input numDev = 2.0;              # Std dev multiplier
input price = close;

# === Bollinger Band Calculation ===
def basis = Average(price, length);
def dev = StDev(price, length);
def upper = basis + numDev * dev;
def lower = basis - numDev * dev;

# === Band Width & Expansion/Contraction ===
def bandWidth = upper - lower;
def prevBandWidth = bandWidth[1];
def isExpanding = bandWidth > prevBandWidth;
def isContracting = bandWidth < prevBandWidth;

# === Price Position vs Bands ===
def wasAbove = price[1] > upper[1];
def wasBelow = price[1] < lower[1];
def isAbove = price > upper;
def isBelow = price < lower;
def inside = price < upper and price > lower;

# === BB%: Position within band range ===
# BB%: 0% = lower band, 100% = upper band
# BB% > 100 = outside upper, < 0 = outside lower
def bbPct = ((price - lower) / (upper - lower)) * 100;

# === Distance from SMA (%) ===
def distFromSMA = ((price - basis) / basis) * 100;

# === Breakout and Reversal Logic ===
def breakoutUp = !wasAbove and isAbove;
def breakoutDown = !wasBelow and isBelow;
def reversalDown = wasAbove and inside;
def reversalUp = wasBelow and inside;

# === Output Labels (Visual Study) ===
AddLabel(yes, "20-SMA: " + Round(basis, 2), Color.WHITE);
AddLabel(yes, "Dist from SMA: " + Round(distFromSMA, 1) + "%",
          if distFromSMA > 0 then Color.GREEN else Color.RED);
AddLabel(yes, "BB%: " + Round(bbPct, 1) + "%",
          if bbPct > 100 then Color.LIGHT_GREEN
          else if bbPct < 0 then Color.PINK
          else Color.YELLOW);

AddLabel(yes,
    if breakoutUp then "Breakout UP"
    else if breakoutDown then "Breakout DN"
    else if reversalUp then "Reversal UP"
    else if reversalDown then "Reversal DN"
    else "No Signal",
    if breakoutUp then Color.GREEN
    else if breakoutDown then Color.RED
    else if reversalUp then Color.CYAN
    else if reversalDown then Color.MAGENTA
    else Color.GRAY
);

AddLabel(yes,
    if isExpanding then "Band Expanding"
    else if isContracting then "Band Contracting"
    else "Band Steady",
    if isExpanding then Color.GREEN
    else if isContracting then Color.RED
    else Color.LIGHT_GRAY
);

# === Optional Plots for Visual Backtesting ===
plot BB_Pct = bbPct;
BB_Pct.SetDefaultColor(Color.CYAN);
BB_Pct.SetLineWeight(2);



# === Midline / Basis Plot ===
plot Midline = Average(close, 20);  # or use 'basis' if already defined
Midline.SetLineWeight(2);
Midline.AssignValueColor(
    if close > Midline then Color.GREEN
    else if close < Midline then Color.RED
    else Color.GRAY
);
Midline.SetStyle(Curve.MEDIUM_DASH);
1766385522201.png
 
Here is a super bullish scan for the GHLA
What This Scan Looks For

ConditionMeaning
upSignal = truePrice is above SuperTrend and GHLA
oscillator <= LowerLineMomentum oscillator is oversold
ResultStrong buy or early trend entry
Code:
# === Bullish Scan for GHLA_SuperTrend_CCI_High_Low_Activator_Oscillator ===

input ghlaPeriod = 10;
input stPeriod = 14;
input stShiftTicks = 20.0;
input useFilter = yes;
input activatorPeriod = 14;

def minTick = TickSize();
def shift = stShiftTicks * minTick;

# GHLA
def ghlaAvgH = Average(high, ghlaPeriod);
def ghlaAvgL = Average(low, ghlaPeriod);
def sw = if close > ghlaAvgH then 1 else if close < ghlaAvgL then -1 else 0;
def ghlaDir = CompoundValue(1, if sw != 0 then sw else ghlaDir[1], 0);
def ghlaLevel = if ghlaDir < 0 then ghlaAvgH else ghlaAvgL;

# SuperTrend (based on CCI)
def tp = (high + low + close) / 3;
def tpSMA = Average(tp, stPeriod);
def meanDev = Average(AbsValue(tp - tpSMA), stPeriod);
def cciValue = if meanDev == 0 then 0 else (tp - tpSMA) / (0.015 * meanDev);

rec stFlag = if IsNaN(stFlag[1]) then if cciValue >= 0 then 1 else -1
else if cciValue > 0 and stFlag[1] <= 0 then 1
else if cciValue < 0 and stFlag[1] >= 0 then -1
else stFlag[1];

rec st = if IsNaN(st[1]) then if cciValue >= 0 then low - shift else high + shift
else
if cciValue > 0 and stFlag[1] <= 0 then low - shift
else if cciValue < 0 and stFlag[1] >= 0 then high + shift
else
if stFlag > 0 and (low - shift) > st[1] then low - shift
else if stFlag < 0 and (high + shift) < st[1] then high + shift
else st[1];

def notNaST = !IsNaN(st);
def upSignal = notNaST and close > st and close > ghlaLevel;

# Gann High/Low Activator
def avgHigh = Average(high, activatorPeriod);
def avgLow = Average(low, activatorPeriod);
def lastState = CompoundValue(1,
    if close > avgHigh then 1
    else if close < avgLow then -1
    else lastState[1], 0);

def avgRange = avgHigh - avgLow;
def oscillator =
if avgRange != 0 then
    if lastState == 1 then (close - avgLow) / avgRange
    else if lastState == -1 then (close - avgHigh) / avgRange
    else 0.5
else 0.5;

# Oscillator Zones
input deviations = 2.0;
input deviations2 = 2.5;
input fullRange = yes;
input length = 300;

def regression = if fullRange then InertiaAll(oscillator) else InertiaAll(oscillator, length);
def stdDeviation = if fullRange then StDevAll(oscillator) else StDevAll(oscillator, length);

def UpperLine1 = regression + deviations * stdDeviation;
def LowerLine1 = regression - deviations * stdDeviation;
def UpperLine2 = regression + deviations2 * stdDeviation;
def LowerLine2 = regression - deviations2 * stdDeviation;

def isOversold1 = oscillator <= LowerLine1;
def isOversold2 = oscillator <= LowerLine2;

# === Bullish Conditions ===
# Strong Buy if upSignal and oversold (1.5σ or 2σ)
# Buy Opportunity if upSignal but not oversold

def strongBuy = upSignal and (isOversold1 or isOversold2);
def buyOpportunity = upSignal and !(isOversold1 or isOversold2);

plot scan = strongBuy or buyOpportunity;

https://tos.mx/!Zlyetirk

Thank you for this! it is very useful but I'm wondering if it can be tweaked to spot these trends early, as in a crossover?
 
Hi Antwerks, I always enjoy your scripts. When I set up your scan in #34 above, I am not getting the Bearish Reversal in the SCAN set up box as shown above. My shows " scan" and "true" not "Gann OSC". Can you help?
sorry I thought you were asking for a GHLA scan and you would need to set this indicator up first then use that study to set up the scan
Code:
# GHLA_SuperTrend_CCI_High_Low_Activator_Oscillator
#By CANDO13579
#STDEV zones by Chewie76
#Oscillator Range Analysis Labels by Antwerks

declare lower;
declare real_size;
input ghlaPeriod = 10;
input stPeriod = 14;
input stShiftTicks = 20.0;
input useFilter = yes;

def minTick = TickSize();
def shift = stShiftTicks * minTick;

# -----------------------------
# GHLA Logic
# -----------------------------
def ghlaAvgH = Average(high, ghlaPeriod);
def ghlaAvgL = Average(low, ghlaPeriod);

def sw = if close > ghlaAvgH then 1
else if close < ghlaAvgL then -1
else 0;

def ghlaDir = CompoundValue(1, if sw != 0 then sw else ghlaDir[1], 0);
def ghlaLevel = if ghlaDir < 0 then ghlaAvgH else ghlaAvgL;

# -----------------------------
# Custom CCI using Typical Price
# -----------------------------
def tp = (high + low + close) / 3;
def tpSMA = Average(tp, stPeriod);
def meanDev = Average(AbsValue(tp - tpSMA), stPeriod);
def cciValue = if meanDev == 0 then 0 else (tp - tpSMA) / (0.015 * meanDev);

# -----------------------------
# SuperTrend State Machine
# -----------------------------

# stFlag: 1=long, -1=short
rec stFlag = if IsNaN(stFlag[1]) then if cciValue >= 0 then 1 else -1
else if cciValue > 0 and stFlag[1] <= 0 then 1
else if cciValue < 0 and stFlag[1] >= 0 then -1
else stFlag[1];

rec st = if IsNaN(st[1]) then if cciValue >= 0 then low - shift else high + shift
else
if cciValue > 0 and stFlag[1] <= 0 then low - shift
else if cciValue < 0 and stFlag[1] >= 0 then high + shift
else
if stFlag > 0 and (low - shift) > st[1] then low - shift
else if stFlag < 0 and (high + shift) < st[1] then high + shift
else
if useFilter and stFlag > 0 and st[1] < st[1] and (close < open or high < high[1]) then st[1]
else if useFilter and stFlag < 0 and st[1] > st[1] and (close > open or low > low[1]) then st[1]
else st[1];

# -----------------------------
# signals
# -----------------------------
def notNaST = !IsNaN(st);
def upSignal = notNaST and close > st and close > ghlaLevel;
def downSignal = notNaST and close < st and close < ghlaLevel;
def neutralSignal = notNaST and !upSignal and !downSignal;

# -----------------------------
# Output (centered)
# -----------------------------
def signalCenter = 0;
plot Sig = signalCenter;
Sig.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Sig.SetLineWeight(4);

Sig.AssignValueColor(
if upSignal then Color.GREEN
else if downSignal then Color.RED
else Color.LIGHT_GRAY
);

# Boundary reference lines
# -----------------------------
plot UpperLine = 1;
UpperLine.SetDefaultColor(Color.DARK_GRAY);
UpperLine.SetStyle(Curve.SHORT_DASH);

plot LowerLine = -1;
LowerLine.SetDefaultColor(Color.DARK_GRAY);
LowerLine.SetStyle(Curve.SHORT_DASH);

# Gann High/Low Activator Oscillator

input activatorPeriod = 14;

# Calculate simple moving averages
def avgHigh = Average(high, activatorPeriod);
def avgLow = Average(low, activatorPeriod);

# Determine state
def bullState = close > avgHigh;
def bearState = close < avgLow;

# Track the last valid state
def lastState = CompoundValue(1,
if bullState then 1
else if bearState then -1
else lastState[1],
0);

def avgRange = avgHigh - avgLow;
def oscillator =
if avgRange != 0 then
if lastState == 1 then (close - avgLow) / avgRange
else if lastState == -1 then (close - avgHigh) / avgRange
else 0.5
else 0.5;

# Plot oscillator
plot GannOsc = oscillator;
GannOsc.SetLineWeight(2);
GannOsc.AssignValueColor(
if lastState == 1 then CreateColor(0, 255, 0) # Green
else if lastState == -1 then CreateColor(255, 165, 0) # Orange
else Color.GRAY
);

# Plot midline
plot MidLine = 0.5;
MidLine.SetDefaultColor(Color.LIGHT_GRAY);
MidLine.SetStyle(Curve.SHORT_DASH);
# 2 Standard Deviation Full

input deviations = 2.0;
input deviations2 = 2.5;
input fullRange = yes;
input length = 300;

def regression;
def stdDeviation;
if (fullRange) {
regression = InertiaAll(GannOsc);
stdDeviation = StDevAll(GannOsc);
} else {
regression = InertiaAll(GannOsc, length);
stdDeviation = StDevAll(GannOsc, length);
}

plot UpperLine1 = regression + deviations * stdDeviation;
#plot MiddleLine = regression;
plot LowerLine1 = regression - deviations * stdDeviation;

UpperLine1.SetDefaultColor(Color.RED);
LowerLine1.SetDefaultColor(Color.GREEN);
UpperLine1.SetLineWeight(1);
LowerLine1.SetLineWeight(1);
UpperLine1.HideBubble();
UpperLine1.HideTitle();
LowerLine1.HideBubble();
LowerLine1.HideTitle();


plot UpperLine2 = regression + deviations2 * stdDeviation;
plot LowerLine2 = regression - deviations2 * stdDeviation;

UpperLine2.SetDefaultColor(Color.RED);
LowerLine2.SetDefaultColor(Color.GREEN);
UpperLine2.SetLineWeight(1);
LowerLine2.SetLineWeight(1);
UpperLine2.HideBubble();
UpperLine2.HideTitle();
LowerLine2.HideBubble();
LowerLine2.HideTitle();

AddCloud(UpperLine2, UpperLine1, Color.RED, Color.CURRENT);
AddCloud(LowerLine1, LowerLine2, Color.GREEN, Color.CURRENT);
# === Oscillator Range Analysis ===

def isOverbought2 = GannOsc >= UpperLine2;
def isOverbought1 = GannOsc >= UpperLine1;
def isOversold2 = GannOsc <= LowerLine2;
def isOversold1 = GannOsc <= LowerLine1;

def oscZone =
if isOverbought2 then 2
else if isOverbought1 then 1
else if isOversold2 then -2
else if isOversold1 then -1
else 0;

def forecastSignal =
if upSignal and oscZone <= -1 then 1 # Strong Buy
else if downSignal and oscZone >= 1 then -1 # Strong Sell
else if upSignal then 2 # Buy Opportunity
else if downSignal then -2 # Sell Opportunity
else 0;

# === AddLabel Displays ===

AddLabel(yes,
if upSignal then "TREND: Bullish (UP)"
else if downSignal then "TREND: Bearish (DOWN)"
else "TREND: Neutral/Sideways",
if upSignal then Color.GREEN
else if downSignal then Color.RED
else Color.GRAY
);

AddLabel(yes,
"GANN STATE: " +
(if lastState == 1 then "Bull Zone"
else if lastState == -1 then "Bear Zone"
else "Neutral"),
if lastState == 1 then CreateColor(0, 255, 0)
else if lastState == -1 then CreateColor(255, 165, 0)
else Color.GRAY
);

AddLabel(yes,
"OSC Range: " +
(if oscZone == 2 then "Overbought (2σ)"
else if oscZone == 1 then "Overbought (1.5σ)"
else if oscZone == -1 then "Oversold (1.5σ)"
else if oscZone == -2 then "Oversold (2σ)"
else "In Range"),
if oscZone >= 1 then Color.RED
else if oscZone <= -1 then Color.GREEN
else Color.LIGHT_GRAY
);

AddLabel(yes,
"FORECAST: " +
(if forecastSignal == 1 then "Strong BUY Zone"
else if forecastSignal == -1 then "Strong SELL Zone"
else if forecastSignal == 2 then "Buying Opportunity"
else if forecastSignal == -2 then "Selling Opportunity"
else "Wait / Watch"),
if forecastSignal == 1 or forecastSignal == 2 then Color.GREEN
else if forecastSignal == -1 or forecastSignal == -2 then Color.RED
else Color.YELLOW
);
 
Hi Antwerks, I always enjoy your scripts. When I set up your scan in #34 above, I am not getting the Bearish Reversal in the SCAN set up box as shown above. My shows " scan" and "true" not "Gann OSC". Can you help?
for scan #34 set up the script in the "thinkscript editor" portion of the scan set up and then chose what extra variables you need
1766537428152.png

https://tos.mx/!L8OEhhtu
 
Last edited:
Here the script with the 2 additional parts by @chewie76 and @antwerks ,thank you.
uFSlaN7.png

Code:
# GHLA_SuperTrend_CCI_High_Low_Activator_Oscillator
#By CANDO13579
#STDEV zones by Chewie76
#Oscillator Range Analysis Labels by Antwerks

declare lower;
declare real_size;
input ghlaPeriod = 10;
input stPeriod = 14;
input stShiftTicks = 20.0;
input useFilter = yes;

def minTick = TickSize();
def shift = stShiftTicks * minTick;

# -----------------------------
# GHLA Logic
# -----------------------------
def ghlaAvgH = Average(high, ghlaPeriod);
def ghlaAvgL = Average(low, ghlaPeriod);

def sw = if close > ghlaAvgH then 1
else if close < ghlaAvgL then -1
else 0;

def ghlaDir = CompoundValue(1, if sw != 0 then sw else ghlaDir[1], 0);
def ghlaLevel = if ghlaDir < 0 then ghlaAvgH else ghlaAvgL;

# -----------------------------
# Custom CCI using Typical Price
# -----------------------------
def tp = (high + low + close) / 3;
def tpSMA = Average(tp, stPeriod);
def meanDev = Average(AbsValue(tp - tpSMA), stPeriod);
def cciValue = if meanDev == 0 then 0 else (tp - tpSMA) / (0.015 * meanDev);

# -----------------------------
# SuperTrend State Machine
# -----------------------------

# stFlag: 1=long, -1=short
rec stFlag = if IsNaN(stFlag[1]) then if cciValue >= 0 then 1 else -1
else if cciValue > 0 and stFlag[1] <= 0 then 1
else if cciValue < 0 and stFlag[1] >= 0 then -1
else stFlag[1];

rec st = if IsNaN(st[1]) then if cciValue >= 0 then low - shift else high + shift
else
if cciValue > 0 and stFlag[1] <= 0 then low - shift
else if cciValue < 0 and stFlag[1] >= 0 then high + shift
else
if stFlag > 0 and (low - shift) > st[1] then low - shift
else if stFlag < 0 and (high + shift) < st[1] then high + shift
else
if useFilter and stFlag > 0 and st[1] < st[1] and (close < open or high < high[1]) then st[1]
else if useFilter and stFlag < 0 and st[1] > st[1] and (close > open or low > low[1]) then st[1]
else st[1];

# -----------------------------
# signals
# -----------------------------
def notNaST = !IsNaN(st);
def upSignal = notNaST and close > st and close > ghlaLevel;
def downSignal = notNaST and close < st and close < ghlaLevel;
def neutralSignal = notNaST and !upSignal and !downSignal;

# -----------------------------
# Output (centered)
# -----------------------------
def signalCenter = 0;
plot Sig = signalCenter;
Sig.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Sig.SetLineWeight(4);

Sig.AssignValueColor(
if upSignal then Color.GREEN
else if downSignal then Color.RED
else Color.LIGHT_GRAY
);

# Boundary reference lines
# -----------------------------
plot UpperLine = 1;
UpperLine.SetDefaultColor(Color.DARK_GRAY);
UpperLine.SetStyle(Curve.SHORT_DASH);

plot LowerLine = -1;
LowerLine.SetDefaultColor(Color.DARK_GRAY);
LowerLine.SetStyle(Curve.SHORT_DASH);

# Gann High/Low Activator Oscillator

input activatorPeriod = 14;

# Calculate simple moving averages
def avgHigh = Average(high, activatorPeriod);
def avgLow = Average(low, activatorPeriod);

# Determine state
def bullState = close > avgHigh;
def bearState = close < avgLow;

# Track the last valid state
def lastState = CompoundValue(1,
if bullState then 1
else if bearState then -1
else lastState[1],
0);

def avgRange = avgHigh - avgLow;
def oscillator =
if avgRange != 0 then
if lastState == 1 then (close - avgLow) / avgRange
else if lastState == -1 then (close - avgHigh) / avgRange
else 0.5
else 0.5;

# Plot oscillator
plot GannOsc = oscillator;
GannOsc.SetLineWeight(2);
GannOsc.AssignValueColor(
if lastState == 1 then CreateColor(0, 255, 0) # Green
else if lastState == -1 then CreateColor(255, 165, 0) # Orange
else Color.GRAY
);

# Plot midline
plot MidLine = 0.5;
MidLine.SetDefaultColor(Color.LIGHT_GRAY);
MidLine.SetStyle(Curve.SHORT_DASH);
# 2 Standard Deviation Full

input deviations = 2.0;
input deviations2 = 2.5;
input fullRange = yes;
input length = 300;

def regression;
def stdDeviation;
if (fullRange) {
regression = InertiaAll(GannOsc);
stdDeviation = StDevAll(GannOsc);
} else {
regression = InertiaAll(GannOsc, length);
stdDeviation = StDevAll(GannOsc, length);
}

plot UpperLine1 = regression + deviations * stdDeviation;
#plot MiddleLine = regression;
plot LowerLine1 = regression - deviations * stdDeviation;

UpperLine1.SetDefaultColor(Color.RED);
LowerLine1.SetDefaultColor(Color.GREEN);
UpperLine1.SetLineWeight(1);
LowerLine1.SetLineWeight(1);
UpperLine1.HideBubble();
UpperLine1.HideTitle();
LowerLine1.HideBubble();
LowerLine1.HideTitle();


plot UpperLine2 = regression + deviations2 * stdDeviation;
plot LowerLine2 = regression - deviations2 * stdDeviation;

UpperLine2.SetDefaultColor(Color.RED);
LowerLine2.SetDefaultColor(Color.GREEN);
UpperLine2.SetLineWeight(1);
LowerLine2.SetLineWeight(1);
UpperLine2.HideBubble();
UpperLine2.HideTitle();
LowerLine2.HideBubble();
LowerLine2.HideTitle();

AddCloud(UpperLine2, UpperLine1, Color.RED, Color.CURRENT);
AddCloud(LowerLine1, LowerLine2, Color.GREEN, Color.CURRENT);
# === Oscillator Range Analysis ===

def isOverbought2 = GannOsc >= UpperLine2;
def isOverbought1 = GannOsc >= UpperLine1;
def isOversold2 = GannOsc <= LowerLine2;
def isOversold1 = GannOsc <= LowerLine1;

def oscZone =
if isOverbought2 then 2
else if isOverbought1 then 1
else if isOversold2 then -2
else if isOversold1 then -1
else 0;

def forecastSignal =
if upSignal and oscZone <= -1 then 1 # Strong Buy
else if downSignal and oscZone >= 1 then -1 # Strong Sell
else if upSignal then 2 # Buy Opportunity
else if downSignal then -2 # Sell Opportunity
else 0;

# === AddLabel Displays ===

AddLabel(yes,
if upSignal then "TREND: Bullish (UP)"
else if downSignal then "TREND: Bearish (DOWN)"
else "TREND: Neutral/Sideways",
if upSignal then Color.GREEN
else if downSignal then Color.RED
else Color.GRAY
);

AddLabel(yes,
"GANN STATE: " +
(if lastState == 1 then "Bull Zone"
else if lastState == -1 then "Bear Zone"
else "Neutral"),
if lastState == 1 then CreateColor(0, 255, 0)
else if lastState == -1 then CreateColor(255, 165, 0)
else Color.GRAY
);

AddLabel(yes,
"OSC Range: " +
(if oscZone == 2 then "Overbought (2σ)"
else if oscZone == 1 then "Overbought (1.5σ)"
else if oscZone == -1 then "Oversold (1.5σ)"
else if oscZone == -2 then "Oversold (2σ)"
else "In Range"),
if oscZone >= 1 then Color.RED
else if oscZone <= -1 then Color.GREEN
else Color.LIGHT_GRAY
);

AddLabel(yes,
"FORECAST: " +
(if forecastSignal == 1 then "Strong BUY Zone"
else if forecastSignal == -1 then "Strong SELL Zone"
else if forecastSignal == 2 then "Buying Opportunity"
else if forecastSignal == -2 then "Selling Opportunity"
else "Wait / Watch"),
if forecastSignal == 1 or forecastSignal == 2 then Color.GREEN
else if forecastSignal == -1 or forecastSignal == -2 then Color.RED
else Color.YELLOW
);

Here:
@chewie76 here is a potentially bullish scan that finds stocks that are bouncing off the lower red band - pretty cool - https://tos.mx/!T9fXcQYn
1766541080540.png
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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