本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © Zeiierman
//@version=5
indicator("Grid by Volatility (Expo)", overlay=true)
// ~~ ToolTips {
t1 = "The length of bars to calculate the Standard Deviation. A larger value will consider more bars, making it smoother but less responsive. \n\nThe second value adjusts the Standard Deviation by multiplying it with this factor."
t2 = "The length of bars to calculate the weighted moving average for smoothing the grid lines."
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Input {
volatilityLengthStdev = input.int(200, title="Volatility Length", minval = 2, inline="stdev", group="STDEV Volatility")
squeezeAdjustment = input.int(6, title="", minval=1, maxval=200, inline="stdev", group="STDEV Volatility", tooltip=t1)
smoothingPeriod = input.int(2, title="Grid Confirmation Length",minval=1, maxval=200, inline="stdev", group="Confirmation", tooltip=t2)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Function to calculate standard deviation
calculateStdev(priceSource, period) =>
if bar_index < period - 1
0.0
else
sum = 0.0
for i = 0 to period - 1
sum := sum + priceSource[i]
mean = sum / period
sumOfSquaredDifferences = 0.0
for i = 0 to period - 1
sumOfSquaredDifferences := sumOfSquaredDifferences + math.pow(priceSource[i] - mean, 2)
stdev = math.sqrt(sumOfSquaredDifferences / period * squeezeAdjustment)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Function to determine the trend direction
calculateTrendDirection(price) =>
price - price[1] > 0 ? 1 : (price - price[1] < 0 ? -1 : 0)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Calculate Volatility-Based Grid
calculateVolatilityBasedGrid() =>
var float midLine = na
var float halfVolatility = na
selectedVolatility = calculateStdev(close, volatilityLengthStdev)
trendDirection = calculateTrendDirection(close)
if (na(midLine))
midLine := close
// Update the Mid Line
priceDifference = math.abs(close - midLine)
midLine := priceDifference > selectedVolatility ? midLine + trendDirection * selectedVolatility : midLine
// Update half volatility
halfVolatility := na(halfVolatility) ? selectedVolatility / 2 : (midLine != midLine[1] ? selectedVolatility / 2 : halfVolatility)
[ta.wma(midLine + halfVolatility * 2, smoothingPeriod), ta.wma(midLine + halfVolatility, smoothingPeriod), ta.wma(midLine, smoothingPeriod), ta.wma(midLine - halfVolatility, smoothingPeriod), ta.wma(midLine - halfVolatility * 2, smoothingPeriod)]
[upperRange2, upperRange1, midLineAvg, lowerRange1, lowerRange2] = calculateVolatilityBasedGrid()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// Plot ~~
OnOff = midLineAvg==midLineAvg[1]?true:false
plot(upperRange2, title="Upper Range 2", color=OnOff?#fd3232:na)
plot(upperRange1, title="Upper Range 1", color=OnOff?#fd3232:na)
plot(midLineAvg, title="Average Mid Line", color=OnOff?#1441f8:na)
plot(lowerRange1, title="Lower Range 1", color=OnOff?#2ce056:na)
plot(lowerRange2, title="Lower Range 2", color=OnOff?#2ce056:na)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}