Prop Firm Daily Loss Guard by Puravida Edge

PuraVidaEdge · study · 114 行 · 点赞 4 · TradingView 原页

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

Pine Script

//@version=6
// Prop Firm Daily Loss Guard by Puravida Edge
// Open-source indicator. Tracks how much of a daily loss limit is used today,
// including the floating P&L of one open position, and shows the price at which
// that position would exhaust the limit. Account data is entered manually.
// It does not place orders and gives no trade signals.
indicator("Prop Firm Daily Loss Guard by Puravida Edge", shorttitle="PF Daily Loss Guard", overlay=true, max_lines_count=5, max_labels_count=5)
 
// ───────── Inputs ─────────
GRP_MAIN = "1. Daily limit (edit these)"
GRP_POS  = "2. Open position (optional)"
GRP_INST = "3. Instrument"
GRP_VIEW = "4. Display & alerts"
 
dailyLimit = input.float(1000, "Daily loss limit ($)", minval=1, step=50, confirm=true, group=GRP_MAIN)
pnlToday   = input.float(0, "Realized P&L today ($, loss negative)", step=10, confirm=true, group=GRP_MAIN)
 
hasPos    = input.bool(false, "Open position on chart", group=GRP_POS)
entryPx   = input.price(0, "Entry price (click the chart)", confirm=true, group=GRP_POS)
posDir    = input.string("Long", "Direction", options=["Long", "Short"], group=GRP_POS)
posSize   = input.int(1, "Size (contracts)", minval=1, group=GRP_POS)
 
instr     = input.string("Auto (chart)", "Instrument", options=["Auto (chart)", "MNQ", "NQ", "MES", "ES", "MGC", "GC", "MYM", "YM", "M2K", "RTY", "MCL", "CL", "Custom"], group=GRP_INST)
customPV  = input.float(1.0, "Custom $ per point (Custom only)", minval=0.0001, group=GRP_INST)
 
showLine  = input.bool(true, "Show limit-hit price line", group=GRP_VIEW)
tblPos    = input.string("Top right", "Table position", options=["Top right", "Top left", "Bottom right", "Bottom left"], group=GRP_VIEW)
warnPct   = input.float(75, "Warning level (% of limit used)", minval=1, maxval=99, step=5, group=GRP_VIEW)
 
// ───────── Helpers ─────────
pointValueOf(s) =>
    switch s
        "MNQ" => 2.0
        "NQ"  => 20.0
        "MES" => 5.0
        "ES"  => 50.0
        "MGC" => 10.0
        "GC"  => 100.0
        "MYM" => 0.5
        "YM"  => 5.0
        "M2K" => 5.0
        "RTY" => 50.0
        "MCL" => 100.0
        "CL"  => 1000.0
        "Custom" => customPV
        => syminfo.pointvalue
 
tablePosition(p) =>
    switch p
        "Top left"     => position.top_left
        "Bottom right" => position.bottom_right
        "Bottom left"  => position.bottom_left
        => position.top_right
 
// ───────── Calculation ─────────
pv       = pointValueOf(instr)
dir      = posDir == "Long" ? 1 : -1
posOpen  = hasPos and entryPx > 0 and posSize > 0
 
// Realized remaining: limit reduced by today's realized loss (profit adds room)
remReal  = dailyLimit + pnlToday
openPnl  = posOpen ? (close - entryPx) * dir * posSize * pv : 0.0
remTotal = remReal + openPnl
usedPct  = math.max(0.0, (dailyLimit - remTotal) / dailyLimit * 100.0)
 
// Price at which the open position exhausts the remaining limit
hitPx    = posOpen and remReal > 0 ? entryPx - dir * remReal / (posSize * pv) : na
distPts  = posOpen and not na(hitPx) ? math.abs(close - hitPx) : na
 
zoneCol  = usedPct >= 100 ? color.red : usedPct >= warnPct ? color.orange : usedPct >= 50 ? color.yellow : color.teal
 
// ───────── Alerts (only indicator in the series with alerts) ─────────
warnHit  = usedPct >= warnPct and usedPct[1] < warnPct
limitHit = usedPct >= 100 and usedPct[1] < 100
alertcondition(warnHit,  "Warning level reached", "Daily Loss Guard: warning level of the daily limit used")
alertcondition(limitHit, "Daily limit reached",   "Daily Loss Guard: 100% of the daily limit used")
 
// ───────── Drawing ─────────
var table t   = table.new(position.top_right, 2, 7, border_width=1, frame_color=color.new(color.gray, 60), border_color=color.new(color.gray, 70))
var line  ln  = na
var label lb  = na
 
if barstate.islast
    line.delete(ln)
    label.delete(lb)
    if showLine and posOpen and not na(hitPx)
        ln := line.new(bar_index - 10, hitPx, bar_index + 10, hitPx, color=color.new(color.red, 0), style=line.style_dashed, width=2)
        lb := label.new(bar_index + 10, hitPx, "Limit hit " + str.tostring(hitPx, format.mintick), style=label.style_label_left, color=color.new(color.red, 80), textcolor=color.red, size=size.small)
 
    table.delete(t)
    t := table.new(tablePosition(tblPos), 2, 7, border_width=1, frame_color=color.new(color.gray, 60), border_color=color.new(color.gray, 70))
    txtCol = color.new(color.gray, 0)
    bgCol  = color.new(color.gray, 90)
 
    // bar: 10 blocks, filled by % used
    blocks = math.min(10, math.round(usedPct / 10))
    bar = ""
    for i = 1 to 10
        bar += i <= blocks ? "█" : "░"
 
    table.cell(t, 0, 0, "Daily Loss Guard", text_color=txtCol, bgcolor=bgCol, text_size=size.small)
    table.cell(t, 1, 0, syminfo.ticker, text_color=txtCol, bgcolor=bgCol, text_size=size.small)
    table.cell(t, 0, 1, "Limit used", text_color=color.white, bgcolor=color.new(zoneCol, 20), text_size=size.normal)
    table.cell(t, 1, 1, bar + " " + str.tostring(math.min(usedPct, 999), "#") + "%", text_color=color.white, bgcolor=color.new(zoneCol, 20), text_size=size.normal)
    table.cell(t, 0, 2, "Left today", text_color=txtCol, text_size=size.small)
    table.cell(t, 1, 2, "$" + str.tostring(remTotal, "#.##"), text_color=txtCol, text_size=size.small)
    table.cell(t, 0, 3, "Realized today", text_color=txtCol, text_size=size.small)
    table.cell(t, 1, 3, "$" + str.tostring(pnlToday, "#.##"), text_color=txtCol, text_size=size.small)
    table.cell(t, 0, 4, "Open P&L", text_color=txtCol, text_size=size.small)
    table.cell(t, 1, 4, posOpen ? "$" + str.tostring(openPnl, "#.##") + " (" + str.tostring(posSize) + " " + posDir + ")" : "no position", text_color=txtCol, text_size=size.small)
    table.cell(t, 0, 5, "Limit-hit price", text_color=txtCol, text_size=size.small)
    table.cell(t, 1, 5, posOpen and not na(hitPx) ? str.tostring(hitPx, format.mintick) + " (" + str.tostring(distPts, format.mintick) + " pts away)" : "—", text_color=txtCol, text_size=size.small)
    table.cell(t, 0, 6, "Note", text_color=txtCol, text_size=size.tiny)
    table.cell(t, 1, 6, usedPct >= 100 ? "Daily limit reached" : usedPct >= warnPct ? "Warning level reached" : remReal <= 0 ? "Realized loss already at limit" : "OK", text_color=txtCol, text_size=size.tiny)

← 返回列表