本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Prop Firm Rules Overlay by Puravida Edge
// Open-source indicator. Draws the prices at which a position hits the daily loss limit and the drawdown floor
// of a prop firm account, for both a long and a short entry, and shows a consistency-rule counter.
// TradingView cannot read your account, so account figures are entered in the Inputs window.
indicator("Prop Firm Rules Overlay by Puravida Edge", shorttitle = "PF Rules", overlay = true, max_lines_count = 10, max_labels_count = 10)
// ---------- Inputs ----------
string grpA = "Account"
string ddModel = input.string("Trailing EOD", "Drawdown model", options = ["Trailing EOD", "Static"], group = grpA, confirm = true, tooltip = "Trailing EOD: floor follows the highest end-of-day balance and locks once it reaches the starting balance. Static: floor fixed below the starting balance.")
float startBal = input.float(100000, "Starting balance (USD)", minval = 0, step = 1000, group = grpA, confirm = true)
float maxDD = input.float(3000, "Max drawdown (USD)", minval = 0, step = 100, group = grpA, confirm = true)
float dailyLim = input.float(0, "Daily loss limit (USD, 0 = none)", minval = 0, step = 100, group = grpA, confirm = true)
string grpS = "Current state"
float balEod = input.float(100000, "Balance at last end of day (USD)", step = 100, group = grpS, confirm = true)
float peakEod = input.float(100000, "Highest end-of-day balance (USD, trailing only)", step = 100, group = grpS, confirm = true)
float realizedToday = input.float(0, "Realized P&L today (USD)", step = 50, group = grpS)
string grpP = "Position"
float entryPrice = input.price(0, "Entry price", group = grpP, confirm = true, tooltip = "Click the last bar of the chart, then fill in your prop account details in Inputs.")
float qty = input.float(1, "Size (contracts or lots)", minval = 0, step = 0.01, group = grpP)
float pvOverride = input.float(0, "Value of 1 point for 1 unit (USD, 0 = from symbol)", minval = 0, step = 0.01, group = grpP, tooltip = "Leave 0 for futures. For CFD or forex enter the USD value of a 1-point move for 1 lot.")
string grpC = "Consistency rule"
float consPct = input.float(0, "Max share of one day in total profit (%, 0 = off)", minval = 0, maxval = 100, step = 1, group = grpC)
float totalProfit = input.float(0, "Total profit since start (USD)", step = 100, group = grpC)
float bestDay = input.float(0, "Largest single day so far (USD)", step = 100, group = grpC)
string grpD = "Display"
string tblPos = input.string("Top right", "Table position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = grpD)
bool showLines = input.bool(true, "Show price lines", group = grpD)
// ---------- Calculations ----------
float pointValue = pvOverride > 0 ? pvOverride : syminfo.pointvalue
float effEntry = entryPrice > 0 ? entryPrice : close
bool hasPos = qty > 0 and pointValue > 0
float perPoint = hasPos ? qty * pointValue : na
// Drawdown floor
float floorTrailing = math.min(math.max(peakEod, startBal) - maxDD, startBal)
float floorLevel = ddModel == "Static" ? startBal - maxDD : floorTrailing
// Account state (closed P&L only; open position shown as price levels)
float equityNow = balEod + realizedToday
float distFloor = equityNow - floorLevel
float dailyLeft = dailyLim > 0 ? dailyLim + realizedToday : na
// Loss in USD the position may still take before each limit
float lossToDaily = dailyLim > 0 ? dailyLim + realizedToday : na
float lossToFloor = distFloor
// Prices: long below entry, short above entry
float dailyLong = hasPos and not na(lossToDaily) and lossToDaily > 0 ? effEntry - lossToDaily / perPoint : na
float dailyShort = hasPos and not na(lossToDaily) and lossToDaily > 0 ? effEntry + lossToDaily / perPoint : na
float floorLong = hasPos and lossToFloor > 0 ? effEntry - lossToFloor / perPoint : na
float floorShort = hasPos and lossToFloor > 0 ? effEntry + lossToFloor / perPoint : na
// Consistency rule
float consFrac = consPct / 100.0
float consShare = totalProfit > 0 ? bestDay / totalProfit * 100.0 : na
bool consBreached = consPct > 0 and totalProfit > 0 and bestDay > consFrac * totalProfit
float consRoom = consPct > 0 and consFrac < 1 ? consFrac * totalProfit / (1 - consFrac) : na
// ---------- Colors ----------
color cOk = color.new(color.green, 70)
color cWarn = color.new(color.yellow, 70)
color cBad = color.new(color.red, 70)
color cNeu = color.new(color.gray, 80)
colorFor(float remaining, float limit) =>
na(remaining) or na(limit) or limit <= 0 ? cNeu : remaining <= 0 ? cBad : remaining / limit < 0.25 ? cWarn : cOk
color cFloor = colorFor(distFloor, maxDD)
color cDaily = colorFor(dailyLeft, dailyLim)
color cCons = consPct == 0 ? cNeu : consBreached ? cBad : na(consShare) ? cNeu : consShare / consPct > 0.75 ? cWarn : cOk
// ---------- Formatting ----------
usd(float v) => na(v) ? "n/a" : (v < 0 ? "-" : "") + "$" + str.tostring(math.abs(v), "#,###")
px(float v) => na(v) ? "n/a" : str.tostring(v, format.mintick)
// ---------- Lines ----------
var line lnDL = na
var line lnDS = na
var line lnFL = na
var line lnFS = na
var label lbDL = na
var label lbDS = na
var label lbFL = na
var label lbFS = na
drawLevel(line ln, label lb, float price, string txt, color col, string sty, int w) =>
line l = ln
label b = lb
if showLines and not na(price)
if na(l)
l := line.new(bar_index - 50, price, bar_index, price, extend = extend.right, color = col, style = sty, width = w)
b := label.new(bar_index, price, "", style = label.style_label_left, color = color.new(col, 20), textcolor = color.white, size = size.small)
line.set_xy1(l, bar_index - 50, price)
line.set_xy2(l, bar_index, price)
label.set_xy(b, bar_index, price)
label.set_text(b, txt + " " + px(price))
else
if not na(l)
line.delete(l)
label.delete(b)
l := na
b := na
[l, b]
if barstate.islast
[l1, b1] = drawLevel(lnDL, lbDL, dailyLong, "Long: daily limit", color.orange, line.style_dashed, 1)
[l2, b2] = drawLevel(lnDS, lbDS, dailyShort, "Short: daily limit", color.orange, line.style_dashed, 1)
[l3, b3] = drawLevel(lnFL, lbFL, floorLong, "Long: drawdown floor", color.red, line.style_solid, 2)
[l4, b4] = drawLevel(lnFS, lbFS, floorShort, "Short: drawdown floor", color.red, line.style_solid, 2)
lnDL := l1, lbDL := b1
lnDS := l2, lbDS := b2
lnFL := l3, lbFL := b3
lnFS := l4, lbFS := b4
// ---------- Table ----------
tablePosition(string p) =>
switch p
"Top left" => position.top_left
"Bottom right" => position.bottom_right
"Bottom left" => position.bottom_left
=> position.top_right
var table tbl = table.new(tablePosition(tblPos), 2, 10, border_width = 1, border_color = color.new(color.gray, 60), frame_color = color.new(color.gray, 60), frame_width = 1)
cell(int row, string k, string v, color bg) =>
table.cell(tbl, 0, row, k, text_color = color.white, text_size = size.small, bgcolor = bg, text_halign = text.align_left)
table.cell(tbl, 1, row, v, text_color = color.white, text_size = size.small, bgcolor = bg, text_halign = text.align_right)
if barstate.islast
string modelTxt = ddModel == "Static" ? "static" : "trailing EOD"
cell(0, "Prop Firm Rules Overlay", modelTxt, color.new(color.black, 20))
cell(1, "Balance now (closed)", usd(equityNow), cNeu)
cell(2, "Drawdown floor", usd(floorLevel) + " room " + usd(distFloor), cFloor)
cell(3, "Floor hit: long / short", px(floorLong) + " / " + px(floorShort), cFloor)
cell(4, "Daily limit left", dailyLim > 0 ? usd(dailyLeft) + " of " + usd(dailyLim) : "off", cDaily)
cell(5, "Daily limit hit: long / short", dailyLim > 0 ? px(dailyLong) + " / " + px(dailyShort) : "off", cDaily)
cell(6, "Size", str.tostring(qty) + " x $" + str.tostring(pointValue) + " per point", cNeu)
cell(7, "Consistency: largest day", consPct > 0 ? (na(consShare) ? "n/a" : str.tostring(consShare, "#.#") + "% of profit (max " + str.tostring(consPct, "#") + "%)") : "off", cCons)
cell(8, "Max profit today for rule", consPct > 0 ? usd(consRoom) : "off", cCons)