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

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