TTD - Key Levels Breakouts + Dashboard

mcarter-griffith · study · 250 行 · 点赞 8 · TradingView 原页

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

Pine Script

//@version=6
indicator("TTD - Key Levels Breakouts + Dashboard", overlay=true, max_labels_count=500)
 
// ==========================================
// THE TRADER DESK - SETTINGS
// ==========================================
 
groupLevels = "Key Levels"
showPD = input.bool(true, "Previous Day High/Low", group=groupLevels)
showPM = input.bool(true, "Premarket High/Low", group=groupLevels)
showPW = input.bool(true, "Previous Week High/Low", group=groupLevels)
 
groupColors = "Line Colors"
pdColor = input.color(color.yellow, "Previous Day", group=groupColors)
pmColor = input.color(color.aqua, "Premarket", group=groupColors)
pwColor = input.color(color.fuchsia, "Previous Week", group=groupColors)
lineWidth = input.int(2, "Line Width", minval=1, maxval=5, group=groupColors)
 
groupSignals = "Breakout Signals"
showSignals = input.bool(true, "Show BUY/SELL Arrows", group=groupSignals)
 
groupLabels = "Price Labels"
showLabels = input.bool(true, "Show Price Labels", group=groupLabels)
labelOffset = input.int(5, "Label Offset", minval=0, maxval=50, group=groupLabels)
 
groupDash = "TTD Dashboard"
showDashboard = input.bool(true, "Show Dashboard", group=groupDash)
dashPosition = input.string("Top Right", "Dashboard Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=groupDash)
dashTextSize = input.string("Small", "Dashboard Text Size", options=["Tiny", "Small", "Normal"], group=groupDash)
 
// ==========================================
// PREVIOUS DAY / WEEK LEVELS
// ==========================================
 
pdh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
pdl = request.security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)
 
pwh = request.security(syminfo.tickerid, "W", high[1], lookahead=barmerge.lookahead_on)
pwl = request.security(syminfo.tickerid, "W", low[1], lookahead=barmerge.lookahead_on)
 
// ==========================================
// PREMARKET HIGH / LOW
// ==========================================
 
pmSession = input.session("0400-0930", "Premarket Session", group=groupLevels)
 
inPM = not na(time(timeframe.period, pmSession, "America/New_York"))
newPM = inPM and not inPM[1]
 
var float pmHigh = na
var float pmLow = na
 
if newPM
    pmHigh := high
    pmLow := low
else if inPM
    pmHigh := math.max(nz(pmHigh, high), high)
    pmLow := math.min(nz(pmLow, low), low)
 
// ==========================================
// DRAW LEVELS
// ==========================================
 
plot(showPD ? pdh : na, "PDH", color=pdColor, linewidth=lineWidth, style=plot.style_stepline)
plot(showPD ? pdl : na, "PDL", color=pdColor, linewidth=lineWidth, style=plot.style_stepline)
 
plot(showPM ? pmHigh : na, "PMH", color=pmColor, linewidth=lineWidth, style=plot.style_stepline)
plot(showPM ? pmLow : na, "PML", color=pmColor, linewidth=lineWidth, style=plot.style_stepline)
 
plot(showPW ? pwh : na, "PWH", color=pwColor, linewidth=lineWidth, style=plot.style_stepline)
plot(showPW ? pwl : na, "PWL", color=pwColor, linewidth=lineWidth, style=plot.style_stepline)
 
// ==========================================
// BREAKOUT CONDITIONS
// ==========================================
 
// Calculate crosses on every bar.
crossUpPDH = ta.crossover(close, pdh)
crossUpPDL = ta.crossover(close, pdl)
crossUpPMH = ta.crossover(close, pmHigh)
crossUpPML = ta.crossover(close, pmLow)
crossUpPWH = ta.crossover(close, pwh)
crossUpPWL = ta.crossover(close, pwl)
 
crossDnPDH = ta.crossunder(close, pdh)
crossDnPDL = ta.crossunder(close, pdl)
crossDnPMH = ta.crossunder(close, pmHigh)
crossDnPML = ta.crossunder(close, pmLow)
crossDnPWH = ta.crossunder(close, pwh)
crossDnPWL = ta.crossunder(close, pwl)
 
