TrendSphere (Zeiierman) for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
MEEvFbv.png

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
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
505 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top