本页源码来自 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/
// © CookiesNCoffee
//
// ============================================================================
// INDICATOR OVERVIEW & VISUAL TAG LEGEND
// ============================================================================
//
// 1. MOMENTUM VECTOR RIBBON (CYAN / MAGENTA CLOUD)
// - Bridges a fast Hull Moving Average (9) and an EMA baseline (20).
// - Hue indicates directional bias (Cyan = bullish, Magenta = bearish).
// - Ribbon spread reflects momentum expansion; narrowing/pinching signals
// compression and potential inflection before a directional flip.
//
// 2. ENTRY & REVERSAL MARKERS
// - LONG / SHORT (Triangles): Printed once when the fast vector crosses
// the baseline, initiating a new simulated setup and risk framework.
// - REV (Amber Diamond): Marks an immediate directional flip while a prior
// setup was still active (before hitting its stop or primary objective).
//
// 3. DYNAMIC RISK & TARGET RAYS (LABELED LEVELS)
// - FILL (Dashed White Line): Reference entry price where the signal fired.
// - TRAIL STOP (Solid Magenta Line): Volatility-adjusted initial stop loss
// (ATR multiple) that ratchets favorably once price reaches your R-threshold.
// - PROTECTED (Solid Amber Line): Indicates the trailing stop has advanced
// at or beyond the FILL price, locking in breakeven or open profit.
// - TP1 / TP2 (Dotted Cyan Lines): Intermediate 1R and 2R scale-out levels.
// - TARGET (Solid Cyan Line): Primary profit objective (default 3.0R).
//
// 4. OUTCOME & MILESTONE BADGES
// - SCALE 1 / SCALE 2: Marks the bar where intermediate R-targets were tagged.
// - OBJ HIT (Cyan Circle): Primary R:R target objective reached.
// - STOPPED (Magenta X): Active trailing or hard stop level triggered.
//
// 5. PROP RISK & SIZING TELEMETRY PANEL (TOP-RIGHT HUD)
// - Displays live Vector State, active level prices, 1-contract dollar risk
// based on current stop distance, and maximum contract size permitted
// within your configured per-trade dollar risk cap.
// ============================================================================
//@version=6
indicator('Buy/Sell & Trailing Stop', shorttitle='Buy/Sell & TS', overlay=true)
// ============================================
// 1. CONFIGURATION PARAMETERS
// ============================================
mvSecRibbon = '1. Momentum Vector Ribbon'
mvFastLen = input.int(9, 'Fast Vector Length (HMA)', minval=2, group=mvSecRibbon)
mvAnchorLen = input.int(20, 'Structural Anchor Length (EMA)', minval=2, group=mvSecRibbon)
mvFilterClose = input.bool(false, 'Require Directional Candle Body', tooltip='When enabled, triggers only if candle closes in the direction of the vector cross', group=mvSecRibbon)
mvDrawCloud = input.bool(true, 'Display Vector Cloud', group=mvSecRibbon)
mvBullCloudCol = input.color(color.new(#00e5ff, 78), 'Bullish Cloud Color', group=mvSecRibbon)
mvBearCloudCol = input.color(color.new(#ff2a85, 78), 'Bearish Cloud Color', group=mvSecRibbon)
mvSecRisk = '2. Dynamic Trail & Target Engine'
mvAtrPeriod = input.int(14, 'Volatility Lookback (ATR)', minval=1, group=mvSecRisk)
mvAtrStopMult = input.float(1.5, 'Stop Distance Multiplier (ATR)', minval=0.1, step=0.1, group=mvSecRisk)
mvTrailActive = input.bool(true, 'Activate Dynamic Trailing Stop', group=mvSecRisk)
mvTrailEngageR = input.float(1.0, 'Engage Trail Threshold (R)', minval=0.0, step=0.25, tooltip='0.0 = trails immediately; 1.0 = locks profit/trails once +1R is reached', group=mvSecRisk)
mvTargetMultR = input.float(3.0, 'Primary Objective Multiple (R)', minval=1.0, step=0.5, group=mvSecRisk)
mvSecOverlay = '3. Chart Projections & Prop Risk Guard'
mvShowPriceTags = input.bool(true, 'Show Price Tags on Active Levels', group=mvSecOverlay)
mvProjectBars = input.int(12, 'Forward Ray Length (Bars)', minval=2, maxval=50, group=mvSecOverlay)
mvMaxTradeUsd = input.float(750.0, 'Max Dollar Risk Per Setup ($)', minval=50.0, step=50.0, group=mvSecOverlay)
// ============================================
// 2. VECTOR & VOLATILITY COMPUTATIONS
// ============================================
mvFastVec = ta.hma(close, mvFastLen)
mvAnchorVec = ta.ema(close, mvAnchorLen)
mvVolRange = ta.atr(mvAtrPeriod)
mvBullState = mvFastVec >= mvAnchorVec
mvBearState = mvFastVec < mvAnchorVec
mvStateFlip = mvBullState != mvBullState[1]
mvGoLong = mvFilterClose ? (mvBullState and mvStateFlip and close > open) : (mvBullState and mvStateFlip)
mvGoShort = mvFilterClose ? (mvBearState and mvStateFlip and close < open) : (mvBearState and mvStateFlip)
// ============================================
// 3. STATE MACHINE & TRAILING STOP ENGINE
// ============================================
var float mvFillPrice = na
var float mvUnitRiskPts = na
var float mvTrailStopPx = na
var float mvPrimaryGoalPx = na
var bool mvInMarket = false
var string mvSide = ''
var float[] mvScalePrices = array.new_float(0)
var bool[] mvScaleReached = array.new_bool(0)
var bool[] mvScaleBadged = array.new_bool(0)
mvGoalReached = false
mvStopHit = false
mvFlippedOut = false
mvPrevSide = ''
// Evaluate Active Exits
if mvInMarket and mvSide == 'LONG'
if low <= mvTrailStopPx
mvStopHit := true
mvInMarket := false
mvSide := ''
else if high >= mvPrimaryGoalPx
mvGoalReached := true
mvInMarket := false
mvSide := ''
if mvInMarket and mvSide == 'SHORT'
if high >= mvTrailStopPx
mvStopHit := true
mvInMarket := false
mvSide := ''
else if low <= mvPrimaryGoalPx
mvGoalReached := true
mvInMarket := false
mvSide := ''
// Ratchet Dynamic Trailing Stop
if mvInMarket and mvTrailActive
if mvSide == 'LONG'
float mvOpenR = (high - mvFillPrice) / math.max(mvUnitRiskPts, syminfo.mintick)
if mvOpenR >= mvTrailEngageR
float mvNextStop = high - (mvVolRange * mvAtrStopMult)
mvTrailStopPx := math.max(mvTrailStopPx, mvNextStop)
else if mvSide == 'SHORT'
float mvOpenR = (mvFillPrice - low) / math.max(mvUnitRiskPts, syminfo.mintick)
if mvOpenR >= mvTrailEngageR
float mvNextStop = low + (mvVolRange * mvAtrStopMult)
mvTrailStopPx := math.min(mvTrailStopPx, mvNextStop)
// Track Intermediate Scale-Out Milestones
if mvInMarket and array.size(mvScalePrices) > 0
for mvI = 0 to array.size(mvScalePrices) - 1
if not array.get(mvScaleReached, mvI)
if (mvSide == 'LONG' and high >= array.get(mvScalePrices, mvI)) or (mvSide == 'SHORT' and low <= array.get(mvScalePrices, mvI))
array.set(mvScaleReached, mvI, true)
// Handle New Entries & Vector Reversals
mvTriggerLong = false
mvTriggerShort = false
if mvGoLong
if not mvInMarket or mvSide != 'LONG'
if mvInMarket and mvSide == 'SHORT'
mvFlippedOut := true
mvPrevSide := 'SHORT'
mvFillPrice := close
mvTrailStopPx := low - (mvVolRange * mvAtrStopMult)
mvUnitRiskPts := math.max(mvFillPrice - mvTrailStopPx, syminfo.mintick * 4)
mvPrimaryGoalPx := mvFillPrice + (mvUnitRiskPts * mvTargetMultR)
array.clear(mvScalePrices)
array.clear(mvScaleReached)
array.clear(mvScaleBadged)
int mvMilestones = math.floor(mvTargetMultR)
if mvMilestones > 1
for mvK = 1 to mvMilestones - 1
array.push(mvScalePrices, mvFillPrice + (mvUnitRiskPts * mvK))
array.push(mvScaleReached, false)
array.push(mvScaleBadged, false)
mvInMarket := true
mvSide := 'LONG'
mvTriggerLong := true
if mvGoShort
if not mvInMarket or mvSide != 'SHORT'
if mvInMarket and mvSide == 'LONG'
mvFlippedOut := true
mvPrevSide := 'LONG'
mvFillPrice := close
mvTrailStopPx := high + (mvVolRange * mvAtrStopMult)
mvUnitRiskPts := math.max(mvTrailStopPx - mvFillPrice, syminfo.mintick * 4)
mvPrimaryGoalPx := mvFillPrice - (mvUnitRiskPts * mvTargetMultR)
array.clear(mvScalePrices)
array.clear(mvScaleReached)
array.clear(mvScaleBadged)
int mvMilestones = math.floor(mvTargetMultR)
if mvMilestones > 1
for mvK = 1 to mvMilestones - 1
array.push(mvScalePrices, mvFillPrice - (mvUnitRiskPts * mvK))
array.push(mvScaleReached, false)
array.push(mvScaleBadged, false)
mvInMarket := true
mvSide := 'SHORT'
mvTriggerShort := true
// ============================================
// 4. RIBBON & EVENT MARKERS
// ============================================
mvPlotFast = plot(mvFastVec, title='Fast Vector', color=mvBullState ? color.new(#00e5ff, 0) : color.new(#ff2a85, 0), linewidth=2)
mvPlotAnchor = plot(mvAnchorVec, title='Structural Anchor', color=mvBullState ? color.new(#00e5ff, 45) : color.new(#ff2a85, 45), linewidth=1)
fill(mvPlotFast, mvPlotAnchor, color=mvDrawCloud ? (mvBullState ? mvBullCloudCol : mvBearCloudCol) : na, title='Vector Cloud')
// Directional Entry Markers
plotshape(mvTriggerLong, title='Long Entry', style=shape.triangleup, location=location.belowbar, color=color.new(#00e5ff, 0), text='LONG', textcolor=color.new(#00e5ff, 0), size=size.normal)
plotshape(mvTriggerShort, title='Short Entry', style=shape.triangledown, location=location.abovebar, color=color.new(#ff2a85, 0), text='SHORT', textcolor=color.new(#ff2a85, 0), size=size.normal)
// Outcome Markers
plotshape(mvGoalReached, title='Objective Reached', style=shape.circle, location=location.abovebar, color=color.new(#00e5ff, 0), text='OBJ HIT', textcolor=color.new(#00e5ff, 0), size=size.normal)
plotshape(mvStopHit, title='Stop Triggered', style=shape.xcross, location=location.belowbar, color=color.new(#ff2a85, 0), text='STOPPED', textcolor=color.white, size=size.normal)
plotshape(mvFlippedOut and mvPrevSide == 'LONG', title='Long Reversed', style=shape.diamond, location=location.abovebar, color=#ffbf00, text='REV', textcolor=#ffbf00, size=size.small)
plotshape(mvFlippedOut and mvPrevSide == 'SHORT', title='Short Reversed', style=shape.diamond, location=location.belowbar, color=#ffbf00, text='REV', textcolor=#ffbf00, size=size.small)
// Scale-Out Milestone Badges
if mvInMarket and array.size(mvScaleReached) > 0
for mvI = 0 to array.size(mvScaleReached) - 1
if array.get(mvScaleReached, mvI) and not array.get(mvScaleBadged, mvI)
label.new(bar_index, mvSide == 'LONG' ? high : low, 'SCALE ' + str.tostring(mvI + 1),
style=mvSide == 'LONG' ? label.style_label_down : label.style_label_up,
color=color.new(#00b8d4, 25), textcolor=color.white, size=size.small)
array.set(mvScaleBadged, mvI, true)
// ============================================
// 5. DYNAMIC LEVEL RAYS & LARGE UNIFORM LABELS
// ============================================
var line mvLineFill = na
var line mvLineStop = na
var line mvLineGoal = na
var label mvLblFill = na
var label mvLblStop = na
var label mvLblGoal = na
var line[] mvScaleLines = array.new_line(0)
var label[] mvScaleLabels = array.new_label(0)
f_mvFmt(_price) => str.tostring(_price, format.mintick)
if mvTriggerLong or mvTriggerShort
if array.size(mvScaleLines) > 0
for mvI = 0 to array.size(mvScaleLines) - 1
line.delete(array.get(mvScaleLines, mvI))
array.clear(mvScaleLines)
if array.size(mvScaleLabels) > 0
for mvI = 0 to array.size(mvScaleLabels) - 1
label.delete(array.get(mvScaleLabels, mvI))
array.clear(mvScaleLabels)
if mvInMarket and mvSide != ''
int mvX0 = bar_index
int mvX1 = bar_index + mvProjectBars
// --- Fill Reference Line & Tag ---
if na(mvLineFill)
mvLineFill := line.new(mvX0, mvFillPrice, mvX1, mvFillPrice, color=color.new(color.white, 20), width=1, style=line.style_dashed)
else
line.set_xy1(mvLineFill, mvX0, mvFillPrice)
line.set_xy2(mvLineFill, mvX1, mvFillPrice)
if mvShowPriceTags
string mvFillText = 'FILL: ' + f_mvFmt(mvFillPrice)
if na(mvLblFill)
mvLblFill := label.new(mvX1, mvFillPrice, mvFillText, style=label.style_label_left, color=color.new(#546e7a, 15), textcolor=color.white, size=size.normal)
else
label.set_xy(mvLblFill, mvX1, mvFillPrice)
label.set_text(mvLblFill, mvFillText)
// --- Dynamic Trailing Stop Line & Tag ---
if na(mvLineStop)
mvLineStop := line.new(mvX0, mvTrailStopPx, mvX1, mvTrailStopPx, color=color.new(#ff2a85, 0), width=2, style=line.style_solid)
else
line.set_xy1(mvLineStop, mvX0, mvTrailStopPx)
line.set_xy2(mvLineStop, mvX1, mvTrailStopPx)
if mvShowPriceTags
bool mvBeSecured = (mvSide == 'LONG' and mvTrailStopPx >= mvFillPrice) or (mvSide == 'SHORT' and mvTrailStopPx <= mvFillPrice)
string mvStopHead = mvTrailActive ? (mvBeSecured ? 'PROTECTED: ' : 'TRAIL STOP: ') : 'HARD STOP: '
string mvStopText = mvStopHead + f_mvFmt(mvTrailStopPx)
color mvStopTint = mvBeSecured ? color.new(#ffbf00, 10) : color.new(#ff2a85, 10)
if na(mvLblStop)
mvLblStop := label.new(mvX1, mvTrailStopPx, mvStopText, style=label.style_label_left, color=mvStopTint, textcolor=color.white, size=size.normal)
else
label.set_xy(mvLblStop, mvX1, mvTrailStopPx)
label.set_text(mvLblStop, mvStopText)
label.set_color(mvLblStop, mvStopTint)
// --- Primary Objective Line & Tag ---
if na(mvLineGoal)
mvLineGoal := line.new(mvX0, mvPrimaryGoalPx, mvX1, mvPrimaryGoalPx, color=color.new(#00e5ff, 0), width=2, style=line.style_solid)
else
line.set_xy1(mvLineGoal, mvX0, mvPrimaryGoalPx)
line.set_xy2(mvLineGoal, mvX1, mvPrimaryGoalPx)
if mvShowPriceTags
string mvGoalText = 'TARGET (' + str.tostring(mvTargetMultR, '#.#') + 'R): ' + f_mvFmt(mvPrimaryGoalPx)
if na(mvLblGoal)
mvLblGoal := label.new(mvX1, mvPrimaryGoalPx, mvGoalText, style=label.style_label_left, color=color.new(#0097a7, 10), textcolor=color.white, size=size.normal)
else
label.set_xy(mvLblGoal, mvX1, mvPrimaryGoalPx)
label.set_text(mvLblGoal, mvGoalText)
// --- Intermediate Scale-Out Lines & Uniform Tags ---
if array.size(mvScaleLines) == 0 and array.size(mvScalePrices) > 0
for mvI = 0 to array.size(mvScalePrices) - 1
float mvLvl = array.get(mvScalePrices, mvI)
array.push(mvScaleLines, line.new(mvX0, mvLvl, mvX1, mvLvl, color=color.new(#00e5ff, 55), width=1, style=line.style_dotted))
if mvShowPriceTags
string mvScaleText = 'TP' + str.tostring(mvI + 1) + ' (' + str.tostring(mvI + 1) + 'R): ' + f_mvFmt(mvLvl)
array.push(mvScaleLabels, label.new(mvX1, mvLvl, mvScaleText, style=label.style_label_left, color=color.new(#0097a7, 35), textcolor=color.white, size=size.normal))
else if array.size(mvScaleLines) > 0
for mvI = 0 to array.size(mvScaleLines) - 1
float mvLvl = array.get(mvScalePrices, mvI)
line.set_xy1(array.get(mvScaleLines, mvI), mvX0, mvLvl)
line.set_xy2(array.get(mvScaleLines, mvI), mvX1, mvLvl)
if mvShowPriceTags and mvI < array.size(mvScaleLabels)
string mvScaleText = 'TP' + str.tostring(mvI + 1) + ' (' + str.tostring(mvI + 1) + 'R): ' + f_mvFmt(mvLvl)
label.set_xy(array.get(mvScaleLabels, mvI), mvX1, mvLvl)
label.set_text(array.get(mvScaleLabels, mvI), mvScaleText)
else
if not na(mvLineFill)
line.delete(mvLineFill), mvLineFill := na
if not na(mvLineStop)
line.delete(mvLineStop), mvLineStop := na
if not na(mvLineGoal)
line.delete(mvLineGoal), mvLineGoal := na
if not na(mvLblFill)
label.delete(mvLblFill), mvLblFill := na
if not na(mvLblStop)
label.delete(mvLblStop), mvLblStop := na
if not na(mvLblGoal)
label.delete(mvLblGoal), mvLblGoal := na
if array.size(mvScaleLines) > 0
for mvI = 0 to array.size(mvScaleLines) - 1
line.delete(array.get(mvScaleLines, mvI))
array.clear(mvScaleLines)
if array.size(mvScaleLabels) > 0
for mvI = 0 to array.size(mvScaleLabels) - 1
label.delete(array.get(mvScaleLabels, mvI))
array.clear(mvScaleLabels)
// ============================================
// 6. PROP RISK & SIZING TELEMETRY PANEL
// ============================================
var table mvTelemetry = table.new(position.top_right, 2, 6, bgcolor=color.new(color.black, 82), border_width=1, border_color=color.new(color.gray, 60))
if barstate.islast
float mvPointVal = syminfo.pointvalue
float mvStopSpan = mvInMarket ? math.abs(mvFillPrice - mvTrailStopPx) : (mvVolRange * mvAtrStopMult)
float mvRiskPerCt = mvStopSpan * mvPointVal
int mvAllowedCts = mvRiskPerCt > 0 ? math.max(1, math.floor(mvMaxTradeUsd / mvRiskPerCt)) : 1
table.cell(mvTelemetry, 0, 0, 'Vector State:', text_color=color.white, text_size=size.normal)
table.cell(mvTelemetry, 1, 0, mvInMarket ? mvSide : (mvBullState ? 'BULL VECTOR' : 'BEAR VECTOR'),
text_color=mvInMarket ? #ffbf00 : (mvBullState ? color.aqua : color.fuchsia), text_size=size.normal)
table.cell(mvTelemetry, 0, 1, 'Reference Fill:', text_color=color.white, text_size=size.normal)
table.cell(mvTelemetry, 1, 1, mvInMarket ? f_mvFmt(mvFillPrice) : '—', text_color=color.white, text_size=size.normal)
table.cell(mvTelemetry, 0, 2, 'Active Stop:', text_color=color.white, text_size=size.normal)
table.cell(mvTelemetry, 1, 2, mvInMarket ? f_mvFmt(mvTrailStopPx) : '—', text_color=color.fuchsia, text_size=size.normal)
table.cell(mvTelemetry, 0, 3, 'Objective:', text_color=color.white, text_size=size.normal)
table.cell(mvTelemetry, 1, 3, mvInMarket ? f_mvFmt(mvPrimaryGoalPx) : '—', text_color=color.aqua, text_size=size.normal)
table.cell(mvTelemetry, 0, 4, '1-Ct Dollar Risk:', text_color=color.white, text_size=size.normal)
table.cell(mvTelemetry, 1, 4, '$' + str.tostring(mvRiskPerCt, '#.##'), text_color=mvRiskPerCt > mvMaxTradeUsd ? color.red : color.aqua, text_size=size.normal)
table.cell(mvTelemetry, 0, 5, 'Size Cap ($' + str.tostring(mvMaxTradeUsd, '#') + '):', text_color=color.white, text_size=size.normal)
table.cell(mvTelemetry, 1, 5, str.tostring(mvAllowedCts) + ' Cts', text_color=color.orange, text_size=size.normal)
// ============================================
// 7. ALERT CONDITIONS
// ============================================
alertcondition(mvTriggerLong, 'Vector Long Trigger', 'Long vector triggered on {{ticker}} at {{close}}')
alertcondition(mvTriggerShort, 'Vector Short Trigger', 'Short vector triggered on {{ticker}} at {{close}}')
alertcondition(mvGoalReached, 'Primary Objective Hit', 'Target objective reached on {{ticker}} at {{close}}')
alertcondition(mvStopHit, 'Trailing Stop Hit', 'Dynamic stop triggered on {{ticker}} at {{close}}')