Help editing code to base actions on Smoothed Heiken Aishi instead of Impulse MACD

hey

New member
VIP
Will anyone help me to edit this code to perform its current function (changing the colors on the borders and wicks of candles) based on the colors of the Smoothed Heiken Aishi instead of changing them based on the colors of the Impulse MACD.

Code:
Code:
#
# Charles Schwab & Co. (c) 2012-2025
#

input length = 13;
input paintBars = yes;

def EMA = ExpAverage(close, length);
def MACD = ExpAverage(close, 12) - ExpAverage(close, 26);
def MACDHist = MACD - ExpAverage(MACD, 9);
def GreenPrice = EMA > EMA[1] and MACDHist > MACDHist[1];
def RedPrice = EMA < EMA[1] and MACDHist < MACDHist[1];

plot Bullish = GreenPrice;
plot Neutral = !GreenPrice and !RedPrice;
plot Bearish = RedPrice;

Bullish.SetDefaultColor(Color.UPTICK);
Bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bullish.SetLineWeight(3);
Bullish.hide();
Neutral.SetDefaultColor(Color.BLUE);
Neutral.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Neutral.SetLineWeight(3);
Neutral.hide();
Bearish.SetDefaultColor(Color.DOWNTICK);
Bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bearish.SetLineWeight(3);
Bearish.hide();

DefineGlobalColor("Bullish", Color.UPTICK);
DefineGlobalColor("Neutral", Color.BLUE);
DefineGlobalColor("Bearish", Color.DOWNTICK);
AssignPriceColor(if !paintBars then Color.CURRENT else if GreenPrice then globalColor("Bullish") else if RedPrice then globalColor("Bearish") else globalColor("Neutral"));


input charttype = ChartType.CANDLE;
def o = open;
def h = high;
def l = low;
def c = close;
def na = double.nan;

def o1 = if o<c then c else na;
def c1 = if o<c then o else na;
def h1 = h;
def l1 = l;

AddChart(growColor = color.green, fallColor = Color.RED, neutralColor = Color.GRAY, high = h1, low = l1, open = c1, close = o1, type = charttype);

def o2 = if o>c then o else double.nan;
def c2 = if o>c then c else double.nan;
def h2 = h;
def l2 = l;

AddChart(growColor = color.red, fallColor = Color.RED, neutralColor = Color.GRAY, high = h2, low = l2, open = c2, close = o2, type = charttype);

#
 
Last edited by a moderator:
Solution
Will anyone help me to edit this code to perform its current function (changing the colors on the borders and wicks of candles) based on the colors of the Smoothed Heiken Aishi instead of changing them based on the colors of the Impulse MACD.

Code:
Code:
#
# Charles Schwab & Co. (c) 2012-2025
#

input length = 13;
input paintBars = yes;

def EMA = ExpAverage(close, length);
def MACD = ExpAverage(close, 12) - ExpAverage(close, 26);
def MACDHist = MACD - ExpAverage(MACD, 9);
def GreenPrice = EMA > EMA[1] and MACDHist > MACDHist[1];
def RedPrice = EMA < EMA[1] and MACDHist < MACDHist[1];

plot Bullish = GreenPrice;
plot Neutral = !GreenPrice and !RedPrice;
plot Bearish = RedPrice;

Bullish.SetDefaultColor(Color.UPTICK)...
Will anyone help me to edit this code to perform its current function (changing the colors on the borders and wicks of candles) based on the colors of the Smoothed Heiken Aishi instead of changing them based on the colors of the Impulse MACD.

Code:
Code:
#
# Charles Schwab & Co. (c) 2012-2025
#

input length = 13;
input paintBars = yes;

def EMA = ExpAverage(close, length);
def MACD = ExpAverage(close, 12) - ExpAverage(close, 26);
def MACDHist = MACD - ExpAverage(MACD, 9);
def GreenPrice = EMA > EMA[1] and MACDHist > MACDHist[1];
def RedPrice = EMA < EMA[1] and MACDHist < MACDHist[1];

plot Bullish = GreenPrice;
plot Neutral = !GreenPrice and !RedPrice;
plot Bearish = RedPrice;

Bullish.SetDefaultColor(Color.UPTICK);
Bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bullish.SetLineWeight(3);
Bullish.hide();
Neutral.SetDefaultColor(Color.BLUE);
Neutral.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Neutral.SetLineWeight(3);
Neutral.hide();
Bearish.SetDefaultColor(Color.DOWNTICK);
Bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bearish.SetLineWeight(3);
Bearish.hide();

DefineGlobalColor("Bullish", Color.UPTICK);
DefineGlobalColor("Neutral", Color.BLUE);
DefineGlobalColor("Bearish", Color.DOWNTICK);
AssignPriceColor(if !paintBars then Color.CURRENT else if GreenPrice then globalColor("Bullish") else if RedPrice then globalColor("Bearish") else globalColor("Neutral"));


