本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Master_Hunter
//@version=6
indicator("Staged Exit Planner", overlay = true)
// ============================ INPUTS ============================
grpTrade = "Trade levels"
entryPrice = input.float(0.0, "Entry price", step = 0.00001, group = grpTrade)
stopPrice = input.float(0.0, "Stop-loss price", step = 0.00001, group = grpTrade)
beTrigger = input.float(0.0, "Breakeven trigger price", step = 0.00001, group = grpTrade, tooltip = "Manually set the exact price where you'll move your stop to breakeven. Leave at 0 to hide this line.")
grpTargets = "Targets"
target1Enabled = input.bool(false, "Enable Target 1", group = grpTargets)
target1Price = input.float(0.0, "Target 1 price", step = 0.00001, group = grpTargets)
target2Enabled = input.bool(false, "Enable Target 2", group = grpTargets)
target2Price = input.float(0.0, "Target 2 price", step = 0.00001, group = grpTargets)
target3Enabled = input.bool(false, "Enable Target 3", group = grpTargets)
target3Price = input.float(0.0, "Target 3 price", step = 0.00001, group = grpTargets)
grpPctMode = "Single-target % -of-path staging"
useSingleTargetPct = input.bool(false, "Enable (ignores Target 2/3 and the Staged exit plan %s below)", group = grpPctMode)
stage1PathPct = input.float(50.0, "Stage 1: % of path to Target 1", minval = 0.0, maxval = 100.0, group = grpPctMode)
stage1ClosePct = input.float(20.0, "Stage 1: % of volume to close", minval = 0.0, maxval = 100.0, group = grpPctMode)
stage2PathPct = input.float(75.0, "Stage 2: % of path to Target 1", minval = 0.0, maxval = 100.0, group = grpPctMode)
stage2ClosePct = input.float(30.0, "Stage 2: % of volume to close", minval = 0.0, maxval = 100.0, group = grpPctMode)
stage3PathPct = input.float(100.0, "Stage 3: % of path to Target 1", minval = 0.0, maxval = 100.0, group = grpPctMode)
stage3ClosePct = input.float(20.0, "Stage 3: % of volume to close", minval = 0.0, maxval = 100.0, group = grpPctMode)
grpSize = "Position size"
posSize = input.float(1.0, "Position size", minval = 0.0, step = 0.01, group = grpSize)
unitLabel = input.string("units", "Size unit label (lots, contracts, coins)", group = grpSize)
grpStages = "Staged exit plan (manual-target mode)"
stage1Enabled = input.bool(false, "Enable stage 1", group = grpStages)
pct1 = input.int(30, "% closed at stage 1", minval = 0, maxval = 100, group = grpStages)
stage2Enabled = input.bool(false, "Enable stage 2", group = grpStages)
pct2 = input.int(30, "% closed at stage 2", minval = 0, maxval = 100, group = grpStages)
stage3Enabled = input.bool(false, "Enable stage 3", group = grpStages)
pct3 = input.int(20, "% closed at stage 3", minval = 0, maxval = 100, group = grpStages)
grpStyle = "Line colors"
entryColor = input.color(color.gray, "Entry line", group = grpStyle)
stopColor = input.color(color.red, "Stop line", group = grpStyle)
beColor = input.color(color.aqua, "Breakeven line", group = grpStyle)
target1Color = input.color(color.aqua, "Target 1 / Stage 1 line", group = grpStyle)
target2Color = input.color(color.yellow, "Target 2 / Stage 2 line", group = grpStyle)
target3Color = input.color(color.green, "Target 3 / Stage 3 line", group = grpStyle)
// ============================ EFFECTIVE % PER STAGE (mode-aware) ============================
effPct1 = useSingleTargetPct ? stage1ClosePct : float(pct1)
effPct2 = useSingleTargetPct ? stage2ClosePct : float(pct2)
effPct3 = useSingleTargetPct ? stage3ClosePct : float(pct3)
// ============================ CROSS-FIELD % VALIDATION ============================
// Each field's own maxval = 100 above already blocks any single stage above 100
// with TradingView's own built-in message. This covers the COMBINATIONS, which
// Pine's input system cannot validate on its own. Uses whichever mode is active.
// In single-target %-of-path mode all three stages are always counted; the manual-mode
// "Enable stage" switches only apply in manual-target mode.
chkPct1 = (useSingleTargetPct or stage1Enabled) ? effPct1 : 0.0
chkPct2 = (useSingleTargetPct or stage2Enabled) ? effPct2 : 0.0
chkPct3 = (useSingleTargetPct or stage3Enabled) ? effPct3 : 0.0
if chkPct1 + chkPct2 > 100.0 or chkPct1 + chkPct3 > 100.0 or chkPct2 + chkPct3 > 100.0 or chkPct1 + chkPct2 + chkPct3 > 100.0
runtime.error("The final value should not be more than 100.")
// ============================ CALCULATIONS ============================
isLong = stopPrice < entryPrice
riskDist = math.abs(entryPrice - stopPrice)
coreValid = entryPrice != 0.0 and stopPrice != 0.0 and entryPrice != stopPrice and riskDist > 0.0
isValid = coreValid
beValid = beTrigger != 0.0
target1Valid = target1Enabled and target1Price != 0.0 and (isLong ? target1Price > entryPrice : target1Price < entryPrice)
target2Valid = target2Enabled and target2Price != 0.0 and (isLong ? target2Price > entryPrice : target2Price < entryPrice)
target3Valid = target3Enabled and target3Price != 0.0 and (isLong ? target3Price > entryPrice : target3Price < entryPrice)
// ============================ TARGET ORDER VALIDATION ============================
// Long : Target 1 < Target 2 < Target 3
// Short: Target 1 > Target 2 > Target 3
// Only enabled targets that have a price are compared. If Target 2 is off, Target 1
// is compared directly with Target 3. Skipped in single-target %-of-path mode
// (it ignores Target 2/3) and until a valid entry / stop-loss is set.
target1Set = target1Enabled and target1Price != 0.0
target2Set = target2Enabled and target2Price != 0.0
target3Set = target3Enabled and target3Price != 0.0
orderBad12 = target1Set and target2Set and (isLong ? target1Price >= target2Price : target1Price <= target2Price)
orderBad23 = target2Set and target3Set and (isLong ? target2Price >= target3Price : target2Price <= target3Price)
orderBad13 = target1Set and not target2Set and target3Set and (isLong ? target1Price >= target3Price : target1Price <= target3Price)
if coreValid and not useSingleTargetPct and (orderBad12 or orderBad23 or orderBad13)
if isLong
runtime.error("Long position: Target 1 must be lower than Target 2, and Target 2 lower than Target 3.")
else
runtime.error("Short position: Target 1 must be higher than Target 2, and Target 2 higher than Target 3.")
// Effective price/validity per stage slot: the manual target, UNLESS single-target
// %-of-path mode is on, in which case all three are measured along the path to
// Target 1 using their own path percentage.
eff1Price = useSingleTargetPct ? entryPrice + (target1Price - entryPrice) * stage1PathPct / 100.0 : target1Price
eff2Price = useSingleTargetPct ? entryPrice + (target1Price - entryPrice) * stage2PathPct / 100.0 : target2Price
eff3Price = useSingleTargetPct ? entryPrice + (target1Price - entryPrice) * stage3PathPct / 100.0 : target3Price
eff1Valid = target1Valid
eff2Valid = useSingleTargetPct ? target1Valid : target2Valid
eff3Valid = useSingleTargetPct ? target1Valid : target3Valid
rMultiple1 = riskDist > 0.0 ? math.abs(eff1Price - entryPrice) / riskDist : na
rMultiple2 = riskDist > 0.0 ? math.abs(eff2Price - entryPrice) / riskDist : na
rMultiple3 = riskDist > 0.0 ? math.abs(eff3Price - entryPrice) / riskDist : na
// Single-target %-of-path mode activates all three stages automatically (no need to turn on
// the manual-mode "Enable stage" switches). In manual-target mode those switches still decide.
stage1Active = coreValid and (useSingleTargetPct or stage1Enabled) and eff1Valid
stage2Active = coreValid and (useSingleTargetPct or stage2Enabled) and eff2Valid
stage3Active = coreValid and (useSingleTargetPct or stage3Enabled) and eff3Valid
qty1 = stage1Active ? posSize * effPct1 / 100.0 : 0.0
qty2 = stage2Active ? posSize * effPct2 / 100.0 : 0.0
qty3 = stage3Active ? posSize * effPct3 / 100.0 : 0.0
qtyRem = posSize - qty1 - qty2 - qty3
// ---- Final R:R and Final Target (weighted-avg price): blends every active
// ---- stage's own (effective) price, plus the leftover runner valued at the
// ---- furthest valid stage, against $ risk.
float farTargetPrice = na
if eff1Valid
farTargetPrice := eff1Price
if eff2Valid and (na(farTargetPrice) or (isLong ? eff2Price > farTargetPrice : eff2Price < farTargetPrice))
farTargetPrice := eff2Price
if eff3Valid and (na(farTargetPrice) or (isLong ? eff3Price > farTargetPrice : eff3Price < farTargetPrice))
farTargetPrice := eff3Price
profit1 = stage1Active ? qty1 * (isLong ? eff1Price - entryPrice : entryPrice - eff1Price) : 0.0
profit2 = stage2Active ? qty2 * (isLong ? eff2Price - entryPrice : entryPrice - eff2Price) : 0.0
profit3 = stage3Active ? qty3 * (isLong ? eff3Price - entryPrice : entryPrice - eff3Price) : 0.0
qtyRemSafe = math.max(qtyRem, 0.0)
runnerCounted = qtyRemSafe > 0.0 and not na(farTargetPrice)
runnerProfit = runnerCounted ? qtyRemSafe * (isLong ? farTargetPrice - entryPrice : entryPrice - farTargetPrice) : 0.0
riskDollar = riskDist * posSize
totalProfitDollar = profit1 + profit2 + profit3 + runnerProfit
finalRRValid = riskDollar > 0.0 and (stage1Active or stage2Active or stage3Active or runnerCounted)
finalRR = finalRRValid ? totalProfitDollar / riskDollar : na
totalQtyForAvg = qty1 + qty2 + qty3 + (runnerCounted ? qtyRemSafe : 0.0)
weightedSum = qty1 * eff1Price + qty2 * eff2Price + qty3 * eff3Price + (runnerCounted ? qtyRemSafe * farTargetPrice : 0.0)
finalTargetPrice = totalQtyForAvg > 0.0 ? weightedSum / totalQtyForAvg : na
// ============================ LINES (created once) ============================
var line entryLine = na
var line stopLine = na
var line beLine = na
var line target1Line = na
var line target2Line = na
var line target3Line = na
if barstate.isfirst and isValid
entryLine := line.new(x1 = bar_index, y1 = entryPrice, x2 = bar_index, y2 = entryPrice, extend = extend.right, color = entryColor, style = line.style_solid, width = 1)
stopLine := line.new(x1 = bar_index, y1 = stopPrice, x2 = bar_index, y2 = stopPrice, extend = extend.right, color = stopColor, style = line.style_dashed, width = 1)
if barstate.isfirst and isValid and beValid
beLine := line.new(x1 = bar_index, y1 = beTrigger, x2 = bar_index, y2 = beTrigger, extend = extend.right, color = beColor, style = line.style_dotted, width = 1)
if barstate.isfirst and isValid and eff1Valid
target1Line := line.new(x1 = bar_index, y1 = eff1Price, x2 = bar_index, y2 = eff1Price, extend = extend.right, color = target1Color, style = line.style_dotted, width = 1)
if barstate.isfirst and isValid and eff2Valid
target2Line := line.new(x1 = bar_index, y1 = eff2Price, x2 = bar_index, y2 = eff2Price, extend = extend.right, color = target2Color, style = line.style_dotted, width = 1)
if barstate.isfirst and isValid and eff3Valid
target3Line := line.new(x1 = bar_index, y1 = eff3Price, x2 = bar_index, y2 = eff3Price, extend = extend.right, color = target3Color, style = line.style_solid, width = 1)
// ============================ LABELS (created once, repositioned on the last bar) ============================
var label entryLbl = na
var label stopLbl = na
var label beLbl = na
var label target1Lbl = na
var label target2Lbl = na
var label target3Lbl = na
if barstate.isfirst and isValid
entryLbl := label.new(x = bar_index, y = entryPrice, text = "", style = label.style_label_left, color = entryColor, textcolor = color.white, size = size.small)
stopLbl := label.new(x = bar_index, y = stopPrice, text = "", style = label.style_label_left, color = stopColor, textcolor = color.white, size = size.small)
if barstate.isfirst and isValid and beValid
beLbl := label.new(x = bar_index, y = beTrigger, text = "", style = label.style_label_left, color = beColor, textcolor = color.black, size = size.small)
if barstate.isfirst and isValid and eff1Valid
target1Lbl := label.new(x = bar_index, y = eff1Price, text = "", style = label.style_label_left, color = target1Color, textcolor = color.black, size = size.small)
if barstate.isfirst and isValid and eff2Valid
target2Lbl := label.new(x = bar_index, y = eff2Price, text = "", style = label.style_label_left, color = target2Color, textcolor = color.black, size = size.small)
if barstate.isfirst and isValid and eff3Valid
target3Lbl := label.new(x = bar_index, y = eff3Price, text = "", style = label.style_label_left, color = target3Color, textcolor = color.white, size = size.small)
if barstate.islast and isValid
label.set_x(entryLbl, bar_index)
label.set_y(entryLbl, entryPrice)
label.set_text(entryLbl, "Entry " + str.tostring(entryPrice, format.mintick))
label.set_x(stopLbl, bar_index)
label.set_y(stopLbl, stopPrice)
label.set_text(stopLbl, "Stop " + str.tostring(stopPrice, format.mintick))
if barstate.islast and isValid and beValid
label.set_x(beLbl, bar_index)
label.set_y(beLbl, beTrigger)
label.set_text(beLbl, "BE trigger -> move stop to " + str.tostring(entryPrice, format.mintick))
if barstate.islast and isValid and eff1Valid
label.set_x(target1Lbl, bar_index)
label.set_y(target1Lbl, eff1Price)
label1Prefix = useSingleTargetPct ? "Stage 1 (" + str.tostring(stage1PathPct, "#.##") + "% of path) " : "Target 1 "
target1Text = label1Prefix + str.tostring(eff1Price, format.mintick) + " R:R " + str.tostring(rMultiple1, "#.##")
label.set_text(target1Lbl, stage1Active ? target1Text + " Close " + str.tostring(effPct1, "#.##") + "% (" + str.tostring(qty1, "#.##") + " " + unitLabel + ")" : target1Text)
if barstate.islast and isValid and eff2Valid
label.set_x(target2Lbl, bar_index)
label.set_y(target2Lbl, eff2Price)
label2Prefix = useSingleTargetPct ? "Stage 2 (" + str.tostring(stage2PathPct, "#.##") + "% of path) " : "Target 2 "
target2Text = label2Prefix + str.tostring(eff2Price, format.mintick) + " R:R " + str.tostring(rMultiple2, "#.##")
label.set_text(target2Lbl, stage2Active ? target2Text + " Close " + str.tostring(effPct2, "#.##") + "% (" + str.tostring(qty2, "#.##") + " " + unitLabel + ")" : target2Text)
if barstate.islast and isValid and eff3Valid
label.set_x(target3Lbl, bar_index)
label.set_y(target3Lbl, eff3Price)
label3Prefix = useSingleTargetPct ? "Stage 3 (" + str.tostring(stage3PathPct, "#.##") + "% of path) " : "Target 3 "
target3Text = label3Prefix + str.tostring(eff3Price, format.mintick) + " R:R " + str.tostring(rMultiple3, "#.##")
label.set_text(target3Lbl, stage3Active ? target3Text + " Close " + str.tostring(effPct3, "#.##") + "% (" + str.tostring(qty3, "#.##") + " " + unitLabel + ")" : target3Text)
// ============================ PRICE-SCALE TAGS (exact price on the right axis) ============================
plot(isValid ? entryPrice : na, title = "Entry (price scale)", color = entryColor, display = display.price_scale)
plot(isValid ? stopPrice : na, title = "Stop (price scale)", color = stopColor, display = display.price_scale)
plot(isValid and beValid ? beTrigger : na, title = "Breakeven (price scale)", color = beColor, display = display.price_scale)
plot(isValid and eff1Valid ? eff1Price : na, title = "Target 1 / Stage 1 (price scale)", color = target1Color, display = display.price_scale)
plot(isValid and eff2Valid ? eff2Price : na, title = "Target 2 / Stage 2 (price scale)", color = target2Color, display = display.price_scale)
plot(isValid and eff3Valid ? eff3Price : na, title = "Target 3 / Stage 3 (price scale)", color = target3Color, display = display.price_scale)
// ============================ SUMMARY TABLE ============================
var table infoTable = table.new(position = position.top_right, columns = 2, rows = 7, border_width = 1, bgcolor = color.black, border_color = color.gray)
if barstate.islast
if isValid
table.cell(infoTable, 0, 0, "Direction", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 1, 0, isLong ? "Long" : "Short", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 0, 1, "Final R:R", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 1, 1, finalRRValid ? str.tostring(finalRR, "#.##") : "N/A", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 0, 2, "Final Target (avg)", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 1, 2, finalRRValid ? str.tostring(finalTargetPrice, format.mintick) : "N/A", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 0, 3, "BE trigger", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 1, 3, beValid ? str.tostring(beTrigger, format.mintick) : "not set", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 0, 4, "Stage 1 (" + str.tostring(effPct1, "#.##") + "%)", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 1, 4, stage1Active ? str.tostring(qty1, "#.##") + " " + unitLabel : "off", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 0, 5, "Stage 2 (" + str.tostring(effPct2, "#.##") + "%)", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 1, 5, stage2Active ? str.tostring(qty2, "#.##") + " " + unitLabel : "off", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 0, 6, "Stage 3 (" + str.tostring(effPct3, "#.##") + "%)", text_color = color.white, bgcolor = color.gray)
table.cell(infoTable, 1, 6, stage3Active ? str.tostring(qty3, "#.##") + " " + unitLabel : "off", text_color = color.white, bgcolor = color.gray)
else
table.cell(infoTable, 0, 0, "Set entry / stop-loss", text_color = color.white, bgcolor = color.red)
table.cell(infoTable, 1, 0, "in the settings above", text_color = color.white, bgcolor = color.red)