STA Trend Bias Dashboard

lenorenagel · study · 436 行 · 点赞 16 · TradingView 原页

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

Pine Script

//@version=6
// ============================================================================
// STA Trend Bias Dashboard (indicator). A study tool.
// Scores trend on four timeframes (default 4H, 15m, 5m, 3m), then adds the
// first two columns into one bias read. The Structure row shows pivot
// structure: UP = HH and HL, DOWN = LH and LL, MIXED = anything else.
// Trend needs the 9 / 21 / 50 EMAs stacked. It reaches the strong level when
// the close clears the last pivot high (or low). EMAs stacked against the
// pivot structure read as neutral. A candle body over 2.5x ATR flags CAUTION.
// Default table position = bottom right.
//
// Developed and designed for members of the Sisterhood Trading Academy.
//
// For education and study. This script reports levels on a chart. It does not
// place orders, it is not financial advice, and nothing here is a claim about
// future results. Futures trading carries risk of loss.
// ============================================================================
indicator("STA Trend Bias Dashboard", shorttitle="STA Trend Bias", overlay=true, max_bars_back=500, max_labels_count=500)
 
// ============================================================
// INPUTS
// ============================================================
pivot_left  = input.int(5, "Pivot Left Bars",  minval=1, maxval=50, group="Pivot Settings")
pivot_right = input.int(5, "Pivot Right Bars", minval=1, maxval=50, group="Pivot Settings")
 
tf_a = input.timeframe("240", "Column 1 Timeframe", group="Timeframes")
tf_b = input.timeframe("15",  "Column 2 Timeframe", group="Timeframes")
tf_c = input.timeframe("5",   "Column 3 Timeframe", group="Timeframes")
tf_d = input.timeframe("3",   "Column 4 Timeframe", group="Timeframes")
 
atr_mult   = input.float(2.5, "ATR Multiplier (spike/drop caution)", minval=1.0, step=0.1, group="Caution Filter")
atr_length = input.int(14, "ATR Length", minval=5, group="Caution Filter")
 
ema_fast = input.int(9,   "EMA Fast", minval=1, group="EMA Settings")
ema_mid  = input.int(21,  "EMA Mid",  minval=1, group="EMA Settings")
ema_slow = input.int(50,  "EMA Slow", minval=1, group="EMA Settings")
ema_200  = input.int(200, "EMA 200",  minval=1, group="EMA Settings")
 
