Mean Reversion Indicator

capissimo · study · 58 行 · 点赞 894 · TradingView 原页

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

Pine Script

//@version=4
study("Mean Reversion Indicator", overlay=false, precision=2)
// author: capissimo
 
// This is a mean reversion indicator that anticipates a local trend reversion, 
// It is best used in candle-by-candle processing. 
// Actually, it is a channel with the mid-line serving as a moving mean baseline.
// Each of the two curves run up and down within this channel bouncing off from the top and bottom bounds.
// Touching the bounds serves as an indication of a local trend reversal. 
// The reversal signal is stronger when there exists a resonance (symmetry) in the two curves.
// The background histogram shows a Karobein oscillator that contributes support or resistance for the signal.
 
//*** Functions 
scaleMinimax(x, p, min, max) => 
    hi = highest(x, p), lo = lowest(x, p)
    (max - min) * (x - lo)/(hi - lo) + min
 
scale(x, p) => scaleMinimax(x, p, lowest(close, p), highest(close, p))
 
percentAboveLow(x, p) => // Percentage of current close above low in lookback window of window_length days
    -(x[1] / lowest(low, p) - 1)
 
percentBelowHigh(x, p) => // Percentage of current close below high in lookback window of window_length days
    -(x[1] / highest(high, p) - 1)
 
karo(x, p) => // Karobein Oscillator
    ma = ema(x, p)
    a  = ema(ma < ma[1] ? ma/ma[1] : 0, p)
    b  = ema(ma > ma[1] ? ma/ma[1] : 0, p)
    c  = (ma/ma[1])/(ma/ma[1] + b)
    (2*((ma/ma[1])/(ma/ma[1] + c*a)) - 1) 
    
//*** Inputs
prix  = input(close, "Price Data")
mmx   = input(14,    "Scaler Lookback", minval=2)
oft   = input(3,     "Flag Offset", minval=1)
 
//*** Main
price     = security(syminfo.tickerid, tostring(timeframe.multiplier*5), prix[1], barmerge.gaps_off, barmerge.lookahead_on)
sn_scaled = scaleMinimax(sign(price-price[1]), 2, -1, 1)
meanslope = 2 / (1 + pow(2.71828182846, -price/price[1])) - 1 // mean of the absolute value of slopes
ms_scaled = scaleMinimax(meanslope, 2, -1, 1)
kro       = scaleMinimax(karo(price, mmx), mmx, -1, 1)
 
state = 0, state := sn_scaled==ms_scaled and sn_scaled<0 ? -1 : sn_scaled==ms_scaled and sn_scaled>0 ? 1 : nz(state[1])
pl    = scaleMinimax(percentAboveLow(price, 2), mmx, -1, 1)
ph    = scaleMinimax(percentBelowHigh(price, 2), mmx, -1, 1)
 
gray = color.gray, lime = color.lime, red = color.red
red50 = color.new(red, 50), lime50 = color.new(lime, 50)
c = pl==0?red:ph==0?lime:abs(pl)<ph?lime50:abs(pl)>ph?red50:falling(abs(pl)+ph,2)?red50:rising(abs(pl)+ph,2)?lime50:na
hline(1, color=gray, linestyle=hline.style_solid, linewidth=1)
hline(0, color=gray, linestyle=hline.style_solid, linewidth=1)
hline(-1, color=gray, linestyle=hline.style_solid, linewidth=1)
plot(kro, color=gray, style=plot.style_histogram, linewidth=3, transp=70) // Karobein Osc.
plot(pl, color=lime, linewidth=1, transp=0)  
plot(ph, color=red, linewidth=1, transp=0)
//plot(0, style=plot.style_circles, linewidth=4, color=c, transp=0, offset=oft, show_last=1) // todo item

← 返回列表