Heiken Ashi Ribbons For ThinkOrSwim

Makavali

New member
Hello usethinkscript fam!

Can someone please help me replicate this indicator from tradingview. I know we have a HA moving average post here in usethinkscript but this one is just a bit different with the ribbons. I am not even sure what values its calculating.

https://www.tradingview.com/script/Bdr9NMqN/

To keep it simple I am only looking for the first ribbon, and hopefully I'll be able to scan for trend or color change of the ribbon.. I think the first ribbon is a 20MA

lkhTRuZ.jpg


Sorry if its something super easy but I am totally trash with pinescript and thinkscript.

Appreciate any help with this and thanks in advance!

Code:
// LEGEND :

//    - GREEN AREA / GREEN CANDLE : moving average in a uptrend

//    - RED   AREA / RED   CANDLE : moving average in a downtrend

//    - GREEN CROSS               : short term buy signal

//    - RED   CROSS               : short term sell signal

//    - GREEN CIRCLE              : long term buy signal

//    - RED   CIRCLE              : long term sell signal

//    - GREEN TRIANGLE            : ribbon direction changes to trending up

//    - RED   TRIANGLE            : ribbon direction changes to trending down

//


// Gunzo Heiken Ashi Ribbons v1.0

study("{Gunzo} Heiken Ashi Ribbons", overlay=true)



// #########################################################################################################

// VARIABLES AND CONSTANTS

// #########################################################################################################


// global input variables

int    ma_period1             = input(title="1st Moving average length",            type=input.integer, defval=20)

int    ma_period2             = input(title="2nd Moving average length",            type=input.integer, defval=50)

int    ma_period3             = input(title="3rd Moving average length",            type=input.integer, defval=100)

string ma_method              = input(title="Moving average method",                type=input.string,  defval='EMA',           options=['EMA', 'SMA', 'WMA'])

string ribbon_type            = input(title="Ribbon type",                          type=input.string,  defval='ma_open_close', options=['ma_open_close', 'ma_high_low'])

bool   display_ribbon_candles = input(title="Display ribbon as candles",            type=input.bool,    defval=false)

bool   shortterm_signals      = input(title="Display short term buy/sell signals",  type=input.bool,    defval=false)

bool   longterm_signals       = input(title="Display long term buy/sell signals",   type=input.bool,    defval=false)

bool   trendup_signals        = input(title="Display ribbon trending up signals",   type=input.bool,    defval=false)

bool   trenddown_signals      = input(title="Display ribbon trending down signals", type=input.bool,    defval=false)


// color constants

color_up   = #26A69A

color_down = #EF5350


// transparent constants

delta_gradient = 20

ribbon_transp0 = 80

ribbon_transp1 = ribbon_transp0 + (0  * delta_gradient)

ribbon_transp2 = ribbon_transp0 + (-1 * delta_gradient)

ribbon_transp3 = ribbon_transp0 + (-2 * delta_gradient)



// #########################################################################################################

// FUNCTION DEFINTION

// #########################################################################################################


fn_calculate_heiken_ashi_ma(ma_period, ma_method, ribbon_type) =>

    // init variables

    ha_open  = 0.0

    ha_close = 0.0

    ha_low   = 0.0

    ha_high  = 0.0


    // moving average of the prices

    ma_open  = ma_method == "EMA" ? ema(open,  ma_period) : ma_method == "SMA" ? sma(open,  ma_period) : ma_method == "WMA" ? wma(open,  ma_period) : na

    ma_close = ma_method == "EMA" ? ema(close, ma_period) : ma_method == "SMA" ? sma(close, ma_period) : ma_method == "WMA" ? wma(close, ma_period) : na

    ma_low   = ma_method == "EMA" ? ema(low,   ma_period) : ma_method == "SMA" ? sma(low,   ma_period) : ma_method == "WMA" ? wma(low,   ma_period) : na

    ma_high  = ma_method == "EMA" ? ema(high,  ma_period) : ma_method == "SMA" ? sma(high,  ma_period) : ma_method == "WMA" ? wma(high,  ma_period) : na

  

    // use heiken ashi algorithm of the moving averages

    ha_open  := (nz(ha_open[1]) + nz(ha_close[1])) / 2

    ha_close := (ma_open + ma_close + ma_low + ma_high) / 4

    ha_high  := max(ma_high, ma_open, ma_close)

    ha_low   := min(ma_low,  ma_open, ma_close)

  

    // calculate result variables

    ribbon_down      = ribbon_type == "ma_high_low" ? min(ha_low, ha_high) : ha_open

    ribbon_top       = ribbon_type == "ma_high_low" ? max(ha_low, ha_high) : ha_close

    ribbon_direction = ha_close > ha_open ? 1 : -1


    [ribbon_down, ribbon_top, ribbon_direction]

  

[ribbon_down1, ribbon_top1, ribbon_direction1] = fn_calculate_heiken_ashi_ma(ma_period1, ma_method, ribbon_type)

[ribbon_down2, ribbon_top2, ribbon_direction2] = fn_calculate_heiken_ashi_ma(ma_period2, ma_method, ribbon_type)

[ribbon_down3, ribbon_top3, ribbon_direction3] = fn_calculate_heiken_ashi_ma(ma_period3, ma_method, ribbon_type)



// #########################################################################################################

// COLOR CALCULATIONS

// #########################################################################################################


ribbon_main_color1 = ribbon_direction1 == 1 ? color.new(color_up, ribbon_transp1) : color.new(color_down, ribbon_transp1)

ribbon_main_color2 = ribbon_direction2 == 1 ? color.new(color_up, ribbon_transp2) : color.new(color_down, ribbon_transp2)

ribbon_main_color3 = ribbon_direction3 == 1 ? color.new(color_up, ribbon_transp3) : color.new(color_down, ribbon_transp3)


ribbon_border_color1 = ribbon_direction1 == 1 ? color.new(color_up, 0) : color.new(color_down, 0)

ribbon_border_color2 = ribbon_direction2 == 1 ? color.new(color_up, 0) : color.new(color_down, 0)

ribbon_border_color3 = ribbon_direction3 == 1 ? color.new(color_up, 0) : color.new(color_down, 0)



// #########################################################################################################

// CROSSOVER AND TREND CHANGE CALCULATIONS

// #########################################################################################################


bullish_short_term = crossover(ribbon_top1, ribbon_down2)

bullish_long_term  = crossover(ribbon_top1, ribbon_down3)


bearish_short_term = crossunder(ribbon_down1, ribbon_top2)

bearish_long_term  = crossunder(ribbon_down1, ribbon_top3)


ribbon_1_trend_up   = ribbon_direction1 == 1  and ribbon_direction1[1] == -1

ribbon_2_trend_up   = ribbon_direction2 == 1  and ribbon_direction2[1] == -1

ribbon_3_trend_up   = ribbon_direction3 == 1  and ribbon_direction3[1] == -1


ribbon_1_trend_down = ribbon_direction1 == -1 and ribbon_direction1[1] == 1

ribbon_2_trend_down = ribbon_direction2 == -1 and ribbon_direction2[1] == 1

ribbon_3_trend_down = ribbon_direction3 == -1 and ribbon_direction3[1] == 1



// #########################################################################################################

// PLOTTING ON CHART

// #########################################################################################################


// if plotting as candles

plotcandle(display_ribbon_candles ? ribbon_down1 : na, display_ribbon_candles ? ribbon_top1 : na, display_ribbon_candles ? ribbon_down1 : na, display_ribbon_candles ? ribbon_top1 : na, title='HA ribbon 1', color=ribbon_main_color1, bordercolor=na)

plotcandle(display_ribbon_candles ? ribbon_down2 : na, display_ribbon_candles ? ribbon_top2 : na, display_ribbon_candles ? ribbon_down2 : na, display_ribbon_candles ? ribbon_top2 : na, title='HA ribbon 2', color=ribbon_main_color2, bordercolor=na)

plotcandle(display_ribbon_candles ? ribbon_down3 : na, display_ribbon_candles ? ribbon_top3 : na, display_ribbon_candles ? ribbon_down3 : na, display_ribbon_candles ? ribbon_top3 : na, title='HA ribbon 3', color=ribbon_main_color3, bordercolor=na)


// if plotting as area - ma1

ribbon_down_line1 = plot(not display_ribbon_candles ? ribbon_down1 : na, title="HA bottom ribbon 1", color=ribbon_border_color1, style=plot.style_line, linewidth=1)

ribbon_top_line1  = plot(not display_ribbon_candles ? ribbon_top1  : na, title="HA top ribbon 1",    color=ribbon_border_color1, style=plot.style_line, linewidth=1)

fill(ribbon_down_line1, ribbon_top_line1, title="HA area ribbon 1", color=ribbon_main_color1)


// if plotting as area - ma2

ribbon_down_line2 = plot(not display_ribbon_candles ? ribbon_down2 : na, title="HA bottom ribbon 2", color=ribbon_border_color2, style=plot.style_line, linewidth=1)

ribbon_top_line2  = plot(not display_ribbon_candles ? ribbon_top2  : na, title="HA top ribbon 2",    color=ribbon_border_color2, style=plot.style_line, linewidth=1)

