Market Structure Shift w/Multi Time Frame Tables

RAYHANARAFATCAPITAL · study · 306 行 · 点赞 11 · TradingView 原页

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

Pine Script

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Orion Trades
 
//@version=6
indicator("Market Structure Shift (MSS) w/Multi Time Frame Tables Orion Trades", shorttitle="Market Structure Shift (MSS) [Orion Trades", overlay = true, max_lines_count = 500, max_labels_count = 500)
 
// Hidden plot to anchor the script to the main price scale (Scale A)
plot(close, "Price Scale Anchor", color = color.new(color.white, 100), editable = false)
 
// ==========================================
// === Inputs
// ==========================================
grp_mss = "MSS Settings"
left  = input.int(2, "Swing Left Bars", minval = 1, group = grp_mss)
right = input.int(2, "Swing Right Bars", minval = 1, group = grp_mss)
confirmWith = input.string("Close", "Break Confirmation", options = ["Close", "Wick"], group = grp_mss)
 
grp_visuals = "Visual Settings (Current TF)"
extendRight = input.bool(false, "Extend lines to the right", group = grp_visuals)
showOnlyLatest = input.bool(false, "Keep only most-recent MSS line per side", group = grp_visuals)
lineLenForNoExtend = input.int(50, "Line bars (when not extended)", minval = 5, group = grp_visuals)
bullClr = input.color(color.new(#40ca5b, 0), "Bullish Color", group = grp_visuals)
bearClr = input.color(color.new(color.red, 0), "Bearish Color", group = grp_visuals)
 
grp_mtf = "Multi-Timeframe Table"
showTable = input.bool(true, "Show MTF Table", group = grp_mtf)
tablePos  = input.string("top_right", "Table Position", options = ["top_right", "bottom_right", "top_left", "bottom_left", "middle_right"], group = grp_mtf)
tableSize = input.string("small", "Table Text Size", options = ["tiny", "small", "normal", "large"], group = grp_mtf)
 
// Table Colors
headerBgClr  = input.color(color.new(#020f02, 0), "Header Background", group = grp_mtf)
headerTxtClr = input.color(color.white, "Header Text", group = grp_mtf)
cellBgClr    = input.color(color.new(color.white, 0), "Cell Background", group = grp_mtf)
cellTxtClr   = input.color(color.black, "Cell Text", group = grp_mtf)
borderClr    = input.color(color.new(color.black, 0), "Border Color", group = grp_mtf)
 
tf1_active = input.bool(true, "", inline = "tf1", group = grp_mtf)
tf1_val    = input.timeframe("1", "TF 1", inline = "tf1", group = grp_mtf)
 
tf2_active = input.bool(true, "", inline = "tf2", group = grp_mtf)
tf2_val    = input.timeframe("2", "TF 2", inline = "tf2", group = grp_mtf)
 
tf3_active = input.bool(true, "", inline = "tf3", group = grp_mtf)
tf3_val    = input.timeframe("3", "TF 3", inline = "tf3", group = grp_mtf)
 
tf4_active = input.bool(true, "", inline = "tf4", group = grp_mtf)
tf4_val    = input.timeframe("5", "TF 4", inline = "tf4", group = grp_mtf)
 
tf5_active = input.bool(true, "", inline = "tf5", group = grp_mtf)
tf5_val    = input.timeframe("8", "TF 5", inline = "tf5", group = grp_mtf)
 
// Higher Timeframe support (Generic Timeframes)
tf6_active = input.bool(true, "", inline = "tf6", group = grp_mtf)
tf6_val    = input.timeframe("15", "TF 6", inline = "tf6", group = grp_mtf)
 
tf7_active = input.bool(true, "", inline = "tf7", group = grp_mtf)
tf7_val    = input.timeframe("30", "TF 7", inline = "tf7", group = grp_mtf)
 
tf8_active = input.bool(true, "", inline = "tf8", group = grp_mtf)
tf8_val    = input.timeframe("60", "TF 8", inline = "tf8", group = grp_mtf)
 
tf9_active = input.bool(false, "", inline = "tf9", group = grp_mtf)
tf9_val    = input.timeframe("W", "TF 9", inline = "tf9", group = grp_mtf)
 
tf10_active = input.bool(false, "", inline = "tf10", group = grp_mtf)
tf10_val    = input.timeframe("M", "TF 10", inline = "tf10", group = grp_mtf)
 
grp_alerts = "Alert Settings"
alert_cur  = input.bool(true, "Current TF MSS", group = grp_alerts)
alert_tf1  = input.bool(false, "TF 1 MSS", group = grp_alerts)
alert_tf2  = input.bool(false, "TF 2 MSS", group = grp_alerts)
alert_tf3  = input.bool(false, "TF 3 MSS", group = grp_alerts)
alert_tf4  = input.bool(false, "TF 4 MSS", group = grp_alerts)
alert_tf5  = input.bool(false, "TF 5 MSS", group = grp_alerts)
alert_tf6  = input.bool(false, "TF 6 MSS", group = grp_alerts)
alert_tf7  = input.bool(false, "TF 7 MSS", group = grp_alerts)
alert_tf8  = input.bool(false, "TF 8 MSS", group = grp_alerts)
alert_tf9  = input.bool(false, "TF 9 MSS", group = grp_alerts)
alert_tf10 = input.bool(false, "TF 10 MSS", group = grp_alerts)
 
// ==========================================
// === MSS Logic Function
// ==========================================
f_get_mss_status(_left, _right, _confirmWith) =>
    var float lastHighVal = na
    var float lastLowVal  = na
    var int   lastHighBar = na
    var int   lastLowBar  = na
    var int   lastDir     = 0  // 0: None, 1: Bullish, -1: Bearish
 
    ph = ta.pivothigh(high, _left, _right)
    pl = ta.pivotlow(low, _left, _right)
 
    if not na(ph)
        lastHighVal := ph
        lastHighBar := bar_index - _right
 
    if not na(pl)
        lastLowVal := pl
        lastLowBar := bar_index - _right
 
    breakUp   = (_confirmWith == "Close" ? close : high) > nz(lastHighVal)
    breakDown = (_confirmWith == "Close" ? close : low)  < nz(lastLowVal)
 
    validUp   = breakUp   and not na(lastHighBar) and bar_index > lastHighBar
    validDown = breakDown and not na(lastLowBar)  and bar_index > lastLowBar
 
    newMSS = (validUp and lastDir != 1) or (validDown and lastDir != -1)
    if validUp and lastDir != 1
        lastDir := 1
    else if validDown and lastDir != -1
        lastDir := -1
 
    [lastDir, lastHighVal, lastLowVal, lastHighBar, lastLowBar, validUp, validDown, newMSS]
 
// ==========================================
// === Current Timeframe Calculation & Visuals
// ==========================================
[curDir, curHighVal, curLowVal, curHighBar, curLowBar, curValidUp, curValidDown, curNewMSS] = f_get_mss_status(left, right, confirmWith)
 
// Visuals for Current TF
var line  lastBullLine  = na
var line  lastBearLine  = na
var label lastBullLabel = na
var label lastBearLabel = na
 
if curValidUp and curDir == 1 and curDir[1] != 1
    if showOnlyLatest
        line.delete(lastBullLine)
        label.delete(lastBullLabel)
    int x1 = curHighBar
    int x2 = extendRight ? bar_index + lineLenForNoExtend : bar_index
    lastBullLine  := line.new(x1, curHighVal, x2, curHighVal, color = bullClr, style = line.style_dashed)
    lastBullLabel := label.new((x1 + x2) / 2, curHighVal, "MSS", style = label.style_label_down, textcolor = bullClr, size = size.small, color = color.new(color.white, 100))
 
if curValidDown and curDir == -1 and curDir[1] != -1
    if showOnlyLatest
        line.delete(lastBearLine)
        label.delete(lastBearLabel)
    int x1 = curLowBar
    int x2 = extendRight ? bar_index + lineLenForNoExtend : bar_index
    lastBearLine  := line.new(x1, curLowVal, x2, curLowVal, color = bearClr, style = line.style_dashed)
    lastBearLabel := label.new((x1 + x2) / 2, curLowVal, "MSS", style = label.style_label_up, textcolor = bearClr, size = size.small, color = color.new(color.white, 100))
 
// ==========================================
// === MTF Data Fetching
// ==========================================
f_sec_logic() =>
    [sDir, sHigh, sLow, sHB, sLB, sVUp, sVDown, sNew] = f_get_mss_status(left, right, confirmWith)
    [sDir, sNew]
 
[s1_raw, n1_raw]   = request.security(syminfo.tickerid, tf1_active  ? tf1_val  : timeframe.period, f_sec_logic())
s1 = tf1_active  ? s1_raw  : na
n1 = tf1_active  ? n1_raw  : false
 
[s2_raw, n2_raw]   = request.security(syminfo.tickerid, tf2_active  ? tf2_val  : timeframe.period, f_sec_logic())
s2 = tf2_active  ? s2_raw  : na
n2 = tf2_active  ? n2_raw  : false
 
[s3_raw, n3_raw]   = request.security(syminfo.tickerid, tf3_active  ? tf3_val  : timeframe.period, f_sec_logic())
s3 = tf3_active  ? s3_raw  : na
n3 = tf3_active  ? n3_raw  : false
 
[s4_raw, n4_raw]   = request.security(syminfo.tickerid, tf4_active  ? tf4_val  : timeframe.period, f_sec_logic())
s4 = tf4_active  ? s4_raw  : na
n4 = tf4_active  ? n4_raw  : false
 
[s5_raw, n5_raw]   = request.security(syminfo.tickerid, tf5_active  ? tf5_val  : timeframe.period, f_sec_logic())
s5 = tf5_active  ? s5_raw  : na
n5 = tf5_active  ? n5_raw  : false
 
[s6_raw, n6_raw]   = request.security(syminfo.tickerid, tf6_active  ? tf6_val  : timeframe.period, f_sec_logic())
s6 = tf6_active  ? s6_raw  : na
n6 = tf6_active  ? n6_raw  : false
 
[s7_raw, n7_raw]   = request.security(syminfo.tickerid, tf7_active  ? tf7_val  : timeframe.period, f_sec_logic())
s7 = tf7_active  ? s7_raw  : na
n7 = tf7_active  ? n7_raw  : false
 
[s8_raw, n8_raw]   = request.security(syminfo.tickerid, tf8_active  ? tf8_val  : timeframe.period, f_sec_logic())
s8 = tf8_active  ? s8_raw  : na
n8 = tf8_active  ? n8_raw  : false
 
[s9_raw, n9_raw]   = request.security(syminfo.tickerid, tf9_active  ? tf9_val  : timeframe.period, f_sec_logic())
s9 = tf9_active  ? s9_raw  : na
n9 = tf9_active  ? n9_raw  : false
 
[s10_raw, n10_raw] = request.security(syminfo.tickerid, tf10_active ? tf10_val : timeframe.period, f_sec_logic())
s10 = tf10_active ? s10_raw : na
n10 = tf10_active ? n10_raw : false
 
// ==========================================
// === Table Implementation
// ==========================================
f_format_tf(_tf) =>
    tf_int = str.tonumber(_tf)
    if _tf == "D" or _tf == "1D"
        "1D"
    else if _tf == "W" or _tf == "1W"
        "1W"
    else if _tf == "M" or _tf == "1M"
        "1M"
    else if str.contains(_tf, "S")
        str.replace_all(_tf, "S", "s")
    else if not na(tf_int) and tf_int >= 60 and tf_int % 60 == 0
        str.tostring(tf_int / 60) + "H"
    else
        _tf + "m"
 
f_collect(_active, _tf, _stat, _strings, _statuses, _durations) =>
    if _active
        array.push(_strings,   _tf)
        array.push(_statuses,  _stat)
        array.push(_durations, timeframe.in_seconds(_tf))
 
f_fill_row(_table, _row, _tfName, _status, _tSize, _bullClr, _bearClr, _cellBg, _cellTxt) =>
    txt    = _status == 1 ? "BULLISH" : _status == -1 ? "BEARISH" : "NEUTRAL"
    clr    = _status == 1 ? _bullClr  : _status == -1 ? _bearClr  : color.gray
    dispTF = f_format_tf(_tfName)
    table.cell(_table, 0, _row, dispTF, text_color = _cellTxt, text_size = _tSize, bgcolor = _cellBg)
    table.cell(_table, 1, _row, txt,    text_color = clr,       text_size = _tSize, bgcolor = _cellBg)
 
var table mtfTable = na
if showTable and barstate.islast
    pos = tablePos == "top_right" ? position.top_right : tablePos == "bottom_right" ? position.bottom_right : tablePos == "top_left" ? position.top_left : tablePos == "bottom_left" ? position.bottom_left : position.middle_right
 
    tSize = tableSize == "tiny" ? size.tiny : tableSize == "small" ? size.small : tableSize == "normal" ? size.normal : size.large
 
    // Collect active data into arrays for sorting
    tf_strings   = array.new_string(0)
    tf_statuses  = array.new_int(0)
    tf_durations = array.new_int(0)
 
    f_collect(tf1_active,  tf1_val,  s1,  tf_strings, tf_statuses, tf_durations)
    f_collect(tf2_active,  tf2_val,  s2,  tf_strings, tf_statuses, tf_durations)
    f_collect(tf3_active,  tf3_val,  s3,  tf_strings, tf_statuses, tf_durations)
    f_collect(tf4_active,  tf4_val,  s4,  tf_strings, tf_statuses, tf_durations)
    f_collect(tf5_active,  tf5_val,  s5,  tf_strings, tf_statuses, tf_durations)
    f_collect(tf6_active,  tf6_val,  s6,  tf_strings, tf_statuses, tf_durations)
    f_collect(tf7_active,  tf7_val,  s7,  tf_strings, tf_statuses, tf_durations)
    f_collect(tf8_active,  tf8_val,  s8,  tf_strings, tf_statuses, tf_durations)
    f_collect(tf9_active,  tf9_val,  s9,  tf_strings, tf_statuses, tf_durations)
    f_collect(tf10_active, tf10_val, s10, tf_strings, tf_statuses, tf_durations)
 
    // Sort by duration ascending (bubble sort)
    int n = array.size(tf_durations)
    if n > 1
        for i = 0 to n - 2
            for j = 0 to n - i - 2
                if array.get(tf_durations, j) > array.get(tf_durations, j + 1)
                    // Swap durations
                    temp_dur = array.get(tf_durations, j)
                    array.set(tf_durations, j, array.get(tf_durations, j + 1))
                    array.set(tf_durations, j + 1, temp_dur)
                    // Swap strings
                    temp_str = array.get(tf_strings, j)
                    array.set(tf_strings, j, array.get(tf_strings, j + 1))
                    array.set(tf_strings, j + 1, temp_str)
                    // Swap statuses
                    temp_stat = array.get(tf_statuses, j)
                    array.set(tf_statuses, j, array.get(tf_statuses, j + 1))
                    array.set(tf_statuses, j + 1, temp_stat)
 
    // Initialize table
    mtfTable := table.new(pos, 2, n + 1, border_width = 1, frame_color = borderClr, border_color = borderClr)
 
    // Header
    table.cell(mtfTable, 0, 0, "Timeframe",  bgcolor = headerBgClr, text_color = headerTxtClr, text_size = tSize)
    table.cell(mtfTable, 1, 0, "MSS Status", bgcolor = headerBgClr, text_color = headerTxtClr, text_size = tSize)
 
    // Render sorted rows
    if n > 0
        for k = 0 to n - 1
            f_fill_row(mtfTable, k + 1, array.get(tf_strings, k), array.get(tf_statuses, k), tSize, bullClr, bearClr, cellBgClr, cellTxtClr)
 
// ==========================================
// === Alerts Logic
// ==========================================
f_fire_alert(_status, _tf, _active) =>
    if _active and _status != 0
        txt = _status == 1 ? "Bullish" : "Bearish"
        alert("MSS " + txt + " on " + f_format_tf(_tf), alert.freq_once_per_bar_close)
 
if curNewMSS and alert_cur
    f_fire_alert(curDir, timeframe.period, true)
 
if n1 and alert_tf1
    f_fire_alert(s1, tf1_val, true)
if n2 and alert_tf2
    f_fire_alert(s2, tf2_val, true)
if n3 and alert_tf3
    f_fire_alert(s3, tf3_val, true)
if n4 and alert_tf4
    f_fire_alert(s4, tf4_val, true)
if n5 and alert_tf5
    f_fire_alert(s5, tf5_val, true)
if n6 and alert_tf6
    f_fire_alert(s6, tf6_val, true)
if n7 and alert_tf7
    f_fire_alert(s7, tf7_val, true)
if n8 and alert_tf8
    f_fire_alert(s8, tf8_val, true)
if n9 and alert_tf9
    f_fire_alert(s9, tf9_val, true)
if n10 and alert_tf10
    f_fire_alert(s10, tf10_val, true)

← 返回列表