Enhanced Bollinger Bands for ThinkorSwim

petergluis

Active member
I converted Enhanced Bollinger Bands for ThinkorSwim. https://www.tradingview.com/script/NodNOgsl-Bollinger-Bands/

Ruby:
def src = close;
input length =20;
def sma = average(src, length);
def stdev = stdev(src, length);

plot upper3 = sma + (stdev *3);
Upper3.AssignValueColor(if upper3> upper3[1] then color.green else color.red);
plot lower3  = sma - (stdev * 3);
Lower3.AssignValueColor(if lower3> lower3[1] then color.green else color.red);
plot upper2 = sma + (stdev * 2);
plot lower2  = sma - (stdev * 2);
plot mid = (upper2 + lower2) / 2;
mid.AssignValueColor(if mid> mid[1] then color.cyan else color.pink);
mid.SetLineWeight(2);
plot upper1 = sma + (stdev * 1);
plot lower1  = sma - (stdev * 1);

addcloud (upper3, upper2, color.light_red, color.light_red);
addcloud (upper2, upper1, color.lime);
addcloud (upper1, lower1, color.gray);
addcloud (lower1, lower2, color.lime);
addcloud (lower2, lower3, color.light_red, color.light_red);
 
I converted Enhanced Bollinger Bands for ThinkorSwim. https://www.tradingview.com/script/NodNOgsl-Bollinger-Bands/

Ruby:
def src = close;
input length =20;
def sma = average(src, length);
def stdev = stdev(src, length);

plot upper3 = sma + (stdev *3);
Upper3.AssignValueColor(if upper3> upper3[1] then color.green else color.red);
plot lower3  = sma - (stdev * 3);
Lower3.AssignValueColor(if lower3> lower3[1] then color.green else color.red);
plot upper2 = sma + (stdev * 2);
plot lower2  = sma - (stdev * 2);
plot mid = (upper2 + lower2) / 2;
mid.AssignValueColor(if mid> mid[1] then color.cyan else color.pink);
mid.SetLineWeight(2);
plot upper1 = sma + (stdev * 1);
plot lower1  = sma - (stdev * 1);

addcloud (upper3, upper2, color.light_red, color.light_red);
addcloud (upper2, upper1, color.lime);
addcloud (upper1, lower1, color.gray);
addcloud (lower1, lower2, color.lime);
addcloud (lower2, lower3, color.light_red, color.light_red);
Two-length Bollinger Bands for ThinkorSwim
Ruby:
def src = close;
input length =20;
input length_1 = 50;
def sma = average(src, length);
def sma_1 = average(src, length_1);
def stdev = stdev(src, length);
def stdev_1 = stdev(src, length_1);

plot upper3 = sma + (stdev *3);
Upper3.AssignValueColor(if upper3> upper3[1] then color.green else color.red);
plot lower3  = sma - (stdev * 3);
Lower3.AssignValueColor(if lower3> lower3[1] then color.green else color.red);
plot upper2 = sma + (stdev * 2);
plot lower2  = sma - (stdev * 2);
plot mid = (upper2 + lower2) / 2;
mid.AssignValueColor(if mid> mid[1] then color.cyan else color.pink);
mid.SetLineWeight(2);
plot upper1 = sma + (stdev * 1);
plot lower1  = sma - (stdev * 1);

addcloud (upper3, upper2, color.light_red, color.light_red);
addcloud (upper2, upper1, color.lime);
addcloud (upper1, lower1, color.gray);
addcloud (lower1, lower2, color.lime);
addcloud (lower2, lower3, color.light_red, color.light_red);

plot upper_3 = sma_1 + (stdev_1 *3);
plot lower_3  = sma_1 - (stdev_1 * 3);
plot upper_2 = sma_1 + (stdev_1 * 2);
plot lower_2  = sma_1 - (stdev_1 * 2);
plot upper_1 = sma_1 + (stdev_1 * 1);
plot lower_1  = sma_1 - (stdev_1 * 1);
 
I add enhanced Bollinger bands to heikin ashi candles with sequential counts and ichimoku in a lower study.

Ruby:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon _last update Jan 17th 2021
# Ichimoku is from ThinkorSwim
declare lower;
input period = 1;
input hideCandles = no;
input candleSmoothing = {default Valcu, Vervoort};
input show_bubble_labels = yes;
input bubbles = yes;
input arrows = yes;
input tenkan_period = 9;
input kijun_period = 26;
def src = close;
input length =20;
def sma = average(src, length);
def stdev = stdev(src, length);
plot Tenkan = (Highest(high, tenkan_period) + Lowest(low, tenkan_period)) / 2;
plot Kijun = (Highest(high, kijun_period) + Lowest(low, kijun_period)) / 2;
plot "Span A" = (Tenkan[kijun_period] + Kijun[kijun_period]) / 2;
plot "Span B" = (Highest(high[kijun_period], 2 * kijun_period) + Lowest(low[kijun_period], 2 * kijun_period)) / 2;
plot Chikou = close[-kijun_period];

Tenkan.SetDefaultColor(GetColor(1));
Kijun.SetDefaultColor(GetColor(2));
"Span A".SetDefaultColor(GetColor(3));
"Span B".SetDefaultColor(GetColor(4));
Chikou.SetDefaultColor(GetColor(5));

DefineGlobalColor("Bullish", Color.YELLOW);
DefineGlobalColor("Bearish", Color.RED);
AddCloud("Span A", "Span B", globalColor("Bullish"), globalColor("Bearish"));

#input smoothingLength = 3;

input movingAverageType = {default  TEMA, Exponential, Hull };

def openMA;
def closeMA;
def highMA;
def lowMA;

switch (movingAverageType) {
case Exponential:
    openMA = CompoundValue(1, ExpAverage(open, period), open);
    closeMA = CompoundValue(1, ExpAverage(close, period), close);
    highMA = CompoundValue(1, ExpAverage(high, period), high);
    lowMA = CompoundValue(1, ExpAverage(low, period), low);
case Hull:
    openMA = CompoundValue(1, HullMovingAvg(open, period), open);
    closeMA = CompoundValue(1,  HullMovingAvg(close, period), close);
    highMA = CompoundValue(1,  HullMovingAvg(high, period), high);
    lowMA = CompoundValue(1,  HullMovingAvg(low, period), low);
case TEMA:
    openMA = CompoundValue(1, TEMA(open, period), open);
    closeMA = CompoundValue(1, TEMA(close, period), close);
    highMA = CompoundValue(1, TEMA(high, period), high);
    lowMA = CompoundValue(1, TEMA(low, period), low);

}


HidePricePlot(hideCandles);

def haOpen;
def haClose;

switch (candleSmoothing){
case Valcu:
    haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0) / 2.0), open);
    haClose = ((((openMA + highMA + lowMA + closeMA) / 4.0) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 4.0);
case Vervoort:
    haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0 ) / 2.0), open);
    haClose = ((((openMA + highMA + lowMA + closeMA) / 4.0) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 4.0);

}

plot o = haOpen;
o.AssignValueColor(if o > o[1] then Color.cyan else if o < o[1] then Color.magenta else color.gray);
o.SetLineWeight (5);
o.Hide();

def haLow =  Min(lowMA, haOpen);
def haHigh = Max(highMA, haOpen);

#Zero Lag System - MetaStock Crossover Formula
#zero-lagging principle
#Zero-lagging TEMA average on closing prices

#Medium-term price reversals - upward trend

def avg = 34;
def TMA1 = reference TEMA(haClose, avg); # triple exponential moving average (TEMA) of 34 bars
def TMA2 =  reference TEMA(TMA1, avg);
def Diff = TMA1 - TMA2;
def ZlHa = TMA1 + Diff; #Zero-lagging TEMA average on closing prices - medium term uptrend;

#Medium-term price reversals - downward trend
def TMA1_ = reference TEMA((high + low) / 2, avg);
def Diff2 = TMA1_ - TMA2;
def ZlCl = TMA1_ + Diff2; #Zero-lagging TEMA average on closing prices - medium term doenwardtrend;

def ZlDif = ZlCl - ZlHa; # Zero-Lag close - Zero-Lag HA(green candle) Uptrend when ZlDif is equal to or greater than zero

#uptrend {green candle}
def keep1 = if (haClose >= haOpen and haClose[1] >= haOpen[1]) then 1 else 0;
def keep2 = if ZlDif >= 0 then 1 else 0;
def keep3 = if (AbsValue(close - open) < ((high - low) * 0.35)) and high >= low[1] then 1 else 0;
def keeping = if (keep1 or keep2) then 1 else 0;
def keepall = if keeping or (keeping[1]) and close >= open or close >= (close[1]) then 1 else 0;

def utr = if keepall or (keepall[1]) and keep3 then 1 else 0;

#downtrend red candle

def keep1_ = if (haClose < haOpen and haClose[1] < haOpen[1]) then 1 else 0;
def keep2_ = if ZlDif < 0 then 1 else 0;
def keep3_ = if (AbsValue(close - open) < ((high - low) * 0.35)) and low <= high[1] then 1 else 0;
def keeping_ = if (keep1_ or keep2_) then 1 else 0;
def dkeepall_ = if keeping_ or (keeping_[1]) and close < open or close < (close[1]) then 1 else 0;

def dtr = if dkeepall_ or (dkeepall_[1] - 1) and keep3_ == 1 then 1 else 0;  #downtrend
def upw = if dtr and (dtr[1]) and utr then 1 else 0;
def dnw = if !utr and (utr[1] ) and dtr then 1 else 0;

def results = if upw then 1 else if dnw then 0 else results[1];

#Change the color of HA and Japanese Candles - turn off to show only HA on chart
#AssignPriceColor(if haClose >= haOpen
             #   then Color.cyan else
            #   if  haClose < haOpen
             #   then Color.magenta else Color.WHITE);


#Heiken_A script

#####################################################################################################
input charttype = ChartType.CANDLE;

def haopen_ = if haClose <= haOpen
              then haOpen + 0
             else Double.NaN;

def HAhi   = if haClose <= haOpen
              then haHigh
              else Double.NaN;

def HAlo =   if haClose <= haOpen
              then haLow
              else Double.NaN;


AddChart(growColor = Color.red, neutralColor = Color.CURRENT, high = HAhi, low = HAlo, open = haopen_, close = haClose, type = ChartType.CANDLE);

def HAclose1 = ohlc4;
def HAopen1  = if haClose >= haOpen
               then CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (open[1] + close[1]) / 2)  
               else Double.NaN;

def haopen_1 = if haOpen <= haClose
               then HAopen1 + 0  else Double.NaN;

def HAhigh1  = haHigh;
def HAlow1   = haLow;


AddChart(growColor = Color.green, neutralColor = Color.CURRENT,  high = HAhigh1, low = HAlow1, open = haopen_1, close = haClose, type = ChartType.CANDLE);