// Prevent signals caused by level resets.
bullPD = showPD and ((crossUpPDH and pdh == pdh[1]) or (crossUpPDL and pdl == pdl[1]))
bearPD = showPD and ((crossDnPDH and pdh == pdh[1]) or (crossDnPDL and pdl == pdl[1]))
 
bullPM = showPM and not inPM and ((crossUpPMH and pmHigh == pmHigh[1]) or (crossUpPML and pmLow == pmLow[1]))
bearPM = showPM and not inPM and ((crossDnPMH and pmHigh == pmHigh[1]) or (crossDnPML and pmLow == pmLow[1]))
 
bullPW = showPW and ((crossUpPWH and pwh == pwh[1]) or (crossUpPWL and pwl == pwl[1]))
bearPW = showPW and ((crossDnPWH and pwh == pwh[1]) or (crossDnPWL and pwl == pwl[1]))
 
bullBreak = barstate.isconfirmed and (bullPD or bullPM or bullPW)
bearBreak = barstate.isconfirmed and (bearPD or bearPM or bearPW)
 
// ==========================================
// BUY / SELL ARROWS
// ==========================================
 
plotshape(showSignals and bullBreak, title="TTD BUY", style=shape.triangleup, location=location.belowbar, color=color.lime, size=size.small, text="BUY", textcolor=color.lime)
 
plotshape(showSignals and bearBreak, title="TTD SELL", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, text="SELL", textcolor=color.red)
 
// ==========================================
// PRICE LABELS
// ==========================================
 
var label pdhLabel = na
var label pdlLabel = na
var label pmhLabel = na
var label pmlLabel = na
var label pwhLabel = na
var label pwlLabel = na
 
if barstate.islast
    label.delete(pdhLabel)
    label.delete(pdlLabel)
    label.delete(pmhLabel)
    label.delete(pmlLabel)
    label.delete(pwhLabel)
    label.delete(pwlLabel)
 
    if showLabels
        if showPD and not na(pdh)
            pdhLabel := label.new(bar_index + labelOffset, pdh, "PDH $" + str.tostring(pdh, format.mintick), style=label.style_label_left, color=pdColor, textcolor=color.black)
            pdlLabel := label.new(bar_index + labelOffset, pdl, "PDL $" + str.tostring(pdl, format.mintick), style=label.style_label_left, color=pdColor, textcolor=color.black)
 
        if showPM and not na(pmHigh)
            pmhLabel := label.new(bar_index + labelOffset, pmHigh, "PMH $" + str.tostring(pmHigh, format.mintick), style=label.style_label_left, color=pmColor, textcolor=color.black)
            pmlLabel := label.new(bar_index + labelOffset, pmLow, "PML $" + str.tostring(pmLow, format.mintick), style=label.style_label_left, color=pmColor, textcolor=color.black)
 
        if showPW and not na(pwh)
            pwhLabel := label.new(bar_index + labelOffset, pwh, "PWH $" + str.tostring(pwh, format.mintick), style=label.style_label_left, color=pwColor, textcolor=color.white)
            pwlLabel := label.new(bar_index + labelOffset, pwl, "PWL $" + str.tostring(pwl, format.mintick), style=label.style_label_left, color=pwColor, textcolor=color.white)
 
// ==========================================
// DASHBOARD HELPERS
// ==========================================
 
dashPos = switch dashPosition
    "Top Left" => position.top_left
    "Bottom Right" => position.bottom_right
    "Bottom Left" => position.bottom_left
    => position.top_right
 
dashSize = switch dashTextSize
    "Tiny" => size.tiny
    "Normal" => size.normal
    => size.small
 
bgDark = color.rgb(15, 15, 20)
bgHeader = color.rgb(50, 40, 15)
bgRow = color.rgb(28, 28, 35)
gold = color.rgb(255, 205, 55)
green = color.lime
red = color.rgb(255, 85, 85)
 
// Distance is level minus current price.
// Positive means the level is above price.
f_distance(float level) =>
    level - close
 
f_percent(float level) =>
    close != 0 ? (level - close) / close * 100 : na
 
f_price(float value) =>
    na(value) ? "N/A" : "$" + str.tostring(value, format.mintick)
 
f_distText(float level) =>
    if na(level)
        "N/A"
    else
        d = f_distance(level)
        (d > 0 ? "+" : "") + "$" + str.tostring(d, "#.00")
 