input charttype = ChartType.CANDLE;
def o = open;
def h = high;
def l = low;
def c = close;
def na = double.nan;

def o1 = if o<c then c else na;
def c1 = if o<c then o else na;
def h1 = h;
def l1 = l;

AddChart(growColor = color.green, fallColor = Color.RED, neutralColor = Color.GRAY, high = h1, low = l1, open = c1, close = o1, type = charttype);

def o2 = if o>c then o else double.nan;
def c2 = if o>c then c else double.nan;
def h2 = h;
def l2 = l;

AddChart(growColor = color.red, fallColor = Color.RED, neutralColor = Color.GRAY, high = h2, low = l2, open = c2, close = o2, type = charttype);

#
Enjoy!


Code:
# --- Smoothed Heiken Ashi (SHA) Candle Logic ---
# Logic: Colors dictated by SHA, Body/Fill dictated by actual Price
# COMPILER FIX: Hard-coded ChartType and Constant Colors

input shaLength = 10;
input paintBars = yes;

# 1. SHA CALCULATION (Recursive)
def avgOpen = ExpAverage(open, shaLength);
def avgHigh = ExpAverage(high, shaLength);
def avgLow = ExpAverage(low, shaLength);
def avgClose = ExpAverage(close, shaLength);

def haClose = (avgOpen + avgHigh + avgLow + avgClose) / 4;
def haOpen = CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (avgOpen + avgClose) / 2);

# 2. TREND DEFINITIONS
def isSHABull = haClose > haOpen;
def isSHABear = haClose < haOpen;
def isPriceGreen = close > open;
def isPriceRed = open > close;

# 3. GLOBAL COLORS
DefineGlobalColor("SHA_Up", Color.UPTICK);
DefineGlobalColor("SHA_Dn", Color.DOWNTICK);

# 4. BASE WICK COLORING
AssignPriceColor(if !paintBars then Color.CURRENT
                 else if isSHABull then GlobalColor("SHA_Up")
                 else if isSHABear then GlobalColor("SHA_Dn")
                 else Color.GRAY);

# 5. CUSTOM CHART OVERLAYS

# --- Case: Price Green / SHA Bullish ---
AddChart(growColor = GlobalColor("SHA_Up"), fallColor = GlobalColor("SHA_Up"),
         high = if isPriceGreen and isSHABull then high else Double.NaN,
         low = low, open = close, close = open, type = ChartType.CANDLE);

# --- Case: Price Green / SHA Bearish ---
AddChart(growColor = GlobalColor("SHA_Dn"), fallColor = GlobalColor("SHA_Dn"),
         high = if isPriceGreen and isSHABear then high else Double.NaN,
         low = low, open = close, close = open, type = ChartType.CANDLE);

# --- Case: Price Red / SHA Bullish ---
AddChart(growColor = GlobalColor("SHA_Up"), fallColor = GlobalColor("SHA_Up"),
         high = if isPriceRed and isSHABull then high else Double.NaN,
         low = low, open = open, close = close, type = ChartType.CANDLE);

# --- Case: Price Red / SHA Bearish ---
AddChart(growColor = GlobalColor("SHA_Dn"), fallColor = GlobalColor("SHA_Dn"),
         high = if isPriceRed and isSHABear then high else Double.NaN,
         low = low, open = open, close = close, type = ChartType.CANDLE);
 
Solution
Enjoy!


Code:
# --- Smoothed Heiken Ashi (SHA) Candle Logic ---
# Logic: Colors dictated by SHA, Body/Fill dictated by actual Price
# COMPILER FIX: Hard-coded ChartType and Constant Colors

input shaLength = 10;
input paintBars = yes;

# 1. SHA CALCULATION (Recursive)
def avgOpen = ExpAverage(open, shaLength);
def avgHigh = ExpAverage(high, shaLength);
def avgLow = ExpAverage(low, shaLength);
def avgClose = ExpAverage(close, shaLength);

def haClose = (avgOpen + avgHigh + avgLow + avgClose) / 4;
def haOpen = CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (avgOpen + avgClose) / 2);

# 2. TREND DEFINITIONS
def isSHABull = haClose > haOpen;
def isSHABear = haClose < haOpen;
def isPriceGreen = close > open;
def isPriceRed = open > close;

# 3. GLOBAL COLORS
DefineGlobalColor("SHA_Up", Color.UPTICK);
DefineGlobalColor("SHA_Dn", Color.DOWNTICK);

# 4. BASE WICK COLORING
AssignPriceColor(if !paintBars then Color.CURRENT
                 else if isSHABull then GlobalColor("SHA_Up")
                 else if isSHABear then GlobalColor("SHA_Dn")
                 else Color.GRAY);

# 5. CUSTOM CHART OVERLAYS

