
Author Message:
The TrendCylinder is a dynamic trading indicator designed to capture trends and volatility in an asset's price. It provides a visualization of the current trend direction and upper and lower bands that adapt to volatility changes. By using this indicator, traders can identify potential breakouts or support and resistance levels. While also gauging the volatility to generate trading ranges. The indicator is a comprehensive tool for traders navigating various market conditions by providing a sophisticated blend of trend-following and volatility-based metrics.
More Details : https://www.tradingview.com/v/PFFQXniH/
CODE:
CSS:
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
# https://www.tradingview.com/v/PFFQXniH/
#// ~~ © Zeiierman {
# indicator("TrendCylinder (Expo)", overlay = true)
# Converted by Sam4Cok@Samer800 - 09/2023
#// ~~ Inputs {
input MovAvgType = AverageType.EXPONENTIAL;
input closingPrice = close;
input MovAvgLength = 200;
input volatilityPeriod = 100; # "Period"
input trendFactor = 9.0; # 'Factor'
input trendStep = 0.7; # "Step"
input atrLength = 200;
def na = Double.NaN;
DefineGlobalColor("line", CreateColor(41,98,255));
DefineGlobalColor("up", CreateColor(73,56,147));
DefineGlobalColor("dn", CreateColor(134,36,88));
#// ~~ Initialize Variables
def trendFact = 1 / trendFactor;
def trendPow = 1 / Power(2, volatilityPeriod);
def nATR = ATR(Length = atrLength);
def averageTrueRange = if IsNaN(nATR) then 0 else nATR;
#// ~~ Function to determine the trend direction
script determineTrendDirection {
input direction = close;
def diff = direction - direction[1];
def dir = if (diff) > 0 then 1 else
if (diff) < 0 then -1 else 0;
plot out = dir;
}
#// ~~ Calculate Trend Direction
def currentTrend;
def crrTrend = currentTrend[1];
def scaledStandardDev = (averageTrueRange * trendFactor);
def trendDir = determineTrendDirection(crrTrend);
def trendDirection = trendDir * (scaledStandardDev);
def priceTrendDiff = if (closingPrice - crrTrend) > 0 then
closingPrice - crrTrend else crrTrend - closingPrice;
def reciprocalFactor = (scaledStandardDev * trendFact * trendPow);
#// ~~ Calculate the Current Trend
def trendAdjustment = if priceTrendDiff > scaledStandardDev * trendStep then
(closingPrice + crrTrend) / 2 else
trendDirection * reciprocalFactor + crrTrend;
currentTrend = trendAdjustment;
#// ~~ Calculate Upper and Lower Bands
def stvVal = StDev(closingPrice, volatilityPeriod);
def ema1 = MovingAverage(MovAvgType, stvVal, MovAvgLength);
def ema = ema1;
def upperBand = currentTrend + scaledStandardDev - averageTrueRange - ema;
def lowerBand = currentTrend - scaledStandardDev + averageTrueRange + ema;
#/ ~~ Plot
def isTrendConsistent = trendDirection == trendDirection[1];
def colorForPlot = reciprocalFactor == 0;
plot Trend = currentTrend; # "Trend Line"
def Upper = if isTrendConsistent then upperBand else na; # "Upper Band"
def Lower = if isTrendConsistent then lowerBand else na; # "Lower Band"
trend.AssignValueColor(if colorForPlot then GlobalColor("line") else GlobalColor("dn"));
def upVal = Trend + (Upper - Trend)/2;
def dnVal = Trend - (Trend - Lower)/2;
AddCloud(Upper, upVal, GlobalColor("up"), GlobalColor("up"), no);
AddCloud(dnVal, Lower, GlobalColor("dn"), GlobalColor("dn"), no);
# End of CODE