Is there a way to define your own script such that the "switch" variable recognizes it?

fungus12

Member
So essentially what I'm trying to do is add a quadruple exponential moving average to this code:

Code:
# Heikin Ashi Smoothed
# HoboTheClown / blt
# 9.15.2016

# HoboTheClown: I recently found a code for smoothed heiken ashi bars,
# however for some reason all the bars are displayed as one color (going up or down).
#
# blt: Modified the code and replaced the addchart code at the bottom,
# you should now see proper coloring. This is how that was coded to plot
# as an overlay to the chart candlesticks. That is two sets of candles,
# with different coloring for each. If you have the heikin ashi candle
# coloring code on your chart, then they will likely appear the same color.

#Update that "fixes" the green candles.
#JTP
#02/7/2021

input period = 20;
input hideCandles = Yes; #Now functional - JTP
input candleSmoothing = {default Valcu, Vervoort};

DefineGlobalColor("RisingMA", color.green);
DefineGlobalColor("FallingMA", color.red);

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

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

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);
case DEMA:
    openMA = compoundValue(1, DEMA(open, period), open);
    closeMA = compoundValue(1, DEMA(close, period), close);
    highMA = compoundValue(1, DEMA(high, period), high);
    lowMA = compoundValue(1, DEMA(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 o = haOpen + 0;
o.hide();

### Wicks and Shadows

def haLow = min(lowMA, haOpen);
def haHigh = max(highMA,haOpen);

### NO LONGER SUPPORTED BY TOS
###
### AddChart(high = haHigh, low = haLow, open = o, close = haclose, type = ChartType.CANDLE, growColor = GlobalColor("RisingMA"), fallColor = GlobalColor("FallingMA"), neutralColor = color.gray);

#Red Candlesticks -----------------------------------------------------------------|

input charttype = ChartType.CANDLE;
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.red, fallColor = Color.green, neutralColor = Color.current, high = haHigh_fall, low = haLow_fall, open = haOpen_fall, close = haClose_fall , type = ChartType.CANDLE);

#Green Candlesticks -----------------------------------------------------------------|

#Removed and/or replaced - JTP
#def HAclose1 = if haOpen<=haClose
               # then hahigh
               # else double.nan;
#def HAclose1 = ohlc4 -1;
#def HAopen1  = if haopen<=haclose
              #  then haclose
              # then CompoundValue(1, (HAopen[1] + HAclose[1]) /2, (open[1] + close[1]) / 2)         
              # else double.nan;

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.green, fallColor = Color.red, neutralColor = Color.current, high = haHigh_rise, low = haLow_rise, open = haOpen_rise, close = HAclose_rise, type = ChartType.CANDLE);

# End Study

I got this study here if anyone's curious: https://usethinkscript.com/threads/smoothed-heikin-ashi-for-thinkorswim.216/page-2

Anyway, my issue is I'm trying to add a QEMA, which is currently not built into TOS. I made my own code for my QEMA based on the code for the TEMA:
Code:
input price = close;
input length = 10;

def ema1 = ExpAverage(price, length);
def ema2 = ExpAverage(ema1, length);
def ema3 = ExpAverage(ema2, length);
def ema4 = ExpAverage (ema3, length);

plot QEMA = 4 * ema1 - 4 * ema2 + ema3;
QEMA.SetDefaultColor(GetColor(0));


My question is, is there some way to get switch(movingAverageType) to recognize a custom script or is that purely for built-in indicators? I had a look at the manual and they said to use the "script" function to be able to reference your own scripts in a piece of code. I tried that but no luck. Any help would be appreciated.
 
So essentially what I'm trying to do is add a quadruple exponential moving average to this code:

Code:
# Heikin Ashi Smoothed
# HoboTheClown / blt
# 9.15.2016

# HoboTheClown: I recently found a code for smoothed heiken ashi bars,
# however for some reason all the bars are displayed as one color (going up or down).
#
# blt: Modified the code and replaced the addchart code at the bottom,
# you should now see proper coloring. This is how that was coded to plot
# as an overlay to the chart candlesticks. That is two sets of candles,
# with different coloring for each. If you have the heikin ashi candle
# coloring code on your chart, then they will likely appear the same color.

#Update that "fixes" the green candles.
#JTP
#02/7/2021

input period = 20;
input hideCandles = Yes; #Now functional - JTP
input candleSmoothing = {default Valcu, Vervoort};

DefineGlobalColor("RisingMA", color.green);
DefineGlobalColor("FallingMA", color.red);

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

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

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);
case DEMA:
    openMA = compoundValue(1, DEMA(open, period), open);
    closeMA = compoundValue(1, DEMA(close, period), close);
    highMA = compoundValue(1, DEMA(high, period), high);
    lowMA = compoundValue(1, DEMA(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 o = haOpen + 0;
o.hide();

### Wicks and Shadows

def haLow = min(lowMA, haOpen);
def haHigh = max(highMA,haOpen);

### NO LONGER SUPPORTED BY TOS
###
### AddChart(high = haHigh, low = haLow, open = o, close = haclose, type = ChartType.CANDLE, growColor = GlobalColor("RisingMA"), fallColor = GlobalColor("FallingMA"), neutralColor = color.gray);

#Red Candlesticks -----------------------------------------------------------------|

input charttype = ChartType.CANDLE;
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.red, fallColor = Color.green, neutralColor = Color.current, high = haHigh_fall, low = haLow_fall, open = haOpen_fall, close = haClose_fall , type = ChartType.CANDLE);

#Green Candlesticks -----------------------------------------------------------------|

#Removed and/or replaced - JTP
#def HAclose1 = if haOpen<=haClose
               # then hahigh
               # else double.nan;
#def HAclose1 = ohlc4 -1;
#def HAopen1  = if haopen<=haclose
              #  then haclose
              # then CompoundValue(1, (HAopen[1] + HAclose[1]) /2, (open[1] + close[1]) / 2)     
              # else double.nan;

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.green, fallColor = Color.red, neutralColor = Color.current, high = haHigh_rise, low = haLow_rise, open = haOpen_rise, close = HAclose_rise, type = ChartType.CANDLE);

# End Study

I got this study here if anyone's curious: https://usethinkscript.com/threads/smoothed-heikin-ashi-for-thinkorswim.216/page-2

Anyway, my issue is I'm trying to add a QEMA, which is currently not built into TOS. I made my own code for my QEMA based on the code for the TEMA:
Code:
input price = close;
input length = 10;

def ema1 = ExpAverage(price, length);
def ema2 = ExpAverage(ema1, length);
def ema3 = ExpAverage(ema2, length);
def ema4 = ExpAverage (ema3, length);

plot QEMA = 4 * ema1 - 4 * ema2 + ema3;
QEMA.SetDefaultColor(GetColor(0));


My question is, is there some way to get switch(movingAverageType) to recognize a custom script or is that purely for built-in indicators? I had a look at the manual and they said to use the "script" function to be able to reference your own scripts in a piece of code. I tried that but no luck. Any help would be appreciated.

You had the right ideas. This uses a script, added to a switch for movingaveragetype

Ruby:
# Heikin Ashi Smoothed
# HoboTheClown / blt
# 9.15.2016

# HoboTheClown: I recently found a code for smoothed heiken ashi bars,
# however for some reason all the bars are displayed as one color (going up or down).
#
# blt: Modified the code and replaced the addchart code at the bottom,
# you should now see proper coloring. This is how that was coded to plot
# as an overlay to the chart candlesticks. That is two sets of candles,
# with different coloring for each. If you have the heikin ashi candle
# coloring code on your chart, then they will likely appear the same color.

#Update that "fixes" the green candles.
#JTP
#02/7/2021

#Sleepyz 20210729 - added QEMA script and switch @usethinkscript request by @fungus12

input period = 20;
input hideCandles = Yes; #Now functional - JTP
input candleSmoothing = {default Valcu, Vervoort};

DefineGlobalColor("RisingMA", color.green);
DefineGlobalColor("FallingMA", color.red);

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

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

script QEMA{
input price = close;
input length = 10;

def ema1 = ExpAverage(price, length);
def ema2 = ExpAverage(ema1, length);
def ema3 = ExpAverage(ema2, length);
def ema4 = ExpAverage (ema3, length);

plot QUAD = ExpAverage(ExpAverage(ExpAverage(ExpAverage(close, 10), 10), 10), 10);
}
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);
case DEMA:
    openMA = compoundValue(1, DEMA(open, period), open);
    closeMA = compoundValue(1, DEMA(close, period), close);
    highMA = compoundValue(1, DEMA(high, period), high);
    lowMA = compoundValue(1, DEMA(low, period), low);
case QEMA:
    openMA = compoundValue(1, QEMA(open,period),open);
    closeMA = compoundValue(1, QEMA(close, period), close);
    highMA = compoundValue(1, QEMA(high, period), high);
    lowMA = compoundValue(1, QEMA(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 o = haOpen + 0;
o.hide();

### Wicks and Shadows

def haLow = min(lowMA, haOpen);
def haHigh = max(highMA,haOpen);

### NO LONGER SUPPORTED BY TOS
###
### AddChart(high = haHigh, low = haLow, open = o, close = haclose, type = ChartType.CANDLE, growColor = GlobalColor("RisingMA"), fallColor = GlobalColor("FallingMA"), neutralColor = color.gray);

#Red Candlesticks -----------------------------------------------------------------|

input charttype = ChartType.CANDLE;
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.red, fallColor = Color.green, neutralColor = Color.current, high = haHigh_fall, low = haLow_fall, open = haOpen_fall, close = haClose_fall , type = ChartType.CANDLE);

#Green Candlesticks -----------------------------------------------------------------|

#Removed and/or replaced - JTP
#def HAclose1 = if haOpen<=haClose
               # then hahigh
               # else double.nan;
#def HAclose1 = ohlc4 -1;
#def HAopen1  = if haopen<=haclose
              #  then haclose
              # then CompoundValue(1, (HAopen[1] + HAclose[1]) /2, (open[1] + close[1]) / 2)       
              # else double.nan;

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.green, fallColor = Color.red, neutralColor = Color.current, high = haHigh_rise, low = haLow_rise, open = haOpen_rise, close = HAclose_rise, type = ChartType.CANDLE);

# End Study
 
Last edited:
You had the right ideas. This uses a script, added to a switch for movingaveragetype
Thanks a lot dude. I asked Mobius this question a few hours ago and he just responded, he told me my QEMA formula was wrong and I should use this instead:

Code:
input length = 10;
plot QuadEMA = ExpAverage(ExpAverage(ExpAverage(ExpAverage(close, 10), 10), 10), 10);

I went ahead and did that and it works. Thanks again!
 
Thanks a lot dude. I asked Mobius this question a few hours ago and he just responded, he told me my QEMA formula was wrong and I should use this instead:

Code:
input length = 10;
plot QuadEMA = ExpAverage(ExpAverage(ExpAverage(ExpAverage(close, 10), 10), 10), 10);

I went ahead and did that and it works. Thanks again!

I edited the script I posted above with the new formula. Thanks for letting me know. Glad it works for you!
 
I edited the script I posted above with the new formula. Thanks for letting me know. Glad it works for you!
Thanks for your help man. Just one more thing I'd like to add to this if it's possible, do you think one can add a case for regular heikin ashi candles? This script is for a double-smoothed heikin ashi but it'd be useful to also have the ability to use just a regular heikin ashi, that way I can have both candlesticks and heikins on my chart.
 
Thanks for your help man. Just one more thing I'd like to add to this if it's possible, do you think one can add a case for regular heikin ashi candles? This script is for a double-smoothed heikin ashi but it'd be useful to also have the ability to use just a regular heikin ashi, that way I can have both candlesticks and heikins on my chart.

You're welcome! Added under smoothing, regular heiken ashi candlesticks as requested

Ruby:
# Heikin Ashi Smoothed
# HoboTheClown / blt
# 9.15.2016

# HoboTheClown: I recently found a code for smoothed heiken ashi bars,
# however for some reason all the bars are displayed as one color (going up or down).
#
# blt: Modified the code and replaced the addchart code at the bottom,
# you should now see proper coloring. This is how that was coded to plot
# as an overlay to the chart candlesticks. That is two sets of candles,
# with different coloring for each. If you have the heikin ashi candle
# coloring code on your chart, then they will likely appear the same color.

#Update that "fixes" the green candles.
#JTP
#02/7/2021

#Sleepyz 20210729 - added QEMA script and switch @usethinkscript request by @fungus12
#Sleepyz 20210730 - added option for regular HA candles under the smoothing option

input period = 20;
input hideCandles = Yes; #Now functional - JTP
input candleSmoothing = {default Valcu, Vervoort, "Heiken Ashi"};

DefineGlobalColor("RisingMA", color.green);
DefineGlobalColor("FallingMA", color.red);

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

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

script QEMA{
input price = close;
input length = 10;

def ema1 = ExpAverage(price, length);
def ema2 = ExpAverage(ema1, length);
def ema3 = ExpAverage(ema2, length);
def ema4 = ExpAverage (ema3, length);

plot QUAD = ExpAverage(ExpAverage(ExpAverage(ExpAverage(close, 10), 10), 10), 10);
}
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);
case DEMA:
    openMA = compoundValue(1, DEMA(open, period), open);
    closeMA = compoundValue(1, DEMA(close, period), close);
    highMA = compoundValue(1, DEMA(high, period), high);
    lowMA = compoundValue(1, DEMA(low, period), low);
case QEMA:
    openMA = compoundValue(1, QEMA(open,period),open);
    closeMA = compoundValue(1, QEMA(close, period), close);
    highMA = compoundValue(1, QEMA(high, period), high);
    lowMA = compoundValue(1, QEMA(low, period), low);
}

hidePricePlot(hideCandles);

def haOpen;
def haClose;
def haLow;
def haHigh;

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) ;
 haLow = min(lowMA, haOpen);
 haHigh = max(highMA,haOpen);
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);
 haLow = min(lowMA, haOpen);
 haHigh = max(highMA,haOpen);
case "Heiken Ashi":
 HAclose   =  (open + high + low + close) / 4;
 HAopen    = CompoundValue( 1, ( HAopen[1] + HAclose[1] ) / 2, HAclose );
 HAhigh    = Max( high, Max( HAopen, HAclose ) );
 HAlow     = Min( low, Min( HAopen, HAclose ) );

}

plot o = haOpen + 0;
o.hide();

### Wicks and Shadows



### NO LONGER SUPPORTED BY TOS
###
### AddChart(high = haHigh, low = haLow, open = o, close = haclose, type = ChartType.CANDLE, growColor = GlobalColor("RisingMA"), fallColor = GlobalColor("FallingMA"), neutralColor = color.gray);

#Red Candlesticks -----------------------------------------------------------------|

input charttype = ChartType.CANDLE;
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.red, fallColor = Color.green, neutralColor = Color.current, high = haHigh_fall, low = haLow_fall, open = haOpen_fall, close = haClose_fall , type = ChartType.CANDLE);

#Green Candlesticks -----------------------------------------------------------------|

#Removed and/or replaced - JTP
#def HAclose1 = if haOpen<=haClose
               # then hahigh
               # else double.nan;
#def HAclose1 = ohlc4 -1;
#def HAopen1  = if haopen<=haclose
              #  then haclose
              # then CompoundValue(1, (HAopen[1] + HAclose[1]) /2, (open[1] + close[1]) / 2)      
              # else double.nan;

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.green, fallColor = Color.red, neutralColor = Color.current, high = haHigh_rise, low = haLow_rise, open = haOpen_rise, close = HAclose_rise, type = ChartType.CANDLE);

# End Study
 

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