# --- Case: Price Green / SHA Bullish ---
AddChart(growColor = GlobalColor("SHA_Up"), fallColor = GlobalColor("SHA_Up"),
         high = if isPriceGreen and isSHABull then high else Double.NaN,
         low = low, open = close, close = open, type = ChartType.CANDLE);

# --- Case: Price Green / SHA Bearish ---
AddChart(growColor = GlobalColor("SHA_Dn"), fallColor = GlobalColor("SHA_Dn"),
         high = if isPriceGreen and isSHABear then high else Double.NaN,
         low = low, open = close, close = open, type = ChartType.CANDLE);

# --- Case: Price Red / SHA Bullish ---
AddChart(growColor = GlobalColor("SHA_Up"), fallColor = GlobalColor("SHA_Up"),
         high = if isPriceRed and isSHABull then high else Double.NaN,
         low = low, open = open, close = close, type = ChartType.CANDLE);

# --- Case: Price Red / SHA Bearish ---
AddChart(growColor = GlobalColor("SHA_Dn"), fallColor = GlobalColor("SHA_Dn"),
         high = if isPriceRed and isSHABear then high else Double.NaN,
         low = low, open = open, close = close, type = ChartType.CANDLE);
Thank you, greatly, however, I input the code and it is painting the entire candle the color of the Smoothed heiken aishi and is not coloring the inside of the candles the original colors of the candle's direction as the original code which I posted was able to do. I even tried changing the "yes" to "no" when it asked about painting the bars and it is still doing the same thing. Do you have any suggestions?
 
Thank you, greatly, however, I input the code and it is painting the entire candle the color of the Smoothed heiken aishi and is not coloring the inside of the candles the original colors of the candle's direction as the original code which I posted was able to do. I even tried changing the "yes" to "no" when it asked about painting the bars and it is still doing the same thing. Do you have any suggestions?
Enjoy #2!


Code:
# --- Smoothed Heiken Ashi (SHA) Candle Logic ---
# Logic: Colors dictated by SHA, Body/Fill dictated by actual Price
# COMPILER FIX: Hard-coded ChartType and Constant Colors

input shaLength = 10;
input paintBars = yes;

# 1. SHA CALCULATION (Recursive)
def avgOpen = ExpAverage(open, shaLength);
def avgHigh = ExpAverage(high, shaLength);
def avgLow = ExpAverage(low, shaLength);
def avgClose = ExpAverage(close, shaLength);

def haClose = (avgOpen + avgHigh + avgLow + avgClose) / 4;
def haOpen = CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (avgOpen + avgClose) / 2);

# 2. TREND DEFINITIONS
def isSHABull = haClose > haOpen;
def isSHABear = haClose < haOpen;
def isPriceGreen = close > open;
def isPriceRed = open > close;

# 3. GLOBAL COLORS
DefineGlobalColor("SHA_Up", Color.UPTICK);
DefineGlobalColor("SHA_Dn", Color.DOWNTICK);

# 4. BASE WICK COLORING
AssignPriceColor(if !paintBars then Color.CURRENT
                 else if isSHABull then GlobalColor("SHA_Up")
                 else if isSHABear then GlobalColor("SHA_Dn")
                 else Color.GRAY);

# 5. CUSTOM CHART OVERLAYS
input charttype = ChartType.CANDLE;

# --- Other Visuals ---
plot Bullish = isPriceGreen;
plot Neutral = !isPriceGreen and !isPriceRed;
plot Bearish = isPriceRed;

Bullish.SetDefaultColor(Color.UPTICK);
Bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bullish.SetLineWeight(3);
Bullish.hide();
Neutral.SetDefaultColor(Color.BLUE);
Neutral.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Neutral.SetLineWeight(3);
Neutral.hide();
Bearish.SetDefaultColor(Color.DOWNTICK);
Bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bearish.SetLineWeight(3);
Bearish.hide();

# --- Case: Open < Close ---

def o = open;
def h = high;
def l = low;
def c = close;
def na = double.nan;

def o1 = if o<c then c else na;
def c1 = if o<c then o else na;
def h1 = h;
def l1 = l;

AddChart(growColor = color.green, fallColor = Color.RED, neutralColor = Color.GRAY, high = h1, low = l1, open = c1, close = o1, type = charttype);

# --- Case: Open > Close ---

def o2 = if o>c then o else double.nan;
def c2 = if o>c then c else double.nan;
def h2 = h;
def l2 = l;

AddChart(growColor = color.red, fallColor = Color.RED, neutralColor = Color.GRAY, high = h2, low = l2, open = c2, close = o2, type = charttype);
 
Once again, I thank you for the work you've put in on my behalf...but there is only one issue.....this time the borders were painted, but it is the opposite of what I'm trying to achieve. The code makes the inside of the candles reflect the smooth heiken aishi colors and the borders and wicks to reflect the actual price up or price down color. If there is any way for you to reverse it so that the inside body of the candle reflects the price up and down colors and the outside borders and wicks reflect the smooth heiken aishi, I would be much appreciative.
 
