TTG SNIPER STRAT DASHBOARD TP/SL

TheTradingGoddess · study · 290 行 · 点赞 4 · TradingView 原页

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

Pine Script

//@version=6
indicator("TTG SNIPER STRAT DASHBOARD TP/SL", overlay=true, dynamic_requests=true)
 
//==================================================
// TIMEFRAMES
//==================================================
groupTF = "Timeframes"
 
tf30 = input.timeframe("30", "TP/SL Timeframe", group=groupTF)
tfMP = input.timeframe("15", "Market Pulse Timeframe", group=groupTF)
tfStrat = input.timeframe("D", "STRAT Timeframe", group=groupTF)
 
//==================================================
// DASHBOARD SETTINGS
//==================================================
groupDash = "Dashboard Settings"
 
dashPosition = input.string("Bottom Right", "Dashboard Position",
     options=["Top Left", "Top Center", "Top Right",
              "Middle Left", "Middle Center", "Middle Right",
              "Bottom Left", "Bottom Center", "Bottom Right"],
     group=groupDash)
 
textColor = input.color(color.white, "Dashboard Text Color", group=groupDash)
 
dashTextSize = input.string("Normal", "Dashboard Text Size",
     options=["Tiny", "Small", "Normal", "Large"],
     group=groupDash)
 
//==================================================
// STRAT SETTINGS
//==================================================
groupStrat = "STRAT Settings"
 
showStrat = input.bool(true, "Show STRAT Column", group=groupStrat)
 
confirmedStrat = input.bool(false, "Use Completed Candles Only", group=groupStrat)
 
stratBullColor = input.color(color.green, "Bullish STRAT Color", group=groupStrat)
stratBearColor = input.color(color.red, "Bearish STRAT Color", group=groupStrat)
stratInsideColor = input.color(color.yellow, "Inside Bar Color", group=groupStrat)
stratNeutralColor = input.color(color.gray, "Outside / Neutral Color", group=groupStrat)
 
//==================================================
// SYMBOLS
//==================================================
groupSymbols = "Symbols"
 
sym1 = input.symbol("AMEX:SPY", "Symbol 1", group=groupSymbols)
sym2 = input.symbol("NASDAQ:QQQ", "Symbol 2", group=groupSymbols)
sym3 = input.symbol("AMEX:XLE", "Symbol 3", group=groupSymbols)
sym4 = input.symbol("NASDAQ:AAPL", "Symbol 4", group=groupSymbols)
sym5 = input.symbol("NASDAQ:TSLA", "Symbol 5", group=groupSymbols)
sym6 = input.symbol("NASDAQ:NVDA", "Symbol 6", group=groupSymbols)
 
//==================================================
// TP / SL SETTINGS
//==================================================
groupTPSL = "TP/SL Settings"
 
atrLen = input.int(14, "ATR Length", minval=1, group=groupTPSL)
tpATRMult = input.float(1.0, "TP ATR Multiplier", step=0.1, group=groupTPSL)
slATRMult = input.float(0.6, "SL ATR Multiplier", step=0.1, group=groupTPSL)
 
//==================================================
// POSITION HELPER
//==================================================
f_position(pos) =>
    switch pos
        "Top Left" => position.top_left
        "Top Center" => position.top_center
        "Top Right" => position.top_right
        "Middle Left" => position.middle_left
        "Middle Center" => position.middle_center
        "Middle Right" => position.middle_right
        "Bottom Left" => position.bottom_left
        "Bottom Center" => position.bottom_center
        => position.bottom_right
 
f_textSize(s) =>
    switch s
        "Tiny" => size.tiny
        "Small" => size.small
        "Large" => size.large
        => size.normal
 
//==================================================
// HELPERS
//==================================================
f_short(sym) =>
    parts = str.split(sym, ":")
    array.size(parts) > 1 ? array.get(parts, 1) : sym
 
f_fmtPrice(val) =>
    str.tostring(val, "#.00")
 
f_lastBg(lastVal, prevVal) =>
    lastVal > prevVal ? color.green : lastVal < prevVal ? color.red : color.gray
 
f_mpText(v) =>
    switch v
        2 => "STR BUY"
        1 => "BUY"
        -1 => "SELL"
        -2 => "STR SELL"
        => "NEUTRAL"
 
f_mpBg(v) =>
    switch v
        2 => color.lime
        1 => color.green
        -1 => color.blue
        -2 => color.red
        => color.gray
 
//==================================================
// TP / SL LOGIC
//==================================================
f_tpsl() =>
    ma20 = ta.ema(close, 20)
    atrv = ta.atr(atrLen)
 
    dir = close > ma20 ? 1 : -1
 
    tp = dir == 1 ? close + atrv * tpATRMult : close - atrv * tpATRMult
    sl = dir == 1 ? close - atrv * slATRMult : close + atrv * slATRMult
 
    [tp, sl]
 
