Mean Reversion

mephistomevo · study · 44 行 · 点赞 347 · TradingView 原页

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

Pine Script

//@version=5
indicator("Mean Reversion", shorttitle = "MR", overlay=true)
 
// ATR calculation
atrLength = input.int(14, minval=1, title="ATR Length")
atr = ta.atr(atrLength)
 
// EMA calculation
emaLength = input.int(200, minval=1, title="EMA Length")
ema = ta.ema(close, emaLength)
 
// Bollinger Bands calculation
bbLength = input.int(20, minval=1, title="BB Length")
bbDeviation = input.float(2, minval=0, maxval=5, title="BB Deviation")
sma = ta.sma(close, bbLength)
stdDev = ta.stdev(close, bbLength)
upperBB = sma + bbDeviation * stdDev
lowerBB = sma - bbDeviation * stdDev
 
// Buy signal calculation
buyDistance = input.float(6, minval=0, title="EMA Buy Distance")
belowEma = close < ema
closeBelow = close < (ema - atr * buyDistance)
belowBB = close < lowerBB
buySignal = belowEma and closeBelow and belowBB
 
// Sell signal calculation
sellDistance = input.float(6, minval=0, title="EMA Sell Distance")
aboveEma = close > ema
closeAbove = close > (ema + atr * sellDistance)
aboveBB = close > upperBB
sellSignal = aboveEma and closeAbove and aboveBB
 
// Plotting
plot(ema, color=color.blue, title="EMA")
plot(lowerBB, color=color.orange, title="Lower BB")
plot(upperBB, color=color.orange, title="Upper BB")
plotchar(buySignal, char="▲", location=location.belowbar, color=color.green, transp=0, size=size.small, text="Buy")
plotchar(sellSignal, char="▼", location=location.abovebar, color=color.red, transp=0, size=size.small, text="Sell")
plot(close, color=color.black, title="Close")
 
// Alerts
alertcondition(buySignal, title="Buy Signal Alert", message="Price below EMA - ATR distance and Bollinger Band!")
alertcondition(sellSignal, title="Sell Signal Alert", message="Price above EMA + ATR distance and Bollinger Band!")

← 返回列表