def na = double.nan;

def t1 = SequenceCounter()."Buy Formation";
def t2 = SequenceCounter()."Sell Formation";
def t3 = SequenceCounter()."Buy Array";
def t4 = SequenceCounter()."Sell Array";
def t5 = SequenceCounter()."Perfect Buy";
def t6 = SequenceCounter()."Perfect Sell";
def t7 = SequenceCounter()."Perfect Array Buy";
def t8 = SequenceCounter()."Perfect Array Sell";

input show_test_labels = no;
addlabel(show_test_labels , "seq1 " + t1, color.cyan);
addlabel(show_test_labels , "seq2 " + t2, color.cyan);
addlabel(show_test_labels , "seq3 " + t3, color.cyan);
addlabel(show_test_labels , "seq4 " + t4, color.cyan);
addlabel(show_test_labels , "seq5 " + t5, color.cyan);
addlabel(show_test_labels , "seq6 " + t6, color.cyan);
addlabel(show_test_labels , "seq7 " + t7, color.cyan);
addlabel(show_test_labels , "seq8 " + t8, color.cyan);

# t1 - lower white
# t2 - upper white
# t3 - lower red
# t4 - uper red

#  keep 8, 9, and 13 , ignore other numbers
plot u1 = if t1 == 8 or t1 == 9 or t1 == 13 then t1 else na;
plot u2 = if t2 == 8 or t2 == 9 or t2 == 13 then t2 else na;
plot u3 = if t3 == 8 or t3 == 9 or t3 == 13 then t3 else na;
plot u4 = if t4 == 8 or t4 == 9 or t4 == 13 then t4 else na;

u1.SetPaintingStrategy(PaintingStrategy.VALUES_below);
u1.SetDefaultColor(Color.white);
u1.hidebubble();

u2.SetPaintingStrategy(PaintingStrategy.VALUES_above);
u2.SetDefaultColor(Color.white);
u2.hidebubble();

u3.SetPaintingStrategy(PaintingStrategy.VALUES_below);
u3.SetDefaultColor(Color.yellow);
u3.hidebubble();

u4.SetPaintingStrategy(PaintingStrategy.VALUES_above);
u4.SetDefaultColor(Color.yellow);
u4.hidebubble();

plot upper3 = sma + (stdev *3);
Upper3.AssignValueColor(if upper3> upper3[1] then color.green else color.red);
plot lower3  = sma - (stdev * 3);
Lower3.AssignValueColor(if lower3> lower3[1] then color.green else color.red);
plot upper2 = sma + (stdev * 2);
plot lower2  = sma - (stdev * 2);
plot mid = (upper2 + lower2) / 2;
mid.AssignValueColor(if mid> mid[1] then color.cyan else color.pink);
mid.SetLineWeight(2);
plot upper1 = sma + (stdev * 1);
plot lower1  = sma - (stdev * 1);

addcloud (upper3, upper2, color.light_red, color.light_red);
addcloud (upper2, upper1, color.lime);
addcloud (upper1, lower1, color.gray);
addcloud (lower1, lower2, color.lime);
addcloud (lower2, lower3, color.light_red, color.light_red);
 
Two-length Bollinger Bands for ThinkorSwim
Ruby:
def src = close;
input length =20;
input length_1 = 50;
def sma = average(src, length);
def sma_1 = average(src, length_1);
def stdev = stdev(src, length);
def stdev_1 = stdev(src, length_1);

plot upper3 = sma + (stdev *3);
Upper3.AssignValueColor(if upper3> upper3[1] then color.green else color.red);
plot lower3  = sma - (stdev * 3);
Lower3.AssignValueColor(if lower3> lower3[1] then color.green else color.red);
plot upper2 = sma + (stdev * 2);
plot lower2  = sma - (stdev * 2);
plot mid = (upper2 + lower2) / 2;
mid.AssignValueColor(if mid> mid[1] then color.cyan else color.pink);
mid.SetLineWeight(2);
plot upper1 = sma + (stdev * 1);
plot lower1  = sma - (stdev * 1);

addcloud (upper3, upper2, color.light_red, color.light_red);
addcloud (upper2, upper1, color.lime);
addcloud (upper1, lower1, color.gray);
addcloud (lower1, lower2, color.lime);
addcloud (lower2, lower3, color.light_red, color.light_red);

plot upper_3 = sma_1 + (stdev_1 *3);
plot lower_3  = sma_1 - (stdev_1 * 3);
plot upper_2 = sma_1 + (stdev_1 * 2);
plot lower_2  = sma_1 - (stdev_1 * 2);
plot upper_1 = sma_1 + (stdev_1 * 1);
plot lower_1  = sma_1 - (stdev_1 * 1);
Can you change this one to 9 and 26 like the ichimoku?
 
Can you change this one to 9 and 26 like the ichimoku?
You can see the following code of the combined indicator:
input tenkan_period = 9;
input kijun_period = 26;

It contains a full Ichimoku indicator, which you can adjust tenkan (9) and kijun (26) linewidths through the indicator properties.
 
You can see the following code of the combined indicator:
input tenkan_period = 9;
input kijun_period = 26;

It contains a full Ichimoku indicator, which you can adjust tenkan (9) and kijun (26) linewidths through the indicator properties.
Yeah I saw that I don't necessarily need to see the ichimoku cloud as long I have the ballinger band with one standard two standard and three standard deviation on top and bottom that's all I care for along with the tenken and kijin
 
Enhanced Bollinger Bands for ThinkorSwim with Slim Ribbon.

