Prime Bands [ChartPrime] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
hWXkaRU.png


Author Message :

The Prime Standard Deviation Bands [ChartPrime] indicator uses custom-calculated bands based on highest and lowest price values over specific period to analyze price volatility and trend direction. Traders can set the bands to 1, 2, or 3 standard deviations from a central base, providing a dynamic view of price behavior in relation to volatility. The indicator also includes color-coded trend signals, standard deviation labels, and mean reversion signals, offering insights into trend strength and potential reversal points.

more Details: https://www.tradingview.com/script/99KbMjoz-Prime-Bands-ChartPrime/

CODE:

CSS:
#// Indicator for TOS
#// @ ChartPrime
#indicator("Prime Bands [ChartPrime]", "Prime Bands [ChartPrime]", overlay = true)
# Converted by Sam4Cok@Samer800    - 01/2025

input colorBars = no;
input length = 50;
input standardDeviation = 3.0; # "Standard Deviation", [1,2,3])
input showForecastPrice = no;
input forecastLength = 15;
input labelOptions = {Default "Volatility & Forecast", "Volatility Only", "Forecast Only", "Don't Show"};

def na = Double.NaN;
def last = isNaN(close);
def n = forecastLength;
def Show = labelOptions!=labelOptions."Don't Show";
def volLab = labelOptions!=labelOptions."Forecast Only" and Show;
def forecast = labelOptions!=labelOptions."Volatility Only" and Show;
##// ~~ Gradient Coloring {
Script gradient_color {
input src = close;
input minVal = 10;
input maxVal = 400;
input loR = 173;
input loG = 216;
input loB = 230;
input hiR = 41;
input hiG = 98;
input hiB = 255;
    def value = if isNaN(src) then 0 else src;
    def clamped_value = max(min(value, maxVal), minVal);
    def normalized_value = (clamped_value - minVal) / (maxVal - minVal);
    def re = floor(loR + (hiR - loR) * normalized_value);
    def gr = floor(loG + (hiG - loG) * normalized_value);
    def bl = floor(loB + (hiB - loB) * normalized_value);
    plot r = re;
    plot g = gr;
    plot b = bl;
}

Script mean_reversion {
input src = hl2;
input mid = close;
input lower = low;
input upper = high;
input n = 15;
    def source   = src - mid;
    def mean_rev1 = (src - lower) / (upper - lower);
    def mean_rev2 = HullMovingAvg(mean_rev1 / stdev(mean_rev1, 200), n) - 1;
    def mean_rev = min(4, max(-4, mean_rev2));
    plot out = mean_rev;
}
Script prime_bands {
input length = 50;
input sd = 3;
input n = 15;
    def na    = Double.NaN;
    def last  = isNaN(close);
    def Hg    = highest(high, length);
    def Lw    = lowest(low, length);
    def base  = (Hg + Lw) / 2;
    def atr   = atr(Length = 200);
    def up_b  = base + atr * sd;
    def low_b = base - atr * sd;
    def upper = if !last then highest(up_b, length - 1) else upper[1];
    def lower = if !last then lowest(low_b, length - 1) else lower[1];
    def upp_band = if !last then Average(upper, n) else upp_band[1];
    def low_band = if !last then Average(lower, n) else low_band[1];
    def mid_band = (upp_band + low_band) / 2;
    def slope1   = if !last then upp_band - upp_band[2] else slope1[1];
    def slope2   = if !last then mid_band - mid_band[2] else slope2[1];
    def slope3   = if !last then low_band - low_band[2] else slope3[1];
    def upp_band1 = if !last then upp_band else upp_band1[1] + (slope1 / n);
    def mid_band1 = if !last then mid_band else mid_band1[1] + (slope2 / n);
    def low_band1 = if !last then low_band else low_band1[1] + (slope3 / n);
    plot up  = upp_band;
    plot mid = mid_band;
    plot lo  = low_band;
    plot up1  = if !last[n-1] then upp_band1 else na;
    plot mid1 = if !last[n-1] then mid_band1 else na;
    plot lo1  = if !last[n-1] then low_band1 else na;
}
def upp_band  = prime_bands(length, standardDeviation, n).up;
def mid_band  = prime_bands(length, standardDeviation, n).mid;
def low_band  = prime_bands(length, standardDeviation, n).lo;
def upp_band1 = prime_bands(length, standardDeviation, n).up1;
def mid_band1 = prime_bands(length, standardDeviation, n).mid1;
def low_band1 = prime_bands(length, standardDeviation, n).lo1;

