本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("EMA Cross + RSI Filter · Rules Template", overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 1, commission_type = strategy.commission.percent, commission_value = 0.06, slippage = 2)
// ── Entry
fastLen = input.int(20, "Fast EMA", minval = 1, group = "Entry")
slowLen = input.int(50, "Slow EMA", minval = 2, group = "Entry")
rsiLen = input.int(14, "RSI length", minval = 2, group = "Entry")
rsiMin = input.float(50, "Long only if RSI above", minval = 0, maxval = 100, group = "Entry")
allowShort = input.bool(false, "Allow shorts (mirrored rules)", group = "Entry")
// ── Exit
stopMode = input.string("ATR", "Stop type", options = ["ATR", "Percent"], group = "Exit")
atrLen = input.int(14, "ATR length", minval = 1, group = "Exit")
atrMult = input.float(1.5, "Stop = ATR x", minval = 0.1, step = 0.1, group = "Exit")
stopPct = input.float(2.0, "Stop %", minval = 0.1, step = 0.1, group = "Exit")
rMult = input.float(2.0, "Target = R x stop distance", minval = 0.1, step = 0.1, group = "Exit")
// ── Filters (UTC)
useDays = input.bool(true, "Monday to Friday only", group = "Filters")
useHours = input.bool(false, "Limit entry hours", group = "Filters")
sess = input.session("0800-2000", "Entry hours (UTC)", group = "Filters")
fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
rsi = ta.rsi(close, rsiLen)
atr = ta.atr(atrLen)
dow = dayofweek(time, "UTC")
dayOk = not useDays or (dow != dayofweek.saturday and dow != dayofweek.sunday)
hourOk = not useHours or not na(time(timeframe.period, sess, "UTC"))
filterOk = dayOk and hourOk
crossUp = ta.crossover(fast, slow)
crossDown = ta.crossunder(fast, slow)
longSignal = crossUp and rsi > rsiMin and filterOk
shortSignal = allowShort and crossDown and rsi < 100 - rsiMin and filterOk
stopDist = stopMode == "ATR" ? atr * atrMult : close * stopPct / 100
if longSignal and strategy.position_size == 0
strategy.entry("Long", strategy.long)
strategy.exit("Long exit", "Long", stop = close - stopDist, limit = close + stopDist * rMult)
if shortSignal and strategy.position_size == 0
strategy.entry("Short", strategy.short)
strategy.exit("Short exit", "Short", stop = close + stopDist, limit = close - stopDist * rMult)
plot(fast, "Fast EMA", color = color.new(#3FD88F, 0))
plot(slow, "Slow EMA", color = color.new(#E8B463, 0))
bgcolor(filterOk ? na : color.new(color.gray, 92), title = "Outside filter window")
// ── Sample size check
var table info = table.new(position.top_right, 1, 2, bgcolor = color.new(#0D1518, 0), border_width = 1)
if barstate.islastconfirmedhistory or barstate.islast
n = strategy.closedtrades
table.cell(info, 0, 0, "Closed trades: " + str.tostring(n), text_color = color.white)
table.cell(info, 0, 1, n < 30 ? "Small sample: under 30 trades" : "30+ trades", text_color = n < 30 ? color.new(#E8756B, 0) : color.new(#3FD88F, 0))