Indicator · Pine Script v5 · On the chart

Snag - Detect Bull Bear

by dearvn· code credited to binancash

Snag - Detect Bull Bear is an indicator that identifies bull and bear market phases on the price chart.

View on GitHub

GitHub stars4on the repository
Pine versionv5runs as is
Lines of code112
Settings3inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago25 Jul 2022

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

112 lines of Pine Script v5 from bullbear.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

bullbear.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © binancash

//@version=5
indicator("Snag - Detect Bull Bear", "Snag - Bull Bear", overlay=true, max_lines_count=500, max_bars_back=4900, max_boxes_count=500)

zigzag_len = input.int(9, "ZigZag Length")
show_zigzag = input.bool(true, "Show Zigzag")
fib_factor = input.float(0.33, "Fib Factor for breakout confirmation", 0, 1, 0.01)

var float[] high_points_arr = array.new_float(5)
var int[] high_index_arr = array.new_int(5)
var float[] low_points_arr = array.new_float(5)
var int[] low_index_arr = array.new_int(5)
var box[] bu_ob_boxes = array.new_box(5)
var box[] be_ob_boxes = array.new_box(5)

to_up = high >= ta.highest(zigzag_len)
to_down = low <= ta.lowest(zigzag_len)

trend = 1
trend := nz(trend[1], 1)
trend := trend == 1 and to_down ? -1 : trend == -1 and to_up ? 1 : trend

last_trend_up_since = ta.barssince(to_up[1])
low_val = ta.lowest(nz(last_trend_up_since > 0 ? last_trend_up_since : 1, 1))
low_index = bar_index - ta.barssince(low_val == low)

last_trend_down_since = ta.barssince(to_down[1])
high_val = ta.highest(nz(last_trend_down_since > 0 ? last_trend_down_since : 1, 1))
high_index = bar_index - ta.barssince(high_val == high)

if ta.change(trend) != 0
    if trend == 1
        array.push(low_points_arr, low_val)
        array.push(low_index_arr, low_index)
    if trend == -1
        array.push(high_points_arr, high_val)
        array.push(high_index_arr, high_index)


f_get_high(ind) =>
    [array.get(high_points_arr, array.size(high_points_arr) - 1 - ind), array.get(high_index_arr, array.size(high_index_arr) - 1 - ind)]


f_get_low(ind) =>
    [array.get(low_points_arr, array.size(low_points_arr) - 1 - ind), array.get(low_index_arr, array.size(low_index_arr) - 1 - ind)]


[h0, h0i] = f_get_high(0)
[h1, h1i] = f_get_high(1)

[l0, l0i] = f_get_low(0)
[l1, l1i] = f_get_low(1)

if ta.change(trend) != 0 and show_zigzag
    if trend == 1
        line.new(h0i, h0, l0i, l0)
    if trend == -1
        line.new(l0i, l0, h0i, h0)

market = 1
market := nz(market[1], 1)
last_l0 = ta.valuewhen(ta.change(market) != 0, l0, 0)
last_h0 = ta.valuewhen(ta.change(market) != 0, h0, 0)
market := last_l0 == l0 or last_h0 == h0 ? market : market == 1 and l0 < l1 and l0 < l1 - math.abs(h0 - l1) * fib_factor ? -1 : market == -1 and h0 > h1 and h0 > h1 + math.abs(h1 - l0) * fib_factor ? 1 : market

bu_ob_index = bar_index
bu_ob_index := nz(bu_ob_index[1], bar_index)
for i=h1i to l0i[zigzag_len]
    index = bar_index - i 
    if open[index] > close[index]
        bu_ob_index := bar_index[index]

bu_ob_since = bar_index - bu_ob_index

be_ob_index = bar_index
be_ob_index := nz(be_ob_index[1], bar_index)
for i=l1i to h0i[zigzag_len]
    index = bar_index - i 
    if open[index] < close[index]
        be_ob_index := bar_index[index]

be_ob_since = bar_index - be_ob_index