Ruby:
#Mr Slim Miller 
#Enhanced Bollinger Bands for ThinkorSwim
input price = close;
input superfast_length = 8;
input fast_length = 13;
input slow_length = 21;
input displace = 0;
def src = close;
input length =20;
def sma = average(src, length);
def stdev = stdev(src, length);
def mov_avg8 = ExpAverage(price[-displace], superfast_length);
def mov_avg13 = ExpAverage(price[-displace], fast_length);
def mov_avg21 = ExpAverage(price[-displace], slow_length);
Plot Superfast = mov_avg8;
plot Fast = mov_avg13;
plot Slow = mov_avg21;
def buy = mov_avg8 > mov_avg13 and mov_avg13 > mov_avg21 and low > mov_avg8;
def stopbuy = mov_avg8 <= mov_avg13;
def buynow = !buy[1] and buy;
def buysignal = CompoundValue(1, if buynow and !stopbuy then 1 else if buysignal[1]==1 and stopbuy then 0 else buysignal[1], 0);
plot Buy_Signal = buysignal[1] == 0 and buysignal==1;
Buy_signal.setpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Buy_signal.setdefaultColor(color.dark_GREEN);
Buy_signal.hidetitle();
Alert(condition = buysignal[1] == 0 and buysignal == 1, text = "Buy Signal", sound = Sound.Bell, "alert type" = Alert.BAR);
plot Momentum_Down = buysignal[1] ==1 and buysignal==0;
Momentum_down.setpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Momentum_Down.setdefaultColor(color.yellow);
Momentum_down.hidetitle();
Alert(condition = buysignal[1] == 1 and buysignal == 0, text = "Momentum_Down", sound = Sound.Bell, "alert type" = Alert.BAR);
def sell = mov_avg8 < mov_avg13 and mov_avg13 < mov_avg21 and high < mov_avg8;
def stopsell = mov_avg8 >= mov_avg13;
def sellnow = !sell[1] and sell;
def sellsignal = CompoundValue(1, if sellnow and !stopsell then 1 else if sellsignal[1]==1 and stopsell then 0 else sellsignal[1], 0);
Plot Sell_Signal = sellsignal[1] ==0 and sellsignal;
Sell_signal.setpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
sell_signal.setDefaultColor(color.red);
Sell_signal.hidetitle();
Alert(condition = sellsignal[1] == 0 and sellsignal == 1, text = "Sell Signal", sound = Sound.Bell, "alert type" = Alert.BAR);
Plot Momentum_Up = sellsignal[1]==1 and sellSignal==0;
Momentum_up.setpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_up);
Momentum_up.setDefaultColor(color.yellow);
Momentum_up.hidetitle();
Alert(condition = sellsignal[1] == 1 and sellSignal == 0, text = "Momentum_Up", sound = Sound.Bell, "alert type" = Alert.BAR);
plot Colorbars = if buysignal ==1 then 1 else if sellsignal ==1 then 2 else if buysignal ==0 or sellsignal==0 then 3 else 0;
colorbars.hide();
Colorbars.definecolor("Buy_Signal_Bars", color.dark_green);
Colorbars.definecolor("Sell_Signal_Bars", color.red);
Colorbars.definecolor("Neutral", color.yellow);
AssignPriceColor(if Colorbars ==1 then colorbars.color("buy_signal_bars") else if colorbars ==2 then colorbars.color("Sell_Signal_bars") else  colorbars.color("neutral"));
plot upper3 = sma + (stdev *3);
Upper3.AssignValueColor(if upper3> upper3[1] then color.green else color.red);
plot lower3  = sma - (stdev * 3);
Lower3.AssignValueColor(if lower3> lower3[1] then color.green else color.red);
plot upper2 = sma + (stdev * 2);
plot lower2  = sma - (stdev * 2);
plot mid = (upper2 + lower2) / 2;
mid.AssignValueColor(if mid> mid[1] then color.cyan else color.pink);
mid.SetLineWeight(2);
plot upper1 = sma + (stdev * 1);
plot lower1  = sma - (stdev * 1);
addcloud (upper3, upper2, color.light_red, color.light_red);
addcloud (upper2, upper1, color.lime);
addcloud (upper1, lower1, color.gray);
addcloud (lower1, lower2, color.lime);
addcloud (lower2, lower3, color.light_red, color.light_red);
 
Last edited:
Yeah I saw that I don't necessarily need to see the ichimoku cloud as long I have the ballinger band with one standard two standard and three standard deviation on top and bottom that's all I care for along with the tenken and kijin
I removed Ichimuko clouds and kept its three moving averages as you requested.

Ruby:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon _last update Jan 17th 2021
# Ichimoku is from ThinkorSwim
declare lower;
input period = 1;
input hideCandles = no;
input candleSmoothing = {default Valcu, Vervoort};
input show_bubble_labels = yes;
input bubbles = yes;
input arrows = yes;
input tenkan_period = 9;
input kijun_period = 26;
def src = close;
input length =20;
def sma = average(src, length);
def stdev = stdev(src, length);
plot Tenkan = (Highest(high, tenkan_period) + Lowest(low, tenkan_period)) / 2;
plot Kijun = (Highest(high, kijun_period) + Lowest(low, kijun_period)) / 2;

plot Chikou = close[-kijun_period];
Tenkan.SetDefaultColor(GetColor(6));
Tenkan.SetLineWeight(2);
Kijun.SetDefaultColor(GetColor(5));
Kijun.SetLineWeight(2);
Chikou.SetDefaultColor(GetColor(0));