#-- Color
def mid_band2 = if mid_band[2] then mid_band[1] else mid_band[2];
def col = if (mid_band > mid_band2) and (mid_band[1] <= mid_band2[1]) then 1 else
          if (mid_band < mid_band2) and (mid_band[1] >= mid_band2[1]) then -1 else col[1];

def mean_rev = mean_reversion(hl2, mid_band, low_band, upp_band, n);
def hiRev = highest(mean_rev, length)[10];
def loRev = lowest(mean_rev, length)[10];
def mean_rev1 = if !mean_rev[1] then hl2 else mean_rev[1];
def sig_up = (mean_rev > mean_rev1) and (mean_rev[1] <= mean_rev1[1]) and low  < low_band and mean_rev < loRev;
def sig_dn = (mean_rev < mean_rev1) and (mean_rev[1] >= mean_rev1[1]) and high > upp_band and mean_rev > hiRev;

def colR = gradient_color(mean_rev, -4, 4, 0, 255, 255, 255, 0, 255).r;
def colG = gradient_color(mean_rev, -4, 4, 0, 255, 255, 255, 0, 255).g;
def colB = gradient_color(mean_rev, -4, 4, 0, 255, 255, 255, 0, 255).b;

#--Signals

AddChartBubble(sig_up[-1], low, "UP", CreateColor(0, ColG, 0), no);
AddChartBubble(sig_dn[-1], high, "DN", CreateColor(colR, 0, 0));

#-- plot

plot ph = if !last and upp_band then upp_band else na;
plot pm = if !last and mid_band then mid_band else na;
plot pl = if !last and low_band then low_band else na;

plot ph1 = if last[-1] and showForecastPrice and upp_band1 then upp_band1 else na;
plot pm1 = if last[-1] and showForecastPrice and mid_band1 then mid_band1 else na;
plot pl1 = if last[-1] and showForecastPrice and low_band1 then low_band1 else na;

pm.SetLineWeight(2);
pm1.SetLineWeight(2);
ph1.SetStyle(Curve.MEDIUM_DASH);
pm1.SetStyle(Curve.MEDIUM_DASH);
pl1.SetStyle(Curve.MEDIUM_DASH);
ph.SetDefaultColor(GetColor(3));
ph1.SetDefaultColor(GetColor(3));
pl.SetDefaultColor(GetColor(3));
pl1.SetDefaultColor(GetColor(3));
pm.AssignValueColor(if col>0 then Color.GREEN else Color.RED);
pm1.AssignValueColor(if col>0 then Color.GREEN else Color.RED);

AddChartBubble(ph1 and isNaN(ph1[-1]), ph1, AsDollars(ph1), GetColor(3));
AddChartBubble(pm1 and isNaN(pm1[-1]), pm1, AsDollars(pm1), if col>0 then Color.GREEN else Color.RED);
AddChartBubble(pl1 and isNaN(pl1[-1]), pl1, AsDollars(pl1), GetColor(3));
#-- Label
def volaPer = mean_rev / 4;
AddLabel(volLab, "Volatility (" + AsPercent(volaPer) + ")", CreateColor(colR, ColG, ColB));
AddLabel(forecast, "+SD(" + AsDollars(upp_band1[1-n]) + ")", GetColor(3));
AddLabel(forecast, "0SD(" + AsDollars(mid_band1[1-n]) + ")", if col[1-n]>0 then Color.GREEN else Color.RED);
AddLabel(forecast, "-SD(" + AsDollars(low_band1[1-n]) + ")", GetColor(3));

#-- Bar Color

AssignPriceColor(if !colorBars then Color.CURRENT else CreateColor(colR, ColG, ColB));
#-- END of CODE
 
Thanks for converting this indicator @samer800, would you be able to either remove or provide an option to remove the information about the "Volatility and 3 sd's " near the top left of the chart, as it uses up space and I do not knnow how to effectively use them beyond the signals plotted. Thanks in advance.
 
Thanks for converting this indicator @samer800, would you be able to either remove or provide an option to remove the information about the "Volatility and 3 sd's " near the top left of the chart, as it uses up space and I do not knnow how to effectively use them beyond the signals plotted. Thanks in advance.
Try going into the settings and look at "Label Options". Click the pull down arrow and select "Don't Show"
 
Fantastic, it worked exactly the way I liked. I appreciate a fast and perfect response. Thanks and regards.
 
  • Like
Reactions: rfb

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
781 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