本页源码来自 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/
// © johnmuchirim
//@version=5
indicator(title="Modified EMA-WMA ADX Trading Strategy with RSI Reversals and Chandelier Exit", shorttitle="JK Combined Strategy", overlay=true)
// Inputs for EMA-WMA ADX Trading Strategy
emaLength = input.int(9, title="EMA Length")
wmaLength = input.int(30, title="WMA Length")
adxLength = input.int(14, title="ADX Length")
adxThreshold = input.int(20, title="ADX Threshold")
// Inputs for RSI Reversals
rsiLength = input.int(14, minval=1, title="RSI Length")
bullishLevel = input.int(30, minval=0, title="Bullish Level")
bearishLevel = input.int(70, minval=0, title="Bearish Level")
rsiSlopePeriods = input.int(3, minval=1, title="RSI Slope Periods")
// Inputs for Chandelier Exit
var string calcGroup = 'Calculation'
ceLength = input.int(title='ATR Period', defval=22, group=calcGroup)
ceMult = input.float(title='ATR Multiplier', step=0.1, defval=3.0, group=calcGroup)
useClose = input.bool(title='Use Close Price for Extremums', defval=true, group=calcGroup)
var string visualGroup = 'Visuals'
showLabels = input.bool(title='Show Buy/Sell Labels', defval=true, group=visualGroup)
highlightState = input.bool(title='Highlight State', defval=true, group=visualGroup)
var string alertGroup = 'Alerts'
awaitBarConfirmation = input.bool(title="Await Bar Confirmation", defval=true, group=alertGroup)
// Calculate EMA and WMA
emaValue = ta.ema(close, emaLength)
wmaValue = ta.wma(close, wmaLength)
// Calculate +DM and -DM
upMove = ta.change(high)
downMove = -ta.change(low)
plusDM = na(upMove) ? na : (upMove > downMove and upMove > 0 ? upMove : 0)
minusDM = na(downMove) ? na : (downMove > upMove and downMove > 0 ? downMove : 0)
// Calculate True Range (TR)
trValue = ta.rma(ta.tr(true), 14)
// Calculate +DI and -DI
plusDI = ta.rma(100 * plusDM / trValue, 14)
minusDI = ta.rma(100 * minusDM / trValue, 14)
// Calculate ADX
dx = 100 * ta.rma(math.abs(plusDI - minusDI) / (plusDI + minusDI), adxLength)
adxValue = ta.rma(dx, adxLength)
// Determine buy and sell signals for EMA-WMA ADX Strategy
buySignalStrong = ta.crossover(emaValue, wmaValue) and adxValue > adxThreshold and ta.rising(adxValue, 1)
buySignalWeak = ta.crossover(emaValue, wmaValue) and adxValue > adxThreshold and ta.falling(adxValue, 1)
sellSignalStrong = ta.crossunder(emaValue, wmaValue) and adxValue > adxThreshold and ta.rising(adxValue, 1)
sellSignalWeak = ta.crossunder(emaValue, wmaValue) and adxValue > adxThreshold and ta.falling(adxValue, 1)
// Calculate RSI
rsiValue = ta.rsi(close, rsiLength)
// Calculate RSI Slope
rsiSlope = rsiValue - rsiValue[rsiSlopePeriods]
// Determine bullish and bearish reversals for RSI
bullishReversalConfirmed = ta.crossover(rsiValue, bullishLevel) and rsiSlope > 0
bearishReversalConfirmed = ta.crossunder(rsiValue, bearishLevel) and rsiSlope < 0
// Chandelier Exit
ceAtr = ceMult * ta.atr(ceLength)
longStop = (useClose ? ta.highest(close, ceLength) : ta.highest(ceLength)) - ceAtr
longStopPrev = nz(longStop[1], longStop)
longStop := close[1] > longStopPrev ? math.max(longStop, longStopPrev) : longStop
shortStop = (useClose ? ta.lowest(close, ceLength) : ta.lowest(ceLength)) + ceAtr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := close[1] < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop
var int dir = 1
dir := close > shortStopPrev ? 1 : close < longStopPrev ? -1 : dir
var color longColor = color.green
var color shortColor = color.red
var color longFillColor = color.new(color.green, 90)
var color shortFillColor = color.new(color.red, 90)
var color textColor = color.new(color.white, 0)
longStopPlot = plot(dir == 1 ? longStop : na, title='Long Stop', style=plot.style_linebr, linewidth=2, color=color.new(longColor, 0))
buySignal = dir == 1 and dir[1] == -1
plotshape(buySignal ? longStop : na, title='Long Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(longColor, 0))
plotshape(buySignal and showLabels ? longStop : na, title='Buy Label', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(longColor, 0), textcolor=textColor)
shortStopPlot = plot(dir == 1 ? na : shortStop, title='Short Stop', style=plot.style_linebr, linewidth=2, color=color.new(shortColor, 0))
sellSignal = dir == -1 and dir[1] == 1
plotshape(sellSignal ? shortStop : na, title='Short Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(shortColor, 0))
plotshape(sellSignal and showLabels ? shortStop : na, title='Sell Label', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(shortColor, 0), textcolor=textColor)
midPricePlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0, display=display.none, editable=false)
longStateFillColor = highlightState ? dir == 1 ? longFillColor : na : na
shortStateFillColor = highlightState ? dir == -1 ? shortFillColor : na : na
fill(midPricePlot, longStopPlot, title='Long State Filling', color=longStateFillColor)
fill(midPricePlot, shortStopPlot, title='Short State Filling', color=shortStateFillColor)
// Plotting the EMA and WMA
plot(emaValue, color=color.blue, title="EMA")
plot(wmaValue, color=color.red, title="WMA")
// Displaying buy and sell signals for EMA-WMA ADX Strategy
plotshape(buySignalStrong, title="Buy Signal (Strong)", location=location.belowbar, color=color.green, style=shape.labelup, text="Buy (Strong)", offset=-1, textcolor=color.black)
plotshape(buySignalWeak, title="Buy Signal (Weak)", location=location.belowbar, color=color.green, style=shape.labelup, text="Buy (Weak)", offset=-1, textcolor=color.black)
plotshape(sellSignalStrong, title="Sell Signal (Strong)", location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell (Strong)", offset=-1, textcolor=color.black)
plotshape(sellSignalWeak, title="Sell Signal (Weak)", location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell (Weak)", offset=-1, textcolor=color.black)
// Displaying bullish and bearish reversals for RSI
plotshape(bullishReversalConfirmed, title="Bullish Reversal", location=location.belowbar, text="Reversal", offset=-1, textcolor=color.rgb(86, 243, 75))
plotshape(bearishReversalConfirmed, title="Bearish Reversal", location=location.abovebar, text="Reversal", offset=-1, textcolor=color.rgb(244, 73, 73))
// Add alerts
await = awaitBarConfirmation ? barstate.isconfirmed : true
alertcondition(dir != dir[1] and await, title='Alert: CE Direction Change', message='Chandelier Exit has changed direction!')
alertcondition(buySignal and await, title='Alert: CE Buy', message='Chandelier Exit Buy!')
alertcondition(sellSignal and await, title='Alert: CE Sell', message='Chandelier Exit Sell!')
alertcondition((buySignalWeak or sellSignalWeak or buySignalStrong or sellSignalStrong), title="Combined Signals", message="Take action Combined Signals Detected!")