Once again, I thank you for the work you've put in on my behalf...but there is only one issue.....this time the borders were painted, but it is the opposite of what I'm trying to achieve. The code makes the inside of the candles reflect the smooth heiken aishi colors and the borders and wicks to reflect the actual price up or price down color. If there is any way for you to reverse it so that the inside body of the candle reflects the price up and down colors and the outside borders and wicks reflect the smooth heiken aishi, I would be much appreciative.
Enjoy 3!

Flopped the growcolor and fallcolor in the addcharts.


Code:
# --- Smoothed Heiken Ashi (SHA) Candle Logic ---
# Logic: Colors dictated by SHA, Body/Fill dictated by actual Price
# COMPILER FIX: Hard-coded ChartType and Constant Colors

input shaLength = 10;
input paintBars = yes;

# 1. SHA CALCULATION (Recursive)
def avgOpen = ExpAverage(open, shaLength);
def avgHigh = ExpAverage(high, shaLength);
def avgLow = ExpAverage(low, shaLength);
def avgClose = ExpAverage(close, shaLength);

def haClose = (avgOpen + avgHigh + avgLow + avgClose) / 4;
def haOpen = CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (avgOpen + avgClose) / 2);

# 2. TREND DEFINITIONS
def isSHABull = haClose > haOpen;
def isSHABear = haClose < haOpen;
def isPriceGreen = close > open;
def isPriceRed = open > close;

# 3. GLOBAL COLORS
DefineGlobalColor("SHA_Up", Color.UPTICK);
DefineGlobalColor("SHA_Dn", Color.DOWNTICK);

# 4. BASE WICK COLORING
AssignPriceColor(if !paintBars then Color.CURRENT
                 else if isSHABull then GlobalColor("SHA_Up")
                 else if isSHABear then GlobalColor("SHA_Dn")
                 else Color.GRAY);

# 5. CUSTOM CHART OVERLAYS
input charttype = ChartType.CANDLE;

# --- Other Visuals ---
plot Bullish = isPriceGreen;
plot Neutral = !isPriceGreen and !isPriceRed;
plot Bearish = isPriceRed;

Bullish.SetDefaultColor(Color.UPTICK);
Bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bullish.SetLineWeight(3);
Bullish.hide();
Neutral.SetDefaultColor(Color.BLUE);
Neutral.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Neutral.SetLineWeight(3);
Neutral.hide();
Bearish.SetDefaultColor(Color.DOWNTICK);
Bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bearish.SetLineWeight(3);
Bearish.hide();

# --- Case: Open < Close ---

def o = open;
def h = high;
def l = low;
def c = close;
def na = double.nan;

def o1 = if o<c then c else na;
def c1 = if o<c then o else na;
def h1 = h;
def l1 = l;

AddChart(growColor = color.red, fallColor = Color.green, neutralColor = Color.GRAY, high = h1, low = l1, open = c1, close = o1, type = charttype);

# --- Case: Open > Close ---

def o2 = if o>c then o else double.nan;
def c2 = if o>c then c else double.nan;
def h2 = h;
def l2 = l;

AddChart(growColor = color.green, fallColor = Color.RED, neutralColor = Color.GRAY, high = h2, low = l2, open = c2, close = o2, type = charttype);
 
Enjoy 3!

Flopped the growcolor and fallcolor in the addcharts.


Code:
# --- Smoothed Heiken Ashi (SHA) Candle Logic ---
# Logic: Colors dictated by SHA, Body/Fill dictated by actual Price
# COMPILER FIX: Hard-coded ChartType and Constant Colors

input shaLength = 10;
input paintBars = yes;

# 1. SHA CALCULATION (Recursive)
def avgOpen = ExpAverage(open, shaLength);
def avgHigh = ExpAverage(high, shaLength);
def avgLow = ExpAverage(low, shaLength);
def avgClose = ExpAverage(close, shaLength);

def haClose = (avgOpen + avgHigh + avgLow + avgClose) / 4;
def haOpen = CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (avgOpen + avgClose) / 2);

# 2. TREND DEFINITIONS
def isSHABull = haClose > haOpen;
def isSHABear = haClose < haOpen;
def isPriceGreen = close > open;
def isPriceRed = open > close;

# 3. GLOBAL COLORS
DefineGlobalColor("SHA_Up", Color.UPTICK);
DefineGlobalColor("SHA_Dn", Color.DOWNTICK);

# 4. BASE WICK COLORING
AssignPriceColor(if !paintBars then Color.CURRENT
                 else if isSHABull then GlobalColor("SHA_Up")
                 else if isSHABear then GlobalColor("SHA_Dn")
                 else Color.GRAY);

# 5. CUSTOM CHART OVERLAYS
input charttype = ChartType.CANDLE;

# --- Other Visuals ---
plot Bullish = isPriceGreen;
plot Neutral = !isPriceGreen and !isPriceRed;
plot Bearish = isPriceRed;

Bullish.SetDefaultColor(Color.UPTICK);
Bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bullish.SetLineWeight(3);
Bullish.hide();
Neutral.SetDefaultColor(Color.BLUE);
Neutral.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Neutral.SetLineWeight(3);
Neutral.hide();
Bearish.SetDefaultColor(Color.DOWNTICK);
Bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bearish.SetLineWeight(3);
Bearish.hide();

# --- Case: Open < Close ---

def o = open;
def h = high;
def l = low;
def c = close;
def na = double.nan;

def o1 = if o<c then c else na;
def c1 = if o<c then o else na;
def h1 = h;
def l1 = l;

AddChart(growColor = color.red, fallColor = Color.green, neutralColor = Color.GRAY, high = h1, low = l1, open = c1, close = o1, type = charttype);

# --- Case: Open > Close ---

def o2 = if o>c then o else double.nan;
def c2 = if o>c then c else double.nan;
def h2 = h;
def l2 = l;

AddChart(growColor = color.green, fallColor = Color.RED, neutralColor = Color.GRAY, high = h2, low = l2, open = c2, close = o2, type = charttype);
Once again; I thank you. We are one step closer. The only problem now is that it is painting the borders red when the Smoothed Heiken aishi should be green (when the trend is up) and it is painting the borders green when it is in downtrend). As before, your help is appreciated.
 
Once again; I thank you. We are one step closer. The only problem now is that it is painting the borders red when the Smoothed Heiken aishi should be green (when the trend is up) and it is painting the borders green when it is in downtrend). As before, your help is appreciated.
Hey. Hope this is it. Confirmed that when SHA Bull the borders are green and SHA Bear the borders are red.


Code:
# --- Smoothed Heiken Ashi (SHA) Candle Logic ---
# Logic: Colors dictated by SHA, Body/Fill dictated by actual Price
# COMPILER FIX: Hard-coded ChartType and Constant Colors

input shaLength = 10;
input paintBars = yes;

# 1. SHA CALCULATION (Recursive)
def avgOpen = ExpAverage(open, shaLength);
def avgHigh = ExpAverage(high, shaLength);
def avgLow = ExpAverage(low, shaLength);
def avgClose = ExpAverage(close, shaLength);

def haClose = (avgOpen + avgHigh + avgLow + avgClose) / 4;
def haOpen = CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (avgOpen + avgClose) / 2);

# 2. TREND DEFINITIONS
def isSHABull = haClose > haOpen;
def isSHABear = haClose < haOpen;
def isPriceGreen = close > open;
def isPriceRed = open > close;

# 3. GLOBAL COLORS
DefineGlobalColor("SHA_Up", Color.UPTICK);
DefineGlobalColor("SHA_Dn", Color.DOWNTICK);

# 4. BASE WICK COLORING
AssignPriceColor(if !paintBars then Color.CURRENT
                 else if isSHABull then GlobalColor("SHA_Up")
                 else if isSHABear then GlobalColor("SHA_Dn")
                 else Color.GRAY);

# 5. CUSTOM CHART OVERLAYS
input charttype = ChartType.CANDLE;

# --- Other Visuals ---
plot Bullish = isPriceGreen;
plot Neutral = !isPriceGreen and !isPriceRed;
plot Bearish = isPriceRed;

Bullish.SetDefaultColor(Color.UPTICK);
Bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bullish.SetLineWeight(3);
Bullish.Hide();
Neutral.SetDefaultColor(Color.BLUE);
Neutral.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Neutral.SetLineWeight(3);
Neutral.Hide();
Bearish.SetDefaultColor(Color.DOWNTICK);
Bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bearish.SetLineWeight(3);
Bearish.Hide();

# --- Case: Open < Close ---

def o = open;
def h = high;
def l = low;
def c = close;
def na = Double.NaN;

def o1 = if o < c then c else na;
def c1 = if o < c then o else na;
def h1 = h;
def l1 = l;

AddChart(growColor = Color.green, fallColor = Color.red, neutralColor = Color.GRAY, high = h1, low = l1, open = c1, close = o1, type = charttype);

# --- Case: Open > Close ---

def o2 = if o > c then o else Double.NaN;
def c2 = if o > c then c else Double.NaN;
def h2 = h;
def l2 = l;

AddChart(growColor = color.red, fallColor = Color.green, neutralColor = Color.GRAY, high = h2, low = l2, open = c2, close = o2, type = charttype);
 
Hey. Hope this is it. Confirmed that when SHA Bull the borders are green and SHA Bear the borders are red.


Code:
# --- Smoothed Heiken Ashi (SHA) Candle Logic ---
# Logic: Colors dictated by SHA, Body/Fill dictated by actual Price
# COMPILER FIX: Hard-coded ChartType and Constant Colors

input shaLength = 10;
input paintBars = yes;

# 1. SHA CALCULATION (Recursive)
def avgOpen = ExpAverage(open, shaLength);
def avgHigh = ExpAverage(high, shaLength);
def avgLow = ExpAverage(low, shaLength);
def avgClose = ExpAverage(close, shaLength);

def haClose = (avgOpen + avgHigh + avgLow + avgClose) / 4;
def haOpen = CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (avgOpen + avgClose) / 2);

# 2. TREND DEFINITIONS
def isSHABull = haClose > haOpen;
def isSHABear = haClose < haOpen;
def isPriceGreen = close > open;
def isPriceRed = open > close;

# 3. GLOBAL COLORS
DefineGlobalColor("SHA_Up", Color.UPTICK);
DefineGlobalColor("SHA_Dn", Color.DOWNTICK);

# 4. BASE WICK COLORING
AssignPriceColor(if !paintBars then Color.CURRENT
                 else if isSHABull then GlobalColor("SHA_Up")
                 else if isSHABear then GlobalColor("SHA_Dn")
                 else Color.GRAY);

# 5. CUSTOM CHART OVERLAYS
input charttype = ChartType.CANDLE;

# --- Other Visuals ---
plot Bullish = isPriceGreen;
plot Neutral = !isPriceGreen and !isPriceRed;
plot Bearish = isPriceRed;

Bullish.SetDefaultColor(Color.UPTICK);
Bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bullish.SetLineWeight(3);
Bullish.Hide();
Neutral.SetDefaultColor(Color.BLUE);
Neutral.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Neutral.SetLineWeight(3);
Neutral.Hide();
Bearish.SetDefaultColor(Color.DOWNTICK);
Bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bearish.SetLineWeight(3);
Bearish.Hide();

# --- Case: Open < Close ---

def o = open;
def h = high;
def l = low;
def c = close;
def na = Double.NaN;

def o1 = if o < c then c else na;
def c1 = if o < c then o else na;
def h1 = h;
def l1 = l;

AddChart(growColor = Color.green, fallColor = Color.red, neutralColor = Color.GRAY, high = h1, low = l1, open = c1, close = o1, type = charttype);

# --- Case: Open > Close ---

def o2 = if o > c then o else Double.NaN;
def c2 = if o > c then c else Double.NaN;
def h2 = h;
def l2 = l;

AddChart(growColor = color.red, fallColor = Color.green, neutralColor = Color.GRAY, high = h2, low = l2, open = c2, close = o2, type = charttype);

The smoothed heiken aishi borders are fixed but now the candle bodies are not showing true price action colors and instead are all green

@hey

I'm taking a stab, is this it?


Cheers!


Code:
# ================================================
# Smoothed Heiken Ashi (SHA) Candle Overlay
# Logic : Color signal = SHA trend
#         Body/Wicks   = Actual OHLC price
# ================================================

input shaLength = 10;
input paintBars = yes;

# ─── SHA Calculation ─────────────────────────────
def avgOpen  = ExpAverage(open,  shaLength);
def avgHigh  = ExpAverage(high,  shaLength);
def avgLow   = ExpAverage(low,   shaLength);
def avgClose = ExpAverage(close, shaLength);

def haClose  = (avgOpen + avgHigh + avgLow + avgClose) / 4;
def haOpen   = CompoundValue(1,
                   (haOpen[1] + haClose[1]) / 2,
                   (avgOpen   + avgClose)   / 2);

# ─── Trend Signal ────────────────────────────────
def isSHABull = haClose > haOpen;
def isSHABear = haClose < haOpen;

# ─── Global Colors ───────────────────────────────
DefineGlobalColor("SHA_Up", Color.GREEN);
DefineGlobalColor("SHA_Dn", Color.RED);

# ─── Bar (Wick) Coloring ─────────────────────────
AssignPriceColor(
    if !paintBars  then Color.CURRENT
    else if isSHABull then GlobalColor("SHA_Up")
    else if isSHABear then GlobalColor("SHA_Dn")
    else Color.GRAY
);

# ─── Dual AddChart: Actual Price Candle Bodies ───
# TOS AddChart fills green when close > open (grow),
# red when close < open (fall).  We split into two
# calls so each case always gets the SHA color,
# regardless of whether price itself is up or down.

# Case 1: Price is bullish (close > open)
# Swap open/close args so TOS always sees "growing"
# → growColor drives the fill = SHA color
def bullBody_O = if close > open then open  else Double.NaN;
def bullBody_C = if close > open then close else Double.NaN;

AddChart(
    high         = high,
    low          = low,
    open         = bullBody_O,
    close        = bullBody_C,
    type         = ChartType.CANDLE,
    growColor    = GlobalColor("SHA_Up"),
    fallColor    = GlobalColor("SHA_Dn"),
    neutralColor = Color.GRAY
);

# Case 2: Price is bearish (open > close)
# Pass open/close normally → TOS sees "falling"
# → fallColor drives the fill = SHA color
def bearBody_O = if open > close then open  else Double.NaN;
def bearBody_C = if open > close then close else Double.NaN;

AddChart(
    high         = high,
    low          = low,
    open         = bearBody_O,
    close        = bearBody_C,
    type         = ChartType.CANDLE,
    growColor    = GlobalColor("SHA_Dn"),
    fallColor    = GlobalColor("SHA_Up"),
    neutralColor = Color.GRAY
);
 
Sorry; actually, the one you just sent has the candle bodies reflecting smoothed heiken aishi and the candle wicks and borders reflecting the normal uptick and downtick candle colors, but I'm desiring the exact opposite (the wicks and borders to relecting smooth heiken aishi and and bodies to reflect normal candle uptick and downtick). And upon further review, the one sent prior to this one had the wicks and borders reflecting actual candle uptick and downtick while the body was reflecting the Smooth Heiken aishi
 
Perhaps this is it?

Cheers!


# ================================================
# Smoothed Heiken Ashi (SHA) Candle Overlay
# Body = Normal price direction (green up / red down)
# Wicks/Border = SHA trend color
# ================================================

input shaLength = 10;
input paintBars = yes;

# ─── SHA Calculation ─────────────────────────────
def avgOpen = ExpAverage(open, shaLength);
def avgHigh = ExpAverage(high, shaLength);
def avgLow = ExpAverage(low, shaLength);
def avgClose = ExpAverage(close, shaLength);

def haClose = (avgOpen + avgHigh + avgLow + avgClose) / 4;
def haOpen = CompoundValue(1,
(haOpen[1] + haClose[1]) / 2,
(avgOpen + avgClose) / 2);

# ─── Trend Signal ────────────────────────────────
def isSHABull = haClose > haOpen;
def isSHABear = haClose <= haOpen;

def priceUp = close >= open;
def priceDown = close < open;

# ─── Global Colors ───────────────────────────────
DefineGlobalColor("SHA_Up", Color.GREEN);
DefineGlobalColor("SHA_Dn", Color.RED);

# ─── Wicks/Borders follow SHA via AssignPriceColor ─
AssignPriceColor(
if !paintBars then Color.CURRENT
else if isSHABull then GlobalColor("SHA_Up")
else GlobalColor("SHA_Dn")
);

# ─── 4 AddChart calls: no open/close swapping ────
# Each call isolates one scenario so TOS never has
# to guess — growColor always fires on a true up
# candle, fallColor always fires on a true down candle.

# 1) Price UP + SHA Bull → green body, green wicks
def o_UU = if priceUp and isSHABull then open else Double.NaN;
def c_UU = if priceUp and isSHABull then close else Double.NaN;
def h_UU = if priceUp and isSHABull then high else Double.NaN;
def l_UU = if priceUp and isSHABull then low else Double.NaN;

AddChart(high = h_UU, low = l_UU, open = o_UU, close = c_UU,
type = ChartType.CANDLE,
growColor = GlobalColor("SHA_Up"),
fallColor = GlobalColor("SHA_Up"),
neutralColor = GlobalColor("SHA_Up"));

# 2) Price UP + SHA Bear → green body, red wicks
def o_UD = if priceUp and isSHABear then open else Double.NaN;
def c_UD = if priceUp and isSHABear then close else Double.NaN;
def h_UD = if priceUp and isSHABear then high else Double.NaN;
def l_UD = if priceUp and isSHABear then low else Double.NaN;

AddChart(high = h_UD, low = l_UD, open = o_UD, close = c_UD,
type = ChartType.CANDLE,
growColor = GlobalColor("SHA_Dn"),
fallColor = GlobalColor("SHA_Dn"),
neutralColor = GlobalColor("SHA_Dn"));

# 3) Price DOWN + SHA Bull → red body, green wicks
def o_DU = if priceDown and isSHABull then open else Double.NaN;
def c_DU = if priceDown and isSHABull then close else Double.NaN;
def h_DU = if priceDown and isSHABull then high else Double.NaN;
def l_DU = if priceDown and isSHABull then low else Double.NaN;

AddChart(high = h_DU, low = l_DU, open = o_DU, close = c_DU,
type = ChartType.CANDLE,
growColor = GlobalColor("SHA_Up"),
fallColor = GlobalColor("SHA_Up"),
neutralColor = GlobalColor("SHA_Up"));

# 4) Price DOWN + SHA Bear → red body, red wicks
def o_DD = if priceDown and isSHABear then open else Double.NaN;
def c_DD = if priceDown and isSHABear then close else Double.NaN;
def h_DD = if priceDown and isSHABear then high else Double.NaN;
def l_DD = if priceDown and isSHABear then low else Double.NaN;

AddChart(high = h_DD, low = l_DD, open = o_DD, close = c_DD,
type = ChartType.CANDLE,
growColor = GlobalColor("SHA_Dn"),
fallColor = GlobalColor("SHA_Dn"),
neutralColor = GlobalColor("SHA_Dn"));
 
No, now wicks, borders and bodies reflect smoothed heiken aishi. The original code which I posted was able to do it, but based on the impulse macd as opposed to the smoothed heiken aishi. Perhaps a meer tweaking of that code would produce the desired result?

Code:
#
# Charles Schwab & Co. (c) 2012-2025
#

input length = 13;
input paintBars = yes;

def EMA = ExpAverage(close, length);
def MACD = ExpAverage(close, 12) - ExpAverage(close, 26);
def MACDHist = MACD - ExpAverage(MACD, 9);
def GreenPrice = EMA > EMA[1] and MACDHist > MACDHist[1];
def RedPrice = EMA < EMA[1] and MACDHist < MACDHist[1];

plot Bullish = GreenPrice;
plot Neutral = !GreenPrice and !RedPrice;
plot Bearish = RedPrice;

Bullish.SetDefaultColor(Color.UPTICK);
Bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bullish.SetLineWeight(3);
Bullish.hide();
Neutral.SetDefaultColor(Color.BLUE);
Neutral.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Neutral.SetLineWeight(3);
Neutral.hide();
Bearish.SetDefaultColor(Color.DOWNTICK);
Bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bearish.SetLineWeight(3);
Bearish.hide();

DefineGlobalColor("Bullish", Color.UPTICK);
DefineGlobalColor("Neutral", Color.BLUE);
DefineGlobalColor("Bearish", Color.DOWNTICK);
AssignPriceColor(if !paintBars then Color.CURRENT else if GreenPrice then globalColor("Bullish") else if RedPrice then globalColor("Bearish") else globalColor("Neutral"));


input charttype = ChartType.CANDLE;
def o = open;
def h = high;
def l = low;
def c = close;
def na = double.nan;

def o1 = if o<c then c else na;
def c1 = if o<c then o else na;
def h1 = h;
def l1 = l;

AddChart(growColor = color.green, fallColor = Color.RED, neutralColor = Color.GRAY, high = h1, low = l1, open = c1, close = o1, type = charttype);

def o2 = if o>c then o else double.nan;
def c2 = if o>c then c else double.nan;
def h2 = h;
def l2 = l;

AddChart(growColor = color.red, fallColor = Color.RED, neutralColor = Color.GRAY, high = h2, low = l2, open = c2, close = o2, type = charttype);
 
Last edited by a moderator:
Ok, hmm try this...

Code:
#
# Charles Schwab & Co. (c) 2012-2025
#

input length = 13;
input paintBars = yes;

# --- Smoothed Heiken Ashi ---
input haLength = 10;

def haClose = (open + high + low + close) / 4;
def haOpen = CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (open + close) / 2);

# Smoothing
def shaClose = ExpAverage(haClose, haLength);
def shaOpen = ExpAverage(haOpen, haLength);

# Direction based on SHA
def GreenPrice = shaClose > shaOpen;
def RedPrice = shaClose < shaOpen;

# --- Plots (unchanged structure) ---
plot Bullish = GreenPrice;
plot Neutral = !GreenPrice and !RedPrice;
plot Bearish = RedPrice;

Bullish.SetDefaultColor(Color.UPTICK);
Bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bullish.SetLineWeight(3);
Bullish.hide();

Neutral.SetDefaultColor(Color.BLUE);
Neutral.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Neutral.SetLineWeight(3);
Neutral.hide();

Bearish.SetDefaultColor(Color.DOWNTICK);
Bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Bearish.SetLineWeight(3);
Bearish.hide();

# --- Global Colors ---
DefineGlobalColor("Bullish", Color.UPTICK);
DefineGlobalColor("Neutral", Color.BLUE);
DefineGlobalColor("Bearish", Color.DOWNTICK);

AssignPriceColor(
    if !paintBars then Color.CURRENT
    else if GreenPrice then GlobalColor("Bullish")
    else if RedPrice then GlobalColor("Bearish")
    else GlobalColor("Neutral")
);

# --- Your Custom Candle Borders/Wicks ---
input charttype = ChartType.CANDLE;

def o = open;
def h = high;
def l = low;
def c = close;
def na = double.nan;

# Up candles
def o1 = if o < c then c else na;
def c1 = if o < c then o else na;

AddChart(
    growColor = Color.GREEN,
    fallColor = Color.RED,
    neutralColor = Color.GRAY,
    high = h,
    low = l,
    open = c1,
    close = o1,
    type = charttype
);

# Down candles
def o2 = if o > c then o else na;
def c2 = if o > c then c else na;

AddChart(
    growColor = Color.RED,
    fallColor = Color.RED,
    neutralColor = Color.GRAY,
    high = h,
    low = l,
    open = o2,
    close = c2,
    type = charttype
);
 

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