Prop Firm Risk Dashboard [Drawdown & Consistency]

aguus_ · study · 252 行 · 点赞 22 · 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/
// © agus83
 
//@version=6
indicator("Prop Firm Risk Dashboard", "PF Risk", overlay = true, max_lines_count = 10, max_labels_count = 10, max_boxes_count = 4)
 
// ═════════════════════════════════════════════════════════════════════════════
// INPUTS
// ═════════════════════════════════════════════════════════════════════════════
grpA = "① Account — update after each session"
acctName    = input.string("50K Account", "Account name",                    group = grpA, display = display.none)
startBal    = input.float(50000, "Starting balance",                         group = grpA, display = display.none)
curBal      = input.float(50000, "Current balance (closed trades)",          group = grpA, display = display.none)
peakBal     = input.float(50000, "Highest balance reached",                  group = grpA, display = display.none,
     tooltip = "EOD trailing: your highest end-of-day balance.\nIntraday trailing: your highest balance at any moment.\nStatic: ignored.")
todayPnl    = input.float(0,     "Today's closed P&L",                       group = grpA, display = display.none)
 
grpR = "② Your firm's rules"
ddType      = input.string("EOD trailing", "Drawdown type", options = ["EOD trailing", "Intraday trailing", "Static"], group = grpR, display = display.none)
maxDD       = input.float(2000, "Max drawdown ($)", minval = 0,              group = grpR, display = display.none)
useLock     = input.bool(true,  "Drawdown stops trailing at start +", inline = "lock", group = grpR, display = display.none)
lockOffset  = input.float(100,  "",                                   inline = "lock", group = grpR, display = display.none)
useDaily    = input.bool(false, "Daily loss limit ($)",               inline = "dl",   group = grpR, display = display.none)
dailyLim    = input.float(1000, "", minval = 0,                       inline = "dl",   group = grpR, display = display.none)
useCons     = input.bool(true,  "Consistency: best day ≤ % of profit", inline = "c",   group = grpR, display = display.none)
consPct     = input.float(50,   "", minval = 1, maxval = 99,          inline = "c",    group = grpR, display = display.none)
payoutTgt   = input.float(0,    "Payout profit target ($, 0 = hide)",           group = grpR, display = display.none)
 
grpC = "③ Payout cycle"
bestDay     = input.float(0,    "Best single day profit",                       group = grpC, display = display.none)
cycleProfit = input.float(0,    "Total profit this cycle (before today)",       group = grpC, display = display.none)
 
grpP = "④ Live position (optional)"
contracts   = input.int(1,      "Contracts", minval = 1,                        group = grpP, display = display.none)
inPos       = input.string("None", "Open position", options = ["None", "Long", "Short"], group = grpP, display = display.none)
entryPx     = input.price(0,    "Entry price",                                  group = grpP, display = display.none,
     tooltip = "Set it once, then drag the entry line on the chart to adjust.")
ptValueIn   = input.float(0,    "Point value override ($, 0 = auto)", minval = 0, group = grpP, display = display.none)
 
grpD = "⑤ Display"
showLines   = input.bool(true,  "Show breach levels",                            group = grpD, display = display.none)
showZones   = input.bool(true,  "Shade breach zones",                            group = grpD, display = display.none)
tblPos      = input.string("Top right", "Panel position", options = ["Top right", "Bottom right", "Top left", "Bottom left", "Middle right"], group = grpD, display = display.none)
textSize    = input.string("Small", "Panel size", options = ["Tiny", "Small", "Normal"], group = grpD, display = display.none)
theme       = input.string("Dark", "Theme", options = ["Dark", "Light"],        group = grpD, display = display.none)
 
// ═════════════════════════════════════════════════════════════════════════════
// THEME
// ═════════════════════════════════════════════════════════════════════════════
dark   = theme == "Dark"
cBg    = dark ? #131722 : #ffffff
cBg2   = dark ? #1c2030 : #f3f4f6
cLine  = dark ? #2a2e39 : #e0e3eb
cTxt   = dark ? #d1d4dc : #131722
cMuted = dark ? #787b86 : #6a6d78
cGreen = #089981
cAmber = #f59e0b
cRed   = #f23645
 
sz   = switch textSize
    "Tiny"  => size.tiny
    "Small" => size.small
    => size.normal
szHero = switch textSize
    "Tiny"  => size.normal
    "Small" => size.large
    => size.huge
 
pos = switch tblPos
    "Top right"    => position.top_right
    "Bottom right" => position.bottom_right
    "Top left"     => position.top_left
    "Middle right" => position.middle_right
    => position.bottom_left
 
