本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("XAUUSD M15 EMA40 - Easy View + Performance", overlay=true, max_labels_count=500, max_lines_count=500)
//=====================================================
// 1. SETTINGS
//=====================================================
emaLength = input.int(40, "EMA Length", minval=1)
pipSize = input.float(0.10, "Gold Pip Size", minval=0.01, step=0.01)
initialOffsetPips = input.float(350, "Initial Entry Distance From EMA (pips)", minval=1)
reentryOffsetPips = input.float(350, "Re-entry Distance After Stop Loss (pips)", minval=1)
breakevenReentryPips = input.float(100, "Re-entry Distance After Breakeven (pips)", minval=1)
stopLossPips = input.float(120, "Stop Loss (pips)", minval=1)
takeProfitPips = input.float(250, "Take Profit (pips)", minval=1)
stage1TriggerPips = input.float(50, "Protection Stage 1 Trigger (pips)", minval=1)
stage1OffsetPips = input.float(30, "Stage 1 Stop Offset From Candle Close (pips)", minval=1)
stage2TriggerPips = input.float(100, "Breakeven Trigger (pips)", minval=1)
maxReentries = input.int(3, "Maximum Re-entries Per Sequence", minval=0, maxval=10)
// Performance settings
riskPct = input.float(1.0, "Fixed Risk Per Trade (% of reference capital)", minval=0.01, step=0.1, tooltip="Used to convert simulated trade results into percentage returns. This is a reference risk unit, not a broker account balance. Each trade uses the same fixed risk amount; results do not compound.")
showPerformance = input.bool(true, "Show Performance Summary")
showEMA = input.bool(true, "Show EMA 40")
showSetupSignals = input.bool(true, "Show BUY / SELL Setup Labels")
showStatusPanel = input.bool(true, "Show Status Panel")
showPriceLabels = input.bool(true, "Show Entry / Stop / Target Labels")
showTradeLines = input.bool(true, "Show Entry / Stop / Target Lines")
keepCompletedTrades = input.bool(false, "Keep Completed Trade Lines")
lineWidth = input.int(2, "Price Line Width", minval=1, maxval=5)
//=====================================================
// 2. COLOURS
//=====================================================
color buyColor = color.rgb(0, 210, 80)
color sellColor = color.rgb(255, 55, 55)
color entryColor = color.rgb(70, 130, 230)
color stopColor = color.rgb(240, 55, 55)
color targetColor = color.rgb(0, 200, 90)
color beColor = color.rgb(170, 170, 170)
color panelBg = color.rgb(35, 39, 48)
color panelText = color.white
//=====================================================
// 3. TIMEFRAME AND EMA
//=====================================================
bool isM15 = timeframe.isminutes and timeframe.multiplier == 15
float emaValue = ta.ema(close, emaLength)
plot(showEMA ? emaValue : na, title="EMA 40", color=color.orange, linewidth=2)
var label timeframeWarning = na
if barstate.islast
if not isM15
if na(timeframeWarning)
timeframeWarning := label.new(bar_index, high, "PLEASE USE M15 CHART", style=label.style_label_down, color=color.red, textcolor=color.white, size=size.normal)
else
label.set_xy(timeframeWarning, bar_index, high)
else
if not na(timeframeWarning)
label.delete(timeframeWarning)
timeframeWarning := na
//=====================================================
// 4. PRICE DISTANCES
//=====================================================
float initialDistance = initialOffsetPips * pipSize
float reentryDistance = reentryOffsetPips * pipSize
float beReentryDistance = breakevenReentryPips * pipSize
float slDistance = stopLossPips * pipSize
float tpDistance = takeProfitPips * pipSize
float stage1Trigger = stage1TriggerPips * pipSize
float stage1Offset = stage1OffsetPips * pipSize
float stage2Trigger = stage2TriggerPips * pipSize
float initialLongLevel = emaValue - initialDistance
float initialShortLevel = emaValue + initialDistance
//=====================================================
// 5. SEQUENCE STATE
//=====================================================
var bool sequenceActive = false
var int sequenceDirection = 0
var int reentriesUsed = 0
// Previous exit: 1 = original SL, 2 = TP, 3 = managed stop
var float previousEntryPrice = na
var int previousExitType = 0
//=====================================================
// 6. PENDING ENTRY STATE
//=====================================================
var bool pendingOrder = false
var int pendingDirection = 0
var float pendingEntry = na
var int pendingBar = na
var bool pendingIsReentry = false
//=====================================================
// 7. ACTIVE TRADE STATE
//=====================================================
var bool inTrade = false
var int tradeDirection = 0
var int fillBar = na
var float entryPrice = na
var float currentSL = na
var float takeProfit = na
var bool stage1Activated = false
var bool stage2Activated = false
//=====================================================
// 8. PERFORMANCE COUNTERS
// These accumulate for all completed simulated trades.
// They are not reset when a sequence ends.
//=====================================================
var int totalTrades = 0
var int totalWins = 0
var int totalLosses = 0
var int totalBreakeven = 0
// Sum of each trade's return in percentage points of reference capital.
var float netReturnPct = 0.0
var float grossProfitPct = 0.0
var float grossLossPct = 0.0
//=====================================================
// 9. DRAWING OBJECTS
//=====================================================
var line entryLine = na
var line slLine = na
var line tpLine = na
var line beLine = na
var label entryLabel = na
var label slLabel = na
var label tpLabel = na
// 14 rows: status, trade details, re-entry count and performance.
var table statusPanel = table.new(position.top_right, 2, 14, bgcolor=panelBg, frame_color=color.gray, frame_width=1, border_width=1)
//=====================================================
// 10. INITIAL SETUPS
//=====================================================
bool longSetup = isM15 and low <= initialLongLevel and not sequenceActive and not inTrade and not pendingOrder
bool shortSetup = isM15 and high >= initialShortLevel and not sequenceActive and not inTrade and not pendingOrder
bool insideEMAZone = close > initialLongLevel and close < initialShortLevel
if isM15 and not inTrade and not pendingOrder and insideEMAZone
sequenceActive := false
sequenceDirection := 0
reentriesUsed := 0
previousExitType := 0
previousEntryPrice := na
if longSetup
sequenceActive := true
sequenceDirection := 1
reentriesUsed := 0
pendingOrder := true
pendingDirection := 1
pendingEntry := math.min(open, close)
pendingBar := bar_index
pendingIsReentry := false
else if shortSetup
sequenceActive := true
sequenceDirection := -1
reentriesUsed := 0
pendingOrder := true
pendingDirection := -1
pendingEntry := math.max(open, close)
pendingBar := bar_index
pendingIsReentry := false
//=====================================================
// 11. RE-ENTRY SETUPS
//=====================================================
bool canReenter = isM15 and sequenceActive and not inTrade and not pendingOrder and reentriesUsed < maxReentries
float longReentryLevel = previousExitType == 3 and not na(previousEntryPrice) ? previousEntryPrice - beReentryDistance : emaValue - reentryDistance
float shortReentryLevel = previousExitType == 3 and not na(previousEntryPrice) ? previousEntryPrice + beReentryDistance : emaValue + reentryDistance
bool longReentrySetup = canReenter and sequenceDirection == 1 and low <= longReentryLevel
bool shortReentrySetup = canReenter and sequenceDirection == -1 and high >= shortReentryLevel
if longReentrySetup
pendingOrder := true
pendingDirection := 1
pendingEntry := math.min(open, close)
pendingBar := bar_index
pendingIsReentry := true
else if shortReentrySetup
pendingOrder := true
pendingDirection := -1
pendingEntry := math.max(open, close)
pendingBar := bar_index
pendingIsReentry := true
//=====================================================
// 12. BUY / SELL LABELS
//=====================================================
bool showLongSignal = longSetup or longReentrySetup
bool showShortSignal = shortSetup or shortReentrySetup
plotshape(showSetupSignals and showLongSignal, title="BUY Setup", style=shape.labelup, location=location.belowbar, color=buyColor, text="BUY", textcolor=color.black, size=size.normal)
plotshape(showSetupSignals and showShortSignal, title="SELL Setup", style=shape.labeldown, location=location.abovebar, color=sellColor, text="SELL", textcolor=color.white, size=size.normal)
//=====================================================
// 13. SIMULATED ENTRY
// Check the pending entry on the next candle only.
//=====================================================
if pendingOrder and bar_index > pendingBar
bool orderTouched = pendingDirection == 1 ? low <= pendingEntry : high >= pendingEntry
if orderTouched
inTrade := true
tradeDirection := pendingDirection
fillBar := bar_index
entryPrice := pendingEntry
currentSL := tradeDirection == 1 ? entryPrice - slDistance : entryPrice + slDistance
takeProfit := tradeDirection == 1 ? entryPrice + tpDistance : entryPrice - tpDistance
stage1Activated := false
stage2Activated := false
if pendingIsReentry
reentriesUsed += 1
if showTradeLines
entryLine := line.new(bar_index, entryPrice, bar_index + 1, entryPrice, color=entryColor, width=1, style=line.style_dotted)
slLine := line.new(bar_index, currentSL, bar_index + 1, currentSL, color=stopColor, width=lineWidth)
tpLine := line.new(bar_index, takeProfit, bar_index + 1, takeProfit, color=targetColor, width=lineWidth)
if showPriceLabels
entryLabel := label.new(bar_index + 1, entryPrice, "ENTRY " + str.tostring(entryPrice, format.mintick), style=label.style_label_left, color=entryColor, textcolor=color.white, size=size.small)
slLabel := label.new(bar_index + 1, currentSL, "STOP LOSS " + str.tostring(currentSL, format.mintick), style=label.style_label_left, color=stopColor, textcolor=color.white, size=size.small)
tpLabel := label.new(bar_index + 1, takeProfit, "TAKE PROFIT " + str.tostring(takeProfit, format.mintick), style=label.style_label_left, color=targetColor, textcolor=color.black, size=size.small)
beLine := na
pendingOrder := false
pendingDirection := 0
pendingEntry := na
pendingBar := na
pendingIsReentry := false
//=====================================================
// 14. EXIT VARIABLES
//=====================================================
bool exitNow = false
int exitType = 0
float exitPrice = na
//=====================================================
// 15. TRADE MANAGEMENT
//=====================================================
if inTrade
if showTradeLines
if not na(entryLine)
line.set_x2(entryLine, bar_index)
if not na(slLine)
line.set_x2(slLine, bar_index)
line.set_y2(slLine, currentSL)
if not na(tpLine)
line.set_x2(tpLine, bar_index)
line.set_y2(tpLine, takeProfit)
if showPriceLabels
if not na(entryLabel)
label.set_x(entryLabel, bar_index + 1)
label.set_y(entryLabel, entryPrice)
if not na(slLabel)
label.set_x(slLabel, bar_index + 1)
label.set_y(slLabel, currentSL)
label.set_text(slLabel, (stage2Activated ? "BREAKEVEN " : "STOP LOSS ") + str.tostring(currentSL, format.mintick))
if not na(tpLabel)
label.set_x(tpLabel, bar_index + 1)
label.set_y(tpLabel, takeProfit)
bool slTouched = tradeDirection == 1 ? low <= currentSL : high >= currentSL
bool tpTouched = tradeDirection == 1 ? high >= takeProfit : low <= takeProfit
// If both are touched in one candle, assume SL first.
if slTouched
exitNow := true
exitType := (stage1Activated or stage2Activated) ? 3 : 1
exitPrice := currentSL
else if tpTouched
exitNow := true
exitType := 2
exitPrice := takeProfit
else
float favorableMove = tradeDirection == 1 ? high - entryPrice : entryPrice - low
// Stage 1: adjust stop relative to trigger candle close.
if not stage1Activated and favorableMove >= stage1Trigger
stage1Activated := true
currentSL := tradeDirection == 1 ? close + stage1Offset : close - stage1Offset
if not na(slLine)
line.set_y1(slLine, currentSL)
line.set_y2(slLine, currentSL)
if not na(slLabel)
label.set_y(slLabel, currentSL)
label.set_text(slLabel, "STOP LOSS " + str.tostring(currentSL, format.mintick))
// Stage 2: move stop to entry price.
if not stage2Activated and favorableMove >= stage2Trigger
stage2Activated := true
currentSL := entryPrice
if not na(slLine)
line.set_y1(slLine, currentSL)
line.set_y2(slLine, currentSL)
line.set_color(slLine, beColor)
if not na(slLabel)
label.set_y(slLabel, currentSL)
label.set_text(slLabel, "BREAKEVEN " + str.tostring(currentSL, format.mintick))
label.set_color(slLabel, beColor)
label.set_textcolor(slLabel, color.black)
if na(beLine) and showTradeLines
beLine := line.new(fillBar, entryPrice, bar_index, entryPrice, color=beColor, width=2, style=line.style_dashed)
//=====================================================
// 16. EXIT AND PERFORMANCE ACCOUNTING
//=====================================================
if inTrade and exitNow
// Close active lines at the exit candle.
if not na(entryLine)
line.set_x2(entryLine, bar_index)
if not na(slLine)
line.set_x2(slLine, bar_index)
if not na(tpLine)
line.set_x2(tpLine, bar_index)
if not na(beLine)
line.set_x2(beLine, bar_index)
// Calculate trade return using the actual simulated exit price.
// Initial SL distance defines one full risk unit.
float priceMove = tradeDirection == 1 ? exitPrice - entryPrice : entryPrice - exitPrice
float tradeReturnPct = (priceMove / slDistance) * riskPct
totalTrades += 1
netReturnPct += tradeReturnPct
if tradeReturnPct > 0
totalWins += 1
grossProfitPct += tradeReturnPct
else if tradeReturnPct < 0
totalLosses += 1
grossLossPct += math.abs(tradeReturnPct)
else
totalBreakeven += 1
if not keepCompletedTrades
if not na(entryLine)
line.delete(entryLine)
if not na(slLine)
line.delete(slLine)
if not na(tpLine)
line.delete(tpLine)
if not na(beLine)
line.delete(beLine)
if not na(entryLabel)
label.delete(entryLabel)
if not na(slLabel)
label.delete(slLabel)
if not na(tpLabel)
label.delete(tpLabel)
previousEntryPrice := entryPrice
previousExitType := exitType
inTrade := false
tradeDirection := 0
fillBar := na
entryPrice := na
currentSL := na
takeProfit := na
stage1Activated := false
stage2Activated := false
entryLine := na
slLine := na
tpLine := na
beLine := na
entryLabel := na
slLabel := na
tpLabel := na
// Take profit ends the sequence; stop exits may permit re-entry.
if exitType == 2
sequenceActive := false
sequenceDirection := 0
reentriesUsed := 0
//=====================================================
// 17. PERFORMANCE METRICS
//=====================================================
float winRate = totalTrades > 0 ? (totalWins * 100.0 / totalTrades) : na
float winLossRatio = totalLosses > 0 ? (totalWins * 1.0 / totalLosses) : na
float profitFactor = grossLossPct > 0 ? (grossProfitPct / grossLossPct) : na
string winRateText = na(winRate) ? "—" : str.tostring(winRate, "#.##") + "%"
string winLossText = na(winLossRatio) ? (totalWins > 0 ? "No losses yet" : "—") : str.tostring(winLossRatio, "#.##") + " : 1"
string netPnlText = (netReturnPct > 0 ? "+" : "") + str.tostring(netReturnPct, "#.##") + "%"
string profitFactorText = na(profitFactor) ? (grossProfitPct > 0 ? "No losses yet" : "—") : str.tostring(profitFactor, "#.##")
string grossProfitText = "+" + str.tostring(grossProfitPct, "#.##") + "%"
string grossLossText = "-" + str.tostring(grossLossPct, "#.##") + "%"
//=====================================================
// 18. STATUS PANEL
//=====================================================
string statusText = "WAITING FOR SETUP"
color statusColor = color.gray
string directionText = "—"
string entryText = "—"
string stopText = "—"
string targetText = "—"
string protectionText = "Not active"
string reentryText = str.tostring(reentriesUsed) + " / " + str.tostring(maxReentries)
if not isM15
statusText := "CHANGE TO M15"
statusColor := color.red
else if inTrade
directionText := tradeDirection == 1 ? "BUY" : "SELL"
statusText := tradeDirection == 1 ? "BUY TRADE ACTIVE" : "SELL TRADE ACTIVE"
statusColor := tradeDirection == 1 ? buyColor : sellColor
entryText := str.tostring(entryPrice, format.mintick)
stopText := str.tostring(currentSL, format.mintick)
targetText := str.tostring(takeProfit, format.mintick)
protectionText := stage2Activated ? "BREAKEVEN ACTIVE" : stage1Activated ? "STOP ADJUSTED" : "Initial stop"
else if pendingOrder
statusText := pendingDirection == 1 ? "BUY SETUP - WAITING" : "SELL SETUP - WAITING"
statusColor := pendingDirection == 1 ? buyColor : sellColor
directionText := pendingDirection == 1 ? "BUY" : "SELL"
entryText := str.tostring(pendingEntry, format.mintick)
protectionText := "Waiting for entry"
else if sequenceActive
statusText := "WAITING FOR RE-ENTRY"
statusColor := color.orange
directionText := sequenceDirection == 1 ? "BUY SEQUENCE" : "SELL SEQUENCE"
protectionText := "No active trade"
if barstate.islast
if showStatusPanel
table.cell(statusPanel, 0, 0, "TRADE STATUS", bgcolor=statusColor, text_color=color.black)
table.cell(statusPanel, 1, 0, statusText, bgcolor=statusColor, text_color=color.black)
table.cell(statusPanel, 0, 1, "Direction", bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 1, 1, directionText, bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 0, 2, "Entry Price", bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 1, 2, entryText, bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 0, 3, "Stop Loss", bgcolor=panelBg, text_color=stopColor)
table.cell(statusPanel, 1, 3, stopText, bgcolor=panelBg, text_color=stopColor)
table.cell(statusPanel, 0, 4, "Take Profit", bgcolor=panelBg, text_color=targetColor)
table.cell(statusPanel, 1, 4, targetText, bgcolor=panelBg, text_color=targetColor)
table.cell(statusPanel, 0, 5, "Protection", bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 1, 5, protectionText, bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 0, 6, "Re-entries Used", bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 1, 6, reentryText, bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 0, 7, "PERFORMANCE", bgcolor=color.rgb(55, 65, 80), text_color=color.white)
table.cell(statusPanel, 1, 7, "ALL COMPLETED TRADES", bgcolor=color.rgb(55, 65, 80), text_color=color.white)
table.cell(statusPanel, 0, 8, "Trades Taken", bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 1, 8, str.tostring(totalTrades), bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 0, 9, "Wins / Losses", bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 1, 9, str.tostring(totalWins) + " / " + str.tostring(totalLosses), bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 0, 10, "Win Rate", bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 1, 10, winRateText, bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 0, 11, "Win : Loss Ratio", bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 1, 11, winLossText, bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 0, 12, "Net P&L", bgcolor=panelBg, text_color=netReturnPct >= 0 ? targetColor : stopColor)
table.cell(statusPanel, 1, 12, netPnlText, bgcolor=panelBg, text_color=netReturnPct >= 0 ? targetColor : stopColor)
table.cell(statusPanel, 0, 13, "Profit Factor", bgcolor=panelBg, text_color=panelText)
table.cell(statusPanel, 1, 13, profitFactorText, bgcolor=panelBg, text_color=panelText)
else
table.clear(statusPanel, 0, 0, 1, 13)
//=====================================================
// 19. EXTEND BREAKEVEN LINE
//=====================================================
if inTrade and not na(beLine)
line.set_x2(beLine, bar_index)
line.set_y2(beLine, entryPrice)