本页源码来自 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/
// © r2keymoneymind
//@version=6
strategy("XAUUSD 15m - 200 EMA (1h) + UT Bot + ADX Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// ==========================================
// 1. INPUTS & PARAMETERS
// ==========================================
// UT Bot Inputs (Matching your chart settings: 2, 1)
keyValue = input.float(2.0, title="UT Bot Key Value")
atrPeriod = input.int(1, title="UT Bot ATR Period")
// ADX Inputs
adxLen = input.int(14, title="ADX Smoothing")
diLen = input.int(14, title="DI Length")
adxThresh = input.int(25, title="ADX Minimum Level")
// HTF EMA Inputs
emaPeriod = input.int(200, title="HTF EMA Period")
htfTime = input.string("60", title="HTF Timeframe (60 = 1 Hour)")
// Risk Management
tp_pct = input.float(1.5, title="Take Profit (%)") / 100
sl_pct = input.float(1.0, title="Stop Loss (%)") / 100
// ==========================================
// 2. INDICATOR CALCULATIONS
// ==========================================
// --- 1-Hour 200 EMA (Fetched using request.security) ---
htfEmaRaw = ta.ema(close, emaPeriod)
htfEma = request.security(syminfo.tickerid, htfTime, htfEmaRaw, barmerge.gaps_off,barmerge.lookahead_off)
// --- ADX Calculation ---
[diPlus, diMinus, adxValue] = ta.dmi(diLen, adxLen)
// --- UT Bot Calculation ---
xATR = ta.atr(atrPeriod)
nLoss = keyValue * xATR
var float xTrailingStop = na
xTrailingStop := math.max(close - nLoss, 0.0) // Fallback initialization
if close[1] > xTrailingStop[1] and close > xTrailingStop[1]
xTrailingStop := math.max(xTrailingStop[1], close - nLoss)
else if close[1] < xTrailingStop[1] and close < xTrailingStop[1]
xTrailingStop := math.min(xTrailingStop[1], close + nLoss)
else
xTrailingStop := close > xTrailingStop[1] ? close - nLoss : close + nLoss
var int pos = 0
if close > xTrailingStop[1] and close[1] <= xTrailingStop[1]
pos := 1
else if close < xTrailingStop[1] and close[1] >= xTrailingStop[1]
pos := -1
utBotBuy = (pos == 1 and pos[1] == -1)
utBotSell = (pos == -1 and pos[1] == 1)
// ==========================================
// 3. STRATEGY CONDITIONS & EXECUTION
// ==========================================
// Rule 1: 1-hour price greater than 200 EMA
htfTrendBullish = (close > htfEma)
// Rule 2 & 3: UT Bot Buy Signal AND ADX > 25
buyCondition = utBotBuy and htfTrendBullish and (adxValue > adxThresh)
// Trade Execution
if buyCondition
strategy.entry("Buy", strategy.long)
// Set Target & Stop levels
longTP = close * (1 + tp_pct)
longSL = close * (1 - sl_pct)
strategy.exit("Exit Buy", "Buy", limit=longTP, stop=longSL)
// ==========================================
// 4. CHART PLOTTING & VISUALS
// ==========================================
// Plot the 1-Hour 200 EMA line directly onto your 15m chart
plot(htfEma, color=color.new(color.orange, 0), title="1H 200 EMA", linewidth=2)
// Plot shapes when a strict valid strategy entry triggers
plotshape(buyCondition, title="Valid Strategy Entry", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.normal)
// Optional Dashboard to see live ADX and HTF EMA status on your screen
var table infoTable = table.new(position.top_right, 2, 2, bgcolor=color.new(color.black, 20), border_width=1)
if barstate.islast
table.cell(infoTable, 0, 0, "1H Trend:", text_color=color.white, text_size=size.small)
table.cell(infoTable, 1, 0, htfTrendBullish ? "BULLISH" : "BEARISH", bgcolor=htfTrendBullish ? color.green : color.red, text_color=color.white, text_size=size.small)
table.cell(infoTable, 0, 1, "Current ADX:", text_color=color.white, text_size=size.small)
table.cell(infoTable, 1, 1, str.tostring(adxValue, "#.##"), bgcolor=adxValue > adxThresh ? color.green : color.navy, text_color=color.white, text_size=size.small)