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
Sorry if its something super easy but I am totally trash with pinescript and thinkscript.
Appreciate any help with this and thanks in advance!
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
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: