Heikin-Ashi Candles Lower Chart For ThinkorSwim (assorted versions)

petergluis

Active member
nmyZvNd.png


Ruby:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon _last update Jan 17th 2021
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 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);
 
Last edited by a moderator:
I combined Ichimoku and Heiken Ashi candles into one indicator in a lower study, which will take less space than the use of a grid of charts to show Heikin Ashi candles. In addition, you can use a mouse to pinpoint normal candles in a upper study and Heiken Ashi candles in the lower study at the same time.

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;

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);


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);
 
Last edited:
I figure it out to show normal candles in a lower study. I would like to know how to add borders to down and up candles.

Ruby:
declare lower;
def o = open;
def h = high;
def l = low;
def c = close;
def UpO;
def UpH;
def UpL;
def UpC;
if o < c
then {
    UpO = o;
    UpH = h;
    UpL = l;
    UpC = c;
} else {
    UpO = Double.NaN;
    UpH = Double.NaN;
    UpL = Double.NaN;
    UpC = Double.NaN;
}

# OHLC DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if o > c
then {
    DnO = o;
    DnH = h;
    DnL = l;
    DnC = c;
} else {
    DnO = Double.NaN;
    DnH = Double.NaN;
    DnL = Double.NaN;
    DnC = Double.NaN;
}
AddChart(high = UpH, low = UpL, open = UpC, close = UpO, type = ChartType.CANDLE, growcolor = Color.GREEN);
AddChart(high = DnH, low = DnL, open = DnO, close = DnC, type = ChartType.CANDLE, growcolor = Color.RED);
 
Last edited:
I would like to know how to add borders to down and up candles.

keep on experimenting and trying things, that's how i learned.
search the site for addchart( and look at examples.

don't know what you mean by, borders?
if you mean hollow candles, then you swap the open and close numbers

Code:
# addchart_peter_redgreen

# https://usethinkscript.com/threads/could-i-have-heikin-ashi-candles-in-lower-section-in-thinkorswim.10428/#post-92406

declare lower;
def o = open;
def h = high;
def l = low;
def c = close;

# solid or hollow bars  ( swap open and close)
input solid = yes;



# OHLC UP candle
def UpO;
def UpH;
def UpL;
def UpC;
if o < c
then {
#    UpO = o;
    UpO = if solid then o else c;
    UpH = h;
    UpL = l;
#    UpC = c;
    UpC = if solid then c else o;

} else {
    UpO = Double.NaN;
    UpH = Double.NaN;
    UpL = Double.NaN;
    UpC = Double.NaN;
}

# OHLC DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if o > c
then {
#    DnO = o;
    DnO = if solid then o else c;
    DnH = h;
    DnL = l;
#    DnC = c;
    DnC = if solid then c else o;
} else {
    DnO = Double.NaN;
    DnH = Double.NaN;
    DnL = Double.NaN;
    DnC = Double.NaN;
}


AddChart(high = UpH, low = UpL, open = UpC, close = UpO, type = ChartType.CANDLE, growcolor = Color.GREEN);
AddChart(high = DnH, low = DnL, open = DnO, close = DnC, type = ChartType.CANDLE, growcolor = Color.RED);
#
 
If you would like to use bar candles in a lower study. Here is the code.
Ruby:
# addchart_peter_redgreen

# https://usethinkscript.com/threads/could-i-have-heikin-ashi-candles-in-lower-section-in-thinkorswim.10428/#post-92406

declare lower;
def o = open;
def h = high;
def l = low;
def c = close;

# solid or hollow bars  ( swap open and close)
input solid = yes;



# OHLC UP candle
def UpO;
def UpH;
def UpL;
def UpC;
if o < c
then {
#    UpO = o;
    UpO = if solid then o else c;
    UpH = h;
    UpL = l;
#    UpC = c;
    UpC = if solid then c else o;

} else {
    UpO = Double.NaN;
    UpH = Double.NaN;
    UpL = Double.NaN;
    UpC = Double.NaN;
}

# OHLC DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if o > c
then {
#    DnO = o;
    DnO = if solid then o else c;
    DnH = h;
    DnL = l;
#    DnC = c;
    DnC = if solid then c else o;
} else {
    DnO = Double.NaN;
    DnH = Double.NaN;
    DnL = Double.NaN;
    DnC = Double.NaN;
}


AddChart(high = UpH, low = UpL, open = UpC, close = UpO, type = ChartType.bar, growcolor = Color.GREEN);
AddChart(high = DnH, low = DnL, open = DnO, close = DnC, type = ChartType.bar, growcolor = Color.RED);
 
I combined regular candles with smoothed Heiken Ashi in a lower study. You can use smoothed the HeiKen Ashi as a moving average to see uptrends or downtrends.
Ruby:
# addchart_peter_redgreen
#Smoothed Heiken Ashi and ATR studies
# https://usethinkscript.com/threads/could-i-have-heikin-ashi-candles-in-lower-section-in-thinkorswim.10428/#post-92406

declare lower;
def o = open;
def h = high;
def l = low;
def c = close;
input period = 10;
input hideCandles = no;
input candleSmoothing = {default Valcu, Vervoort};

input movingAverageType = {Simple, default Exponential, Weighted, Hull, Variable, TEMA};

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

# solid or hollow bars  ( swap open and close)
input solid = yes;

# OHLC UP candle
def UpO;
def UpH;
def UpL;
def UpC;
if o < c
then {
#    UpO = o;
    UpO = if solid then o else c;
    UpH = h;
    UpL = l;
#    UpC = c;
    UpC = if solid then c else o;

} else {
    UpO = Double.NaN;
    UpH = Double.NaN;
    UpL = Double.NaN;
    UpC = Double.NaN;
}

# OHLC DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if o > c
then {
#    DnO = o;
    DnO = if solid then o else c;
    DnH = h;
    DnL = l;
#    DnC = c;
    DnC = if solid then c else o;
} else {
    DnO = Double.NaN;
    DnH = Double.NaN;
    DnL = Double.NaN;
    DnC = Double.NaN;
}

AddChart(high = UpH, low = UpL, open = UpC, close = UpO, type = ChartType.CANDLE, growcolor = Color.GREEN);
AddChart(high = DnH, low = DnL, open = DnO, close = DnC, type = ChartType.CANDLE, growcolor = Color.RED);

switch (movingAverageType) {
case Simple:
    openMA = CompoundValue(1, Average(open, period), open);
    closeMA = CompoundValue(1, Average(close, period), close);
    highMA = CompoundValue(1, Average(high, period), high);
    lowMA = CompoundValue(1, Average(low, period), low);
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 Weighted:
    openMA = CompoundValue(1, WMA(open, period), open);
    closeMA = CompoundValue(1, WMA(close, period), close);
    highMA = CompoundValue(1, WMA(high, period), high);
    lowMA = CompoundValue(1, WMA(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 Variable:
    openMA = CompoundValue(1, VariableMA(open, period), open);
    closeMA = CompoundValue(1, VariableMA(close, period), close);
    highMA = CompoundValue(1, VariableMA(high, period), high);
    lowMA = CompoundValue(1, VariableMA(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) ;
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 o1 = haOpen + 0;
o1.Hide();

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

def haOpen_fall = if haOpen > haClose
              then haOpen
              else Double.NaN;
def haHigh_fall   = if haOpen >= haClose
              then haHigh
              else Double.NaN;
def haLow_fall    = if haOpen >= haClose
              then haLow
              else Double.NaN;
def haClose_fall    = if haOpen >= haClose
              then haClose
              else Double.NaN;

AddChart(growColor = Color.PLUM, fallColor = Color.BLUE, neutralColor = Color.CURRENT, high = haHigh_fall, low = haLow_fall, open = haOpen_fall, close = haClose_fall , type = ChartType.CANDLE);

def haOpen_rise = if haOpen < haClose
                then haClose
                else Double.NaN;
def haHigh_rise  = if haOpen <= haClose
              then haHigh
              else Double.NaN;
def haLow_rise   = if haOpen <= haClose
              then haLow
              else Double.NaN;
def haClose_rise   = if haOpen <= haClose
              then haOpen
              else Double.NaN;

AddChart(growColor = Color.BLUE, fallColor = Color.PLUM, neutralColor = Color.CURRENT, high = haHigh_rise, low = haLow_rise, open = haOpen_rise, close = haClose_rise, type = ChartType.CANDLE);

input TradeInFlatRange = Yes;
input BarsForFlatRange = 4;
input BarsReqToStayInRange = 8;

def HH = Highest(high[1], BarsForFlatRange);
def LL = Lowest(low[1], BarsForFlatRange);
def maxH = Highest(HH, BarsReqToStayInRange);
def maxL = Lowest(LL, BarsReqToStayInRange);
def HHn = if maxH == maxH[1] or maxL == maxL then maxH else HHn[1];
def LLn = if maxH == maxH[1] or maxL == maxL then maxL else LLn[1];
def Bh = if high <= HHn and HHn == HHn[1] then HHn else Double.NaN;
def Bl = if low >= LLn and LLn == LLn[1] then LLn else Double.NaN;
def CountH = if IsNaN(Bh) or IsNaN(Bl) then 2 else CountH[1] + 1;
def CountL = if IsNaN(Bh) or IsNaN(Bl) then 2 else CountL[1] + 1;
def ExpH = if BarNumber() == 1 then Double.NaN else
            if CountH[-BarsReqToStayInRange] >= BarsReqToStayInRange then HHn[-BarsReqToStayInRange] else
            if high <= ExpH[1] then ExpH[1] else Double.NaN;
def ExpL = if BarNumber() == 1 then Double.NaN else
            if CountL[-BarsReqToStayInRange] >= BarsReqToStayInRange then LLn[-BarsReqToStayInRange] else
            if low >= ExpL[1] then ExpL[1] else Double.NaN;

def BoxHigh = if !IsNaN(ExpL) and !IsNaN(ExpH) then ExpH else Double.NaN;
def BoxLow = if !IsNaN(ExpL) and !IsNaN(ExpH) then ExpL else Double.NaN;

AddCloud( BoxHigh, BoxLow, Color.GRAY, Color.GRAY);

def Flat = if (!IsNaN(BoxHigh[1]) and !IsNaN(BoxLow[1])) and !TradeInFlatRange then 1 else 0;

input trailType = {default modified, unmodified};
input ATRPeriod = 7;
input ATRFactor = 2.5;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;

Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);

def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);

def trueRange;
switch (trailType) {
case modified:
    trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange = TrueRange(high, close, low);
}
def loss = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);

def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
    if (!IsNaN(loss)) {
        switch (firstTrade) {
        case long:
            state = state.long;
            trail =  close - loss;
        case short:
            state = state.short;
            trail = close + loss;
    }
    } else {
        state = state.init;
        trail = Double.NaN;
    }
case long:
    if (close > trail[1]) {
        state = state.long;
        trail = Max(trail[1], close - loss);
    } else {
        state = state.short;
        trail = close + loss;
    }
case short:
    if (close < trail[1]) {
        state = state.short;
        trail = Min(trail[1], close + loss);
    } else {
        state = state.long;
        trail =  close - loss;
    }
}

#
 
Last edited:
I add Double Bollinger Bands to regular candles in a lower study.

Ruby:
# addchart_peter_redgreen
#Double Bollinger Bands
#Assembled by Chewie76 on UseThinkscript.com
# https://usethinkscript.com/threads/could-i-have-heikin-ashi-candles-in-lower-section-in-thinkorswim.10428/#post-92406
declare lower;
def o = open;
def h = high;
def l = low;
def c = close;
def UpO;
def UpH;
def UpL;
def UpC;
if o < c
then {
    UpO = o;
    UpH = h;
    UpL = l;
    UpC = c;
} else {
    UpO = Double.NaN;
    UpH = Double.NaN;
    UpL = Double.NaN;
    UpC = Double.NaN;
}

# OHLC DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if o > c
then {
    DnO = o;
    DnH = h;
    DnL = l;
    DnC = c;
} else {
    DnO = Double.NaN;
    DnH = Double.NaN;
    DnL = Double.NaN;
    DnC = Double.NaN;
}
AddChart(high = UpH, low = UpL, open = UpC, close = UpO, type = ChartType.CANDLE, growcolor = Color.GREEN);
AddChart(high = DnH, low = DnL, open = DnO, close = DnC, type = ChartType.CANDLE, growcolor = Color.RED);

input price = close;
input long_length = 180;
input aggregationPeriod = AggregationPeriod.FOUR_HOURS;
input short = yes;
input short_length = 50;
input averageType = AverageType.SIMPLE;
def displace = 0;
input price_c = close;
input length_c = 50;
input displace_c = 0;
input showBreakoutSignals_c = no;
input price_a = close;
input length = 20;
input displace_a =0;
input Num_Dev_Dn_a = -2.0;
input Num_Dev_up_a = 2.0;
input averageType_a = AverageType.Simple;
input timeframe = AggregationPeriod.DAY;
input timeframe1 = aggregationperiod.day;
input length1 = 25;
input num_dev_dn1 = -2;
input num_dev_up1 = 2;

plot AvgExp_c = ExpAverage(price_c[-displace_c], length_c);
plot UpSignal_c = price_c crosses above AvgExp_c;
plot DownSignal_c = price_c crosses below AvgExp_c;
UpSignal_c.SetHiding(!showBreakoutSignals_c);
DownSignal_c.SetHiding(!showBreakoutSignals_c);

AvgExp_c.SetDefaultColor(GetColor(1));
AvgExp_c.SetLineWeight(2);
AvgExp_c.DefineColor("Positive and Up", Color.DARK_GREEN);
AvgExp_c.DefineColor("Positive and Down", Color.DARK_RED);
AvgExp_c.DefineColor("Negative and Down", Color.DARK_RED);
AvgExp_c.DefineColor("Negative and Up", Color.DARK_GREEN);
AvgExp_c.AssignValueColor(if AvgExp_c >= 0 then if AvgExp_c > AvgExp_c[1] then AvgExp_c.color("Positive and Up") else AvgExp_c.color("Positive and Down") else if AvgExp_c < AvgExp_c[1] then AvgExp_c.color("Negative and Down") else AvgExp_c.color("Negative and Up"));
UpSignal_c.SetDefaultColor(Color.UPTICK);
UpSignal_c.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal_c.SetDefaultColor(Color.DOWNTICK);
DownSignal_c.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

def Num_Dev_Dn = -1.0;
def Num_Dev_up = 1.0;
def price1 = close(period = aggregationPeriod);
def sDev = StDev(data = price1[-displace], length = long_length);
plot MidLine = MovingAverage(averageType, data = price1[-displace], length = long_length);
plot LowerBand1 = MidLine + Num_Dev_Dn * sDev;
plot UpperBand1 = MidLine + Num_Dev_up * sDev;
LowerBand1.SetDefaultColor(CreateColor(153, 255, 153));
MidLine.SetDefaultColor(Color.YELLOW);
MidLine.SetLineWeight(2);
UpperBand1.SetDefaultColor(CreateColor(255, 153, 51));
def Num_Dev_Dn138 = -1.38;
def Num_Dev_up138 = 1.38;
plot LowerBand138 = MidLine + Num_Dev_Dn138 * sDev;
plot UpperBand138 = MidLine + Num_Dev_up138 * sDev;
LowerBand138.SetDefaultColor(CreateColor(105, 205, 255));
UpperBand138.SetDefaultColor(GetColor(0));
LowerBand138.SetStyle(Curve.LONG_DASH);
UpperBand138.SetStyle(Curve.LONG_DASH);

def Num_Dev_Dn2 = -2.0;
def Num_Dev_up2 = 2.0;
plot LowerBand2 = MidLine + Num_Dev_Dn2 * sDev;
plot UpperBand2 = MidLine + Num_Dev_up2 * sDev;
UpperBand2.SetDefaultColor(GetColor(0));
LowerBand2.SetDefaultColor(CreateColor(105, 205, 255));

def Num_Dev_Dn3 = -3.0;
def Num_Dev_up3 = 3.0;
plot LowerBand3 = MidLine + Num_Dev_Dn3 * sDev;
plot UpperBand3 = MidLine + Num_Dev_up3 * sDev;
LowerBand3.SetDefaultColor(GetColor(0));
UpperBand3.SetDefaultColor(GetColor(5));
LowerBand3.SetDefaultColor(Color.GREEN);
UpperBand3.SetDefaultColor(Color.RED);
LowerBand3.SetLineWeight(2);
UpperBand3.SetLineWeight(2);

def sDev_short = StDev(data = price[-displace], length = short_length);
plot MidLine_short = if short then MovingAverage(averageType, data = price[-displace], length = short_length) else Double.NaN;
plot LowerBand1_short =  if short then MidLine_short + Num_Dev_Dn * sDev_short else Double.NaN;
plot UpperBand1_short = if short then MidLine_short + Num_Dev_up * sDev_short else Double.NaN;
LowerBand1_short.SetDefaultColor(CreateColor(153, 255, 153));
MidLine_short.SetDefaultColor(Color.YELLOW);
MidLine_short.SetLineWeight(2);
UpperBand1_short.SetDefaultColor(CreateColor(255, 153, 51));

plot LowerBand138_short = if short then MidLine_short + Num_Dev_Dn138 * sDev_short else Double.NaN;
plot UpperBand138_short = if short then MidLine_short + Num_Dev_up138 * sDev_short else Double.NaN;
LowerBand138_short.SetDefaultColor(CreateColor(105, 205, 255));
UpperBand138_short.SetDefaultColor(GetColor(0));
LowerBand138_short.SetStyle(Curve.LONG_DASH);
UpperBand138_short.SetStyle(Curve.LONG_DASH);

plot LowerBand2_short = if short then MidLine_short + Num_Dev_Dn2 * sDev_short else Double.NaN;
plot UpperBand2_short = if short then MidLine_short + Num_Dev_up2 * sDev_short else Double.NaN;
UpperBand2_short.SetDefaultColor(GetColor(0));
LowerBand2_short.SetDefaultColor(CreateColor(105, 205, 255));

plot LowerBand3_short = if short then MidLine_short + Num_Dev_Dn3 * sDev_short else Double.NaN;
;
plot UpperBand3_short = if short then MidLine_short + Num_Dev_up3 * sDev_short else Double.NaN;
;
LowerBand3_short.SetDefaultColor(GetColor(0));
UpperBand3_short.SetDefaultColor(GetColor(5));
LowerBand3_short.SetDefaultColor(Color.GREEN);
UpperBand3_short.SetDefaultColor(Color.RED);
LowerBand3_short.SetLineWeight(1);
UpperBand3_short.SetLineWeight(1);

AddCloud(UpperBand3, UpperBand2, Color.DARK_RED, Color.CURRENT);
AddCloud(LowerBand2, LowerBand3, Color.DARK_GREEN, Color.CURRENT);

AddCloud(UpperBand3_short, UpperBand2_short, Color.DARK_RED, Color.CURRENT);
AddCloud(LowerBand2_short, LowerBand3_short, Color.DARK_GREEN, Color.CURRENT);

def sDev_a = stdev(data = close(period= timeframe) [-displace], length = length);
def sdev1 = stdev(data = close(period= timeframe1) [-displace], length = length1);

plot MidLine_a = MovingAverage(averageType_a, data = close (period=timeframe)[-displace_a], length = length);
plot LowerBand_a = MidLine_a + num_Dev_Dn_a * sDev_a;
plot UpperBand_a = MidLine_a + num_Dev_Up_a * sDev_a;

LowerBand_a.SetDefaultColor(GetColor(2));
LowerBand_a.SetLineWeight(4);
MidLine_a.SetDefaultColor(GetColor(1));
UpperBand_a.SetDefaultColor(GetColor(5));

plot MidLine_b = MovingAverage(averageType, data = close (period=timeframe)[-displace], length = length);
plot LowerBand_B = MidLine_b + num_Dev_Dn1 * sDev1;
plot UpperBand_B = MidLine_b + num_Dev_Up1 * sDev1;
plot mid = (lowerband_b + lowerband_a)/2;
plot mid1 = (upperband_b + upperband_a)/2;
LowerBand_b.SetDefaultColor(GetColor(0));
MidLine_b.SetDefaultColor(GetColor(1));
UpperBand_b.SetDefaultColor(GetColor(5));

addcloud(lowerband_a,lowerband_b,color.white, color.yellow);
addcloud(upperband_a,upperband_b,color.white, color.yellow);
 
Last edited:
I add Fib bands to normal candles in a lower study.


Ruby:
# addchart_peter_redgreen

# https://usethinkscript.com/threads/could-i-have-heikin-ashi-candles-in-lower-section-in-thinkorswim.10428/#post-92406

declare lower;
def o = open;
def h = high;
def l = low;
def c = close;

# solid or hollow bars  ( swap open and close)
input solid = yes;



# OHLC UP candle
def UpO;
def UpH;
def UpL;
def UpC;
if o < c
then {
#    UpO = o;
    UpO = if solid then o else c;
    UpH = h;
    UpL = l;
#    UpC = c;
    UpC = if solid then c else o;

} else {
    UpO = Double.NaN;
    UpH = Double.NaN;
    UpL = Double.NaN;
    UpC = Double.NaN;
}

# OHLC DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if o > c
then {
#    DnO = o;
    DnO = if solid then o else c;
    DnH = h;
    DnL = l;
#    DnC = c;
    DnC = if solid then c else o;
} else {
    DnO = Double.NaN;
    DnH = Double.NaN;
    DnL = Double.NaN;
    DnC = Double.NaN;
}


AddChart(high = UpH, low = UpL, open = UpC, close = UpO, type = ChartType.CANDLE, growcolor = Color.GREEN);
AddChart(high = DnH, low = DnL, open = DnO, close = DnC, type = ChartType.CANDLE, growcolor = Color.RED);


input lenSMA = 50;
input lenATR = 14;
input firstFactor = 1.610;
input secondFactor = 2.618;
input thirdFactor = 3.618;
input fourthFactor = 4.236;

script MTF_ATR {
    input TF = AggregationPeriod.HOUR;
    input Length = 14;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            Length
        )
    ;
}
;
def VAtr = ATR(lenATR);
def vSma = Average(close, lenSMA);
def fib1u = vSma + VAtr * firstFactor;
def fib1l = vSma - VAtr * firstFactor;
def fib2u = vSma + VAtr * secondFactor;
def fib2l = vSma - VAtr * secondFactor;
def fib3u = vSma + VAtr * thirdFactor;
def fib3l = vSma - VAtr * thirdFactor;
def fib4u = vSma + VAtr * fourthFactor;
def fib4l = vSma - VAtr * fourthFactor;
plot  Centerline = vSma;
plot band1upper = fib1u;
band1upper.SetDefaultColor(GetColor(9));
plot band1lower = fib1l;
band1lower.SetDefaultColor(GetColor(9));
plot band2upper = fib2u;
band2upper.SetDefaultColor(GetColor(6));
plot band2lower = fib2l;
band2lower.SetDefaultColor(GetColor(6));
plot band3upper = fib3u;
band3upper.SetDefaultColor(GetColor(5));
plot band3lower = fib3l;
band3lower.SetDefaultColor(GetColor(5));
plot band4upper = fib4u;
band3upper.SetDefaultColor(GetColor(5));
plot band4lower = fib4l;
band3lower.SetDefaultColor(GetColor(5));

AddCloud(band1upper, band1lower, color.light_green, color.light_green);
AddCloud(band2upper, band3upper, color.yellow, color.yellow);
AddCloud(band2upper, band1upper, color.cyan, color.cyan);
AddCloud(band2lower, band3lower, color.yellow, color.yellow);
AddCloud(band2lower, band1lower, color.cyan, color.cyan);
AddCloud(band4lower, band3lower, color.red, color.red);
AddCloud(band4upper, band3upper, color.red, color.red);
 
Here is a lower study with Bollinger bands, Fib Bands, Keltner bands for ThinkorSwim.
Ruby:
declare lower;
def o = open;
def h = high;
def l = low;
def c = close;
def UpO;
def UpH;
def UpL;
def UpC;
if o < c

then {
    UpO = o;
    UpH = h;
    UpL = l;
    UpC = c;
} else {
    UpO = Double.NaN;
    UpH = Double.NaN;
    UpL = Double.NaN;
    UpC = Double.NaN;
}

# OHLC DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if o > c
then {
    DnO = o;
    DnH = h;
    DnL = l;
    DnC = c;
} else {
    DnO = Double.NaN;
    DnH = Double.NaN;
    DnL = Double.NaN;
    DnC = Double.NaN;
}
AddChart(high = UpH, low = UpL, open = UpC, close = UpO, type = ChartType.CANDLE, growcolor = Color.GREEN);
AddChart(high = DnH, low = DnL, open = DnO, close = DnC, type = ChartType.CANDLE, growcolor = Color.RED);

input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.SIMPLE;
input factor = 1.5;
input length_1 = 20;
input lenSMA = 50;
input lenATR = 14;
input firstFactor = 1.610;
input secondFactor = 2.618;
input thirdFactor = 4.236;
def sDev = StDev(data = price[-displace], length = length);
plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + Num_Dev_Dn * sDev;
plot UpperBand = MidLine + Num_Dev_up * sDev;
LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

input trueRangeAverageType = AverageType.SIMPLE;
def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def average = MovingAverage(averageType, price, length);
plot Avg = average[-displace];
Avg.SetDefaultColor(GetColor(1));
plot Upper_Band = average[-displace] + shift[-displace];
Upper_Band.SetDefaultColor(GetColor(8));
plot Lower_Band = average[-displace] - shift[-displace];
Lower_Band.SetDefaultColor(GetColor(5));
AddCloud(UpperBand, Upper_Band, Color.CYAN);
AddCloud (LowerBand, Lower_Band, Color.GREEN);
script MTF_ATR {
    input TF = AggregationPeriod.HOUR;
    input Length = 14;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            Length
        )
    ;
}
;
def VAtr = ATR(lenATR);
def vSma = Average(close, lenSMA);
def fib1u = vSma + VAtr * firstFactor;
def fib1l = vSma - VAtr * firstFactor;
def fib2u = vSma + VAtr * secondFactor;
def fib2l = vSma - VAtr * secondFactor;
def fib3u = vSma + VAtr * thirdFactor;
def fib3l = vSma - VAtr * thirdFactor;
plot  Centerline = vSma;
plot band1upper = fib1u;
band1upper.SetDefaultColor(GetColor(9));
plot band1lower = fib1l;
band1lower.SetDefaultColor(GetColor(9));
plot band2upper = fib2u;
band2upper.SetDefaultColor(GetColor(6));
plot band2lower = fib2l;
band2lower.SetDefaultColor(GetColor(6));
plot band3upper = fib3u;
band3upper.SetDefaultColor(GetColor(5));
plot band3lower = fib3l;
band3lower.SetDefaultColor(GetColor(5));
 
A lower study with Heikin Ashi candles and Fib bands.


Ruby:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon _last update Jan 17th 2021
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 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);

input lenSMA = 50;
input lenATR = 14;
input firstFactor = 1.610;
input secondFactor = 2.618;
input thirdFactor = 3.618;
input fourthFactor = 4.236;
input sPeriod = {default DAY};
def offset = 1;

script MTF_ATR {
    input TF = AggregationPeriod.HOUR;
    input Length = 14;
    input averageType = AverageType.WILDERS;
    plot ATR =
        MovingAverage(
            averageType,
            TrueRange(
                high(period = TF),
                close(period = TF),
                low(period = TF)
            ),
            Length
        )
    ;
}
;
def VAtr = ATR(lenATR);
def vSma = Average(close, lenSMA);
def fib1u = vSma + VAtr * firstFactor;
def fib1l = vSma - VAtr * firstFactor;
def fib2u = vSma + VAtr * secondFactor;
def fib2l = vSma - VAtr * secondFactor;
def fib3u = vSma + VAtr * thirdFactor;
def fib3l = vSma - VAtr * thirdFactor;
def fib4u = vSma + VAtr * fourthFactor;
def fib4l = vSma - VAtr * fourthFactor;
plot  Centerline = vSma;
plot band1upper = fib1u;
band1upper.SetDefaultColor(GetColor(9));
plot band1lower = fib1l;
band1lower.SetDefaultColor(GetColor(9));
plot band2upper = fib2u;
band2upper.SetDefaultColor(GetColor(6));
plot band2lower = fib2l;
band2lower.SetDefaultColor(GetColor(6));
plot band3upper = fib3u;
band3upper.SetDefaultColor(GetColor(5));
plot band3lower = fib3l;
band3lower.SetDefaultColor(GetColor(5));
plot band4upper = fib4u;
band3upper.SetDefaultColor(GetColor(5));
plot band4lower = fib4l;
band3lower.SetDefaultColor(GetColor(5));

AddCloud(band1upper, band1lower, color.light_green, color.light_green);
AddCloud(band2upper, band3upper, color.yellow, color.yellow);
AddCloud(band2upper, band1upper, color.cyan, color.cyan);
AddCloud(band2lower, band3lower, color.yellow, color.yellow);
AddCloud(band2lower, band1lower, color.cyan, color.cyan);
AddCloud(band4lower, band3lower, color.red, color.red);
AddCloud(band4upper, band3upper, color.red, color.red);

def varhigh = high(period = sPeriod)[offset];
def varlow = low(period = sPeriod)[offset];
def h = fold ih = 0 to 1 while !IsNaN(close[10]) do varhigh;
def l = fold il = 0 to 1 while !IsNaN(close[10]) do varlow;

plot DayHigh = if h > 1 then h else Double.NaN;
DayHigh.SetPaintingStrategy(PaintingStrategy.LINE);
DayHigh.SetDefaultColor(Color.LIGHT_GREEN);

plot DayLow = if l > 1 then l else Double.NaN;
DayLow.SetPaintingStrategy(PaintingStrategy.LINE);
DayLow.SetDefaultColor(Color.LIGHT_RED);

AddCloud(if close > DayHigh or close[-1] > DayHigh then DayHigh else Double.NEGATIVE_INFINITY,if close > DayHigh or close[-1] > DayHigh then DayLow else Double.NEGATIVE_INFINITY,color.LIGHT_GREEN,color.LIGHT_GREEN,no);

AddCloud(if close < DayLow or close[-1] < DayLow then DayHigh else Double.NEGATIVE_INFINITY,if close < DayLow or close[-1] < DayLow then DayLow else Double.NEGATIVE_INFINITY,color.LIGHT_RED,color.LIGHT_RED,no);

AddCloud(if (close <= DayHigh and close >= DayLow) or (close[-1] <= DayHigh and close[-1] >= DayLow) then DayHigh else Double.NEGATIVE_INFINITY,if (close <= DayHigh and close >= DayLow) or (close[-1] <= DayHigh and close[-1] >= DayLow) then DayLow else Double.NEGATIVE_INFINITY,color.LIGHT_GRAY,color.LIGHT_GRAY,no);
 
A lower study with Heikin Ashi Candles, ichimoku and Acceleration Bands


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;

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);

input Length = 20;
plot UpperBand = Average(data = high * (1 + 4 * (high - low) / (high + low)), length = Length);
plot LowerBand = Average(data = low * (1 - 4 * (high - low) / (high + low)), length = Length);
 
A lower study with Heikin Ashi candles and Fib Bollinger Bands.

Ruby:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon

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 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);

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);
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
input fib1 = 0.382;
input fib2 = 0.618;
input fib3 = 0.764;
def sDev = stdev(data = price[-displace], length = length);
plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;
plot UpperFibLine1 = (UpperBand - MidLine) * fib1 + MidLine;
plot UpperFibLine2 = (UpperBand - MidLine) * fib2 + MidLine;
plot UpperFibLine3 = (UpperBand - MidLine) * fib3 + MidLine;
plot LowerFibLine1 = (LowerBand - MidLine) * fib1 + MidLine;
plot LowerFibLine2 = (LowerBand - MidLine) * fib2 + MidLine;
plot LowerFibLine3 = (LowerBand - MidLine) * fib3 + MidLine;
LowerBand.SetLineWeight(2);
MidLine.SetLineWeight(1);
UpperBand.SetLineWeight(2);
LowerBand.SetDefaultColor(Color.DARK_RED);
MidLine.SetDefaultColor(Color.GRAY);
UpperBand.SetDefaultColor(Color.DARK_RED);
UpperFibLine1.SetDefaultColor(Color.GRAY);
UpperFibLine2.SetDefaultColor(Color.GRAY);
UpperFibLine3.SetDefaultColor(Color.GRAY);
LowerFibLine1.SetDefaultColor(Color.GRAY);
LowerFibLine2.SetDefaultColor(Color.GRAY);
LowerFibLine3.SetDefaultColor(Color.GRAY);
UpperFibLine1.SetStyle(Curve.SHORT_DASH);
UpperFibLine2.SetStyle(Curve.SHORT_DASH);
UpperFibLine3.SetStyle(Curve.SHORT_DASH);
LowerFibLine1.SetStyle(Curve.SHORT_DASH);
LowerFibLine2.SetStyle(Curve.SHORT_DASH);
LowerFibLine3.SetStyle(Curve.SHORT_DASH);
 
A lower study with Heikin Ashi Candles and Bollinger Bands.
Ruby:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon
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 displace = 0;
input length = 20;
input factor_Dn1 = -1.0;
input factor_Up1 = 1.0;
input factor_Dn2 = -2.0;
input factor_Up2 = 2.0;
input factor_Dn3 = -3.0;
input factor_Up3 = 3.0;
input factor_Dn4 = -4.0;
input factor_Up4 = 4.0;
input averageType = averageType.EXPONENTIAL;

#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);


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;


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 ATR = MovingAverage(averageType, TrueRange(high, close, low), length);

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand1 = MidLine + factor_Dn1 * atr;
plot UpperBand1 = MidLine + factor_Up1 * atr;
plot LowerBand2 = midLine + factor_Dn2 * atr;
plot UpperBand2 = midline + factor_Up2 * atr;
plot LowerBand3 = midLine + factor_Dn3 * atr;
plot UpperBand3 = midLine + factor_Up3 * atr;
plot LowerBand4 = midLine + factor_Dn4 * atr;
plot UpperBand4 = midLine + factor_Up4 * atr;


AddCloud(UpperBand2, upperBand1, Color.ligHT_RED, Color.ligHT_RED);
AddCloud(LowerBand2, lowerband1, Color.ligHT_GREEN, Color.lighT_GREEN);
AddCloud(UpperBand3, upperBand2, Color.RED, Color.RED);
AddCloud(LowerBand3, lowerband2, Color.GREEN, Color.GREEN);
AddCloud(UpperBand4, upperBand3, Color.darK_RED, Color.darK_RED);
AddCloud(LowerBand4, lowerband3, Color.dARK_GREEN, Color.darK_GREEN);
 
A lower study with Heikin Ashi and Fib Bollinger Bands.

Ruby:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon
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 length = 30;
input src = hlc3;
input mult = 3.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);

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;
 
A lower study with Heikin Ashi and Guppy moving arrays.


Ruby:
#Daryl Guppy,
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon _last update Jan 17th 2021
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 period0 = 3;
input period1 = 6;
input period2 = 9;
input period3 = 12;
input period4 = 15;
input period5 = 18;
input period6 = 21;
input period7 = 24;
input period8 = 27;
input period9 = 30;
input period10 = 33;
input period11 = 36;
input period12 = 39;
input period13 = 42;
input period14 = 45;
input period15 = 48;
input period16 = 51;
input period17 = 54;
input period18 = 57;
input period19 = 60;
input period20 = 63;
input period21 = 66;
input period22 = 200;
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);



input AvgType = AverageType.Exponential;
input data = close;
input seperator = 1.00;

script Avg
{
input AvgType = AverageType.Exponential;
input length = 10;
input data = close;
plot MA = MovingAverage(AverageType = AvgType, data = close, length = length);
}

DefineGlobalColor("Up", Color.GREEN);
DefineGlobalColor("Down", Color.RED);
DefineGlobalColor("NUp", Color.DARK_GREEN);
DefineGlobalColor("NDown", Color.DARK_RED);
DefineGlobalColor("Neutral", Color.BLUE);

DefineGlobalColor("G1", CreateColor(42,137,71));
DefineGlobalColor("G2", CreateColor(54,156,72));
DefineGlobalColor("G3", CreateColor(69,176,73));
DefineGlobalColor("G4", CreateColor(96,195,85));
DefineGlobalColor("G5", CreateColor(129,215,102));
DefineGlobalColor("G6", CreateColor(164,235,122));
DefineGlobalColor("R1", CreateColor(235,24,9));
DefineGlobalColor("R2", CreateColor(222,34,35));
DefineGlobalColor("R3", CreateColor(209,56,66));
DefineGlobalColor("R4", CreateColor(196,75,91));
DefineGlobalColor("R5", CreateColor(183,91,109));
DefineGlobalColor("R6", CreateColor(170,105,122));

HidePricePlot(No);

plot MA0 = Avg(AvgType = AvgType, data = data, length = period0);
plot MA1 = Avg(AvgType = AvgType, data = data, length = period1);
plot MA2 = Avg(AvgType = AvgType, data = data, length = period2);
plot MA3 = Avg(AvgType = AvgType, data = data, length = period3);
plot MA4 = Avg(AvgType = AvgType, data = data, length = period4);
plot MA5 = Avg(AvgType = AvgType, data = data, length = period5);
plot MA6 = Avg(AvgType = AvgType, data = data, length = period6);
plot MA7 = Avg(AvgType = AvgType, data = data, length = period7);
plot MA8 = Avg(AvgType = AvgType, data = data, length = period8);
plot MA9 = Avg(AvgType = AvgType, data = data, length = period9);
plot MA10 = Avg(AvgType = AvgType, data = data, length = period10);
plot MA11 = Avg(AvgType = AvgType, data = data, length = period11);
plot MA12 = Avg(AvgType = AvgType, data = data, length = period12);
plot MA13 = Avg(AvgType = AvgType, data = data, length = period13);
plot MA14 = Avg(AvgType = AvgType, data = data, length = period14);
plot MA15 = Avg(AvgType = AvgType, data = data, length = period15);
plot MA16 = Avg(AvgType = AvgType, data = data, length = period16);
plot MA17 = Avg(AvgType = AvgType, data = data, length = period17);
plot MA18 = Avg(AvgType = AvgType, data = data, length = period18);
plot MA19 = Avg(AvgType = AvgType, data = data, length = period19);
plot MA20 = Avg(AvgType = AvgType, data = data, length = period20);
plot MA21 = Avg(AvgType = AvgType, data = data, length = period21);
plot MA22 = Avg(AvgType = AvgType, data = data, length = period22);


MA0.SetDefaultColor(COLOR.LIME);

MA1.SetDefaultColor(GlobalColor("G1"));
MA2.SetDefaultColor(GlobalColor("G2"));
MA3.SetDefaultColor(GlobalColor("G3"));
MA4.SetDefaultColor(GlobalColor("G4"));
MA5.SetDefaultColor(GlobalColor("G5"));

MA7.SetDefaultColor(COLOR.RED);
MA7.SetLineWeight(2);

MA8.SetDefaultColor(GlobalColor("R2"));
MA9.SetDefaultColor(GlobalColor("R3"));
MA10.SetDefaultColor(GlobalColor("R4"));
MA11.SetDefaultColor(GlobalColor("R5"));
MA12.SetDefaultColor(GlobalColor("R6"));

MA1.HideTitle();
MA2.HideTitle();
MA3.HideTitle();
MA4.HideTitle();
MA5.HideTitle();
MA6.HideTitle();
MA7.HideTitle();
MA8.HideTitle();
MA9.HideTitle();
MA10.HideTitle();
MA11.HideTitle();
MA12.HideTitle();

MA2.HideBubble();
MA3.HideBubble();
MA4.HideBubble();
MA5.HideBubble();
MA6.HideBubble();
MA8.HideBubble();
MA9.HideBubble();
MA10.HideBubble();
MA11.HideBubble();
MA12.HideBubble();
 
A lower study with Heikin Ashi candles, Fib bands and MTF moving averages.

Ruby:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
 # R1V3 2011KumoBob
#Bon Bon  2021
declare lower;
input period = 1;
input hideCandles = yes;
input candleSmoothing = {default Valcu, Vervoort};
input show_bubble_labels = yes;
input bubbles = yes;
input arrows = yes;
def src = close;
input length =20;
def sma_a = average(src, length);
def stdev = stdev(src, length);
input movingAverageType = {default  TEMA, Exponential, Hull };
 input FastMovingAverage = 5;
    input SlowMovingAverage = 13;
    Input Method = {"At Fast MA", "MA Cross",Default "Buy at Min MA Sell at Max MA"};
    input AverageMode = {"Bar",default DAY, "2 DAYS", "3 DAYS", "4 DAYS", WEEK, MONTH};

    input BubblesOn = Yes;
    input ShowPlot = Yes;
    input ShowPoints = Yes;
    input ShowArrows = Yes;
    Input PriceColorOn = No;
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 Mode;
    switch (Method){
    case "At Fast MA":
    Mode = 1;
    case "MA Cross":
    Mode = 2;
    case "Buy at Min MA Sell at Max MA":
    Mode = 3;
    }

    


    def PeriodChoice;
    switch (AverageMode){
    case "Day":
    PeriodChoice = 1;
    case "2 DAYS":
    PeriodChoice = 2;
    case "3 DAYS":
    PeriodChoice = 3;
    case "4 DAYS":
    PeriodChoice = 4;
    case WEEK:
    PeriodChoice = 7;
    case MONTH:
    PeriodChoice = 30;
    case "Bar":
    PeriodChoice = 0;
    }


    def BarMA1 = Average(close () , FastMovingAverage );
    Def Day1MA1 = Average(close (period = "DAY" ), FastMovingAverage );
    Def Day2MA1 = Average(close (period = "2 DAYS"), FastMovingAverage );
    Def Day3MA1 = Average(close (period = "3 DAYS"), FastMovingAverage );
    Def Day4MA1 = Average(close (period = "4 DAYS"), FastMovingAverage );
    Def Day7MA1 = Average(close (period = "WEEK" ), FastMovingAverage);
    Def Day30MA1 = Average(close (period = "MONTH") , FastMovingAverage);

    def BarMA2 = Average(close () , SlowMovingAverage);
    Def Day1MA2 = Average(close (period = "DAY" ), SlowMovingAverage);
    Def Day2MA2 = Average(close (period = "2 DAYS"), SlowMovingAverage);
    Def Day3MA2 = Average(close (period = "3 DAYS"), SlowMovingAverage);
    Def Day4MA2 = Average(close (period = "4 DAYS"), SlowMovingAverage);
    Def Day7MA2 = Average(close (period = "WEEK" ), SlowMovingAverage);
    Def Day30MA2 = Average(close (period = "MONTH") , SlowMovingAverage);


    def FMA =
    If PeriodChoice == 1 then Day1MA1 else
    If PeriodChoice == 2 then Day2MA1 else
    If PeriodChoice == 3 then Day3MA1 else
    If PeriodChoice == 4 then Day4MA1 else
    If PeriodChoice == 7 then Day7MA1 else
    If PeriodChoice == 30 then Day30MA1 else BarMA1;
    plot FastMA = if !ShowPlot then Double.NaN else FMA;

    FastMA.AssignValueColor(if Close > FMA then Color.Green else if Close < FMA then Color.Red else Color.Yellow); AddChartBubble(FastMA && BubblesOn && !IsNaN(close) and IsNaN(close[-1]), FastMA, concat("", FastMovingAverage ), if Close > FMA then Color.Green else if Close < FMA then Color.Red else Color.Yellow); def SMA = If PeriodChoice == 1 then Day1MA2 else If PeriodChoice == 2 then Day2MA2 else If PeriodChoice == 3 then Day3MA2 else If PeriodChoice == 4 then Day4MA2 else If PeriodChoice == 7 then Day7MA2 else If PeriodChoice == 30 then Day30MA2 else BarMA2; plot SlowMA = if !ShowPlot then Double.NaN else SMA; SlowMA.AssignValueColor(if Close > SMA then Color.Green else if Close < SMA then Color.Red else Color.Yellow); AddChartBubble(SlowMA && BubblesOn && !IsNaN(close) and IsNaN(close[-1]), SlowMA, concat("", SlowMovingAverage), if Close > SMA then Color.Green else if Close < SMA then Color.Red else Color.Yellow);  def Max = Max(FMA, SMA); def Min = Min(FMA, SMA); def Position = if Close > Max(FMA, SMA) then 2 else if Close < Min(FMA, SMA) then -2 else 0; rec Trend = If Mode == 3 then if Position[1] == 2 && Position[0] <= 0 then -2 else if Position[1] == -2 && Position[0] >= 0 then 2 else
    if Position[1] == 0 && Position[0] == 2 then 2 else
    if Position[1] == 0 && Position[0] == -2 then -2 else Trend[1]
    else If Mode == 1 then if Close > FMA then 2 else -2
    else if FMA > SMA then 2 else -2;

    AssignPriceColor(if !PriceColorOn then Color.Current else If Trend > 0 then Color.Green else Color.Red);
    rec Point = If Mode == 3 then
    if Position[1] == 2 && Position[0] <= 0 then Max else if Position[1] == -2 && Position[0] >= 0 then Min else
    if Position[1] == 0 && Position[0] == 2 then Max else
    if Position[1] == 0 && Position[0] == -2 then Min else Point[1]
    else If Mode == 1 then if Close > FMA then Close else Point[1]
    else if FMA > SMA then Close else Point[1];
    plot SignalPoint = If !ShowPoints then Double.NaN else Point;
    SignalPoint.SetLineWeight(1);
    SignalPoint.SetPaintingStrategy(PaintingStrategy.Points);
    SignalPoint.AssignValueColor(If Trend > 0 then Color.Green else Color.Red);
    # ARROWS ROUTINE
    # ___
    def x_over = Crosses(Trend, 0);
    plot up = if ShowArrows && x_over && Trend > 0 then Low else Double.NaN;
    up.SetPaintingStrategy(PaintingStrategy.Arrow_up);
    up.SetDefaultColor(Color.white);
    plot dn = if ShowArrows && x_over && Trend < 0 then High else Double.NaN; dn.SetPaintingStrategy(PaintingStrategy.Arrow_down); dn.SetDefaultColor(Color.white);

plot upper3 = sma_a + (stdev *3);
Upper3.AssignValueColor(if upper3> upper3[1] then color.green else color.red);
plot lower3  = sma_a - (stdev * 3);
Lower3.AssignValueColor(if lower3> lower3[1] then color.green else color.red);
plot upper2 = sma_a + (stdev * 2);
plot lower2  = sma_a - (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_a + (stdev * 1);
plot lower1  = sma_a - (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);
 
Here is a lower study with moving averages, Heikin Ashi candles, daily high and low, and Fib bands.


Ruby:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon _last update Jan 17th 2021
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 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);


input lenSMA = 50;
input lenATR = 14;
input firstFactor = 1.610;
 
You may wonder why I figure out this indicator provided by ThinkorSwim without the code. Since I would like to apply this indicator to a lower study. Without the code, I am unable to put it in the study. Here is is an example of a lower study with Heikin Ashi candles and moving averages. Sometime, I combined more than 10 indicators into one indicator in a lower study so that you can load a combined indicator just clicking a mouse. In addition, this lower study allows for you to see the stocks at the same aggregation periods without need to use Grid options. You can enlarge main study and lower study at the same time, but Grid options will take more times to do that

Ruby:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon
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_a = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input averageType_a = AverageType.exponential;
input averageType1 = AverageType.SIMPLE;
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);


input price = close;

input displace = 0;

input length = 10;

input Num_Dev_Dn = -1.0;

input Num_Dev_up = 1.0;

def nan  =  Double.NaN;

input averageType = AverageType.SIMPLE;

input fill = yes;

def sDev = StDev(data = price[-displace], length = length);

def MidLine = MovingAverage(averageType, data = price[-displace], length = length);

plot LowerBand = MidLine + Num_Dev_Dn * sDev;

plot UpperBand = MidLine + Num_Dev_up * sDev;

UpperBand.AssignValueColor(if UpperBand > UpperBand[1] then Color.UPTICK else Color.RED);

LowerBand.AssignValueColor(if LowerBand > LowerBand[1] then Color.UPTICK else Color.RED);

AddCloud(UpperBand, LowerBand, Color.GREEN, Color.RED);
 

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