ICT Structure Mapping (Combined 3-Candle STL & STH)

ccomedy020 · study · 195 行 · 点赞 186 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Combined 3-Candle STL & STH [Non-Inside Filter]", overlay=true, max_labels_count=500)
 
// Inputs
show_stl      = input.bool(true, title="Show STL & P1/P2 (Down)")
show_sth      = input.bool(true, title="Show STH & P1/P2 (Up)")
breakout_mode = input.string("Either", title="Breakout Trigger", options=["Close Break", "High/Low Sweep", "Either"])
 
stl_p1_col    = input.color(color.blue, title="STL P1 Color")
stl_p2_col    = input.color(color.orange, title="STL P2 Color")
stl_col       = input.color(color.green, title="STL Label Color")
 
sth_p1_col    = input.color(color.blue, title="STH P1 Color")
sth_p2_col    = input.color(color.orange, title="STH P2 Color")
sth_col       = input.color(color.red, title="STH Label Color")
 
// ITH & ITL Colors
ith_col       = input.color(color.purple, title="ITH Label Color")
itl_col       = input.color(color.aqua, title="ITL Label Color")
 
// 1. Mother Bar Filter
var float mother_high = na
var float mother_low  = na
 
if bar_index == 0
    mother_high := high
    mother_low  := low
 
bool is_inside = (bar_index > 0) and (high <= mother_high and low >= mother_low)
 
if is_inside
    mother_high := math.max(mother_high, high)
    mother_low  := math.min(mother_low, low)
else
    mother_high := high
    mother_low  := low
 
bool is_valid_candle = not is_inside
 
// -------------------------------------------------------------
// 2. STL (Swing Trade Low) State Tracking
// -------------------------------------------------------------
var int   stl_p1_bar  = na
var float stl_p1_low  = na
var float stl_p1_high = na
 
var int   stl_p2_bar  = na
var float stl_p2_low  = na
var float stl_p2_high = na
 
var label lbl_stl_p1 = na
var label lbl_stl_p2 = na
 
// ITL Tracking
var label stl_prev_label = na
var label stl_last_label = na
 
var float stl_prev_low = na
var float stl_last_low = na
 
// -------------------------------------------------------------
// 3. STH (Swing Trade High) State Tracking
// -------------------------------------------------------------
var int   sth_p1_bar  = na
var float sth_p1_high = na
var float sth_p1_low  = na
 
var int   sth_p2_bar  = na
var float sth_p2_high = na
var float sth_p2_low  = na
 
var label lbl_sth_p1 = na
var label lbl_sth_p2 = na
 
// ITH Tracking
var label sth_prev_label = na
var label sth_last_label = na
 
var float sth_prev_high = na
var float sth_last_high = na
 
// -------------------------------------------------------------
// 4. Process Every Valid Candle
// -------------------------------------------------------------
if is_valid_candle
 
    // --- STL Evaluation (Downside) ---
    if show_stl
        if not na(stl_p1_bar) and not na(stl_p2_bar)
            bool stl_high_crossed = false
 
            if breakout_mode == "Close Break"
                stl_high_crossed := close > stl_p2_high
            else if breakout_mode == "High/Low Sweep"
                stl_high_crossed := high > stl_p2_high
            else
                stl_high_crossed := (high > stl_p2_high) or (close > stl_p2_high)
 
            bool is_three_candle_stl = (stl_p2_low < stl_p1_low) and (low > stl_p2_low) and stl_high_crossed
 
            if is_three_candle_stl
                // Create STL Label
                label new_stl = label.new(stl_p2_bar, stl_p2_low, text="STL", style=label.style_label_up, color=color.new(color.black, 100), textcolor=stl_col, size=size.small)
 
                // Inverse Head & Shoulders Detection - ITL
                if not na(stl_prev_label) and not na(stl_last_label)
                    bool is_inverse_head_shoulders = (stl_last_low < stl_prev_low) and (stl_last_low < stl_p2_low)
 
                    if is_inverse_head_shoulders
                        label.set_text(stl_last_label, "ITL")
                        label.set_textcolor(stl_last_label, itl_col)
                        label.set_size(stl_last_label, size.normal)
 
                // Roll STL History
                stl_prev_label := stl_last_label
                stl_prev_low   := stl_last_low
 
                stl_last_label := new_stl
                stl_last_low   := stl_p2_low
 
        // Roll STL Pointers
        stl_p1_bar  := stl_p2_bar
        stl_p1_low  := stl_p2_low
        stl_p1_high := stl_p2_high
 
        stl_p2_bar  := bar_index
        stl_p2_low  := low
        stl_p2_high := high
 
        if not na(lbl_stl_p1)
            label.delete(lbl_stl_p1)
 
        if not na(stl_p1_bar)
            lbl_stl_p1 := label.new(stl_p1_bar, stl_p1_low, text="P1", style=label.style_label_up, color=color.new(color.black, 100), textcolor=stl_p1_col, size=size.small)
 
        if not na(lbl_stl_p2)
            label.delete(lbl_stl_p2)
 
        if not na(stl_p2_bar)
            lbl_stl_p2 := label.new(stl_p2_bar, stl_p2_low, text="P2", style=label.style_label_up, color=color.new(color.black, 100), textcolor=stl_p2_col, size=size.small)
 
    // --- STH Evaluation (Upside) ---
    if show_sth
        if not na(sth_p1_bar) and not na(sth_p2_bar)
            bool sth_low_crossed = false
 
            if breakout_mode == "Close Break"
                sth_low_crossed := close < sth_p2_low
            else if breakout_mode == "High/Low Sweep"
                sth_low_crossed := low < sth_p2_low
            else
                sth_low_crossed := (low < sth_p2_low) or (close < sth_p2_low)
 
            bool is_three_candle_sth = (sth_p2_high > sth_p1_high) and (high < sth_p2_high) and sth_low_crossed
 
            if is_three_candle_sth
                // Create STH Label
                label new_sth = label.new(sth_p2_bar, sth_p2_high, text="STH", style=label.style_label_down, color=color.new(color.black, 100), textcolor=sth_col, size=size.small)
 
                // Head & Shoulders Detection - ITH
                if not na(sth_prev_label) and not na(sth_last_label)
                    bool is_head_shoulders = (sth_last_high > sth_prev_high) and (sth_last_high > sth_p2_high)
 
                    if is_head_shoulders
                        label.set_text(sth_last_label, "ITH")
                        label.set_textcolor(sth_last_label, ith_col)
                        label.set_size(sth_last_label, size.normal)
 
                // Roll STH History
                sth_prev_label := sth_last_label
                sth_prev_high  := sth_last_high
 
                sth_last_label := new_sth
                sth_last_high  := sth_p2_high
 
        // Roll STH Pointers
        sth_p1_bar  := sth_p2_bar
        sth_p1_high := sth_p2_high
        sth_p1_low  := sth_p2_low
 
        sth_p2_bar  := bar_index
        sth_p2_high := high
        sth_p2_low  := low
 
        if not na(lbl_sth_p1)
            label.delete(lbl_sth_p1)
 
        if not na(sth_p1_bar)
            lbl_sth_p1 := label.new(sth_p1_bar, sth_p1_high, text="P1", style=label.style_label_down, color=color.new(color.black, 100), textcolor=sth_p1_col, size=size.small)
 
        if not na(lbl_sth_p2)
            label.delete(lbl_sth_p2)
 
        if not na(sth_p2_bar)
            lbl_sth_p2 := label.new(sth_p2_bar, sth_p2_high, text="P2", style=label.style_label_down, color=color.new(color.black, 100), textcolor=sth_p2_col, size=size.small)

← 返回列表