// ═════════════════════════════════════════════════════════════════════════════
// HELPERS
// ═════════════════════════════════════════════════════════════════════════════
usd(float v) =>
    na(v) ? "—" : (v < 0 ? "-$" : "$") + str.tostring(math.abs(v), "#,##0")
 
px(float v) =>
    na(v) ? "—" : str.tostring(v, "#,##0.00")
 
bar(float frac, int n) =>
    f = math.max(math.min(frac, 1.0), 0.0)
    full = int(math.round(f * n))
    s = ""
    for i = 1 to n
        s += i <= full ? "█" : "░"
    s
 
levelColor(float frac) =>
    frac <= 0.25 ? cRed : frac <= 0.6 ? cAmber : cGreen
 
// ═════════════════════════════════════════════════════════════════════════════
// CALCULATIONS
// ═════════════════════════════════════════════════════════════════════════════
ptValue = ptValueIn > 0 ? ptValueIn : syminfo.pointvalue
hasPos  = inPos != "None" and entryPx > 0
unreal  = hasPos ? (inPos == "Long" ? close - entryPx : entryPx - close) * ptValue * contracts : 0.0
liveBal = curBal + unreal
 
float threshold = na
if ddType == "Static"
    threshold := startBal - maxDD
else
    peakUsed = ddType == "Intraday trailing" ? math.max(peakBal, curBal, liveBal) : math.max(peakBal, curBal)
    threshold := peakUsed - maxDD
    if useLock
        threshold := math.min(threshold, startBal + lockOffset)
 
roomDD    = liveBal - threshold
roomDaily = useDaily ? dailyLim + todayPnl + unreal : na
dailyIsTighter = useDaily and roomDaily < roomDD
room      = dailyIsTighter ? roomDaily : roomDD
refLimit  = dailyIsTighter ? dailyLim : maxDD
frac      = refLimit > 0 ? room / refLimit : 1.0
roomPts   = ptValue > 0 ? math.max(room, 0) / (ptValue * contracts) : na
 
longBreach  = close - roomPts
shortBreach = close + roomPts
 
status = frac <= 0 ? "BREACHED" : frac <= 0.25 ? "DANGER" : frac <= 0.6 ? "CAUTION" : "SAFE"
cStat  = levelColor(frac)
 
dayNow    = todayPnl + unreal
totalNow  = cycleProfit + dayNow
bestNow   = math.max(bestDay, dayNow)
consNow   = totalNow > 0 ? bestNow / totalNow * 100 : na
k         = consPct / 100
maxToday  = cycleProfit > 0 ? k * cycleProfit / (1 - k) : na
needMore  = bestNow > 0 ? math.max(bestNow / k - totalNow, 0) : 0.0
consOk    = na(consNow) or consNow <= consPct
 
// ═════════════════════════════════════════════════════════════════════════════
// PANEL
// ═════════════════════════════════════════════════════════════════════════════
var table t = table.new(pos, 2, 22, bgcolor = cBg, frame_color = cLine, frame_width = 1, border_color = cBg, border_width = 0)
 
kv(int r, string key, string val, color c) =>
    table.cell(t, 0, r, key, text_color = cMuted, text_size = sz, text_halign = text.align_left)
    table.cell(t, 1, r, val, text_color = c,      text_size = sz, text_halign = text.align_right)
 
section(int r, string title) =>
    table.cell(t, 0, r, title, text_color = cMuted, text_size = size.tiny, text_halign = text.align_left, bgcolor = cBg2)
    table.cell(t, 1, r, "",    bgcolor = cBg2)
    table.merge_cells(t, 0, r, 1, r)
 
full(int r, string txt, color c, string s, color bgc) =>
    table.cell(t, 0, r, txt, text_color = c, text_size = s, text_halign = text.align_left, bgcolor = bgc)
    table.cell(t, 1, r, "", bgcolor = bgc)
    table.merge_cells(t, 0, r, 1, r)
 
