本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Trade Compass Pro | Regime + Signals + Risk", shorttitle="Trade Compass", overlay=true, max_labels_count=500)
// ==========================================
// INPUTS
// ==========================================
groupTrend = "1. Trend & Market Regime"
fastLen = input.int(9, "Fast EMA", minval=1, group=groupTrend)
midLen = input.int(21, "Trend EMA", minval=1, group=groupTrend)
slowLen = input.int(50, "Slow EMA", minval=1, group=groupTrend)
diLen = input.int(14, "DI Length", minval=1, group=groupTrend)
adxSmooth = input.int(14, "ADX Smoothing", minval=1, group=groupTrend)
adxMin = input.float(20.0, "Minimum ADX", minval=1, group=groupTrend)
rsiLen = input.int(14, "RSI Length", minval=1, group=groupTrend)
groupSignals = "2. Entry Filters"
useVWAP = input.bool(false, "Require VWAP Alignment", group=groupSignals)
useRSI = input.bool(true, "Require RSI Confirmation", group=groupSignals)
showSignals = input.bool(true, "Show Entry Signals", group=groupSignals)
allowEntries = input.bool(true, "Enable New Trade Tracking", group=groupSignals)
groupRisk = "3. Risk Management"
riskDollars = input.float(200, "Risk Per Trade ($)", minval=0.01, group=groupRisk)
atrLen = input.int(14, "ATR Length", minval=1, group=groupRisk)
atrMult = input.float(1.5, "Stop Distance (ATR)", minval=0.1, step=0.1, group=groupRisk)
rewardR = input.float(2.0, "Reward / Risk", minval=0.1, step=0.1, group=groupRisk)
showTradeLevels = input.bool(true, "Show Entry, Stop & Target", group=groupRisk)
groupVisual = "4. Visual Settings"
showEMAs = input.bool(true, "Show Moving Averages", group=groupVisual)
showVWAP = input.bool(true, "Show VWAP", group=groupVisual)
showDashboard = input.bool(true, "Show Dashboard", group=groupVisual)
colorBars = input.bool(false, "Color Bars by Regime", group=groupVisual)
// ==========================================
// CALCULATIONS
// ==========================================
emaFast = ta.ema(close, fastLen)
emaMid = ta.ema(close, midLen)
emaSlow = ta.ema(close, slowLen)
vwapValue = ta.vwap(hlc3)
[diPlus, diMinus, adxValue] = ta.dmi(diLen, adxSmooth)
rsiValue = ta.rsi(close, rsiLen)
atrValue = ta.atr(atrLen)
atrAverage = ta.sma(atrValue, 50)
bool vwapAvailable = timeframe.isintraday and not na(vwapValue)
bool vwapLongOK = not useVWAP or (vwapAvailable and close > vwapValue)
bool vwapShortOK = not useVWAP or (vwapAvailable and close < vwapValue)
bool rsiLongOK = not useRSI or rsiValue > 50
bool rsiShortOK = not useRSI or rsiValue < 50
// ==========================================
// MARKET REGIME
// ==========================================
bool bullishStructure = emaFast > emaMid and emaMid > emaSlow
bool bearishStructure = emaFast < emaMid and emaMid < emaSlow
bool bullishMomentum = diPlus > diMinus
bool bearishMomentum = diMinus > diPlus
bool strongTrend = adxValue >= adxMin
bool bullRegime = bullishStructure and bullishMomentum and strongTrend
bool bearRegime = bearishStructure and bearishMomentum and strongTrend
bool chopRegime = not bullRegime and not bearRegime
string regimeText = bullRegime ? "BULLISH TREND" : bearRegime ? "BEARISH TREND" : "NO TRADE / MIXED"
color regimeColor = bullRegime ? color.lime : bearRegime ? color.red : color.orange
// ==========================================
// ENTRY SIGNALS
// ==========================================
// Signals require a confirmed candle crossing
// back through the fast EMA in trend direction.
bool crossedUp = ta.crossover(close, emaFast)
bool crossedDown = ta.crossunder(close, emaFast)
bool longSetup = bullRegime and crossedUp and close > emaMid and vwapLongOK and rsiLongOK
bool shortSetup = bearRegime and crossedDown and close < emaMid and vwapShortOK and rsiShortOK
bool confirmedLong = barstate.isconfirmed and longSetup
bool confirmedShort = barstate.isconfirmed and shortSetup
// ==========================================
// ACTIVE TRADE MANAGEMENT
// ==========================================
// This is an indicator, not a broker execution
// engine or an official strategy backtest.
var int tradeDir = 0
var float entryPrice = na
var float stopPrice = na
var float targetPrice = na
var float positionQty = na
bool stopHit = false
bool targetHit = false
// Process exits only after the signal bar.
if tradeDir != 0
bool canExit = bar_index > nz(ta.valuewhen(tradeDir == 0, bar_index, 0), -1)
if tradeDir == 1
stopHit := low <= stopPrice
targetHit := high >= targetPrice
else
stopHit := high >= stopPrice
targetHit := low <= targetPrice
// If both levels were touched during one bar,
// conservatively classify it as a stop.
if stopHit or targetHit
if stopHit
targetHit := false
tradeDir := 0
entryPrice := na
stopPrice := na
targetPrice := na
positionQty := na
bool newLong = false
bool newShort = false
float pointValue = syminfo.pointvalue
float stopDistance = atrValue * atrMult
float riskPerUnit = stopDistance * pointValue
if allowEntries and tradeDir == 0 and not stopHit and not targetHit and stopDistance > 0
if confirmedLong and vwapLongOK
tradeDir := 1
entryPrice := close
stopPrice := math.round_to_mintick(close - stopDistance)
targetPrice := math.round_to_mintick(close + stopDistance * rewardR)
positionQty := riskPerUnit > 0 ? math.floor(riskDollars / riskPerUnit) : na
newLong := true
else if confirmedShort and vwapShortOK
tradeDir := -1
entryPrice := close
stopPrice := math.round_to_mintick(close + stopDistance)
targetPrice := math.round_to_mintick(close - stopDistance * rewardR)
positionQty := riskPerUnit > 0 ? math.floor(riskDollars / riskPerUnit) : na
newShort := true
// ==========================================
// CHART VISUALS
// ==========================================
plot(showEMAs ? emaFast : na, "Fast EMA", color=color.aqua, linewidth=2)
plot(showEMAs ? emaMid : na, "Trend EMA", color=color.yellow, linewidth=2)
plot(showEMAs ? emaSlow : na, "Slow EMA", color=color.gray, linewidth=2)
plot(showVWAP and vwapAvailable ? vwapValue : na, "VWAP", color=color.purple, linewidth=2)
barcolor(colorBars ? color.new(regimeColor, 55) : na)
plotshape(showSignals and newLong, title="LONG", style=shape.labelup, location=location.belowbar, color=color.lime, text="LONG", textcolor=color.black, size=size.small)
plotshape(showSignals and newShort, title="SHORT", style=shape.labeldown, location=location.abovebar, color=color.red, text="SHORT", textcolor=color.white, size=size.small)
plot(showTradeLevels and tradeDir != 0 ? entryPrice : na, "Active Entry", color=color.white, linewidth=2, style=plot.style_linebr)
plot(showTradeLevels and tradeDir != 0 ? stopPrice : na, "Active Stop", color=color.red, linewidth=2, style=plot.style_linebr)
plot(showTradeLevels and tradeDir != 0 ? targetPrice : na, "Active Target", color=color.lime, linewidth=2, style=plot.style_linebr)
// ==========================================
// DASHBOARD
// ==========================================
var table dash = table.new(position.top_right, 2, 9, border_width=1)
if barstate.islast and showDashboard
color bg = color.new(color.black, 15)
color hdr = color.new(color.blue, 55)
string directionText = tradeDir == 1 ? "LONG ACTIVE" : tradeDir == -1 ? "SHORT ACTIVE" : "FLAT"
string atrText = str.tostring(atrValue, format.mintick)
string qtyText = tradeDir != 0 ? str.tostring(positionQty, "#") : str.tostring(riskPerUnit > 0 ? math.floor(riskDollars / riskPerUnit) : 0, "#")
string volatilityText = na(atrAverage) ? "CALCULATING" : atrValue > atrAverage * 1.3 ? "HIGH" : atrValue < atrAverage * 0.7 ? "LOW" : "NORMAL"
table.cell(dash, 0, 0, "TRADE COMPASS", bgcolor=hdr, text_color=color.white)
table.cell(dash, 1, 0, "PRO", bgcolor=hdr, text_color=color.white)
table.cell(dash, 0, 1, "Market Regime", bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 1, regimeText, bgcolor=bg, text_color=regimeColor)
table.cell(dash, 0, 2, "ADX", bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 2, str.tostring(adxValue, "#.0"), bgcolor=bg, text_color=strongTrend ? color.lime : color.orange)
table.cell(dash, 0, 3, "RSI", bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 3, str.tostring(rsiValue, "#.0"), bgcolor=bg, text_color=rsiValue > 50 ? color.lime : color.red)
table.cell(dash, 0, 4, "Volatility", bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 4, volatilityText, bgcolor=bg, text_color=color.white)
table.cell(dash, 0, 5, "ATR", bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 5, atrText, bgcolor=bg, text_color=color.white)
table.cell(dash, 0, 6, "Trade Status", bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 6, directionText, bgcolor=bg, text_color=tradeDir == 0 ? color.gray : color.lime)
table.cell(dash, 0, 7, "Risk Budget", bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 7, "$" + str.tostring(riskDollars, "#.00"), bgcolor=bg, text_color=color.white)
table.cell(dash, 0, 8, "Max Units (est.)", bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 8, qtyText, bgcolor=bg, text_color=color.aqua)
// ==========================================
// ALERTS
// ==========================================
alertcondition(newLong, title="Trade Compass LONG", message="Trade Compass LONG on {{ticker}} at {{close}}")
alertcondition(newShort, title="Trade Compass SHORT", message="Trade Compass SHORT on {{ticker}} at {{close}}")
alertcondition(stopHit, title="Trade Compass STOP", message="Trade Compass stop touched on {{ticker}}")
alertcondition(targetHit, title="Trade Compass TARGET", message="Trade Compass target touched on {{ticker}}")
alertcondition(barstate.isconfirmed and chopRegime, title="Trade Compass No Trend", message="Trade Compass indicates mixed or weak trend conditions on {{ticker}}")