Indicator · Pine Script v5 · On the chart

Market Structure Trend Targets [ChartPrime]

by theonetrade· code credited to ChartPrime

Market Structure Trend Targets [ChartPrime] identifies market structure shifts using pivot highs and pivot lows calculated over a user-defined lookback length.

View on GitHub

GitHub stars34on the repository
Pine versionv5runs as is
Lines of code148
Settings5inputs
LicenceMIT
Last update2 weeks ago8 Nov 2026

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

148 lines of Pine Script v5 from assets/indicator/bit-exact/market_structure_trend_targets_chartprime_F3b5dGLx.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

market_structure_trend_targets_chartprime_F3b5dGLx.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ChartPrime

//@version=5
indicator("Market Structure Trend Targets [ChartPrime]", "Market Structure TT [ChartPrime]",
          overlay = true, max_lines_count = 500, max_labels_count = 500)

// --------------------------------------------------------------------------------------------------------------------}
// 𝙐𝙎𝙀𝙍 𝙄𝙉𝙋𝙐𝙏𝙎
// --------------------------------------------------------------------------------------------------------------------{
int   length       = input.int(10, "Length", maxval = 30, minval = 2)
bool  display_per  = input.bool(false, "Display %")
bool  display_stop = input.bool(true, "Display Trailing Stop")
color col_up       = input.color(#54b6d4, "+", inline = "col")
color col_dn       = input.color(#cf3737, "-", inline = "col")


// --------------------------------------------------------------------------------------------------------------------}
// 𝙄𝙉𝘿𝙄𝘾𝘼𝙏𝙊𝙍 𝘾𝘼𝙇𝘾𝙐𝙇𝘼𝙏𝙄𝙊𝙉𝙎
// --------------------------------------------------------------------------------------------------------------------{
series float ph = ta.pivothigh(length, length)
series float pl = ta.pivotlow(length, length)


market_structure()=>
    var upper       = float(na)
    var lower       = float(na)
    var upper_count = 0
    var lower_count = 0
    var upper_index = 0
    var lower_index = 0
    var trend       = bool(na)

    float source     = ta.sma(high - low, 50)*2
    float source1    = ta.sma(ta.median(close, 40), 10)
    float volatility = math.avg(ta.highest(source, 200), ta.lowest(source, 200))
    float trend_line = float(na)

    var line_h = line(na)
    var line_l = line(na)

    if not na(ph) 
        upper_index := bar_index[length]
        upper  := high[length]
        line_h := line.new(upper_index, upper, upper_index, upper, color = col_up)

    if not na(pl) 
        lower_index := bar_index[length]        
        lower  := low[length]
        line_l := line.new(lower_index, lower, lower_index, lower, color = col_dn)

    if ta.crossover(high, upper)
        upper_count += 1

        var price_enter = 0.
        int index       = bar_index - (bar_index - upper_index)/2
        
        if upper_count == 1
            price_enter := line_h.get_y1()

        float  percent = (line_h.get_y1() - price_enter) / price_enter * 100
        string txt     = upper_count == 1 
                          ? str.tostring(price_enter, "#.# △") 
                          : (display_per ? str.tostring(percent, format.percent) : str.tostring(upper_count-1))

        label.new(index, line_h.get_y1(), txt, textcolor = chart.fg_color, color = color(na))

        line_h.set_x2(bar_index)

        line_h      := na
        upper       := na
        lower_count := 0
        trend       := true

    if ta.crossunder(low, lower)
        lower_count += 1

        var price_enter = 0.
        int index       = bar_index - (bar_index - lower_index)/2

        if lower_count == 1
            price_enter := line_l.get_y1()

        float  percent = (line_l.get_y1() - price_enter) / price_enter * 100
        string txt     = lower_count == 1 
                          ? str.tostring(price_enter, "#.# ▽") 
                          : (display_per ? str.tostring(percent, format.percent) : str.tostring(lower_count-1))

        label.new(index, line_l.get_y1(), txt, textcolor = chart.fg_color, color = color(na), style = label.style_label_up)

        line_l.set_x2(bar_index)

        line_l      := na
        lower       := na
        upper_count := 0
        trend       := false

    if trend
        trend_line := source1 - volatility
    else 
        trend_line := source1 + volatility

    color color = trend ? col_up : col_dn

    if trend and ta.crossover(low, trend_line) and display_stop and not (trend and not trend[1])
        label.new(bar_index[1], low[1], text = "◈", style = label.style_label_up, textcolor = color, color = color(na))

    if not trend and ta.crossunder(high, trend_line) and display_stop and not (trend[1] and not trend)
        label.new(bar_index[1], high[1], text = "◈", style = label.style_label_down, textcolor = color, color = color(na))

    if trend and not trend[1]
        label.new(bar_index, trend_line, text = "△", textcolor = color, style = label.style_label_center, color = color(na), size = size.large)

    if not trend and trend[1]
        label.new(bar_index, trend_line, text = "▽", textcolor = color, style = label.style_label_center, color = color(na), size = size.large)

    [color, trend_line, trend]

[color, trend_line, trend] = market_structure()


float stop_loss = trend_line,
      stop_loss := ta.change(trend) 
             ? na 
             :
             high > stop_loss and not trend 
             ? na 
             : low < stop_loss and trend 
             ? na
             : stop_loss


// --------------------------------------------------------------------------------------------------------------------}
// 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉
// --------------------------------------------------------------------------------------------------------------------{
display = display_stop ? display.all : display.none
color1  = display_stop ? 90 : 100

sl = plot(stop_loss, style = plot.style_linebr, color = color, display = display),
     plot(stop_loss, style = plot.style_linebr, color = color.new(color, 80), linewidth = 5, display = display)
pp = plot(hl2, color = color(na))

fill(sl, pp, stop_loss, hl2, color.new(color, color1), na)

candle_col = color.new(color, 50)
plotcandle(open, high, low, close, title='Candles Trend', color = candle_col, wickcolor=candle_col, bordercolor = candle_col)

// --------------------------------------------------------------------------------------------------------------------}

Overview

The indicator detects price breakouts above historical pivot highs or below pivot lows, drawing dynamic structure lines and target labels directly on the price overlay. Users can adjust the pivot length, display target progression as raw count or percentage changes, toggle a trailing stop visual, and select colors for bullish and bearish trends.

Details

Published by

theonetrade on GitHub

Code credit

ChartPrime, as written in the script's header

Draws

On the price chart

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

algorithmic-tradingbacktestingbitcoinccxtcrypto-tradingllm-agentoffline-backtestingopen-source-tradingpine-scriptpine-to-pythonpinescriptpynecorepythonquantquantitative-financestrategy-backtestingtechnical-analysistrading-indicatorstrading-strategiestradingview

Settings

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

SettingTypeDefaultVariable
Lengthint10length
Display %boolfalsedisplay_per
Display Trailing Stopbooltruedisplay_stop
+color#54b6d4col_up
-color#cf3737col_dn
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

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
228 stars

This indicator plots market structure on the price chart by identifying swing highs and lows. It connects these points with lines to visualize trend direction and detect Break of Structure (BOS) when price breaks a prior swing in the current trend. Change of C

Indicator v6 TrendSupport & resistance
Updated 4 months ago · None stated GitHub repository of Market Structure - BOS & CHoCH

Open Source Fractal

by cantolab
183 stars

This indicator implements the Fractal Model concept to plot market structure shifts, liquidity sweeps, and Change in State of Delivery (CISD) levels. It overlays custom higher-timeframe candle previews, open prices, and time markers directly on the price chart

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 3 months ago · MIT GitHub repository of Open Source Fractal
56 stars

BigBeluga Smart Money Concepts identifies and overlays market structure and volumetric order blocks directly on the price chart. It analyzes market swings, trend sweeps, and mapping structure using customizable pivot lengths and calculation modes. The indicato

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 3 months ago · None stated GitHub repository of BigBeluga - Smart Money Concepts
53 stars

Apex Trend & Liquidity Master calculates a trend cloud using customizable moving averages expanded by a volatility multiplier directly on the main price chart. It identifies dynamic support and resistance liquidity zones from swing high and low pivots, extendi

Indicator v6 TrendSupport & resistance
Updated 3 months ago · MPL-2.0 GitHub repository of Apex Trend & Liquidity Master

More SMC / ICT scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers