You are on page 1of 2

//@version=4

study("Supertrend MTF Heatmap", shorttitle="Supertrend MTF Heatmap", overlay=false,


precision=6)

Factor=input(3,title="[SUPERTREND] Factor", minval=1,maxval = 100,


type=input.float)
Pd=input(7, title="[SUPERTREND] PD", minval=1,maxval = 100)

res1 = input("60", type=input.resolution, title="First Timeframe")


res2 = input("120", type=input.resolution, title="Second Timeframe")
res3 = input("240", type=input.resolution, title="Third Timeframe")
res4 = input("D", type=input.resolution, title="Fourth Timeframe")
res5 = input("W", type=input.resolution, title="Fifth Timeframe")

Supertrend(Factor, Pd) =>


Up=hl2-(Factor*atr(Pd))
Dn=hl2+(Factor*atr(Pd))

TrendUp = 0.0
TrendUp := close[1]>TrendUp[1] ? max(Up,TrendUp[1]) : Up
TrendDown = 0.0
TrendDown := close[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn
Trend = 0.0
Trend := close > TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1],1)
Tsl = Trend==1? TrendUp: TrendDown

S_Buy = Trend == 1 ? 1 : 0
S_Sell = Trend != 1 ? 1 : 0

[Trend, Tsl]

[Trend,Tsl] = Supertrend(Factor, Pd)

// Security
ST1_Trend_MTF = security(syminfo.tickerid, res1, Trend[1],
lookahead=barmerge.lookahead_on)
ST2_Trend_MTF = security(syminfo.tickerid, res2, Trend[1],
lookahead=barmerge.lookahead_on)
ST3_Trend_MTF = security(syminfo.tickerid, res3, Trend[1],
lookahead=barmerge.lookahead_on)
ST4_Trend_MTF = security(syminfo.tickerid, res4, Trend[1],
lookahead=barmerge.lookahead_on)
ST5_Trend_MTF = security(syminfo.tickerid, res5, Trend[1],
lookahead=barmerge.lookahead_on)

h0 = hline(1, color=color.new(color.black, 10), linewidth=2,


linestyle=hline.style_solid)
h1 = hline(2, color=color.new(color.black, 10), linewidth=2,
linestyle=hline.style_solid)
h2 = hline(3, color=color.new(color.black, 10), linewidth=2,
linestyle=hline.style_solid)
h3 = hline(4, color=color.new(color.black, 10), linewidth=2,
linestyle=hline.style_solid)
h4 = hline(5, color=color.new(color.black, 10), linewidth=2,
linestyle=hline.style_solid)
h5 = hline(6, color=color.new(color.black, 10), linewidth=2,
linestyle=hline.style_solid)

get_bars(Trend)=>
since_st_buy = barssince(Trend == 1)
since_st_sell = barssince(Trend == -1)
[since_st_buy, since_st_sell]

[since_st_1_buy, since_st_1_sell] = get_bars(ST1_Trend_MTF)


[since_st_2_buy, since_st_2_sell] = get_bars(ST2_Trend_MTF)
[since_st_3_buy, since_st_3_sell] = get_bars(ST3_Trend_MTF)
[since_st_4_buy, since_st_4_sell] = get_bars(ST4_Trend_MTF)
[since_st_5_buy, since_st_5_sell] = get_bars(ST5_Trend_MTF)

// select the right heatmsap color


heatmap_color(cond1, cond2) =>
cond1 ? color.new(color.green, 20) : cond2 ? color.new(color.red, 20) : na

lapos_x = timenow + round(change(time)*3)


lapos_y = highest(5)// + (0.15 * highest(20))

// Displays the timeframe labels at the right


f_draw_label(x,y,_text,_textcolor, _size)=>
var label Label = na
label.delete(Label)
Label := label.new(x, y, _text, color=color.new(color.white, 20),
textcolor=_textcolor, style=label.style_none, yloc=yloc.price, xloc=xloc.bar_time,
size=_size)

// Display the labels in a more readable way


// If timeframe selected is "Same as symbol" displays the timeframe.period string
value
res_to_string(res)=>
str = iff(res == "1", "m1", iff(res == "5", "m5", iff(res == "15", "m15",
iff(res == "30", "m30", iff(res == "60", "H1", iff(res == "120", "H2",
iff(res == "240", "H4", iff(res == "480", "H8", iff(res == "D", "Daily",
iff(res == "W", "Weekly", iff(res == "360", "H3", timeframe.period)))))))))))
str

// draw the TF labels


f_draw_label(lapos_x, 1, res_to_string(res1), color.black, size.large)
f_draw_label(lapos_x, 2, res_to_string(res2), color.black, size.large)
f_draw_label(lapos_x, 3, res_to_string(res3), color.black, size.large)
f_draw_label(lapos_x, 4, res_to_string(res4), color.black, size.large)
f_draw_label(lapos_x, 5, res_to_string(res5), color.black, size.large)

// Draw the green/red heatmap raws


fill(h0 ,h1, color=heatmap_color(since_st_1_sell>since_st_1_buy,
since_st_1_sell<since_st_1_buy))
fill(h1, h2, color=heatmap_color(since_st_2_sell>since_st_2_buy,
since_st_2_sell<since_st_2_buy))
fill(h2, h3, color=heatmap_color(since_st_3_sell>since_st_3_buy,
since_st_3_sell<since_st_3_buy))
fill(h3, h4, color=heatmap_color(since_st_4_sell>since_st_4_buy,
since_st_4_sell<since_st_4_buy))
fill(h4, h5, color=heatmap_color(since_st_5_sell>since_st_5_buy,
since_st_5_sell<since_st_5_buy))

You might also like