input movingAverageType = {default  TEMA, Exponential, Hull };

def openMA;
def closeMA;
def highMA;
def lowMA;

switch (movingAverageType) {
case Exponential:
    openMA = CompoundValue(1, ExpAverage(open, period), open);
    closeMA = CompoundValue(1, ExpAverage(close, period), close);
    highMA = CompoundValue(1, ExpAverage(high, period), high);
    lowMA = CompoundValue(1, ExpAverage(low, period), low);
case Hull:
    openMA = CompoundValue(1, HullMovingAvg(open, period), open);
    closeMA = CompoundValue(1,  HullMovingAvg(close, period), close);
    highMA = CompoundValue(1,  HullMovingAvg(high, period), high);
    lowMA = CompoundValue(1,  HullMovingAvg(low, period), low);
case TEMA:
    openMA = CompoundValue(1, TEMA(open, period), open);
    closeMA = CompoundValue(1, TEMA(close, period), close);
    highMA = CompoundValue(1, TEMA(high, period), high);
    lowMA = CompoundValue(1, TEMA(low, period), low);

}


HidePricePlot(hideCandles);

def haOpen;
def haClose;

switch (candleSmoothing){
case Valcu:
    haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0) / 2.0), open);
    haClose = ((((openMA + highMA + lowMA + closeMA) / 4.0) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 4.0);
case Vervoort:
    haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0 ) / 2.0), open);
    haClose = ((((openMA + highMA + lowMA + closeMA) / 4.0) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 4.0);

}

plot o = haOpen;
o.AssignValueColor(if o > o[1] then Color.cyan else if o < o[1] then Color.magenta else color.gray);
o.SetLineWeight (5);
o.Hide();

def haLow =  Min(lowMA, haOpen);
def haHigh = Max(highMA, haOpen);

#Zero Lag System - MetaStock Crossover Formula
#zero-lagging principle
#Zero-lagging TEMA average on closing prices

#Medium-term price reversals - upward trend

def avg = 34;
def TMA1 = reference TEMA(haClose, avg); # triple exponential moving average (TEMA) of 34 bars
def TMA2 =  reference TEMA(TMA1, avg);
def Diff = TMA1 - TMA2;
def ZlHa = TMA1 + Diff; #Zero-lagging TEMA average on closing prices - medium term uptrend;

#Medium-term price reversals - downward trend
def TMA1_ = reference TEMA((high + low) / 2, avg);
def Diff2 = TMA1_ - TMA2;
def ZlCl = TMA1_ + Diff2; #Zero-lagging TEMA average on closing prices - medium term doenwardtrend;

def ZlDif = ZlCl - ZlHa; # Zero-Lag close - Zero-Lag HA(green candle) Uptrend when ZlDif is equal to or greater than zero

#uptrend {green candle}
def keep1 = if (haClose >= haOpen and haClose[1] >= haOpen[1]) then 1 else 0;
def keep2 = if ZlDif >= 0 then 1 else 0;
def keep3 = if (AbsValue(close - open) < ((high - low) * 0.35)) and high >= low[1] then 1 else 0;
def keeping = if (keep1 or keep2) then 1 else 0;
def keepall = if keeping or (keeping[1]) and close >= open or close >= (close[1]) then 1 else 0;

def utr = if keepall or (keepall[1]) and keep3 then 1 else 0;

#downtrend red candle

def keep1_ = if (haClose < haOpen and haClose[1] < haOpen[1]) then 1 else 0;
def keep2_ = if ZlDif < 0 then 1 else 0;
def keep3_ = if (AbsValue(close - open) < ((high - low) * 0.35)) and low <= high[1] then 1 else 0;
def keeping_ = if (keep1_ or keep2_) then 1 else 0;
def dkeepall_ = if keeping_ or (keeping_[1]) and close < open or close < (close[1]) then 1 else 0;

def dtr = if dkeepall_ or (dkeepall_[1] - 1) and keep3_ == 1 then 1 else 0;  #downtrend
def upw = if dtr and (dtr[1]) and utr then 1 else 0;
def dnw = if !utr and (utr[1] ) and dtr then 1 else 0;

def results = if upw then 1 else if dnw then 0 else results[1];

#Change the color of HA and Japanese Candles - turn off to show only HA on chart
#AssignPriceColor(if haClose >= haOpen
             #   then Color.cyan else
            #   if  haClose < haOpen
             #   then Color.magenta else Color.WHITE);


#Heiken_A script

#####################################################################################################
input charttype = ChartType.CANDLE;

def haopen_ = if haClose <= haOpen
              then haOpen + 0
             else Double.NaN;

def HAhi   = if haClose <= haOpen
              then haHigh
              else Double.NaN;

def HAlo =   if haClose <= haOpen
              then haLow
              else Double.NaN;


AddChart(growColor = Color.red, neutralColor = Color.CURRENT, high = HAhi, low = HAlo, open = haopen_, close = haClose, type = ChartType.CANDLE);

def HAclose1 = ohlc4;
def HAopen1  = if haClose >= haOpen
               then CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (open[1] + close[1]) / 2) 
               else Double.NaN;

def haopen_1 = if haOpen <= haClose
               then HAopen1 + 0  else Double.NaN;

def HAhigh1  = haHigh;
def HAlow1   = haLow;


AddChart(growColor = Color.green, neutralColor = Color.CURRENT,  high = HAhigh1, low = HAlow1, open = haopen_1, close = haClose, type = ChartType.CANDLE);

