EMA Cross + RSI + ADX - Strategy V2

varuns_back · strategy · 93 行 · 点赞 1,191 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
strategy("EMA Cross + RSI + ADX - Strategy V2", overlay=true, initial_capital=1000, default_qty_value=10, default_qty_type=strategy.fixed, commission_type=strategy.commission.percent, commission_value=0.075)
 
// Inputs
ema_fast = input.int(9, "Fast EMA", minval=1)
ema_slow = input.int(21, "Slow EMA", minval=1)
rsi_length = input.int(14, "RSI Length", minval=1)
rsi_long = input.int(55, "RSI Long Threshold", minval=1, maxval=100)
rsi_short = input.int(40, "RSI Short Threshold", minval=1, maxval=100)
sl_percent = input.float(2.0, "Stop Loss %", minval=0.1, maxval=10)
 
// Filter Inputs
use_adx = input.bool(true, "Use ADX Filter")
adx_length = input.int(14, "ADX Length", minval=1)
adx_threshold = input.float(0, "ADX Threshold", minval=0, maxval=100)
 
// Calculations
ema9 = ta.ema(close, ema_fast)
ema21 = ta.ema(close, ema_slow)
rsi = ta.rsi(close, rsi_length)
 
// ADX Calculation
[diPlus, diMinus, adx] = ta.dmi(adx_length, adx_length)
 
// Crossover Detection
bullCross = ta.crossover(ema9, ema21)
bearCross = ta.crossunder(ema9, ema21)
 
// Entry Conditions with optional ADX filter
adx_ok = use_adx ? adx > adx_threshold : true
longCondition = bullCross and rsi > rsi_long and adx_ok
shortCondition = bearCross and rsi < rsi_short and adx_ok
 
// Stop Loss Tracking
var float longSL = na
var float shortSL = na
 
// Update SL on entry
if longCondition
    longSL := close * (1 - sl_percent/100)
 
if shortCondition
    shortSL := close * (1 + sl_percent/100)
 
// Exit Conditions
closeLongCondition = bearCross and strategy.position_size > 0
closeShortCondition = bullCross and strategy.position_size < 0
 
// SL Hit Detection (wick touch)
longSLHit = strategy.position_size > 0 and not na(longSL) and low <= longSL
shortSLHit = strategy.position_size < 0 and not na(shortSL) and high >= shortSL
 
// === STRATEGY EXECUTION ===
 
// Entries
if longCondition
    strategy.entry("LONG", strategy.long, alert_message="Entry Buy")
 
if shortCondition
    strategy.entry("SHORT", strategy.short, alert_message="Entry Sell")
 
// Close on opposite crossover
if closeLongCondition
    strategy.close("LONG", comment="Opposite Cross", alert_message="Close Long")
    longSL := na
 
if closeShortCondition
    strategy.close("SHORT", comment="Opposite Cross", alert_message="Close Short")
    shortSL := na
 
// Stop Loss hits
if longSLHit
    strategy.close("LONG", comment="SL Hit", alert_message="SL Hit Long")
    longSL := na
 
if shortSLHit
    strategy.close("SHORT", comment="SL Hit", alert_message="SL Hit Short")
    shortSL := na
 
// === PLOTS ===
plot(ema9, "EMA 9", color.blue, 2)
plot(ema21, "EMA 21", color.red, 2)
plot(strategy.position_size > 0 ? longSL : na, "Long SL", color.orange, 2, plot.style_linebr)
plot(strategy.position_size < 0 ? shortSL : na, "Short SL", color.orange, 2, plot.style_linebr)
 
plotshape(longCondition, "Long Entry", shape.triangleup, location.belowbar, color.new(color.green, 0), size=size.large)
plotshape(shortCondition, "Short Entry", shape.triangledown, location.abovebar, color.new(color.red, 0), size=size.large)
 
plotarrow(closeLongCondition ? -1 : na, "Close Long", color.red, minheight=15, maxheight=15)
plotarrow(closeShortCondition ? 1 : na, "Close Short", color.green, minheight=15, maxheight=15)
 
plotshape(longSLHit, "Long SL Hit", shape.xcross, location.belowbar, color.new(color.orange, 0), size=size.normal)
plotshape(shortSLHit, "Short SL Hit", shape.xcross, location.abovebar, color.new(color.orange, 0), size=size.normal)

← 返回列表