fill(ribbon_down_line2, ribbon_top_line2, title="HA area ribbon 2", color=ribbon_main_color2)


// if plotting as area - ma3

ribbon_down_line3 = plot(not display_ribbon_candles ? ribbon_down3 : na, title="HA bottom ribbon 3", color=ribbon_border_color3, style=plot.style_line, linewidth=1)

ribbon_top_line3  = plot(not display_ribbon_candles ? ribbon_top3  : na, title="HA top ribbon 3",    color=ribbon_border_color3, style=plot.style_line, linewidth=1)

fill(ribbon_down_line3, ribbon_top_line3, title="HA area ribbon 3", color=ribbon_main_color3)



// #########################################################################################################

// SIGNALS

// #########################################################################################################


plotshape(shortterm_signals   and bullish_short_term, title="Short term buy signal",  style=shape.xcross,       color=color.green, size=size.tiny, location=location.bottom)

plotshape(shortterm_signals   and bearish_short_term, title="Short term sell signal", style=shape.xcross,       color=color.red,   size=size.tiny, location=location.bottom)

plotshape(longterm_signals    and bullish_long_term,  title="Long term buy signal",   style=shape.circle,       color=color.green, size=size.tiny, location=location.bottom)

plotshape(longterm_signals    and bearish_long_term,  title="Long term sell signal",  style=shape.circle,       color=color.red,   size=size.tiny, location=location.bottom)


plotshape(trendup_signals    and ribbon_1_trend_up,   title="Ribbon 1 trend up",      style=shape.triangleup,   color=color.green, size=size.tiny, location=location.bottom)

plotshape(trendup_signals    and ribbon_2_trend_up,   title="Ribbon 2 trend up",      style=shape.triangleup,   color=color.green, size=size.tiny, location=location.bottom)

plotshape(trendup_signals    and ribbon_3_trend_up,   title="Ribbon 3 trend up",      style=shape.triangleup,   color=color.green, size=size.tiny, location=location.bottom)

plotshape(trenddown_signals  and ribbon_1_trend_down, title="Ribbon 1 trend down",    style=shape.triangledown, color=color.red,   size=size.tiny, location=location.bottom)

plotshape(trenddown_signals  and ribbon_2_trend_down, title="Ribbon 2 trend down",    style=shape.triangledown, color=color.red,   size=size.tiny, location=location.bottom)

plotshape(trenddown_signals  and ribbon_3_trend_down, title="Ribbon 3 trend down",    style=shape.triangledown, color=color.red,   size=size.tiny, location=location.bottom)



// #########################################################################################################

// ALERTS

// #########################################################################################################


alertcondition(bullish_short_term,  title="Short term buy signal",  message='1st ribbon crosses above 2nd ribbon.')

alertcondition(bearish_short_term,  title="Short term sell signal", message='1st ribbon crosses under 2rd ribbon.')

alertcondition(bullish_long_term,   title="Long term buy signal",   message='1st ribbon crosses above 3nd ribbon.')

alertcondition(bearish_long_term,   title="Long term sell signal",  message='1st ribbon crosses under 3nd ribbon.')


alertcondition(ribbon_1_trend_up,   title="Ribbon 1 trend up",      message='1st ribbon is now trendin up.')

alertcondition(ribbon_2_trend_up,   title="Ribbon 2 trend up",      message='2nd ribbon is now trendin up.')

alertcondition(ribbon_3_trend_up,   title="Ribbon 3 trend up",      message='3nd ribbon is now trendin up.')

alertcondition(ribbon_1_trend_down, title="Ribbon 1 trend down",    message='1st ribbon is now trendin down.')

alertcondition(ribbon_2_trend_down, title="Ribbon 2 trend down",    message='2nd ribbon is now trendin down.')

alertcondition(ribbon_3_trend_down, title="Ribbon 3 trend down",    message='3nd ribbon is now trendin down.')
 
Last edited:
Here is some script that will draw the lines for you. Just make them all 20 in the Settings if you want just one line. You can also delete the "#" in from of the Assign command and it will cold code you price bars when all 3 are true in each trend direction.

# START
input length1 = 20;
input length2 = 50;
input length3 = 100;
def HAopen;
def HAhigh;
def HAlow;
def HAclose;
HAopen = CompoundValue(1, (HAopen[1] + HAclose[1]) / 2, (open[1] + close) / 2);
HAhigh = Max(high, close[1]);
HAlow = Min(low, close[1]);
HAclose = (HAopen + HAclose[1] + HAlow + close) / 4;

plot HAMA1 = Average(HAclose, length1);
plot HAMA2 = Average(HAclose, length2);
plot HAMA3 = Average(HAclose, length3);

# Delete the "#" below to color code your price bars.
# AssignPriceColor(if HAMA1 > HAMA1[1] and HAMA2 > HAMA2[1] and HAMA3 > HAMA3[1] then Color.GREEN else if HAMA1 < HAMA1[1] and HAMA2 < HAMA2[1] and HAMA3 < HAMA3[1] then color.RED else Color.gray);

HAMA1.AssignValueColor(if HAMA1 > HAMA1[1] then Color.GREEN else if HAMA1 < HAMA1[1] then Color.RED else Color.GRAY);
HAMA2.AssignValueColor(if HAMA2 > HAMA2[1] then Color.GREEN else if HAMA2 < HAMA2[1] then Color.RED else Color.GRAY);
HAMA3.AssignValueColor(if HAMA3 > HAMA3[1] then Color.GREEN else if HAMA3 < HAMA3[1] then Color.RED else Color.GRAY);

# END
 
Pretty close! thanks @Investingtogive but not quite what I was looking for. I did comment out hama2 and hama3 and just kept the 20hama and results did not match the trading view indicator and I couldnt get it to scan either.

I don't know that the second part of that code is that makes up the ribbon with the 20HA moving average on the tradingview indicator but I have been using it a lot and love it. It would be even better in ToS

I know clouds/ribbons are possiable in ToS since I also use Rippster's EMA clouds in ToS.

appreciate if you could further assist.
 
I actually wrote a Heikin Ashi Ribbon from scratch but wasn't impressed... I played with it for a bit last night again and even experimented with different price combinations but it still didn't do it for me... The ribbons are so thin that one might as well use a regular moving average... I have attached the code I have played with...

Ruby:
# HA_MA_Ribbon

input length = 20;

def ha_close = (open + high + low + close) / 4;
def ha_open = (open[1] + close[1]) / 2;
def ha_high = max(open, max(close, high));
def ha_low = min(open, min(close, high));

plot openLine = ExpAverage(ha_open, length);

plot closeLine = ExpAverage(ha_close, length);

openLine.AssignValueColor(if openLine < closeLine then Color.GREEN else Color.RED);
closeLine.AssignValueColor(if openLine < closeLine then Color.GREEN else Color.RED);

AddCloud(openLine, closeLine, Color.RED, Color.GREEN, yes);
 
Thanks! @Investingtogive I just use it visualize direction/alerts, entry/exits are pure judgement but this indicator works well for me on top movers.

@rad14733 this is it! I know what you mean about it being super thin, you can use a secondary HAMA like from my OP code. I personally like the single ribbon with some EMA clouds.

I know its a simple code and probably took you just a few mins to create but it means a lot to me and I really appreciate y'all helping me out here!!

Is there a way I can scan for stocks that have changed the ribbon from red to green within a given amount of bars? (I tried but got mixed results)
 
Last edited:
Thanks! @Investingtogive I just use it visualize direction/alerts, entry/exits are pure judgement but this indicator works well for me on top movers.

@rad14733 this is it! I know what you mean about it being super thin, you can use a secondary HAMA like from my OP code. I personally like the single ribbon with some EMA clouds.

I know its a simple code and probably took you just a few mins to create but it means a lot to me and I really appreciate y'all helping me out here!!

Is there a way I can scan for stocks that have changed the ribbon from red to green within a given amount of bars? (I tried but got mixed results)
I actually wrote a Heikin Ashi Ribbon from scratch but wasn't impressed... I played with it for a bit last night again and even experimented with different price combinations but it still didn't do it for me... The ribbons are so thin that one might as well use a regular moving average... I have attached the code I have played with...

Ruby:
# HA_MA_Ribbon

input length = 20;

def ha_close = (open + high + low + close) / 4;
def ha_open = (open[1] + close[1]) / 2;
def ha_high = max(open, max(close, high));
def ha_low = min(open, min(close, high));

plot openLine = ExpAverage(ha_open, length);

plot closeLine = ExpAverage(ha_close, length);

openLine.AssignValueColor(if openLine < closeLine then Color.GREEN else Color.RED);
closeLine.AssignValueColor(if openLine < closeLine then Color.GREEN else Color.RED);

AddCloud(openLine, closeLine, Color.RED, Color.GREEN, yes);
should < sign be the same for the following or one should be >?
openLine.AssignValueColor(if openLine < closeLine then Color.GREEN else Color.RED);
closeLine.AssignValueColor(if openLine < closeLine then Color.GREEN else Color.RED);
 
Last edited:
@majidg If you try that you will see that the lines won't paint properly... Try all combinations... The idea is to have the lines be the same color as the clouds... It might look wrong due to the goofy-footed logic I used due to Copy & Paste rather than typing both lines in logical fashion... Nonetheless, the logic is correct...
 
@majidg If you try that you will see that the lines won't paint properly... Try all combinations... The idea is to have the lines be the same color as the clouds... It might look wrong due to the goofy-footed logic I used due to Copy & Paste rather than typing both lines in logical fashion... Nonetheless, the logic is correct...
yes, thanks. I left it as is and both lines are the same color
 
Help matching Bjorgum Triple MEA Strat from TradingView. Here's an attempt to convert TV indicator for Bjorgum Triple EMA Strat https://www.tradingview.com/script/pr3d5mum-Bjorgum-Triple-EMA-Strat/ based on code from post #2 above . It is close to Tradingview but doesn't match exactly. Thank you @tomsk and @Investingtogive for original script and Bjorgum for open source TV Code. Can someone see what part of code is inccorect such that it doesn't match TradingView? I like the indicator even if it doesn't match Bjorgum's exactly. I also tweaked slightly to allow painting bars to be turned on or off and to be able to change cloud color and candle colors under "Globals".

#Code below

# Credit to tomsk and Investingtogive for original coding and BJorgum for open source Trading View Indicator


# START
input length1 = 5;
input length2 = 9;
input length3 = 21;
def HAopen;
def HAhigh;
def HAlow;
def HAclose;
HAclose = (open + high + low + close) / 4;
#HAopen = CompoundValue(1, (HAopen[1] + HAclose[1]) / 2, (open[1] + close) / 2);
HAopen = CompoundValue(1, (open + close)/2, (HAopen[1] + HAclose[1]) / 2);
HAhigh = Max(high, max (HAopen, HAclose));
HAlow = Min(low, min (HAopen, HAclose));

#hahl2 = average(HAhigh, HAlow);


plot HAMA1 = Average(HAOpen, length1);
plot HAMA2 = Average(HAOpen, length2);
plot HAMA3 = Average(HAOpen, length3);

# Delete the "#" below to color code your price bars.

DefineGlobalColor( "Bull Candle", color.green);
DefineGlobalColor( "Bear Candle", color.red);

input paintbars = yes;
AssignPriceColor(if paintbars and close > HAMA1 and close > HAMA2 then GlobalColor ("Bull Candle") else if paintbars and close < HAMA1 and close < HAMA2 then GlobalColor ("Bear Candle") else color.yellow);
# AssignPriceColor(if paintbars and HAMA1 > HAMA1[1] and HAMA2 > HAMA2[1] and HAMA3 > HAMA3[1] then Color.GREEN else if paintbars and HAMA1 < HAMA1[1] and HAMA2 < HAMA2[1] and HAMA3 < HAMA3[1] then color.RED else Color.light_gray);

DefineGlobalColor( "Bull Lines", color.green);
DefineGlobalColor( "Bear Lines", color.red);

HAMA1.AssignValueColor(if HAMA1 > HAMA1[1] then GlobalColor ("Bull Lines") else if HAMA1 < HAMA1[1] then GlobalColor ("Bear Lines") else Color.GRAY);
HAMA2.AssignValueColor(if HAMA2 > HAMA2[1] then GlobalColor ("Bull Lines") else if HAMA2 < HAMA2[1] then GlobalColor ("Bear Lines") else Color.GRAY);
HAMA3.AssignValueColor(if HAMA3 > HAMA3[1] then GlobalColor ("Bull Lines") else if HAMA3 < HAMA3[1] then GlobalColor ("Bear Lines") else Color.GRAY);

# END

# --------------------------------------------------- Add Cloud -----------------------------------------------
input ShowfastCloud = yes;


DefineGlobalColor( "Bullish Fast Cloud", CreateColor(201, 255, 234));
DefineGlobalColor( "Bearish Fast Cloud", CreateColor(255, 105, 105));



AddCloud(if ShowfastCloud then HAMA1 else Double.NaN, HAMA2, GlobalColor("Bullish Fast Cloud"), GlobalColor("Bearish Fast Cloud"));



input ShowSlowCloud = yes;

DefineGlobalColor( "Bullish Slow Cloud", CreateColor(201, 255, 234));
DefineGlobalColor( "Bearish Slow Cloud", CreateColor(255, 105, 105));


AddCloud(if ShowSlowCloud then HAMA2 else Double.NaN, HAMA3, GlobalColor("Bullish Slow Cloud"), GlobalColor("Bearish Slow Cloud"));
 
Last edited:

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