f_pctText(float level) =>
    if na(level)
        "N/A"
    else
        p = f_percent(level)
        (p > 0 ? "+" : "") + str.tostring(p, "#.00") + "%"
 
f_status(float level) =>
    na(level) ? "N/A" : close > level ? "ABOVE" : close < level ? "BELOW" : "AT LEVEL"
 
f_statusColor(float level) =>
    na(level) ? color.gray : close > level ? green : close < level ? red : gold
 
// ==========================================
// DASHBOARD TABLE
// ==========================================
 
var table dash = table.new(dashPos, 5, 9, bgcolor=bgDark, border_width=1, border_color=color.rgb(90, 75, 35))
 
f_row(int row, string name, float level, color levelColor, bool enabled) =>
    if enabled
        table.cell(dash, 0, row, name, text_color=levelColor, bgcolor=bgRow, text_size=dashSize)
        table.cell(dash, 1, row, f_price(level), text_color=color.white, bgcolor=bgRow, text_size=dashSize)
        table.cell(dash, 2, row, f_distText(level), text_color=f_statusColor(level), bgcolor=bgRow, text_size=dashSize)
        table.cell(dash, 3, row, f_pctText(level), text_color=f_statusColor(level), bgcolor=bgRow, text_size=dashSize)
        table.cell(dash, 4, row, f_status(level), text_color=f_statusColor(level), bgcolor=bgRow, text_size=dashSize)
    else
        for col = 0 to 4
            table.cell(dash, col, row, "", bgcolor=bgDark)
 
if barstate.islast
    table.clear(dash, 0, 0, 4, 8)
 
    if showDashboard
        table.cell(dash, 0, 0, "THE TRADER DESK", text_color=gold, bgcolor=bgHeader, text_size=dashSize)
        table.cell(dash, 1, 0, "KEY LEVELS", text_color=gold, bgcolor=bgHeader, text_size=dashSize)
        table.cell(dash, 2, 0, "LIVE", text_color=green, bgcolor=bgHeader, text_size=dashSize)
        table.cell(dash, 3, 0, syminfo.ticker, text_color=color.white, bgcolor=bgHeader, text_size=dashSize)
        table.cell(dash, 4, 0, f_price(close), text_color=gold, bgcolor=bgHeader, text_size=dashSize)
 
        table.cell(dash, 0, 1, "LEVEL", text_color=gold, bgcolor=bgDark, text_size=dashSize)
        table.cell(dash, 1, 1, "PRICE", text_color=gold, bgcolor=bgDark, text_size=dashSize)
        table.cell(dash, 2, 1, "DIST $", text_color=gold, bgcolor=bgDark, text_size=dashSize)
        table.cell(dash, 3, 1, "DIST %", text_color=gold, bgcolor=bgDark, text_size=dashSize)
        table.cell(dash, 4, 1, "POSITION", text_color=gold, bgcolor=bgDark, text_size=dashSize)
 
        f_row(2, "PDH", pdh, pdColor, showPD)
        f_row(3, "PDL", pdl, pdColor, showPD)
        f_row(4, "PMH", pmHigh, pmColor, showPM)
        f_row(5, "PML", pmLow, pmColor, showPM)
        f_row(6, "PWH", pwh, pwColor, showPW)
        f_row(7, "PWL", pwl, pwColor, showPW)
 
        table.cell(dash, 0, 8, "TTD", text_color=gold, bgcolor=bgHeader, text_size=dashSize)
        table.cell(dash, 1, 8, "DISCIPLINE", text_color=color.white, bgcolor=bgHeader, text_size=dashSize)
        table.cell(dash, 2, 8, "PATIENCE", text_color=color.white, bgcolor=bgHeader, text_size=dashSize)
        table.cell(dash, 3, 8, "TECHNICAL", text_color=color.white, bgcolor=bgHeader, text_size=dashSize)
        table.cell(dash, 4, 8, "ANALYSIS", text_color=color.white, bgcolor=bgHeader, text_size=dashSize)
 
// ==========================================
// ALERTS
// ==========================================
 
alertcondition(bullBreak, title="TTD Bullish Breakout", message="TTD Bullish Breakout: {{ticker}} at {{close}}")
 
alertcondition(bearBreak, title="TTD Bearish Breakout", message="TTD Bearish Breakout: {{ticker}} at {{close}}")

← 返回列表