if ta.change(market) != 0
    if market == 1
        line.new(h1i, h1, h0i, h1, color=color.green, width=1)
        bull = box.new(bu_ob_index, high[bu_ob_since], bar_index, low[bu_ob_since], bgcolor=color.new(color.black, 70), border_color=color.green, text="BULL", text_color=color.green, text_halign=text.align_right, text_size=size.normal)
        array.push(bu_ob_boxes, bull)
    if market == -1
        line.new(l1i, l1, l0i, l1, color=color.red, width=1)
        bear = box.new(be_ob_index, high[be_ob_since], bar_index, low[be_ob_since], bgcolor=color.new(color.black, 70), border_color=color.red, text="BEAR", text_color=color.red, text_halign=text.align_right, text_size=size.normal)
        array.push(be_ob_boxes, bear)

for bull_ob in bu_ob_boxes
    bottom = box.get_bottom(bull_ob)
    if close < bottom
        box.delete(bull_ob)
    else if array.size(bu_ob_boxes) == 5
        box.delete(array.shift(bu_ob_boxes))
    else
        box.set_right(bull_ob, bar_index)
    
for bear_ob in be_ob_boxes
    top = box.get_top(bear_ob)
    if close > top
        box.delete(bear_ob)
    else if array.size(be_ob_boxes) == 5
        box.delete(array.shift(be_ob_boxes))
    else
        box.set_right(bear_ob, bar_index)

Overview

Snag - Detect Bull Bear uses a customizable ZigZag to detect trend reversals, tracking high and low points. The script applies Fibonacci-based rules to confirm breakout shifts between bull and bear regimes. When the market state changes, it draws connecting trend lines between pivot points and fills rectangular boxes with bullish or bearish labels highlighting order block zones. Primary settings control ZigZag length, show/hide the zigzag lines, and the Fibonacci factor for confirmation. It operates directly on the price chart without separate panes.

Details

Published by

dearvn on GitHub

Code credit

binancash, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Trend, Support & resistance, SMC / ICT / price action, Breakout

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

futurestrading-strategiestradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
ZigZag Lengthint9zigzag_len
Show Zigzagbooltrueshow_zigzag
Fib Factor for breakout confirmationfloat0.33fib_factor
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

24 stars

This indicator maps Smart Money Concepts (SMC) market structure directly onto the price chart. It detects internal and swing structure breaks (BOS and CHoCH), tracks bullish and bearish order blocks, identifies fair value gaps (FVG), highlights equal highs and

Indicator v5 SMC / ICT / price actionSupport & resistance Repaints
Updated 10 days ago · Custom licence GitHub repository of Smart Money Concepts [LuxAlgo] Test B

714AutoM8tr

by alboogycOdR
6 stars

714AutoM8tr is a Smart Money Concepts indicator that plots internal and swing market structures directly on the price chart. It identifies Break of Structure and Change of Character points alongside internal and swing order blocks. Users can customize order bl

Indicator v5 SMC / ICT / price actionSupport & resistance Repaints
Updated 7 months ago · None stated GitHub repository of 714AutoM8tr
2 stars

This indicator detects market structure breaks using a configurable ZigZag length to identify swing highs and lows. It marks bullish and bearish order blocks along with breaker blocks on the price chart. Breakouts are confirmed via a Fibonacci retracement fact

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 2 months ago · None stated GitHub repository of Market Structure Break & Order Block

Week 02

by moonyroy
1 stars

This indicator plots ZigZag lines on the price chart to map swing highs and lows. It detects break_high and break_low events to identify pivot points. The code supports a primary chart timeframe and optional second timeframe ZigZag with selectable modes such a

Indicator v6 TrendSupport & resistance
Updated 4 days ago · None stated GitHub repository of Week 02
575 stars

This indicator detects equal highs and lows by comparing prices within user-defined left and right bars using a tolerance percentage. It plots horizontal lines on the price chart for equal highs in one color and lows in another. A screener allows input of mult

Indicator v5 Support & resistanceTrend
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows with Screener

This Pine Script indicator plots a ZigZag on the price chart. It identifies reversal points when price moves more than a set percentage from the last pivot. The script calculates forecasts using zigzag data and labels detected geometric patterns including harm

Indicator v4 TrendSupport & resistance

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers