本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator("ADR% and Position Size Table", shorttitle = "ADR Size", overlay = true)
// ── Inputs ─────────────────────────────────────────────────────────────
string GRP_ADR = "Average daily range"
int adrLen = input.int(20, "ADR length (days)", minval = 2, group = GRP_ADR)
string GRP_RISK = "Position sizing (your numbers)"
float account = input.float(10000.0, "Account size", minval = 1.0, group = GRP_RISK,
tooltip = "In the symbol's currency. Used only to size the example position; nothing is sent anywhere.")
float riskPct = input.float(0.5, "Risk per trade (% of account)", minval = 0.01, step = 0.05, group = GRP_RISK)
float stopMult = input.float(0.5, "Stop distance (× ADR%)", minval = 0.05, step = 0.05, group = GRP_RISK,
tooltip = "The stop is placed this multiple of the average daily range below the entry price.")
float maxPos = input.float(25.0, "Max position size (% of account)", minval = 1.0, maxval = 100.0, group = GRP_RISK)
bool wholeSh = input.bool(true, "Round shares down to whole numbers", group = GRP_RISK)
float entryIn = input.float(0.0, "Entry price (0 = last close)", minval = 0.0, group = GRP_RISK)
string GRP_SHOW = "Display"
bool showStop = input.bool(true, "Draw stop level", group = GRP_SHOW)
bool showTbl = input.bool(true, "Show table", group = GRP_SHOW)
// ── ADR% from daily bars, on any chart timeframe ───────────────────────
float adrRaw = request.security(syminfo.tickerid, "D", ta.sma(high / low, adrLen), lookahead = barmerge.lookahead_off)
float adrPct = (adrRaw - 1) * 100
// ── Sizing ─────────────────────────────────────────────────────────────
float entry = entryIn > 0 ? entryIn : close
float stopPct = adrPct * stopMult
float stopDist = entry * stopPct / 100
float stopPrice = entry - stopDist
float riskAmt = account * riskPct / 100
float sharesRisk = stopDist > 0 ? riskAmt / stopDist : na
float sharesCap = entry > 0 ? account * maxPos / 100 / entry : na
float sharesRaw = math.min(sharesRisk, sharesCap)
float shares = wholeSh ? math.floor(sharesRaw) : sharesRaw
float posValue = shares * entry
float posPct = account > 0 ? posValue / account * 100 : na
float realRisk = shares * stopDist
bool capped = sharesCap < sharesRisk
// ── Plots (Data Window and Pine Screener) ──────────────────────────────
plot(adrPct, "ADR %", display = display.data_window)
plot(stopPct, "Stop distance %", display = display.data_window)
plot(shares, "Shares", display = display.data_window)
plot(posPct, "Position % of account", display = display.data_window)
var line stopLine = na
if barstate.islast
line.delete(stopLine)
if showStop and not na(stopPrice)
stopLine := line.new(bar_index - 20, stopPrice, bar_index + 5, stopPrice, color = color.new(color.red, 20), style = line.style_dashed)
// ── Table ──────────────────────────────────────────────────────────────
var table tbl = table.new(position.bottom_right, 2, 9, bgcolor = color.new(color.gray, 85), border_width = 1)
row(int r, string label, string value, color clr = color.gray) =>
table.cell(tbl, 0, r, label, text_color = color.gray, text_size = size.small)
table.cell(tbl, 1, r, value, text_color = clr, text_size = size.small)
if showTbl and barstate.islast
string cur = syminfo.currency
row(0, "ADR% (" + str.tostring(adrLen) + "d)", na(adrPct) ? "–" : str.tostring(adrPct, "0.00") + "%", color.teal)
row(1, "Entry", str.tostring(entry, format.mintick) + " " + cur)
row(2, "Stop (" + str.tostring(stopMult, "0.##") + "× ADR)", na(stopPrice) ? "–" : str.tostring(stopPrice, format.mintick) + " (-" + str.tostring(stopPct, "0.00") + "%)", color.red)
row(3, "Risk / trade", str.tostring(riskAmt, "#,##0.00") + " " + cur + " (" + str.tostring(riskPct, "0.##") + "%)")
row(4, "Shares", na(shares) ? "–" : str.tostring(shares, wholeSh ? "#,##0" : "#,##0.####"), color.teal)
row(5, "Position value", na(posValue) ? "–" : str.tostring(posValue, "#,##0") + " " + cur)
row(6, "% of account", na(posPct) ? "–" : str.tostring(posPct, "0.0") + "%")
row(7, "Risk if stopped", na(realRisk) ? "–" : str.tostring(realRisk, "#,##0.00") + " " + cur)
row(8, "Limited by", capped ? "max position size" : "risk per trade", capped ? color.orange : color.gray)