def na = double.nan;

def t1 = SequenceCounter()."Buy Formation";
def t2 = SequenceCounter()."Sell Formation";
def t3 = SequenceCounter()."Buy Array";
def t4 = SequenceCounter()."Sell Array";
def t5 = SequenceCounter()."Perfect Buy";
def t6 = SequenceCounter()."Perfect Sell";
def t7 = SequenceCounter()."Perfect Array Buy";
def t8 = SequenceCounter()."Perfect Array Sell";

input show_test_labels = no;
addlabel(show_test_labels , "seq1 " + t1, color.cyan);
addlabel(show_test_labels , "seq2 " + t2, color.cyan);
addlabel(show_test_labels , "seq3 " + t3, color.cyan);
addlabel(show_test_labels , "seq4 " + t4, color.cyan);
addlabel(show_test_labels , "seq5 " + t5, color.cyan);
addlabel(show_test_labels , "seq6 " + t6, color.cyan);
addlabel(show_test_labels , "seq7 " + t7, color.cyan);
addlabel(show_test_labels , "seq8 " + t8, color.cyan);

# t1 - lower white
# t2 - upper white
# t3 - lower red
# t4 - uper red

#  keep 8, 9, and 13 , ignore other numbers
plot u1 = if t1 == 8 or t1 == 9 or t1 == 13 then t1 else na;
plot u2 = if t2 == 8 or t2 == 9 or t2 == 13 then t2 else na;
plot u3 = if t3 == 8 or t3 == 9 or t3 == 13 then t3 else na;
plot u4 = if t4 == 8 or t4 == 9 or t4 == 13 then t4 else na;

u1.SetPaintingStrategy(PaintingStrategy.VALUES_below);
u1.SetDefaultColor(Color.white);
u1.hidebubble();

u2.SetPaintingStrategy(PaintingStrategy.VALUES_above);
u2.SetDefaultColor(Color.white);
u2.hidebubble();

u3.SetPaintingStrategy(PaintingStrategy.VALUES_below);
u3.SetDefaultColor(Color.yellow);
u3.hidebubble();

u4.SetPaintingStrategy(PaintingStrategy.VALUES_above);
u4.SetDefaultColor(Color.yellow);
u4.hidebubble();

plot upper3 = sma + (stdev *3);
Upper3.AssignValueColor(if upper3> upper3[1] then color.green else color.red);
plot lower3  = sma - (stdev * 3);
Lower3.AssignValueColor(if lower3> lower3[1] then color.green else color.red);
plot upper2 = sma + (stdev * 2);
plot lower2  = sma - (stdev * 2);
plot mid = (upper2 + lower2) / 2;
mid.AssignValueColor(if mid> mid[1] then color.cyan else color.pink);
mid.SetLineWeight(2);
plot upper1 = sma + (stdev * 1);
plot lower1  = sma - (stdev * 1);

addcloud (upper3, upper2, color.light_red, color.light_red);
addcloud (upper2, upper1, color.lime);
addcloud (upper1, lower1, color.gray);
addcloud (lower1, lower2, color.lime);
addcloud (lower2, lower3, color.light_red, color.light_red);
 
A lower study with Fib bands, Heikin Ashi, and Madrid moving averages.

Ruby:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon _last update Jan 17th 2021
# #http://madridjourneyonws.blogspot.com/
declare lower;
input period = 1;
input hideCandles = no;
input candleSmoothing = {default Valcu, Vervoort};
input show_bubble_labels = yes;
input bubbles = yes;
input arrows = yes;
input price = close;


input movingAverageType = {default  TEMA, Exponential, Hull };

def openMA;
def closeMA;
def highMA;
def lowMA;

switch (movingAverageType) {
case Exponential:
    openMA = CompoundValue(1, ExpAverage(open, period), open);
    closeMA = CompoundValue(1, ExpAverage(close, period), close);
    highMA = CompoundValue(1, ExpAverage(high, period), high);
    lowMA = CompoundValue(1, ExpAverage(low, period), low);
case Hull:
    openMA = CompoundValue(1, HullMovingAvg(open, period), open);
    closeMA = CompoundValue(1,  HullMovingAvg(close, period), close);
    highMA = CompoundValue(1,  HullMovingAvg(high, period), high);
    lowMA = CompoundValue(1,  HullMovingAvg(low, period), low);
case TEMA:
    openMA = CompoundValue(1, TEMA(open, period), open);
    closeMA = CompoundValue(1, TEMA(close, period), close);
    highMA = CompoundValue(1, TEMA(high, period), high);
    lowMA = CompoundValue(1, TEMA(low, period), low);

}


HidePricePlot(hideCandles);

def haOpen;
def haClose;

switch (candleSmoothing){
case Valcu:
    haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0) / 2.0), open);
    haClose = ((((openMA + highMA + lowMA + closeMA) / 4.0) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 4.0);
case Vervoort:
    haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0 ) / 2.0), open);
    haClose = ((((openMA + highMA + lowMA + closeMA) / 4.0) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 4.0);

}

plot o = haOpen;
o.AssignValueColor(if o > o[1] then Color.cyan else if o < o[1] then Color.magenta else color.gray);
o.SetLineWeight (5);
o.Hide();

def haLow =  Min(lowMA, haOpen);
def haHigh = Max(highMA, haOpen);

def avg = 34;
def TMA1 = reference TEMA(haClose, avg); # triple exponential moving average (TEMA) of 34 bars
def TMA2 =  reference TEMA(TMA1, avg);
def Diff = TMA1 - TMA2;
def ZlHa = TMA1 + Diff; #Zero-lagging TEMA average on closing prices - medium term uptrend;


def TMA1_ = reference TEMA((high + low) / 2, avg);
def Diff2 = TMA1_ - TMA2;
def ZlCl = TMA1_ + Diff2; #Zero-lagging TEMA average on closing prices - medium term doenwardtrend;

def ZlDif = ZlCl - ZlHa; # Zero-Lag close - Zero-Lag HA(green candle) Uptrend when ZlDif is equal to or greater than zero

def keep1 = if (haClose >= haOpen and haClose[1] >= haOpen[1]) then 1 else 0;
def keep2 = if ZlDif >= 0 then 1 else 0;
def keep3 = if (AbsValue(close - open) < ((high - low) * 0.35)) and high >= low[1] then 1 else 0;
def keeping = if (keep1 or keep2) then 1 else 0;
def keepall = if keeping or (keeping[1]) and close >= open or close >= (close[1]) then 1 else 0;

def utr = if keepall or (keepall[1]) and keep3 then 1 else 0;


def keep1_ = if (haClose < haOpen and haClose[1] < haOpen[1]) then 1 else 0;
def keep2_ = if ZlDif < 0 then 1 else 0;
def keep3_ = if (AbsValue(close - open) < ((high - low) * 0.35)) and low <= high[1] then 1 else 0;
def keeping_ = if (keep1_ or keep2_) then 1 else 0;
def dkeepall_ = if keeping_ or (keeping_[1]) and close < open or close < (close[1]) then 1 else 0;

def dtr = if dkeepall_ or (dkeepall_[1] - 1) and keep3_ == 1 then 1 else 0;  #downtrend
def upw = if dtr and (dtr[1]) and utr then 1 else 0;
def dnw = if !utr and (utr[1] ) and dtr then 1 else 0;

def results = if upw then 1 else if dnw then 0 else results[1];


input charttype = ChartType.CANDLE;

def haopen_ = if haClose <= haOpen
              then haOpen + 0
             else Double.NaN;

def HAhi   = if haClose <= haOpen
              then haHigh
              else Double.NaN;

def HAlo =   if haClose <= haOpen
              then haLow
              else Double.NaN;


AddChart(growColor = Color.red, neutralColor = Color.CURRENT, high = HAhi, low = HAlo, open = haopen_, close = haClose, type = ChartType.CANDLE);

def HAclose1 = ohlc4;
def HAopen1  = if haClose >= haOpen
               then CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (open[1] + close[1]) / 2)
               else Double.NaN;

def haopen_1 = if haOpen <= haClose
               then HAopen1 + 0  else Double.NaN;

def HAhigh1  = haHigh;
def HAlow1   = haLow;


AddChart(growColor = Color.green, neutralColor = Color.CURRENT,  high = HAhigh1, low = HAlow1, open = haopen_1, close = haClose, type = ChartType.CANDLE);


def src = close;
input length =20;
def sma = average(src, length);
def stdev = stdev(src, length);

plot upper3 = sma + (stdev *3);
Upper3.AssignValueColor(if upper3> upper3[1] then color.green else color.red);
plot lower3  = sma - (stdev * 3);
Lower3.AssignValueColor(if lower3> lower3[1] then color.green else color.red);
plot upper2 = sma + (stdev * 2);
plot lower2  = sma - (stdev * 2);
plot mid = (upper2 + lower2) / 2;
mid.AssignValueColor(if mid> mid[1] then color.cyan else color.pink);
mid.SetLineWeight(2);
plot upper1 = sma + (stdev * 1);
plot lower1  = sma - (stdev * 1);
addcloud (upper3, upper2, color.light_red, color.light_red);
addcloud (upper2, upper1, color.lime);
addcloud (upper1, lower1, color.gray);
addcloud (lower1, lower2, color.lime);
addcloud (lower2, lower3, color.light_red, color.light_red);
plot ma05 = ExpAverage(price, 5);
plot ma10 = ExpAverage(price, 10);
plot ma15 = ExpAverage(price, 15);
plot ma20 = ExpAverage(price, 20);
plot ma25 = ExpAverage(price, 25);
plot ma30 = ExpAverage(price, 30);
plot ma35 = ExpAverage(price, 35);
plot ma40 = ExpAverage(price, 40);
plot ma45 = ExpAverage(price, 45);
plot ma50 = ExpAverage(price, 50);
plot ma55 = ExpAverage(price, 55);
plot ma60 = ExpAverage(price, 60);
plot ma65 = ExpAverage(price, 65);
plot ma70 = ExpAverage(price, 70);
plot ma75 = ExpAverage(price, 75);
plot ma80 = ExpAverage(price, 80);
plot ma85 = ExpAverage(price, 85);
plot ma90 = ExpAverage(price, 90);
plot ma95 = ExpAverage(price, 95);
plot ma100 = ExpAverage(price, 100);
ma05.AssignValueColor( if ma05 >= ma05[1] and ma05>ma100 then color.Green  else if ma05 < ma05[1] and ma05>ma100 then color.Dark_red else if ma05<= ma05[1] and ma05 <ma100 then color.red else if ma05 >= ma05[1] and ma05 < ma100 then color.Dark_green else color.gray);

