
Author Message :
TrendSphere is designed to capture and visualize market trends and volatility effectively. It combines various volatility measures and trend analysis techniques, producing dynamic bands and a central trend line on the price chart. Its essence is to offer a real-time, reliable estimate of the underlying linear trend in the price.
More Details : https://www.tradingview.com/v/qSC6hZNa/
CODE:
CSS:
# https://www.tradingview.com/v/qSC6hZNa/
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0
#// ~~ © Zeiierman {
#indicator("TrendSphere (Zeiierman)", overlay = true)
# converted by Sam4Cok@Samer800 - 10 / 2023
input SelectVolatility = {default "ATR", "Historical Volatility", "Bollinger Band Width"}; # "Select Volatility?"
input volatilityScalingFactor = 5.0;
input volatilityStrength = 0.8; # "volatility Strength"
input VolatilityLength = 200; # "Volatility Length"
input trendSensitivity = 15; # "Trend Sensitivity"
input Scaling = 1; # "Scaling"
def na = Double.NaN;
DefineGlobalColor("lineUp", CreateColor(41, 98, 255));
DefineGlobalColor("lineDn", CreateColor(136, 14, 79));
DefineGlobalColor("up", CreateColor(73, 56, 147));
DefineGlobalColor("dn", CreateColor(134, 36, 88));
#// ~~ Function to calculate scaling factor
#// ~~ Function to determine the trend direction {
script nz {
input data = close;
input repl = 0;
def ret_val = if isNaN(data) then repl else data;
plot return = ret_val;
}
#trendDirectionFunction(dir) =>
script trendDirectionFunction {
input trendir = close;
def diff = trendir - trendir[1];
def dir = if diff > 0 then 1 else
(if diff < 0 then -1 else 0);
plot out = dir;
}
#getScalingFactor(_price) =>
script getScalingFactor {
input _price = close;
def scalingFactor = if _price >= 10 then 1 else
if _price < 100 then 10 else scalingFactor[1];
plot out = scalingFactor;
}
#countLeftDigits(_price) =>
script countLeftDigits {
input _price = close;
def absPrice;
def count;
def abssrc = if absPrice[1] > 0 then absPrice[1] else AbsValue(_price);
if abssrc >= 1 {
count = count[1] + 1;
absPrice = abssrc / 10;
} else {
count = count[1];
absPrice = abssrc;
}
plot out = count;
}
#getScale2(digits) =>
script getScale2 {
input digits = 1;
def getScale2 = if digits == 1 then 1 else
if digits == 2 then 1 else
if digits == 3 then 10 else
if digits == 4 then 100 else
if digits == 5 then 1000 else getScale2[1];
plot out = getScale2;
}
def scale = getScalingFactor(close);
def digits = countLeftDigits(close);
def scale2 = getScale2(digits) + Scaling;
#/ ~~ Volatility Measures {
#// Historical Volatility
def logRet = Log(close / close[1]);
def stDevLog = StDev(logRet, VolatilityLength);
def histVol = if scale == 1 then
(stDevLog * Sqrt(252)) * scale2 else
(stDevLog * Sqrt(252)) / scale * (10 + Scaling);
#// Bollinger Band Width
def StDevBB = StDev(close, VolatilityLength) * volatilityScalingFactor;
def basis = Average(close, VolatilityLength);
def upperBand = basis + StDevBB;
def lowerBand = basis - StDevBB;
def bandWidth = if scale == 1 then
(upperBand - lowerBand) / basis * scale2 else
((upperBand - lowerBand) / basis) / scale * (10 + Scaling);
def nATR = ATR(Length = VolatilityLength);
def scalStdDev;
switch (SelectVolatility) {
case "Historical Volatility" :
scalStdDev = (histVol * volatilityScalingFactor) * volatilityStrength;
case "Bollinger Band Width" :
scalStdDev = bandWidth * volatilityStrength;
default :
scalStdDev = (nATR * volatilityScalingFactor) * volatilityStrength;
}
def scaledStdDev = if isNaN(scalStdDev) then scaledStdDev[1] else scalStdDev;
#/ ~~ Call Trend Direction {
def trend;
def trendDirection = trendDirectionFunction(trend[1]);
def priceTrend = AbsValue(close - trend[1]);
#// ~~ Calculate the Trend {
def trendAdjustment = if priceTrend > scaledStdDev then (close + trend[1]) / 2 else
trendDirection * (scaledStdDev * (1 / volatilityScalingFactor) * (1 / trendSensitivity)) + trend[1];
trend = trendAdjustment;
#// ~~ Get Upper and Lower Bands {
def upper1 = trend + scaledStdDev;
def lower1 = trend - scaledStdDev;
#/ ~~ Plot {
def trueTrend = trendDirection == trendDirection[2];
def col = trendDirection == 1;
plot trend_ = if trueTrend then trend else na; # "Trend Line"
plot upper_ = if trueTrend then upper1 else na; # "Upper Line"
plot lower_ = if trueTrend then lower1 else na; # "Lower Line"
trend_.EnableApproximation();
trend_.AssignValueColor(if col then GlobalColor("lineUp") else GlobalColor("lineDn"));
upper_.SetDefaultColor(GlobalColor("up"));
lower_.SetDefaultColor(GlobalColor("dn"));
def halfUp = upper_ - (upper_ - trend_) / 2;
def haldDn = trend_ - (trend_ - lower_) / 2;
AddCloud(upper1, halfUp , GlobalColor("up"));
AddCloud(haldDn, lower1 , GlobalColor("dn"));
# END OF CODE