//==================================================
// MARKET PULSE LOGIC
//==================================================
f_marketPulse() =>
    tmp1 = close > close[1] ? close - close[1] : 0.0
    tmp2 = close[1] > close ? close[1] - close : 0.0
 
    d2 = ta.sma(tmp1, 10) * 10
    d4 = ta.sma(tmp2, 10) * 10
 
    cond = d2 + d4 == 0
    ad3 = not cond ? (d2 - d4) / (d2 + d4) * 100.0 : 0.0
    coeff = 2.0 / 11.0 * math.abs(ad3) / 100.0
 
    var float vma = na
    vma := na(vma[1]) ? close : coeff * close + vma[1] * (1.0 - coeff)
 
    ma20 = ta.ema(close, 20)
    ma72 = ta.ema(close, 72)
    ma200 = ta.ema(close, 200)
    vwapVal = ta.vwap(close)
 
    strongBuy = close > vwapVal and close > ma20 and close > ma72 and close > ma200
    strongSell = close < vwapVal and close < ma20 and close < ma72 and close < ma200
 
    basicBuy = close > vma
    basicSell = close < vma
 
    out = 0
 
    if strongBuy
        out := 2
    else if strongSell
        out := -2
    else if basicBuy
        out := 1
    else if basicSell
        out := -1
 
    out
 
//==================================================
// STRAT CLASSIFICATION
//==================================================
// 1  = Inside bar
// 2U = Directional up
// 2D = Directional down
// 3  = Outside bar
//==================================================
f_stratType() =>
    isInside = high <= high[1] and low >= low[1]
    isOutside = high > high[1] and low < low[1]
    is2U = high > high[1] and low >= low[1]
    is2D = low < low[1] and high <= high[1]
 
    isInside ? 1 : isOutside ? 3 : is2U ? 2 : is2D ? -2 : 0
 
f_stratPair() =>
    currentType = f_stratType()
    previousType = currentType[1]
 
    if confirmedStrat
        [previousType[1], currentType[1]]
    else
        [previousType, currentType]
 
//==================================================
// STRAT TEXT AND COLORS
//==================================================
f_stratName(v) =>
    switch v
        1 => "1"
        2 => "2U"
        -2 => "2D"
        3 => "3"
        => "—"
 
f_stratColor(v) =>
    v == 1 ? stratInsideColor :
     v == 2 ? stratBullColor :
     v == -2 ? stratBearColor :
     stratNeutralColor
 
f_stratText(prev, curr) =>
    f_stratName(prev) + "-" + f_stratName(curr)
 
// Color follows the CURRENT candle classification.
f_stratBg(prev, curr) =>
    f_stratColor(curr)
 
//==================================================
// TABLE
//==================================================
var table t = table.new(f_position(dashPosition), 5, 7, border_width=1)
 
//==================================================
// DASHBOARD DATA
//==================================================
string[] symbols = array.from(sym1, sym2, sym3, sym4, sym5, sym6)
 
for i = 0 to array.size(symbols) - 1
    s = array.get(symbols, i)
 
    [lastPx, prevLastPx] = request.security(s, timeframe.period, [close, close[1]])
 
    [tp, sl] = request.security(s, tf30, f_tpsl())
 
    mp = request.security(s, tfMP, f_marketPulse())
 
    // Live STRAT: current developing candle
    [livePrev, liveCurr] = request.security(
         s, tfStrat, f_stratPair(),
         lookahead=barmerge.lookahead_off)
 
    // Confirmed STRAT: previous two completed candles
    [confirmedPrev, confirmedCurr] = request.security(
         s, tfStrat, [f_stratType()[2], f_stratType()[1]],
         lookahead=barmerge.lookahead_on)
 
    stratPrev = confirmedStrat ? confirmedPrev : livePrev
    stratCurr = confirmedStrat ? confirmedCurr : liveCurr
 
    if barstate.islast
        row = i + 1
        txtSize = f_textSize(dashTextSize)
 
        table.cell(t, 0, row, f_short(s),
             bgcolor=color.black,
             text_color=textColor,
             text_size=txtSize)
 
        table.cell(t, 1, row, f_fmtPrice(lastPx),
             bgcolor=f_lastBg(lastPx, prevLastPx),
             text_color=textColor,
             text_size=txtSize)
 
        table.cell(t, 2, row,
             "TP: " + f_fmtPrice(tp) + " | SL: " + f_fmtPrice(sl),
             bgcolor=color.new(color.gray, 40),
             text_color=textColor,
             text_size=txtSize)
 
        table.cell(t, 3, row, f_mpText(mp),
             bgcolor=f_mpBg(mp),
             text_color=textColor,
             text_size=txtSize)
 
        table.cell(t, 4, row,
             showStrat ? f_stratText(stratPrev, stratCurr) : "",
             bgcolor=showStrat ? f_stratBg(stratPrev, stratCurr) : color.black,
             text_color=stratCurr == 1 ? color.black : textColor,
             text_size=txtSize)
 
if barstate.islast
    txtSize = f_textSize(dashTextSize)
 
    table.cell(t, 0, 0, "SYMBOL", bgcolor=color.black, text_color=textColor, text_size=txtSize)
    table.cell(t, 1, 0, "LAST", bgcolor=color.black, text_color=textColor, text_size=txtSize)
    table.cell(t, 2, 0, "TP/SL", bgcolor=color.black, text_color=textColor, text_size=txtSize)
    table.cell(t, 3, 0, "MP", bgcolor=color.black, text_color=textColor, text_size=txtSize)
    table.cell(t, 4, 0, "STRAT", bgcolor=color.black, text_color=textColor, text_size=txtSize)

← 返回列表