本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bharatTrader
//@version=4
study(title="Price - Mean Reversion Expansion", shorttitle="Reversion & Expansion")
src = input(title="Source", type=input.source, defval=close)
offset = input(0, title="Envolope Offset", type=input.integer, minval=0, maxval=0, tooltip="Envolope offset")
len1 = input(20, type=input.integer, title="EMA shortest")
len2 = input(50, type=input.integer, title="EMA shorter")
len3 = input(100, type=input.integer, title="EMA intermediate")
len4 = input(200, type=input.integer, title="EMA longest")
showTableForTrendDirection = input(defval=true, title="Show Multi Time Trend", type=input.bool)
//Consts
Pi = 3.14159
PiBy2 = Pi / 2
//Functions
angleColor(angle) =>
factor = (100 - (abs(angle) / PiBy2 * 100))
col = iff(angle > 0.0, color.new(color.green, factor), iff(angle < 0.0, color.new(color.maroon, factor), color.gray))
//Set up
//Table Def
var table dataDisplay = table.new(position.bottom_right, 4, 1, frame_width=1, frame_color=color.new(color.orange, 80))
//Just in case, lengths are not aligned
isInputCorrect = (len1 <= len2) and (len2 <= len3) and (len3 <= len4)
l1 = len1
l2 = len2
l3 = len3
l4 = len4
ema1 = nz(ema(src, l1))
ema2 = nz(ema(src, l2))
ema3 = nz(ema(src, l3))
ema4 = nz(ema(src, l4))
min4 = min(ema1, ema2, ema3, ema4)
min3 = min(ema1, ema2, ema3)
min2 = min(ema1, ema2)
highestEMA = max(ema1, ema2, ema3, ema4)
lowestEMA = iff(min4 == 0, iff(min3 == 0, min2 , min3), min4)
delta = iff(lowestEMA != 0, (highestEMA - lowestEMA) / lowestEMA, 0)
emaDelta = ema(delta, 7)
//Slope calcs
y1 = ema1 - ema1[5]
y2 = ema2 - ema2[5]
y3 = ema3 - ema3[5]
y4 = ema4 - ema4[5]
angle1 = atan(y1/5) // radians
angle2 = atan(y2/5) // radians
angle3 = atan(y3/5) // radians
angle4 = atan(y4/5) // radians
//Reversion Expansion Colors
plotColor = iff(src > highestEMA, color.lime, iff(src < lowestEMA, color.red, color.blue))
markSignFlag = iff(crossover(delta, emaDelta), 1, iff(crossunder(delta, emaDelta), -1, 0)) // 1 - Up, -1 - Down, 0 - Flat
markColor = iff(markSignFlag == 1, color.green, iff(markSignFlag == -1, color.maroon, na))
//Angle Color
angle1Color = angleColor(angle1)
angle2Color = angleColor(angle2)
angle3Color = angleColor(angle3)
angle4Color = angleColor(angle4)
//Plot
plot(isInputCorrect ? delta : na, title='Delta', color=plotColor, linewidth=2, style=plot.style_histogram)
plot(isInputCorrect ? emaDelta : na, color=color.blue, offset=-offset)
lbl_char = markSignFlag == 1 or markSignFlag == -1 ? label.new(bar_index, emaDelta + 0.03, style=label.style_circle, size=size.auto, color=markColor) : na
//Table
if barstate.islast and showTableForTrendDirection
table.cell(dataDisplay, 0, 0, bgcolor=angle1Color)
table.cell(dataDisplay, 1, 0, bgcolor=angle2Color)
table.cell(dataDisplay, 2, 0, bgcolor=angle3Color)
table.cell(dataDisplay, 3, 0, bgcolor=angle4Color)