本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// RCV Risk Manager 2.6
// Click an entry, a stop and a target on the chart (drag their lines to adjust), or let the entry follow the live price and
// the stop come from a fixed distance or the recent swing. The script sizes the position from the balance and risk with
// the contract detected automatically (or your broker's contract size), converts the point value into the account
// currency, and draws the plan with 1-4 take profits and an optional breakeven level. It plans; it does not track.
indicator("RCV Risk Manager", "RCV Risk", overlay = true, behind_chart = false, max_boxes_count = 10, max_lines_count = 20,
max_labels_count = 20)
// ── Fixed parameters ─────────────────────────────────────────────────────────────────────────────────────────────────
const float EPS = 1e-9
const int FIT_BARS = 200
const float FIT_STOP = 0.25
const int BOX_AHEAD = 10
enum TargetMode
price = "Clicked"
rMultiple = "R Multiple"
enum EntryMode
clicked = "Clicked"
live = "Live"
enum StopMode
clicked = "Clicked"
fixed = "Fixed"
swing = "Swing"
enum CloseMode
equal = "Equal"
custom = "Custom %"
enum BeMode
off = "Off"
tp1 = "After TP1"
level = "At R Level"
enum Ccy
usd = "USD"
eur = "EUR"
gbp = "GBP"
jpy = "JPY"
aud = "AUD"
cad = "CAD"
chf = "CHF"
nzd = "NZD"
enum RiskMode
pct = "Percent"
cash = "Amount"
enum SizeUnit
auto = "Auto"
lots = "Lots"
contracts = "Contracts"
enum Theme
light = "Light"
dark = "Dark"
custom = "Custom"
enum Psize
mobile = "Mobile"
small = "Small"
normal = "Normal"
large = "Large"
enum Ppos
tl = "Top Left"
tc = "Top Center"
tr = "Top Right"
ml = "Middle Left"
mr = "Middle Right"
bl = "Bottom Left"
bc = "Bottom Center"
br = "Bottom Right"
const string G_T = "Plan"
const string G_A = "Account"
const string G_M = "Take Profits"
const string G_I = "Instrument"
const string G_D = "Chart"
const string G_P = "Panel"
const string G_R = "Panel Rows"
// Each Plan row's tooltip is also TradingView's prompt while you place that point, so it starts with what to click
const string TIP_EN = "Click your entry. Drag its line to move it. Live: the entry follows price."
const string TIP_SM = "Click your stop. Below the entry = long, above = short. Fixed or Swing: set from the boxes below."
const string TIP_TG = "Click your target. R Multiple: set from the R Multiple box below."
const string TIP_RM = "Target distance in stops (2 = twice the stop) when Target is R Multiple."
const string TIP_RK = "Percent of the balance or a cash amount. Size rounds down, so you never risk more."
const string TIP_HA = "Hides the balance and shows cash as a percent. Good for screenshots."
const string TIP_TP = "Targets spaced evenly up to the last one. Equal: same share at each. Custom: set TP1-TP3; the last target closes the rest."
const string TIP_BE = "Move the stop to entry after TP1 (needs 2+ targets) or at an R level."
const string TIP_SZ = "Auto picks lots, contracts or shares for the market. Step 0 = automatic."
const string TIP_CS = "Units per lot from your broker's specs. Index CFDs differ (US30 is 1 or 10). 0 = standard size."
const string TIP_RW = "Untick to hide a row. Details is the right-hand column."
const string TIP_TAG = "What the chart tags show."
const string TIP_PN = "Show the panel, its text size (tags too) and position."
const string TIP_LN = "Grid color and width, then frame width. 0 hides a line."
const string TIP_TH = "Light for white charts, Dark for dark charts, Custom for your own colors. Up/Down follow Profit / Loss."
// ── Inputs ───────────────────────────────────────────────────────────────────────────────────────────────────────────
// Price-only points: when the study is selected each one is a horizontal handle on its own line of the box
// Each level on one row: its clicked price, then where it comes from
float clickPx = math.round_to_mintick(input.price(0, "Entry", inline = "en", group = G_T, confirm = true)) // pinelint: allow PL06
EntryMode entryIn = input.enum(EntryMode.clicked, "", inline = "en", group = G_T, tooltip = TIP_EN)
float stopClk = math.round_to_mintick(input.price(0, "Stop", inline = "st", group = G_T, confirm = true)) // pinelint: allow PL06
StopMode stopIn = input.enum(StopMode.clicked, "", inline = "st", group = G_T, tooltip = TIP_SM) // pinelint: allow PL06
float targetPx = math.round_to_mintick(input.price(0, "Target", inline = "tg", group = G_T, confirm = true)) // pinelint: allow PL06
TargetMode tgtMode = input.enum(TargetMode.price, "", inline = "tg", group = G_T, tooltip = TIP_TG)
float stopPips = input.float(20, "Fixed (pips/pts)", minval = 0.1, step = 1, inline = "sm", group = G_T) // pinelint: allow PL06
int swingLen = input.int(10, "Swing Bars", minval = 2, maxval = 200, inline = "sm", group = G_T) // pinelint: allow PL06
float tgtR = input.float(2.0, "R Multiple", minval = 0.1, step = 0.25, group = G_T, tooltip = TIP_RM) // pinelint: allow PL06
float balance = input.float(100000, "Balance", minval = 0, step = 100, inline = "ac", group = G_A)
Ccy acctIn = input.enum(Ccy.usd, "", inline = "ac", group = G_A)
RiskMode riskMode = input.enum(RiskMode.pct, "Risk", inline = "rk", group = G_A)
float riskIn = input.float(1.0, "", minval = 0, step = 0.25, inline = "rk", group = G_A, tooltip = TIP_RK)
bool hideAcct = input.bool(false, "Hide Account (show %)", group = G_A, tooltip = TIP_HA)
int tpCount = input.int(1, "Count", minval = 1, maxval = 4, inline = "tp", group = G_M) // pinelint: allow PL06
CloseMode closeIn = input.enum(CloseMode.equal, "Close", inline = "tp", group = G_M, tooltip = TIP_TP)
float c1Pct = input.float(50, "TP1 %", minval = 0, maxval = 100, step = 5, inline = "cp", group = G_M)
float c2Pct = input.float(25, "TP2 %", minval = 0, maxval = 100, step = 5, inline = "cp", group = G_M)
float c3Pct = input.float(15, "TP3 %", minval = 0, maxval = 100, step = 5, inline = "cp", group = G_M)
BeMode beIn = input.enum(BeMode.off, "Breakeven", inline = "be", group = G_M) // pinelint: allow PL06
float beR = input.float(1.0, "R", minval = 0.1, step = 0.25, inline = "be", group = G_M, tooltip = TIP_BE) // pinelint: allow PL06
SizeUnit unitIn = input.enum(SizeUnit.auto, "Size In", inline = "sz", group = G_I)
float stepIn = input.float(0, "Step", minval = 0, step = 0.01, inline = "sz", group = G_I, tooltip = TIP_SZ)
float ctrIn = input.float(0, "Contract Size", minval = 0, step = 1, group = G_I, tooltip = TIP_CS)
color profitC = input.color(#01579B, "Profit / Loss", inline = "zc", group = G_D)
color lossC = input.color(#B23C00, "", inline = "zc", group = G_D)
int fillTr = input.int(85, "Transparency", minval = 0, maxval = 100, step = 5, inline = "zc", group = G_D)
bool tagsOn = input.bool(true, "Tags", inline = "tg2", group = G_D)
bool tagPx = input.bool(false, "Price", inline = "tg2", group = G_D)
bool tagR = input.bool(true, "R", inline = "tg2", group = G_D)
bool tagCash = input.bool(false, "Cash", inline = "tg2", group = G_D, tooltip = TIP_TAG)
bool panelOn = input.bool(true, "Show Panel", inline = "pn_a", group = G_P)
Psize panelSz = input.enum(Psize.small, "", inline = "pn_a", group = G_P)
Ppos panelPos = input.enum(Ppos.br, "", inline = "pn_a", group = G_P, tooltip = TIP_PN)
Theme themeIn = input.enum(Theme.light, "Theme", group = G_P, tooltip = TIP_TH)
color cBg = input.color(color.new(#FFFFFF, 5), "Background / Text / Muted", inline = "pn_d", group = G_P)
color cTx = input.color(#0F171B, "", inline = "pn_d", group = G_P)
color cNeu = input.color(#5F6B73, "", inline = "pn_d", group = G_P)
color cGrid = input.color(color.new(#2E2E2E, 50), "Grid", inline = "pn_e", group = G_P)
int gridW = input.int(1, "", minval = 0, maxval = 5, inline = "pn_e", group = G_P)
int frameW = input.int(1, "Frame", minval = 0, maxval = 5, inline = "pn_e", group = G_P, tooltip = TIP_LN)
bool rwTitle = input.bool(true, "Title", inline = "rw_a", group = G_R)
bool rwSide = input.bool(true, "Side + Size", inline = "rw_a", group = G_R)
bool rwDetail = input.bool(true, "Details", inline = "rw_a", group = G_R, tooltip = TIP_RW)
bool rwEntry = input.bool(true, "Entry", inline = "rw_b", group = G_R)
bool rwStop = input.bool(true, "Stop", inline = "rw_b", group = G_R)
bool rwTarget = input.bool(true, "Targets", inline = "rw_b", group = G_R)
bool rwRisk = input.bool(true, "Risk", inline = "rw_c", group = G_R)
bool rwRR = input.bool(true, "R:R", inline = "rw_c", group = G_R)
bool rwPlan = input.bool(true, "Plan", inline = "rw_c", group = G_R)
bool rwValue = input.bool(true, "Value", group = G_R)
// ── Theme ────────────────────────────────────────────────────────────────────────────────────────────────────────────
pick(color lightC, color darkC, color customC) =>
switch themeIn
Theme.light => lightC
Theme.dark => darkC
Theme.custom => customC
color upC = pick(profitC, #42A5F5, profitC)
color dnC = pick(lossC, #FF8A50, lossC)
color neuC = pick(#5F6B73, #8B949E, cNeu)
color pnlBg = pick(color.new(#FFFFFF, 5), color.new(#131722, 5), cBg)
color pnlTx = pick(#0F171B, #D1D4DC, cTx)
color gridC = pick(color.new(#2E2E2E, 50), color.new(#434651, 30), cGrid)
// Text sizes in points (panel and chart tags): data rows step by 2, the title and headers stay about 1.2x the data
int headSz = switch panelSz
Psize.mobile => 10
Psize.small => 12
Psize.normal => 14
Psize.large => 17
int dataSz = switch panelSz
Psize.mobile => 8
Psize.small => 10
Psize.normal => 12
Psize.large => 14
// ── Instrument ───────────────────────────────────────────────────────────────────────────────────────────────────────
string tk = syminfo.ticker
bool isFx = syminfo.type == "forex"
bool isFut = syminfo.type == "futures"
bool isStock = syminfo.type == "stock" or syminfo.type == "fund" or syminfo.type == "dr"
bool isCoin = syminfo.type == "crypto"
bool isGold = not isFx and not isFut and (str.contains(tk, "XAU") or str.contains(tk, "GOLD"))
bool isSilv = not isFx and not isFut and (str.contains(tk, "XAG") or str.contains(tk, "SILVER"))
bool isOil = not isFut and (str.contains(tk, "WTI") or str.contains(tk, "OIL") or str.contains(tk, "XTI")
or str.contains(tk, "XBR") or str.contains(tk, "BCO") or str.contains(tk, "BRENT"))
float stdSize = isFx ? 100000 : isGold ? 100 : isSilv ? 5000 : isOil ? 100 : 1
float contract = ctrIn > 0 ? ctrIn : stdSize
float autoStep = switch unitIn
SizeUnit.lots => 0.01
SizeUnit.contracts => 1
=> isFut or isStock ? 1 : isCoin ? 0.0001 : 0.01
float lotStep = stepIn > 0 ? stepIn : autoStep
float pipSize = isFx ? (syminfo.currency == "JPY" ? 0.01 : 0.0001) : isGold ? 0.1 : isSilv ? 0.01 : 1.0
string unitName = switch unitIn
SizeUnit.lots => "lots"
SizeUnit.contracts => "contracts"
=> isFut ? "contracts" : isStock ? "shares" : isCoin ? "units" : "lots"
string distName = isFx or isGold or isSilv ? "pips" : "pts"
string acct = str.tostring(acctIn)
// Quote currency -> account currency through USD, using liquid legs only: the chart's own price when the pair has a USD
// side, otherwise the FX_IDC major against USD. Thin inverted crosses (TRYUSD, MXNEUR) lag, so they are never used.
usdLeg(string ccy) =>
bool usdBase = not (ccy == "EUR" or ccy == "GBP" or ccy == "AUD" or ccy == "NZD")
float px = request.security("FX_IDC:" + (usdBase ? "USD" + ccy : ccy + "USD"), timeframe.period, close, ignore_invalid_symbol = true)
usdBase ? 1 / px : px
bool sameCcy = syminfo.currency == acct
float qLeg = usdLeg(syminfo.currency)
float aLeg = usdLeg(acct)
float quoteUsd = syminfo.currency == "USD" ? 1.0 : isFx and syminfo.basecurrency == "USD" ? 1 / close : qLeg
float acctUsd = acct == "USD" ? 1.0 : isFx and syminfo.basecurrency == acct and syminfo.currency == "USD" ? close : aLeg
float fxLive = quoteUsd / acctUsd
float fxDay = request.currency_rate(syminfo.currency, acct, ignore_invalid_currency = true)
float fx = sameCcy ? 1.0 : not na(fxLive) and fxLive > 0 ? fxLive : fxDay
string fxSrc = sameCcy ? "same currency" : not na(fxLive) and fxLive > 0 ? "live" : "prior-day rate"
// ── Plan ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
// Clicked points stay in use while the entry is within one recent range (last 200 bars) of the current price. Farther
// away (another symbol, or price has left the plan behind) each Clicked level is fitted once, at the price when the chart
// loads: the entry there, the stop a quarter of that range away, the R-multiple target. Live, Fixed, Swing and R keep
// working. The side is continuation: long when the newest extreme of those 200 bars is the high, short when it is the low
float fitRange = ta.highest(high, FIT_BARS) - ta.lowest(low, FIT_BARS)
bool autoFit = clickPx > 0 and not na(fitRange) and math.abs(clickPx - close) > fitRange
int lastLeg = ta.highestbars(high, FIT_BARS) > ta.lowestbars(low, FIT_BARS) ? 1 : -1
varip float fitEntry = na
varip float fitDist = na
varip int fitDir = 0
if autoFit and barstate.islast and na(fitEntry)
fitEntry := close
fitDist := math.max(syminfo.mintick, math.round_to_mintick(fitRange * FIT_STOP))
fitDir := lastLeg
bool isLive = entryIn == EntryMode.live
float entryPx = isLive ? close : autoFit ? fitEntry : clickPx
bool fitStop = autoFit and stopIn == StopMode.clicked
TargetMode tgtUse = autoFit and tgtMode == TargetMode.price ? TargetMode.rMultiple : tgtMode
// The clicked entry fixes the side, so a live price that crosses the stop invalidates the plan instead of flipping it
int dir = autoFit ? fitDir : stopClk < clickPx ? 1 : -1
// The swing is the lowest low (long) or highest high (short) of the latest swingLen bars
float loNow = ta.lowest(low, swingLen)
float hiNow = ta.highest(high, swingLen)
float swingX = dir == 1 ? loNow : hiNow
float stopRaw = switch stopIn
StopMode.fixed => entryPx - dir * stopPips * pipSize
StopMode.swing => swingX
StopMode.clicked => stopClk
float stopPx = math.round_to_mintick(fitStop ? entryPx - dir * fitDist : stopRaw)
float riskDist = (entryPx - stopPx) * dir
float finalPx = tgtUse == TargetMode.rMultiple ? entryPx + dir * tgtR * riskDist : targetPx
float rewDist = (finalPx - entryPx) * dir
bool valid = entryPx > 0 and stopPx > 0 and riskDist > 0 and rewDist > 0 and finalPx > 0
float finalR = valid ? rewDist / riskDist : na
// Take profits sit evenly from the entry to the final target; the last one is the target. Custom shares never pass 100%
array<float> tpR = array.new<float>()
array<float> tpFrac = array.new<float>()
float unclosed = 1.0
for k = 1 to tpCount
float share = closeIn == CloseMode.equal ? 1.0 / tpCount : k == 1 ? c1Pct / 100 : k == 2 ? c2Pct / 100 : c3Pct / 100
float frac = k == tpCount ? unclosed : math.min(share, unclosed)
unclosed -= frac
tpR.push(finalR * k / tpCount)
tpFrac.push(frac)
bool multi = tpCount > 1
float tp1Px = entryPx + dir * tpR.get(0) * riskDist
bool beLive = valid and (beIn == BeMode.level ? beR < finalR : beIn == BeMode.tp1 and multi)
float bePx = beIn == BeMode.tp1 ? tp1Px : entryPx + dir * beR * riskDist
float valPerPx = contract * syminfo.pointvalue * fx
float lossLot = riskDist * valPerPx
float riskCash = riskMode == RiskMode.pct ? balance * riskIn / 100 : riskIn
float rawLots = valid and lossLot > 0 ? riskCash / lossLot : na
float lots = math.floor(rawLots / lotStep + EPS) * lotStep
bool minBreach = valid and lots < lotStep
lots := minBreach ? lotStep : lots
float realRisk = lots * lossLot
float cashPerR = lots * riskDist * valPerPx
float reward = finalR * cashPerR
float riskPct = valid and balance > 0 ? realRisk / balance * 100 : na
float planR = 0.0
for [k, r] in tpR
planR += tpFrac.get(k) * r
// Values for the RCV Daily Checklist link (Data Window only)
plot(valid ? riskPct : na, "Risk %", display = display.data_window)
plot(valid ? finalR : na, "R:R", display = display.data_window)
// Level alerts: the bar's range reaches the level
reached(float lvl) =>
valid and high >= lvl and low <= lvl
alertcondition(not isLive and reached(entryPx), "Price reached entry", "{{ticker}} RCV Risk: price reached the entry")
alertcondition(reached(stopPx), "Price reached stop", "{{ticker}} RCV Risk: price reached the stop")
bool partHit = false
if multi
for k = 0 to tpCount - 2
partHit := partHit or reached(entryPx + dir * tpR.get(k) * riskDist)
alertcondition(multi and partHit, "Price reached a partial target", "{{ticker}} RCV Risk: price reached a partial target")
alertcondition(beLive and reached(bePx), "Price reached breakeven level", "{{ticker}} RCV Risk: breakeven level reached, move the stop to the entry")
alertcondition(reached(finalPx), "Price reached target", "{{ticker}} RCV Risk: price reached the final target")
// ── Formatting ───────────────────────────────────────────────────────────────────────────────────────────────────────
string sym = switch acctIn
Ccy.usd => "$"
Ccy.eur => "€"
Ccy.gbp => "£"
Ccy.jpy => "¥"
Ccy.aud => "A$"
Ccy.cad => "C$"
Ccy.nzd => "NZ$"
Ccy.chf => "CHF "
cash(float v, bool signed) =>
string body = sym + str.tostring(math.abs(v), acctIn == Ccy.jpy ? "#,##0" : "#,##0.00")
na(v) ? "—" : signed ? (v < 0 ? "−" : "+") + body : body
// Account-sized amounts: cash, or a percent of the balance when the account is hidden
money(float v, bool signed) =>
string pct = str.tostring(math.abs(v) / balance * 100, "0.00") + "%"
hideAcct ? (na(v) ? "—" : signed ? (v < 0 ? "−" : "+") + pct : pct) : cash(v, signed)
fmtR(float r) =>
na(r) ? "—" : (r < 0 ? "−" : "+") + str.tostring(math.abs(r), "0.00") + "R"
fmtDist(float d) =>
str.tostring(d / pipSize, "#,##0.0") + " " + distName
int stepDec = math.max(0, int(math.ceil(-math.log10(lotStep) - EPS)))
string lotFmt = stepDec == 0 ? "#,##0" : "#,##0." + str.repeat("0", stepDec)
string unitSing = str.substring(unitName, 0, str.length(unitName) - 1)
// Singular only when the size shows no decimals ("1 contract", but "1.00 lots")
string unitOne = lots == 1 and stepDec == 0 ? unitSing : unitName // pinelint: allow PL08 (lots is the float size, not the "lots" enum text)
string sizeTxt = na(lots) ? "—" : str.tostring(lots, lotFmt) + " " + unitOne
// ── Chart drawing ────────────────────────────────────────────────────────────────────────────────────────────────────
var box profitBox = box.new(na, na, na, na, border_width = 0)
var box lossBox = box.new(na, na, na, na, border_width = 0)
var line entryLn = line.new(na, na, na, na, width = 1)
var array<line> tpLn = array.from(line.new(na, na, na, na, style = line.style_dashed),
line.new(na, na, na, na, style = line.style_dashed), line.new(na, na, na, na, style = line.style_dashed))
var line beLn = line.new(na, na, na, na, style = line.style_dotted)
var label targetTag = label.new(na, na, "", style = label.style_label_left, textcolor = color.white)
var label stopTag = label.new(na, na, "", style = label.style_label_left, textcolor = color.white)
var label entryTag = label.new(na, na, "", style = label.style_label_left)
var array<label> tpTag = array.from(label.new(na, na, "", style = label.style_label_left),
label.new(na, na, "", style = label.style_label_left), label.new(na, na, "", style = label.style_label_left))
var label beTag = label.new(na, na, "", style = label.style_label_left, color = color.new(color.black, 100))
setLevel(line ln, label tg, bool on, int x1, int x2, int xTag, float y, color c, string txt) =>
ln.set_xy1(x1, on ? y : na)
ln.set_xy2(x2, on ? y : na)
ln.set_color(c)
tg.set_xy(xTag, on and tagsOn ? y : na)
tg.set_text(txt)
tg.set_textcolor(c)
tg.set_color(color.new(pnlBg, 15))
tg.set_size(dataSz)
tagText(string name, float px, float r, float cash) =>
string t = tagPx ? name + " " + str.tostring(px, format.mintick) : name
t := tagR and not na(r) ? t + " " + fmtR(r) : t
tagCash and not na(cash) ? t + " " + money(cash, true) : t
if barstate.islast
// The box starts at the latest bar and reaches 10 bars into the empty space ahead, so it never covers candles.
// Counted in bars, not time, so a session close or weekend ahead cannot shrink it
int left = bar_index
int right = bar_index + BOX_AHEAD
// Tags start at the box's right edge and read outward, pointing back at the box
int tagX = right
float shown = valid ? 1.0 : na
profitBox.set_lefttop(left, finalPx * shown)
profitBox.set_rightbottom(right, entryPx * shown)
profitBox.set_bgcolor(color.new(profitC, fillTr))
lossBox.set_lefttop(left, entryPx * shown)
lossBox.set_rightbottom(right, stopPx * shown)
lossBox.set_bgcolor(color.new(lossC, fillTr))
entryLn.set_xy1(left, entryPx * shown)
entryLn.set_xy2(right, entryPx * shown)
entryLn.set_color(pnlTx)
for k = 0 to 2
bool on = valid and k < tpCount - 1
float px = on ? entryPx + dir * tpR.get(k) * riskDist : na
string t = on ? "TP" + str.tostring(k + 1) + " " + str.tostring(tpFrac.get(k) * 100, "0") + "% " + fmtR(tpR.get(k))
+ (k == 0 and beIn == BeMode.tp1 ? " · then BE" : "") : ""
setLevel(tpLn.get(k), tpTag.get(k), on, left, right, tagX, px, profitC, t)
setLevel(beLn, beTag, beLive and beIn == BeMode.level, left, right, tagX, bePx, neuC, "BE at " + fmtR(beR))
bool tagShow = valid and tagsOn
targetTag.set_xy(tagX, tagShow ? finalPx : na)
targetTag.set_text(tagText(multi ? "TP" + str.tostring(tpCount) : "Target", finalPx, finalR, reward))
targetTag.set_color(profitC)
targetTag.set_size(dataSz)
stopTag.set_xy(tagX, tagShow ? stopPx : na)
stopTag.set_text(tagText("Stop", stopPx, -1.0, -realRisk))
stopTag.set_color(lossC)
stopTag.set_size(dataSz)
entryTag.set_xy(tagX, tagShow ? entryPx : na)
entryTag.set_text((dir == 1 ? "Long " : "Short ") + sizeTxt + (tagPx ? " " + str.tostring(entryPx, format.mintick) : ""))
entryTag.set_color(pnlTx)
entryTag.set_textcolor(color.new(pnlBg, 0))
entryTag.set_size(dataSz)
// ── Panel (RCV Direction look) ──────────────────────────────────────────────────────────────────────────────────────
pnlPosition = switch panelPos
Ppos.tl => position.top_left
Ppos.tc => position.top_center
Ppos.tr => position.top_right
Ppos.ml => position.middle_left
Ppos.mr => position.middle_right
Ppos.bl => position.bottom_left
Ppos.bc => position.bottom_center
=> position.bottom_right
int nCols = rwDetail ? 3 : 2
var table tbl = na
cell(int c, int r, string t, color tc, int sz, bool mono) =>
tbl.cell(c, r, t, text_color = tc, bgcolor = pnlBg, text_size = sz, text_halign = c == 0 ? text.align_left : text.align_right,
text_font_family = mono ? font.family_monospace : font.family_default)
// One row when its toggle is on: head, value and (with Details) the detail column. Returns the next free row
row(bool on, int r, string head, string val, string detail, color vc, string tip) =>
int nxt = r
if on
cell(0, r, head, pnlTx, dataSz, true)
cell(1, r, val, vc, dataSz, false)
if rwDetail
cell(2, r, detail, neuC, dataSz, false)
if tip != ""
tbl.cell_set_tooltip(1, r, tip)
if rwDetail
tbl.cell_set_tooltip(2, r, tip)
nxt += 1
nxt
// Full-width text row (title, messages)
wide(int r, string t, color tc, int sz, bool mono, string halign) =>
cell(0, r, t, tc, sz, mono)
tbl.merge_cells(0, r, nCols - 1, r)
tbl.cell_set_text_halign(0, r, halign)
r + 1
// Rebuilt on every update of the latest bar, so merges always match the rows on show
if panelOn and barstate.islast
tbl.delete()
tbl := table.new(pnlPosition, nCols, 20, bgcolor = pnlBg, frame_color = pnlTx, frame_width = frameW, border_color = gridC, border_width = gridW)
int r = 0
if rwTitle
r := wide(r, "RCV Risk Manager", pnlTx, headSz, true, text.align_center)
tbl.cell_set_tooltip(0, 0, syminfo.ticker + " quoted in " + syminfo.currency
+ ", account in " + acct)
if not valid
wide(r, clickPx > 0 ? "Price is past the stop or the target: drag them or click new points." : "Click Entry, Stop and Target on the chart.",
neuC, dataSz, false, text.align_left)
else
color dirC = dir == 1 ? upC : dnC
if rwSide
cell(0, r, dir == 1 ? "▲ LONG" : "▼ SHORT", dirC, headSz, true)
cell(1, r, sizeTxt, pnlTx, headSz, true)
if rwDetail
cell(2, r, autoFit ? "Auto-fit" : isLive ? "Live" : "Planned", isLive ? dirC : neuC, headSz, false)
if autoFit
tbl.cell_set_tooltip(2, r, "Your points are from another chart, so this plan was set at load: side from the newest 200-bar high or low, stop a "
+ "quarter of that range away, target at " + str.tostring(tgtR) + "R. Right-click > Reset points to place your own.")
r += 1
r := row(rwEntry, r, "Entry", str.tostring(entryPx, format.mintick), isLive ? "live price" : autoFit ? "fitted" : "clicked", pnlTx, "")
string stopSrc = switch stopIn
StopMode.fixed => "fixed"
StopMode.swing => str.tostring(swingLen) + "-bar swing"
StopMode.clicked => fitStop ? "fitted, a quarter of the 200-bar range" : "clicked"
r := row(rwStop, r, "Stop", str.tostring(stopPx, format.mintick), fmtDist(riskDist) + " · " + money(-realRisk, true), dnC, "Stop from: " + stopSrc + ".")
for k = 0 to tpCount - 1
bool last = k == tpCount - 1
float rk = tpR.get(k)
string nm = multi ? "TP" + str.tostring(k + 1) : "Target"
string dt = (multi ? str.tostring(tpFrac.get(k) * 100, "0") + "% at " + fmtR(rk) : fmtDist(rewDist)) + " · "
+ money(multi ? tpFrac.get(k) * rk * cashPerR : reward, true)
r := row(rwTarget, r, nm, str.tostring(entryPx + dir * rk * riskDist, format.mintick), dt, upC, last ? "Final target." : "")
string riskTip = minBreach ? "The minimum size risks more than planned (" + money(riskCash, false) + ")." : "Planned " + money(riskCash, false)
+ ", rounded down to the size step."
r := row(rwRisk, r, "Risk", money(realRisk, false), str.tostring(riskPct, "0.00") + "% of " + (hideAcct ? "balance" : money(balance, false)),
minBreach ? dnC : pnlTx, riskTip)
r := row(rwRR, r, "R:R", str.tostring(finalR, "0.00"), "reward : risk", pnlTx, "")
r := row(rwPlan and multi, r, "Plan", fmtR(planR), money(planR * cashPerR, true) + " if every target fills", pnlTx,
"Average result if every target fills.")
r := row(rwValue, r, "Value", cash(pipSize * valPerPx, false) + " / " + str.substring(distName, 0, str.length(distName) - 1),
"per " + unitSing + " · " + str.tostring(contract, "#,##0.####") + " units", neuC,
"Contract size " + str.tostring(contract, "#,##0.####") + (ctrIn > 0 ? " (yours)" : " (standard)") + ", point value "
+ str.tostring(syminfo.pointvalue) + ", 1 " + syminfo.currency + " = " + str.tostring(fx, "0.#########") + " " + acct + " (" + fxSrc
+ "). Check these with your broker.")
// Always shown: without the rate every cash value is missing
if na(fx)
r := row(true, r, "Check", "no rate", syminfo.currency + " to " + acct + " unavailable", dnC, "Cash values are missing without this rate.")
// Always shown on a new pair: the clicked points keep the old pair's prices, so they sit off this chart until reset
if autoFit
wide(r, "New pair: right-click the box › Reset points", neuC, dataSz, false, text.align_center)