SMT Divergence [Badr Edge]

kh_badrr · study · 82 行 · 点赞 2 · TradingView 原页

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

Pine Script

//@version=6
// SMT Divergence [Badr Edge] · v1.0.0
// Educational chart-marking tool. Markers show what happened on a bar. They are not trade signals.
indicator("SMT Divergence [Badr Edge]", shorttitle = "BE SMT", overlay = true, max_lines_count = 300, max_labels_count = 300)
const color GOLD = #D9B46C
const color TEAL = #4FB8A0
const color ROSE = #E07A6C
const color GREY = #8F939B
const string G_M = "Markets"
cmpSym = input.symbol("CME_MINI:MES1!", "Compare with", group = G_M, tooltip = "A market that normally moves with the chart symbol, for example MNQ with MES, or MGC with SIL.")
const string G_S = "Swings"
swLen  = input.int(5, "Swing length (bars each side)", minval = 2, maxval = 30, group = G_S, tooltip = "A swing high is a bar with this many lower highs on both sides. A swing is confirmed this many bars after it forms, so a mark appears with that delay and never moves afterwards.")
maxGap = input.int(60, "Max bars between the two swings", minval = 5, maxval = 500, group = G_S)
const string G_V = "Visuals"
showHigh = input.bool(true, "Divergence at highs", group = G_V)
showLow  = input.bool(true, "Divergence at lows", group = G_V)
showTxt  = input.bool(true, "SMT label", group = G_V)
const string G_Y = "Style"
colHigh = input.color(ROSE, "At highs", inline = "c", group = G_Y)
colLow  = input.color(TEAL, "At lows", inline = "c", group = G_Y)
showWm  = input.bool(true, "Watermark", group = G_Y)
const string G_A = "Alerts"
useAlerts = input.bool(true, "Alert when a divergence is confirmed", group = G_A, tooltip = "Create the alert with \"Any alert() function call\". Fires once, after the bar closes.")
 
[cH, cL] = request.security(cmpSym, timeframe.period, [high, low])
sameSym  = str.contains(cmpSym, syminfo.ticker) or na(cH)
win   = 2 * swLen + 1
ph    = ta.pivothigh(high, swLen, swLen)
pl    = ta.pivotlow(low, swLen, swLen)
cWinH = ta.highest(cH, win)
cWinL = ta.lowest(cL, win)
 
var float pPh = na
var int   pPhBar = na
var float pCh = na
var float pPl = na
var int   pPlBar = na
var float pCl = na
bool bear = false
bool bull = false
 
if not na(ph)
    int b = bar_index - swLen
    if showHigh and not na(pPh) and b - pPhBar <= maxGap and ph != pPh and cWinH != pCh
        if (ph > pPh) != (cWinH > pCh)
            bear := true
            line.new(pPhBar, pPh, b, ph, color = color.new(colHigh, 10), width = 1)
            if showTxt
                label.new(b, ph, "SMT", style = label.style_label_down, color = color.new(color.black, 100), textcolor = colHigh, size = size.tiny)
    pPh := ph
    pPhBar := b
    pCh := cWinH
if not na(pl)
    int b = bar_index - swLen
    if showLow and not na(pPl) and b - pPlBar <= maxGap and pl != pPl and cWinL != pCl
        if (pl < pPl) != (cWinL < pCl)
            bull := true
            line.new(pPlBar, pPl, b, pl, color = color.new(colLow, 10), width = 1)
            if showTxt
                label.new(b, pl, "SMT", style = label.style_label_up, color = color.new(color.black, 100), textcolor = colLow, size = size.tiny)
    pPl := pl
    pPlBar := b
    pCl := cWinL
 
plot(bear ? 1 : 0, "Divergence at highs confirmed", display = display.data_window)
plot(bull ? 1 : 0, "Divergence at lows confirmed", display = display.data_window)
plot(pPh, "Last swing high", display = display.data_window)
plot(pPl, "Last swing low", display = display.data_window)
plot(pCh, "Compare market high at that swing", display = display.data_window)
plot(pCl, "Compare market low at that swing", display = display.data_window)
 
if useAlerts and bear
    alert(syminfo.ticker + " and " + cmpSym + " disagree at the last two swing highs (SMT divergence)", alert.freq_once_per_bar_close)
if useAlerts and bull
    alert(syminfo.ticker + " and " + cmpSym + " disagree at the last two swing lows (SMT divergence)", alert.freq_once_per_bar_close)
 
var table wm = table.new(position.bottom_right, 1, 1)
if barstate.islast and showWm
    wm.cell(0, 0, "BADR EDGE", text_color = color.new(GREY, 72), text_size = size.small)
var table note = table.new(position.top_right, 1, 1)
if barstate.islast and sameSym
    note.cell(0, 0, "Choose a different market in \"Compare with\".", text_color = GOLD, bgcolor = color.new(color.black, 20), text_size = size.small)

← 返回列表