if barstate.islast
    table.clear(t, 0, 0, 1, 21)
    r = 0
    // Header: account + status badge
    table.cell(t, 0, r, acctName, text_color = cTxt, text_size = sz, text_halign = text.align_left)
    table.cell(t, 1, r, "● " + status, text_color = color.white, text_size = sz, bgcolor = cStat, text_halign = text.align_center)
    r += 1
    // Hero number
    full(r, "ROOM BEFORE BREACH", cMuted, size.tiny, cBg)
    r += 1
    table.cell(t, 0, r, usd(room), text_color = cStat, text_size = szHero, text_halign = text.align_left)
    table.cell(t, 1, r, str.tostring(roomPts, "#,##0.0") + " pts\n" + str.tostring(contracts) + " × " + syminfo.ticker, text_color = cTxt, text_size = sz, text_halign = text.align_right)
    r += 1
    full(r, bar(1 - frac, 20) + "  " + str.tostring(math.max(math.min((1 - frac) * 100, 100), 0), "0") + "% used", cStat, sz, cBg)
    r += 1
    full(r, "Limited by " + (dailyIsTighter ? "daily loss limit" : "max drawdown"), cMuted, size.tiny, cBg)
    r += 1
    if ddType != "Static" and peakBal < startBal
        full(r, "⚠ Check inputs: highest balance is below starting balance", cAmber, size.tiny, cBg)
        r += 1
 
    section(r, "DRAWDOWN · " + str.upper(ddType))
    r += 1
    kv(r, "Balance" + (hasPos ? " (live)" : ""), usd(liveBal), cTxt)
    r += 1
    kv(r, "Breach at", usd(threshold), cTxt)
    r += 1
    if useDaily
        kv(r, "Daily loss left", usd(roomDaily), levelColor(dailyLim > 0 ? roomDaily / dailyLim : 1.0))
        r += 1
 
    if hasPos
        section(r, "YOUR POSITION · " + str.upper(inPos) + " " + str.tostring(contracts) + " @ " + px(entryPx))
        r += 1
        kv(r, "Open P&L", usd(unreal), unreal >= 0 ? cGreen : cRed)
        r += 1
        kv(r, "Account blown at", px(inPos == "Long" ? longBreach : shortBreach), cRed)
        r += 1
    else
        section(r, "IF YOU ENTER NOW")
        r += 1
        kv(r, "Long: account blown at", px(longBreach), cRed)
        r += 1
        kv(r, "Short: account blown at", px(shortBreach), cRed)
        r += 1
 
    if useCons
        section(r, "CONSISTENCY · MAX " + str.tostring(consPct, "0") + "%")
        r += 1
        kv(r, "Best day share", na(consNow) ? "—" : str.tostring(consNow, "0.0") + "%", consOk ? cGreen : cRed)
        r += 1
        kv(r, "Max profit today", usd(maxToday), cTxt)
        r += 1
        if needMore > 0
            kv(r, "Profit still needed", usd(needMore), cAmber)
            r += 1
 
    if payoutTgt > 0
        pfrac = totalNow / payoutTgt
        section(r, "PAYOUT")
        r += 1
        full(r, bar(pfrac, 20) + "  " + usd(totalNow) + " / " + usd(payoutTgt), pfrac >= 1 ? cGreen : cTxt, sz, cBg)
        r += 1
 
// ═════════════════════════════════════════════════════════════════════════════
// CHART LEVELS
// ═════════════════════════════════════════════════════════════════════════════
var line  lnL = na
var line  lnS = na
var line  lnE = na
var label lbL = na
var label lbS = na
var box   bxL = na
var box   bxS = na
 
if barstate.islast
    line.delete(lnL), line.delete(lnS), line.delete(lnE), label.delete(lbL), label.delete(lbS), box.delete(bxL), box.delete(bxS)
    if showLines and not na(roomPts)
        x1 = bar_index - 30
        x2 = bar_index + 15
        if inPos != "Short"
            lnL := line.new(x1, longBreach, x2, longBreach, color = cRed, width = 2)
            lbL := label.new(x2, longBreach, "Account blown (long)  " + px(longBreach), style = label.style_label_left, color = cRed, textcolor = color.white, size = size.small)
            if showZones
                bxL := box.new(x1, longBreach, x2, longBreach - roomPts * 0.15, border_color = na, bgcolor = color.new(cRed, 85))
        if inPos != "Long"
            lnS := line.new(x1, shortBreach, x2, shortBreach, color = cRed, width = 2)
            lbS := label.new(x2, shortBreach, "Account blown (short)  " + px(shortBreach), style = label.style_label_left, color = cRed, textcolor = color.white, size = size.small)
            if showZones
                bxS := box.new(x1, shortBreach + roomPts * 0.15, x2, shortBreach, border_color = na, bgcolor = color.new(cRed, 85))
        if hasPos
            lnE := line.new(x1, entryPx, x2, entryPx, color = cMuted, style = line.style_dotted)
 
plot(room,    "Room to breach ($)",   display = display.data_window)
plot(roomPts, "Room to breach (pts)", display = display.data_window)
 
alertcondition(frac <= 0.25, "Danger: 75% of limit used", "Prop Firm Risk: 75% of your loss limit is used")
alertcondition(frac <= 0,   "Limit breached", "Prop Firm Risk: loss limit reached")

← 返回列表