ma10.AssignValueColor(if ma10 >= ma10[1] and ma05>ma100
then color.Green  else if ma10 < ma10[1] and ma05>ma100
then color.Dark_red else if ma10<= ma10[1] and ma05 <ma100
then color.red else if ma10 >= ma10[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma15.AssignValueColor(if ma15 >= ma15[1] and ma05>ma100
then color.Green  else if ma15 < ma15[1] and ma05>ma100
then color.Dark_red else if ma15<= ma15[1] and ma05 <ma100
then color.red else if ma15 >= ma15[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma20.AssignValueColor(if ma20 >= ma20[1] and ma05>ma100
then color.Green  else if ma20 < ma20[1] and ma05>ma100
then color.Dark_red else if ma20<= ma20[1] and ma05 <ma100
then color.red else if ma20 >= ma20[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma25.AssignValueColor(if ma25 >= ma25[1] and ma05>ma100
then color.Green  else if ma25 < ma25[1] and ma05>ma100
then color.Dark_red else if ma25<= ma25[1] and ma05 <ma100
then color.red else if ma25 >= ma25[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma30.AssignValueColor(if ma30 >= ma30[1] and ma05>ma100
then color.Green  else if ma30 < ma30[1] and ma05>ma100
then color.Dark_red else if ma30<= ma30[1] and ma05 <ma100
then color.red else if ma30 >= ma30[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma35.AssignValueColor(if ma35 >= ma35[1] and ma05>ma100
then color.Green  else if ma35 < ma35[1] and ma05>ma100
then color.Dark_red else if ma35<= ma35[1] and ma05 <ma100
then color.red else if ma35 >= ma35[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma40.AssignValueColor(if ma40 >= ma40[1] and ma05>ma100
then color.Green  else if ma40 < ma40[1] and ma05>ma100
then color.Dark_red else if ma40<= ma40[1] and ma05 <ma100
then color.red else if ma40 >= ma40[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma45.AssignValueColor(if ma45 >= ma45[1] and ma05>ma100
then color.Green  else if ma45 < ma45[1] and ma05>ma100
then color.Dark_red else if ma45<= ma45[1] and ma05 <ma100
then color.red else if ma45 >= ma45[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma50.AssignValueColor(if ma50 >= ma50[1] and ma05>ma100
then color.Green  else if ma50 < ma50[1] and ma05>ma100
then color.Dark_red else if ma50<= ma50[1] and ma05 <ma100
then color.red else if ma50 >= ma50[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma55.AssignValueColor(if ma55 >= ma55[1] and ma05>ma100
then color.Green  else if ma55 < ma55[1] and ma05>ma100
then color.Dark_red else if ma55<= ma55[1] and ma05 <ma100
then color.red else if ma55 >= ma55[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma60.AssignValueColor(if ma60 >= ma60[1] and ma05>ma100
then color.Green  else if ma60 < ma60[1] and ma05>ma100
then color.Dark_red else if ma60<= ma60[1] and ma05 <ma100
then color.red else if ma60 >= ma60[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma65.AssignValueColor(if ma65 >= ma65[1] and ma05>ma100
then color.Green  else if ma65 < ma65[1] and ma05>ma100
then color.Dark_red else if ma65<= ma65[1] and ma05 <ma100
then color.red else if ma65 >= ma65[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma70.AssignValueColor(if ma70 >= ma70[1] and ma05>ma100
then color.Green  else if ma70 < ma70[1] and ma05>ma100
then color.Dark_red else if ma70<= ma70[1] and ma05 <ma100
then color.red else if ma70 >= ma70[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma75.AssignValueColor(if ma75 >= ma75[1] and ma05>ma100
then color.Green  else if ma75 < ma75[1] and ma05>ma100
then color.Dark_red else if ma75<= ma75[1] and ma05 <ma100
then color.red else if ma75 >= ma75[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma80.AssignValueColor(if ma80 >= ma80[1] and ma05>ma100
then color.Green  else if ma80 < ma80[1] and ma05>ma100
then color.Dark_red else if ma80<= ma80[1] and ma05 <ma100
then color.red else if ma80 >= ma80[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma85.AssignValueColor(if ma85 >= ma85[1] and ma05>ma100
then color.Green  else if ma85 < ma85[1] and ma05>ma100
then color.Dark_red else if ma85<= ma85[1] and ma05 <ma100
then color.red else if ma85 >= ma85[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma90.AssignValueColor(if ma90 >= ma90[1] and ma05>ma100
then color.Green  else if ma90 < ma90[1] and ma05>ma100
then color.Dark_red else if ma90<= ma90[1] and ma05 <ma100
then color.red else if ma90 >= ma90[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma95.AssignValueColor(if ma95 >= ma95[1] and ma05>ma100
then color.Green  else if ma95 < ma95[1] and ma05>ma100
then color.Dark_red else if ma95<= ma95[1] and ma05 <ma100
then color.red else if ma95 >= ma95[1] and ma05 < ma100
then color.Dark_green else color.gray);

ma100.AssignValueColor(if ma100 >= ma100[1] and ma05>ma100
then color.Green  else if ma100 < ma100[1] and ma05>ma100
then color.Dark_red else if ma100<= ma100[1] and ma05 <ma100
then color.red else if ma100 >= ma100[1] and ma05 < ma100
then color.Dark_green else color.gray);
 
WOW I really like it - Many Thanks

How can I get this in a scan? Based on the Buy Single's from the Chart

Let me please ask you something else, all these marks (Buy/Sell Singles) are REPAINTING?
 
Last edited by a moderator:
There are not buy and sell marks. The indicators in a lower study include moving averages, Heiken Ashi candles, and Fib bands.
 

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