show_ema       = input.bool(true, "Show EMAs", group="EMA Display")
ema_fast_col   = input.color(color.new(#E08D2D, 0), "EMA Fast Color",  group="EMA Display")
ema_mid_col    = input.color(color.new(#E56F33, 0), "EMA Mid Color",   group="EMA Display")
ema_slow_col   = input.color(color.new(#18454E, 0), "EMA Slow Color",  group="EMA Display")
ema_200_col    = input.color(color.new(#766E35, 0), "EMA 200 Color",   group="EMA Display")
ema_fast_width = input.int(1, "EMA Fast Width", minval=1, maxval=4, group="EMA Display")
ema_mid_width  = input.int(1, "EMA Mid Width",  minval=1, maxval=4, group="EMA Display")
ema_slow_width = input.int(2, "EMA Slow Width", minval=1, maxval=4, group="EMA Display")
ema_200_width  = input.int(2, "EMA 200 Width",  minval=1, maxval=4, group="EMA Display")
 
tbl_size         = input.string("normal",    "Text Size",      options=["tiny","small","normal","large","huge"], group="Table Display")
tbl_pos          = input.string("bottom_right", "Table Position", options=["top_right","top_left","bottom_right","bottom_left","top_center","bottom_center"], group="Table Display")
col_bull_confirm = input.color(color.new(#2E6B4F, 0),  "Strong Bull Color",  group="Table Display")
col_bull_caution = input.color(color.new(#A9CDB9, 0),  "Bull Color",  group="Table Display")
col_bear_confirm = input.color(color.new(#F3492B, 0),  "Strong Bear Color",  group="Table Display")
col_bear_caution = input.color(color.new(#F1A3A4, 0),  "Bear Color",  group="Table Display")
col_neutral      = input.color(color.new(#7C9094, 0),  "NEUTRAL Color",   group="Table Display")
col_caution_atr  = input.color(color.new(#E08D2D, 0),  "CAUTION Color",   group="Table Display")
col_header       = input.color(color.new(#18454E, 0),  "Header Color (Deep Ocean)",    group="Table Display")
col_txt          = input.color(color.new(#F6EDE7, 0),  "Text Color (Paper)",      group="Table Display")
col_txt_dark     = input.color(color.new(#18454E, 0),  "Text Dark Color", group="Table Display")
col_bg           = input.color(color.new(#F6EDE7, 0),  "Table BG (Paper)",        group="Table Display")
 
show_labels = input.bool(true,       "Show Labels",   group="Pivot Labels")
label_size  = input.string("normal", "Label Size", options=["tiny","small","normal","large","huge"], group="Pivot Labels")
col_hh = input.color(color.new(#18454E, 0), "HH Color", group="Pivot Labels")
col_lh = input.color(color.new(#E56F33, 0), "LH Color", group="Pivot Labels")
col_hl = input.color(color.new(#2E6B4F, 0), "HL Color", group="Pivot Labels")
col_ll = input.color(color.new(#F3492B, 0), "LL Color", group="Pivot Labels")
 
// ============================================================
// HELPER FUNCTIONS
// ============================================================
txt_size_map(s) =>
    switch s
        "tiny"   => size.tiny
        "small"  => size.small
        "large"  => size.large
        "huge"   => size.huge
        => size.normal
 
pos_map(p) =>
    switch p
        "top_left"      => position.top_left
        "bottom_right"  => position.bottom_right
        "bottom_left"   => position.bottom_left
        "top_center"    => position.top_center
        "bottom_center" => position.bottom_center
        => position.top_right
 
tf_label() =>
    switch timeframe.period
        "1"   => "1M"
        "3"   => "3M"
        "5"   => "5M"
        "15"  => "15M"
        "30"  => "30M"
        "60"  => "1H"
        "120" => "2H"
        "240" => "4H"
        "D"   => "1D"
        "W"   => "1W"
        "M"   => "1Mo"
        => timeframe.period
 
tf_name(tf) =>
    sec = timeframe.in_seconds(tf)
    if sec < 3600
        str.tostring(sec / 60, "#") + "M"
    else if sec < 86400
        str.tostring(sec / 3600, "#") + "H"
    else if sec == 86400
        "1D"
    else if sec < 2592000
        str.tostring(sec / 604800, "#") + "W"
    else
        "1Mo"
 
trend_label(t, c) =>
    if c
        "CAUTION"
    else if t == 2
        "BULL"
    else if t == 1
        "BULL"
    else if t == -1
        "BEAR"
    else if t == -2
        "BEAR"
    else
        "NEUTRAL"
 
trend_bg(t, c) =>
    if c
        col_caution_atr
    else if t == 2
        col_bull_confirm
    else if t == 1
        col_bull_caution
    else if t == -1
        col_bear_caution
    else if t == -2
        col_bear_confirm
    else
        col_neutral
 
trend_txt(t, c) =>
    if c
        col_txt
    else if t == 1 or t == -1
        col_txt_dark
    else
        col_txt
 
ema_lvl(e9, e21, e50) =>
    if e9 > e21 and e21 > e50
        2
    else if e9 > e21 and e21 < e50
        1
    else if e9 < e21 and e21 < e50
        -2
    else if e9 < e21 and e21 > e50
        -1
    else
        0
 
struct_str(x) =>
    x == 1 ? "UP" : x == -1 ? "DOWN" : "MIXED"
 
struct_bg(x) =>
    x == 1 ? col_bull_caution : x == -1 ? col_bear_caution : col_neutral
 
struct_fg(x) =>
    x == 0 ? col_txt : col_txt_dark
 
align_str(t, score) =>
    if t == 0 or score == 0
        "NEUTRAL"
    else if (t > 0 and score > 0) or (t < 0 and score < 0)
        "WITH BIAS"
    else
        "AGAINST"
 
align_bg(t, score) =>
    if t == 0 or score == 0
        col_neutral
    else if (t > 0 and score > 0) or (t < 0 and score < 0)
        col_bull_caution
    else
        col_caution_atr
 
align_fg(t, score) =>
    t == 0 or score == 0 ? col_txt : col_txt_dark
 
bias_str(score, c_1d, c_4h) =>
    if c_1d or c_4h
        "CAUTION"
    else if score >= 4
        "STRONG BULL"
    else if score >= 2
        "BULL"
    else if score <= -4
        "STRONG BEAR"
    else if score <= -2
        "BEAR"
    else
        "NEUTRAL"
 
bias_bg(score, c_1d, c_4h) =>
    if c_1d or c_4h
        col_caution_atr
    else if score >= 4
        col_bull_confirm
    else if score >= 2
        col_bull_caution
    else if score <= -4
        col_bear_confirm
    else if score <= -2
        col_bear_caution
    else
        col_neutral
 
bias_fg(score, c_1d, c_4h) =>
    if c_1d or c_4h
        col_txt
    else if score >= 2 and score < 4
        col_txt_dark
    else if score <= -2 and score > -4
        col_txt_dark
    else
        col_txt
 
// ============================================================
// EMA plots (current timeframe)
// ============================================================
e9_cur   = ta.ema(close, ema_fast)
e21_cur  = ta.ema(close, ema_mid)
e50_cur  = ta.ema(close, ema_slow)
e200_cur = ta.ema(close, ema_200)
 
plot(show_ema ? e9_cur   : na, title="EMA Fast", color=ema_fast_col,  linewidth=ema_fast_width)
plot(show_ema ? e21_cur  : na, title="EMA Mid",  color=ema_mid_col,   linewidth=ema_mid_width)
plot(show_ema ? e50_cur  : na, title="EMA Slow", color=ema_slow_col,  linewidth=ema_slow_width)
plot(show_ema ? e200_cur : na, title="EMA 200",  color=ema_200_col,   linewidth=ema_200_width)
 
// ============================================================
// 5-level trend function
// ============================================================
get_trend_5() =>
    e9  = ta.ema(close, ema_fast)
    e21 = ta.ema(close, ema_mid)
    e50 = ta.ema(close, ema_slow)
    ema_bull = e9 > e21 and e21 > e50
    ema_bear = e9 < e21 and e21 < e50
 
    ph = ta.pivothigh(high, pivot_left, pivot_right)
    pl = ta.pivotlow( low,  pivot_left, pivot_right)
 
    var float last_ph = na
    var float last_pl = na
    var float prev_ph = na
    var float prev_pl = na
 
    if not na(ph)
        prev_ph := last_ph
        last_ph := ph
    if not na(pl)
        prev_pl := last_pl
        last_pl := pl
 
    close_above_ph = not na(last_ph) and close > last_ph
    close_below_pl = not na(last_pl) and close < last_pl
    is_lh          = not na(last_ph) and not na(prev_ph) and last_ph < prev_ph
    is_ll          = not na(last_pl) and not na(prev_pl) and last_pl < prev_pl
    is_hh          = not na(last_ph) and not na(prev_ph) and last_ph > prev_ph
    is_hl          = not na(last_pl) and not na(prev_pl) and last_pl > prev_pl
    bearish_struct = is_lh or is_ll
    bullish_struct = is_hh or is_hl
 
    atr     = ta.atr(atr_length)
    body    = math.abs(close - open)
    caution = body > atr * atr_mult
 
    trend = 0
    if caution
        trend := 0
    else if ema_bull
        if close_above_ph
            trend := 2
        else if bearish_struct
            trend := 0
        else
            trend := 1
    else if ema_bear
        if close_below_pl
            trend := -2
        else if bullish_struct
            trend := 0
        else
            trend := -1
    else
        trend := 0
 
    structure = is_hh and is_hl ? 1 : is_lh and is_ll ? -1 : 0
 
    [trend, caution, structure]
 
// ============================================================
// Higher timeframe 1, higher timeframe 2, and current timeframe
// ============================================================
[t_1d,  c_1d,  s_a]  = request.security(syminfo.tickerid, tf_a,  get_trend_5(), lookahead=barmerge.lookahead_off)
[t_4h,  c_4h,  s_b]  = request.security(syminfo.tickerid, tf_b, get_trend_5(), lookahead=barmerge.lookahead_off)
[t_c, c_c, s_c] = request.security(syminfo.tickerid, tf_c, get_trend_5(), lookahead=barmerge.lookahead_off)
[t_d, c_d, s_d] = request.security(syminfo.tickerid, tf_d, get_trend_5(), lookahead=barmerge.lookahead_off)
 
// ============================================================
// EMA values for the table
// ============================================================
ema9_1d  = request.security(syminfo.tickerid, tf_a,  ta.ema(close, ema_fast))
ema21_1d = request.security(syminfo.tickerid, tf_a,  ta.ema(close, ema_mid))
ema50_1d = request.security(syminfo.tickerid, tf_a,  ta.ema(close, ema_slow))
ema9_4h  = request.security(syminfo.tickerid, tf_b, ta.ema(close, ema_fast))
ema21_4h = request.security(syminfo.tickerid, tf_b, ta.ema(close, ema_mid))
ema50_4h = request.security(syminfo.tickerid, tf_b, ta.ema(close, ema_slow))
ema9_c   = request.security(syminfo.tickerid, tf_c, ta.ema(close, ema_fast))
ema21_c  = request.security(syminfo.tickerid, tf_c, ta.ema(close, ema_mid))
ema50_c  = request.security(syminfo.tickerid, tf_c, ta.ema(close, ema_slow))
ema9_d   = request.security(syminfo.tickerid, tf_d, ta.ema(close, ema_fast))
ema21_d  = request.security(syminfo.tickerid, tf_d, ta.ema(close, ema_mid))
ema50_d  = request.security(syminfo.tickerid, tf_d, ta.ema(close, ema_slow))
 
// ============================================================
// Current timeframe pivot detection (labels only)
// ============================================================
ph_label = ta.pivothigh(high, pivot_left, pivot_right)
pl_label = ta.pivotlow( low,  pivot_left, pivot_right)
 
var float lbl_last_ph = na
var float lbl_last_pl = na
var float lbl_prev_ph = na
var float lbl_prev_pl = na
 
if not na(ph_label)
    lbl_prev_ph := lbl_last_ph
    lbl_last_ph := ph_label
if not na(pl_label)
    lbl_prev_pl := lbl_last_pl
    lbl_last_pl := pl_label
 
lbl_is_hh = not na(lbl_last_ph) and not na(lbl_prev_ph) and lbl_last_ph > lbl_prev_ph
lbl_is_lh = not na(lbl_last_ph) and not na(lbl_prev_ph) and lbl_last_ph < lbl_prev_ph
lbl_is_hl = not na(lbl_last_pl) and not na(lbl_prev_pl) and lbl_last_pl > lbl_prev_pl
lbl_is_ll = not na(lbl_last_pl) and not na(lbl_prev_pl) and lbl_last_pl < lbl_prev_pl
 
// ============================================================
// Pivot label drawing
// ============================================================
if show_labels
    sz = txt_size_map(label_size)
    if lbl_is_hh and not na(ph_label)
        label.new(bar_index[pivot_right], ph_label, "HH", style=label.style_none, textcolor=col_hh, size=sz)
    if lbl_is_lh and not na(ph_label)
        label.new(bar_index[pivot_right], ph_label, "LH", style=label.style_none, textcolor=col_lh, size=sz)
    if lbl_is_hl and not na(pl_label)
        label.new(bar_index[pivot_right], pl_label, "HL", style=label.style_none, textcolor=col_hl, size=sz)
    if lbl_is_ll and not na(pl_label)
        label.new(bar_index[pivot_right], pl_label, "LL", style=label.style_none, textcolor=col_ll, size=sz)
 
// ============================================================
// Bias score calculation
// ============================================================
bias_score = t_1d + t_4h
 
// ============================================================
// Table drawing (row order: EMA / Trend / Structure / Bias)
// ============================================================
var table tbl = table.new(pos_map(tbl_pos), 5, 5,
     border_width=1, border_color=color.new(#18454E, 60))
 
if barstate.islast
    sz  = txt_size_map(tbl_size)
    bs  = bias_score
 
    // Header
    table.cell(tbl, 0, 0, "   ",        bgcolor=col_header, text_color=col_txt, text_size=sz)
    table.cell(tbl, 1, 0, "  " + tf_name(tf_a) + "  ", bgcolor=col_header, text_color=col_txt, text_size=sz)
    table.cell(tbl, 2, 0, "  " + tf_name(tf_b) + "  ", bgcolor=col_header, text_color=col_txt, text_size=sz)
    table.cell(tbl, 3, 0, "  " + tf_name(tf_c) + "  ", bgcolor=col_header, text_color=col_txt, text_size=sz)
    table.cell(tbl, 4, 0, "  " + tf_name(tf_d) + "  ", bgcolor=col_header, text_color=col_txt, text_size=sz)
 
    // EMA
    table.cell(tbl, 0, 1, "EMA",
         bgcolor=col_header, text_color=col_txt, text_size=sz)
    table.cell(tbl, 1, 1, trend_label(ema_lvl(ema9_1d, ema21_1d, ema50_1d), false),
         bgcolor=trend_bg(ema_lvl(ema9_1d, ema21_1d, ema50_1d), false), text_color=trend_txt(ema_lvl(ema9_1d, ema21_1d, ema50_1d), false), text_size=sz)
    table.cell(tbl, 2, 1, trend_label(ema_lvl(ema9_4h, ema21_4h, ema50_4h), false),
         bgcolor=trend_bg(ema_lvl(ema9_4h, ema21_4h, ema50_4h), false), text_color=trend_txt(ema_lvl(ema9_4h, ema21_4h, ema50_4h), false), text_size=sz)
    table.cell(tbl, 3, 1, trend_label(ema_lvl(ema9_c, ema21_c, ema50_c), false),
         bgcolor=trend_bg(ema_lvl(ema9_c, ema21_c, ema50_c), false), text_color=trend_txt(ema_lvl(ema9_c, ema21_c, ema50_c), false), text_size=sz)
    table.cell(tbl, 4, 1, trend_label(ema_lvl(ema9_d, ema21_d, ema50_d), false),
         bgcolor=trend_bg(ema_lvl(ema9_d, ema21_d, ema50_d), false), text_color=trend_txt(ema_lvl(ema9_d, ema21_d, ema50_d), false), text_size=sz)
 
    // Trend
    table.cell(tbl, 0, 2, "Trend",
         bgcolor=col_header, text_color=col_txt, text_size=sz)
    table.cell(tbl, 1, 2, trend_label(t_1d, c_1d),
         bgcolor=trend_bg(t_1d, c_1d), text_color=trend_txt(t_1d, c_1d), text_size=sz)
    table.cell(tbl, 2, 2, trend_label(t_4h, c_4h),
         bgcolor=trend_bg(t_4h, c_4h), text_color=trend_txt(t_4h, c_4h), text_size=sz)
    table.cell(tbl, 3, 2, trend_label(t_c, c_c),
         bgcolor=trend_bg(t_c, c_c), text_color=trend_txt(t_c, c_c), text_size=sz)
    table.cell(tbl, 4, 2, trend_label(t_d, c_d),
         bgcolor=trend_bg(t_d, c_d), text_color=trend_txt(t_d, c_d), text_size=sz)
 
    // Structure
    table.cell(tbl, 0, 3, "Structure",
         bgcolor=col_header, text_color=col_txt, text_size=sz)
    table.cell(tbl, 1, 3, struct_str(s_a),
         bgcolor=struct_bg(s_a), text_color=struct_fg(s_a), text_size=sz)
    table.cell(tbl, 2, 3, struct_str(s_b),
         bgcolor=struct_bg(s_b), text_color=struct_fg(s_b), text_size=sz)
    table.cell(tbl, 3, 3, struct_str(s_c),
         bgcolor=struct_bg(s_c), text_color=struct_fg(s_c), text_size=sz)
    table.cell(tbl, 4, 3, struct_str(s_d),
         bgcolor=struct_bg(s_d), text_color=struct_fg(s_d), text_size=sz)
 
    // Bias
    table.cell(tbl, 0, 4, "Bias",
         bgcolor=col_header, text_color=col_txt, text_size=sz)
    table.cell(tbl, 1, 4, bias_str(bs, c_1d, c_4h),
         bgcolor=bias_bg(bs, c_1d, c_4h), text_color=bias_fg(bs, c_1d, c_4h), text_size=sz)
    table.cell(tbl, 2, 4, "",
         bgcolor=bias_bg(bs, c_1d, c_4h), text_size=sz)
    table.cell(tbl, 3, 4, align_str(t_c, bs),
         bgcolor=align_bg(t_c, bs), text_color=align_fg(t_c, bs), text_size=sz)
    table.cell(tbl, 4, 4, align_str(t_d, bs),
         bgcolor=align_bg(t_d, bs), text_color=align_fg(t_d, bs), text_size=sz)
    table.merge_cells(tbl, 1, 4, 2, 4)

← 返回列表