Prop Firm Guard: Contract Sizing & Flatten Countdown

ClockworkFutures · study · 80 行 · 点赞 6 · 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/
// © Clockwork Futures
//@version=6
indicator("Prop Firm Guard — risk panel & flat-by-close timer", shorttitle="Prop Guard", overlay=true, max_labels_count=10)
 
// ───────────── Inputs ─────────────
grpRisk  = "Prop account rules"
dll      = input.float(1000, "Daily loss limit ($)", minval=0, group=grpRisk, tooltip="Your firm's daily loss limit. Set 0 if your account has none.")
maxDD    = input.float(2000, "Max drawdown remaining ($)", minval=0, group=grpRisk, tooltip="Distance from your current balance to the trailing / EOD drawdown floor.")
riskPct  = input.float(50, "Risk per trade (% of daily limit)", minval=1, maxval=100, group=grpRisk, tooltip="How much of the daily loss limit one losing trade may use.")
maxCts   = input.int(5, "Max contracts allowed by firm", minval=1, group=grpRisk)
 
grpStop  = "Stop size"
stopMode = input.string("ATR", "Stop based on", options=["ATR", "Ticks"], group=grpStop)
atrLen   = input.int(20, "ATR length (daily)", minval=1, group=grpStop)
atrMult  = input.float(0.4, "Stop = ATR x", minval=0.05, step=0.05, group=grpStop)
stopTks  = input.int(40, "Stop in ticks (if Ticks)", minval=1, group=grpStop)
 
grpTime  = "Flat-by-close"
tz       = input.string("America/New_York", "Time zone", group=grpTime)
flatH    = input.int(16, "Firm flatten hour", minval=0, maxval=23, group=grpTime, tooltip="Many futures prop firms force-flatten around 16:45–16:59 ET. Check your firm's rule.")
flatM    = input.int(45, "Firm flatten minute", minval=0, maxval=59, group=grpTime)
warnMin  = input.int(15, "Warn me this many minutes before", minval=1, group=grpTime)
shade    = input.bool(true, "Shade the chart in the warning window", group=grpTime)
 
grpUI    = "Display"
pos      = input.string("top_right", "Panel position", options=["top_right", "top_left", "bottom_right", "bottom_left"], group=grpUI)
 
// ───────────── Calculations ─────────────
dATR      = request.security(syminfo.tickerid, "D", ta.atr(atrLen)[1], lookahead=barmerge.lookahead_on)
ptValue   = syminfo.pointvalue
stopPts   = stopMode == "ATR" ? dATR * atrMult : stopTks * syminfo.mintick
stopUSD   = stopPts * ptValue
budget    = dll > 0 ? dll * riskPct / 100 : maxDD * riskPct / 100
ctsByRisk = stopUSD > 0 ? math.floor(budget / stopUSD) : 0
ctsAllowed= math.max(0, math.min(ctsByRisk, maxCts))
lossesToDLL = dll > 0 and ctsAllowed > 0 ? math.floor(dll / (stopUSD * ctsAllowed)) : na
 
// minutes until today's flatten time (in chosen time zone)
nowMin   = hour(timenow, tz) * 60 + minute(timenow, tz)
flatMin  = flatH * 60 + flatM
minsLeft = flatMin - nowMin
inWarn   = minsLeft >= 0 and minsLeft <= warnMin
pastFlat = minsLeft < 0 and minsLeft > -120
 
barMin   = hour(time, tz) * 60 + minute(time, tz)
barWarn  = barMin >= flatMin - warnMin and barMin < flatMin
bgcolor(shade and barWarn ? color.new(color.orange, 85) : na, title="Flat-by-close warning window")
bgcolor(shade and barMin >= flatMin and barMin < flatMin + 15 ? color.new(color.red, 85) : na, title="Past flatten time")
 
f_money(x) => "$" + str.tostring(x, "#,##0")
f_time(m) => m >= 60 ? str.tostring(math.floor(m / 60)) + "h " + str.tostring(m % 60) + "m" : str.tostring(m) + "m"
 
// ───────────── Panel ─────────────
var table t = table.new(pos == "top_right" ? position.top_right : pos == "top_left" ? position.top_left : pos == "bottom_right" ? position.bottom_right : position.bottom_left, 2, 8, bgcolor=color.new(#0d0d0d, 5), border_color=color.new(#3a3226, 0), border_width=1, frame_color=color.new(#d4a63a, 0), frame_width=1)
 
if barstate.islast
    gold = #d4a63a
    txt  = #f2efe8
    dim  = #a9a49a
    table.cell(t, 0, 0, "PROP FIRM GUARD", text_color=gold, text_size=size.small, text_halign=text.align_left)
    table.cell(t, 1, 0, syminfo.ticker, text_color=dim, text_size=size.small)
    table.cell(t, 0, 1, "Stop / contract", text_color=dim, text_size=size.small, text_halign=text.align_left)
    table.cell(t, 1, 1, f_money(stopUSD), text_color=txt, text_size=size.small)
    table.cell(t, 0, 2, "Risk budget / trade", text_color=dim, text_size=size.small, text_halign=text.align_left)
    table.cell(t, 1, 2, f_money(budget), text_color=txt, text_size=size.small)
    table.cell(t, 0, 3, "Max contracts", text_color=dim, text_size=size.small, text_halign=text.align_left)
    table.cell(t, 1, 3, ctsAllowed == 0 ? "0 — stop too wide" : str.tostring(ctsAllowed), text_color=ctsAllowed == 0 ? color.red : color.new(#7fd18b, 0), text_size=size.small)
    table.cell(t, 0, 4, "Losers to daily limit", text_color=dim, text_size=size.small, text_halign=text.align_left)
    table.cell(t, 1, 4, na(lossesToDLL) ? "—" : str.tostring(lossesToDLL), text_color=txt, text_size=size.small)
    table.cell(t, 0, 5, "Drawdown room", text_color=dim, text_size=size.small, text_halign=text.align_left)
    table.cell(t, 1, 5, f_money(maxDD), text_color=txt, text_size=size.small)
    flatTxt = pastFlat ? "PAST FLATTEN" : minsLeft >= 0 ? f_time(minsLeft) : "—"
    flatCol = pastFlat ? color.red : inWarn ? color.orange : txt
    table.cell(t, 0, 6, "Time to flatten", text_color=dim, text_size=size.small, text_halign=text.align_left)
    table.cell(t, 1, 6, flatTxt, text_color=flatCol, text_size=size.small)
    table.cell(t, 0, 7, "Clockwork Futures", text_color=color.new(dim, 30), text_size=size.tiny, text_halign=text.align_left)
    table.cell(t, 1, 7, "not advice", text_color=color.new(dim, 30), text_size=size.tiny)
 
alertcondition(inWarn, "Flatten warning", "Prop Guard: firm flatten time is coming up — check open positions.")

← 返回列表