0% found this document useful (0 votes)
17 views12 pages

01 Market Patashala 2.0

This document is a Pine Script code for a trading indicator that calculates and plots pivot points, support and resistance levels, and various moving averages (EMA) on a chart. It allows users to customize settings such as the pivot resolution and visibility of different elements like daily high/low and VWAP. The code also includes functions to handle weekends and holidays when calculating the next working day for plotting purposes.

Uploaded by

GAGAN RAJU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views12 pages

01 Market Patashala 2.0

This document is a Pine Script code for a trading indicator that calculates and plots pivot points, support and resistance levels, and various moving averages (EMA) on a chart. It allows users to customize settings such as the pivot resolution and visibility of different elements like daily high/low and VWAP. The code also includes functions to handle weekends and holidays when calculating the next working day for plotting purposes.

Uploaded by

GAGAN RAJU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

// This source code is subject to the terms of the Mozilla Public License 2.

0 at
https://mozilla.org/MPL/2.0/
// @Arjun

//@version=5
indicator(title='indicator', shorttitle='indicator', overlay=true)
pivottimeframe = input.string(title='Pivot Resolution', defval='D', options=['D',
'W', 'M'])
dp = input(true, title='Show Pivot Ranges')
sp = input(true, title='Show Pivot Support Levels')
rp = input(true, title='Show Pivot Resistance Levels')
dl = input(true, title='Show PDH/PDL')
showTomorrowCPR = input(title='Show tomorrow\'s CPR', defval=true)
showPDHL = input(title='Show Tommorrow PDH/PDL', defval=true)
showEMA20 = input(title='Show EMA-20', defval=true)
showEMA8 = input(title='Show EMA-8', defval=false)
showEMA200 = input(title='Show EMA-200', defval=true)
showVWAP = input(title='Show VWAP', defval=true)

//dp in the prefix implies daily pivot calculation


dpopen = request.security(syminfo.tickerid, pivottimeframe, open[1],
barmerge.gaps_off, barmerge.lookahead_on)
dphigh = request.security(syminfo.tickerid, pivottimeframe, high[1],
barmerge.gaps_off, barmerge.lookahead_on)
dplow = request.security(syminfo.tickerid, pivottimeframe, low[1],
barmerge.gaps_off, barmerge.lookahead_on)
dpclose = request.security(syminfo.tickerid, pivottimeframe, close[1],
barmerge.gaps_off, barmerge.lookahead_on)
dprange = dphigh - dplow

//Pivot Boss Pivots Formula


pivot = (dphigh + dplow + dpclose) / 3.0
bc = (dphigh + dplow) / 2.0
tc = pivot - bc + pivot
r1 = pivot * 2 - dplow
r2 = pivot + dphigh - dplow
r3 = dphigh + 2 * (pivot - dplow)
s1 = pivot * 2 - dphigh
s2 = pivot - (dphigh - dplow)
s3 = dplow - 2 * (dphigh - pivot)
r4 = r3 + r2 - r1
s4 = s3 - (s1 - s2)

//m,w,d in the prefix implies daily and hourly

dhigh = request.security(syminfo.tickerid, 'D', high[1],


lookahead=barmerge.lookahead_on)
dlow = request.security(syminfo.tickerid, 'D', low[1],
lookahead=barmerge.lookahead_on)
dclose = request.security(syminfo.tickerid, 'D', close[1],
lookahead=barmerge.lookahead_on)
dopen = request.security(syminfo.tickerid, 'D', open,
lookahead=barmerge.lookahead_on)

whigh = request.security(syminfo.tickerid, 'W', high[1],


lookahead=barmerge.lookahead_on)
wlow = request.security(syminfo.tickerid, 'W', low[1],
lookahead=barmerge.lookahead_on)
wclose = request.security(syminfo.tickerid, 'W', close[1],
lookahead=barmerge.lookahead_on)
wopen = request.security(syminfo.tickerid, 'W', open,
lookahead=barmerge.lookahead_on)

mhigh = request.security(syminfo.tickerid, 'M', high[1],


lookahead=barmerge.lookahead_on)
mlow = request.security(syminfo.tickerid, 'M', low[1],
lookahead=barmerge.lookahead_on)
mclose = request.security(syminfo.tickerid, 'M', close[1],
lookahead=barmerge.lookahead_on)
mopen = request.security(syminfo.tickerid, 'M', open,
lookahead=barmerge.lookahead_on)

//Plotting
plot(dp and pivot ? pivot : na, title='Pivot', color=pivot != pivot[1] ? na :
color.rgb(243, 33, 184), linewidth=2)
plot(dp and bc ? bc : na, title='BC', color=bc != bc[1] ? na : color.rgb(243, 33,
240), linewidth=2)
plot(dp and tc ? tc : na, title='TC', color=tc != tc[1] ? na : color.rgb(243, 33,
240), linewidth=2)
plot(rp and r1 ? r1 : na, title='R1', color=r1 != r1[1] ? na : color.red,
linewidth=2)
plot(rp and r2 ? r2 : na, title='R2', color=r2 != r2[1] ? na : color.red,
linewidth=2)
plot(rp and r3 ? r3 : na, title='R3', color=r3 != r3[1] ? na : color.red,
linewidth=2)
plot(rp and r4 ? r4 : na, title='R4', color=r4 != r4[1] ? na : color.red,
linewidth=2)
plot(sp and s1 ? s1 : na, title='S1', color=s1 != s1[1] ? na : color.green,
linewidth=2)
plot(sp and s2 ? s2 : na, title='S2', color=s2 != s2[1] ? na : color.green,
linewidth=2)
plot(sp and s3 ? s3 : na, title='S3', color=s3 != s3[1] ? na : color.green,
linewidth=2)
plot(sp and s4 ? s4 : na, title='S4', color=s4 != s4[1] ? na : color.green,
linewidth=2)

plot(dl and dhigh ? dhigh : na, title='PDH', style=plot.style_line, color=dhigh !=


dhigh[1] ? na : color.new(#f8f404, 0), linewidth=5)
plot(dl and dlow ? dlow : na, title='PDL', style=plot.style_line, color=dlow !=
dlow[1] ? na : color.new(#f8f404, 0), linewidth=5)

///EMA8

EMA8_Len = input.int(8, minval=1, title='EMA8_Length')


//EMA8_src = input(close, title="EMA8_Source")
//EMA8_offset = input(title="EMA8_Offset", type=input.integer, defval=0, minval=-
500, maxval=500)
EMA8_out = ta.ema(close, EMA8_Len)
plot(showEMA8 ? EMA8_out : na, title='EMA8', color=color.rgb(241, 214, 10, 5),
offset=0, linewidth=3)

///EMA200

EMA20_Len = input.int(20, minval=1, title='EMA20_Length')


//EMA20_src = input(close, title="EMA20_Source")
//EMA20_offset = input(title="EMA20_Offset", type=input.integer, defval=0,
minval=-500, maxval=500)
EMA20_out = ta.ema(close, EMA20_Len)
plot(showEMA20 ? EMA20_out : na, title='EMA20', color=color.rgb(234, 24, 17, 5),
offset=0, linewidth=3)

///EMA200

EMA200_Len = input.int(200, minval=1, title='EMA200_Length')


//EMA200_src = input(close, title="EMA200_Source")
//EMA200_offset = input(title="EMA200_Offset", type=input.integer, defval=0,
minval=-500, maxval=500)
EMA200_out = ta.ema(close, EMA200_Len)
plot(showEMA200 ? EMA200_out : na, title='EMA200', color=color.rgb(17, 234, 60),
offset=0, linewidth=4)

///////////////////////// VWAP //////////////


//vwaplength = input(title="VWAP Length", type=input.integer, defval=1)
//cvwap = ema(vwap,vwaplength)
cvwap1 = ta.vwap(hlc3)
plotvwap = plot(showVWAP ? cvwap1 : na, title='VWAP', color=color.rgb(4, 8, 239,
3), linewidth=3, transp=0)

// User inputs

// Defaults
// CPR Colors
cprColor = color.purple
cpprColor = color.rgb(243, 33, 184)
rColor = color.red
sColor = color.green
cColor = color.new(#f8f404, 0)

// Line style & Transparency


lStyle = plot.style_line
lTransp = 20

//Fill Transparency
fTransp = 95

// Global Variables & Flags


// TODO : Update the No of Holidays
noOfHolidays = 13

// Global Functions
// TODO : Update the list of Holiday here in format YYYY, MM, DD, 09, 15
// **09, 15 are session start hour & minutes
IsHoliday(_date) =>
iff_1 = _date == timestamp(2021, 11, 19, 09, 15) ? true : false
iff_2 = _date == timestamp(2021, 11, 05, 09, 15) ? true : iff_1
iff_3 = _date == timestamp(2021, 10, 15, 09, 15) ? true : iff_2
iff_4 = _date == timestamp(2021, 09, 10, 09, 15) ? true : iff_3
iff_5 = _date == timestamp(2021, 08, 19, 09, 15) ? true : iff_4
iff_6 = _date == timestamp(2021, 07, 21, 09, 15) ? true : iff_5
iff_7 = _date == timestamp(2021, 05, 13, 09, 15) ? true : iff_6
iff_8 = _date == timestamp(2021, 04, 21, 09, 15) ? true : iff_7
iff_9 = _date == timestamp(2021, 04, 14, 09, 15) ? true : iff_8
iff_10 = _date == timestamp(2021, 04, 02, 09, 15) ? true : iff_9
iff_11 = _date == timestamp(2021, 03, 29, 09, 15) ? true : iff_10
iff_12 = _date == timestamp(2021, 03, 11, 09, 15) ? true : iff_11
iff_13 = _date == timestamp(2021, 01, 26, 09, 15) ? true : iff_12
_date == timestamp(2021, 02, 21, 09, 15) ? true : iff_13
// Note: Week of Sunday=1...Saturday=7
IsWeekend(_date) =>
dayofweek(_date) == 7 or dayofweek(_date) == 1

// Skip Weekend
SkipWeekend(_date) =>
_d = dayofweek(_date)
_mul = _d == 6 ? 3 : _d == 7 ? 2 : 1

_date + _mul * 86400000

// Get Next Working Day


GetNextWorkingDay(_date) =>
_dt = SkipWeekend(_date)

for i = 1 to noOfHolidays by 1
if IsHoliday(_dt)
_dt := SkipWeekend(_dt)
continue
else
break

_dt

// Today's Session Start timestamp


y = year(timenow)
m = month(timenow)
d = dayofmonth(timenow)

// Start & End time for Today's CPR


start = timestamp(y, m, d, 09, 15)
end = start + 86400000

// Plot Today's CPR


shouldPlotToday = timenow > start

tom_start = start
tom_end = end

// Start & End time for Tomorrow's CPR


if shouldPlotToday
tom_start := GetNextWorkingDay(start)
tom_end := tom_start + 86400000
tom_end

// Get series
getSeries(e, timeFrame) =>
request.security(syminfo.tickerid, 'D', e, lookahead=barmerge.lookahead_on)

// Calculate Today's CPR


//Get High, Low and Close
H = getSeries(high[1], 'D')
L = getSeries(low[1], 'D')
C = getSeries(close[1], 'D')

// Pivot Range
P = (H + L + C) / 3
TC = (H + L) / 2
BC = P - TC + P

// Resistance Levels
R1 = P * 2 - L
R2 = P + H - L
R3 = H + 2 * (P - L)
R4 = R3 + R2 - R1

// Support Levels
S1 = P * 2 - H
S2 = P - (H - L)
S3 = L - 2 * (H - P)
S4 = S3 - (S1 - S2)

// Plot Today's CPR


if not IsHoliday(start) and not IsWeekend(start) and shouldPlotToday

_r4 = line.new(start, R4, end, R4, xloc.bar_time, color=color.new(rColor,


lTransp), width=2)
line.delete(_r4[1])
_r3 = line.new(start, R3, end, R3, xloc.bar_time, color=color.new(rColor,
lTransp), width=2)
line.delete(_r3[1])
_r2 = line.new(start, R2, end, R2, xloc.bar_time, color=color.new(rColor,
lTransp), width=2)
line.delete(_r2[1])
_r1 = line.new(start, R1, end, R1, xloc.bar_time, color=color.new(rColor,
lTransp), width=2)
line.delete(_r1[1])

_tc = line.new(start, TC, end, TC, xloc.bar_time, color=color.new(cprColor,


lTransp), width=2)
line.delete(_tc[1])
_p = line.new(start, P, end, P, xloc.bar_time, color=color.new(cpprColor,
lTransp), width=2)
line.delete(_p[1])
_bc = line.new(start, BC, end, BC, xloc.bar_time, color=color.new(cprColor,
lTransp), width=2)
line.delete(_bc[1])

_s1 = line.new(start, S1, end, S1, xloc.bar_time, color=color.new(sColor,


lTransp), width=2)
line.delete(_s1[1])
_s2 = line.new(start, S2, end, S2, xloc.bar_time, color=color.new(sColor,
lTransp), width=2)
line.delete(_s2[1])

_s3 = line.new(start, S3, end, S3, xloc.bar_time, color=color.new(sColor,


lTransp), width=2)
line.delete(_s3[1])
_s4 = line.new(start, S4, end, S4, xloc.bar_time, color=color.new(sColor,
lTransp), width=2)
line.delete(_s4[1])

if showPDHL
_pdh = line.new(start, H, end, H, xloc.bar_time, color=color.new(cColor,
lTransp), width=5)
line.delete(_pdh[1])
_pdl = line.new(start, L, end, L, xloc.bar_time, color=color.new(cColor,
lTransp), width=5)
line.delete(_pdl[1])

// Plot Today's Labels


if not IsHoliday(start) and not IsWeekend(start) and shouldPlotToday

l_r4 = label.new(start, R4, text='R4', xloc=xloc.bar_time, textcolor=rColor,


style=label.style_none)
label.delete(l_r4[1])
l_r3 = label.new(start, R3, text='R3', xloc=xloc.bar_time, textcolor=rColor,
style=label.style_none)
label.delete(l_r3[1])
l_r2 = label.new(start, R2, text='R2', xloc=xloc.bar_time, textcolor=rColor,
style=label.style_none)
label.delete(l_r2[1])
l_r1 = label.new(start, R1, text='R1', xloc=xloc.bar_time, textcolor=rColor,
style=label.style_none)
label.delete(l_r1[1])
l_tc = label.new(start, TC, text='BC', xloc=xloc.bar_time, textcolor=cprColor,
style=label.style_none)
label.delete(l_tc[1])
l_p = label.new(start, P, text='P', xloc=xloc.bar_time, textcolor=cpprColor,
style=label.style_none)
label.delete(l_p[1])
l_bc = label.new(start, BC, text='TC', xloc=xloc.bar_time, textcolor=cprColor,
style=label.style_none)
label.delete(l_bc[1])
l_s1 = label.new(start, S1, text='S1', xloc=xloc.bar_time, textcolor=sColor,
style=label.style_none)
label.delete(l_s1[1])
l_s2 = label.new(start, S2, text='S2', xloc=xloc.bar_time, textcolor=sColor,
style=label.style_none)
label.delete(l_s2[1])
l_s3 = label.new(start, S3, text='S3', xloc=xloc.bar_time, textcolor=sColor,
style=label.style_none)
label.delete(l_s3[1])
l_s4 = label.new(start, S4, text='S4', xloc=xloc.bar_time, textcolor=sColor,
style=label.style_none)
label.delete(l_s4[1])

if showPDHL
l_pdh = label.new(start, H, text='PDH', xloc=xloc.bar_time,
textcolor=cColor, style=label.style_none)
label.delete(l_pdh[1])
l_pdl = label.new(start, L, text='PDL', xloc=xloc.bar_time,
textcolor=cColor, style=label.style_none)
label.delete(l_pdl[1])

// Calculate Tomorrow's CPR


// Get High, Low and Close
tH = getSeries(high, 'D')
tL = getSeries(low, 'D')
tC = getSeries(close, 'D')

// Pivot Range
tP = (tH + tL + tC) / 3
tTC = (tH + tL) / 2
tBC = tP - tTC + tP

// Resistance Levels
tR3 = tH + 2 * (tP - tL)
tR2 = tP + tH - tL
tR1 = tP * 2 - tL

// Support Levels
tS1 = tP * 2 - tH
tS2 = tP - (tH - tL)
tS3 = tL - 2 * (tH - tP)

// Plot Tomorrow's CPR


if showTomorrowCPR

_t_r3 = line.new(tom_start, tR3, tom_end, tR3, xloc.bar_time,


color=color.new(rColor, lTransp), width=3)
line.delete(_t_r3[1])
_t_r2 = line.new(tom_start, tR2, tom_end, tR2, xloc.bar_time,
color=color.new(rColor, lTransp), width=3)
line.delete(_t_r2[1])
_t_r1 = line.new(tom_start, tR1, tom_end, tR1, xloc.bar_time,
color=color.new(rColor, lTransp), width=3)
line.delete(_t_r1[1])

_t_tc = line.new(tom_start, tTC, tom_end, tTC, xloc.bar_time,


color=color.new(cprColor, lTransp), width=3)
line.delete(_t_tc[1])
_t_p = line.new(tom_start, tP, tom_end, tP, xloc.bar_time,
color=color.new(cpprColor, lTransp), width=3)
line.delete(_t_p[1])
_t_bc = line.new(tom_start, tBC, tom_end, tBC, xloc.bar_time,
color=color.new(cprColor, lTransp), width=3)
line.delete(_t_bc[1])

_t_s1 = line.new(tom_start, tS1, tom_end, tS1, xloc.bar_time,


color=color.new(sColor, lTransp), width=3)
line.delete(_t_s1[1])
_t_s2 = line.new(tom_start, tS2, tom_end, tS2, xloc.bar_time,
color=color.new(sColor, lTransp), width=3)
line.delete(_t_s2[1])

_t_s3 = line.new(tom_start, tS3, tom_end, tS3, xloc.bar_time,


color=color.new(sColor, lTransp), width=3)
line.delete(_t_s3[1])
if showPDHL
_pdth = line.new(tom_start, tH, tom_end, tH, xloc.bar_time,
color=color.new(cColor, lTransp), width=5)
line.delete(_pdth[1])
_pdtl = line.new(tom_start, tL, tom_end, tL, xloc.bar_time,
color=color.new(cColor, lTransp), width=5)
line.delete(_pdtl[1])

// Plot Tomorrow's Labels


if showTomorrowCPR

l_t_r3 = label.new(tom_start, tR3, text='R3', xloc=xloc.bar_time,


textcolor=rColor, style=label.style_none)
label.delete(l_t_r3[1])
l_t_r2 = label.new(tom_start, tR2, text='R2', xloc=xloc.bar_time,
textcolor=rColor, style=label.style_none)
label.delete(l_t_r2[1])
l_t_r1 = label.new(tom_start, tR1, text='R1', xloc=xloc.bar_time,
textcolor=rColor, style=label.style_none)
label.delete(l_t_r1[1])

l_t_tc = label.new(tom_start, tTC, text='TC', xloc=xloc.bar_time,


textcolor=cprColor, style=label.style_none)
label.delete(l_t_tc[1])
l_t_p = label.new(tom_start, tP, text='P', xloc=xloc.bar_time,
textcolor=cpprColor, style=label.style_none)
label.delete(l_t_p[1])
l_t_bc = label.new(tom_start, tBC, text='BC', xloc=xloc.bar_time,
textcolor=cprColor, style=label.style_none)
label.delete(l_t_bc[1])

l_t_s1 = label.new(tom_start, tS1, text='S1', xloc=xloc.bar_time,


textcolor=sColor, style=label.style_none)
label.delete(l_t_s1[1])
l_t_s2 = label.new(tom_start, tS2, text='S2', xloc=xloc.bar_time,
textcolor=sColor, style=label.style_none)
label.delete(l_t_s2[1])

l_t_s3 = label.new(tom_start, tS3, text='S3', xloc=xloc.bar_time,


textcolor=sColor, style=label.style_none)
label.delete(l_t_s3[1])
if showPDHL
l_pdth = label.new(tom_start, tH, text='PDH', xloc=xloc.bar_time,
textcolor=cColor, style=label.style_none)
label.delete(l_pdth[1])
l_pdtl = label.new(tom_start, tL, text='PDL', xloc=xloc.bar_time,
textcolor=cColor, style=label.style_none)
label.delete(l_pdtl[1])

g_panel = 'Panel Options'


i_orientation = input.string('Vertical', 'Orientation', options = ['Vertical',
'Horizontal'], group = g_panel)
i_position = input.string('Bottom Right', 'Position', options = ['Top Left', 'Top
Right', 'Top Center', 'Bottom Left', 'Bottom Right', 'Bottom Center', 'Middle
Left', 'Middle Right', 'Middle Center'], group = g_panel)

i_border_width = input.int(1, 'Border Width', minval = 0, maxval = 10, group =


g_panel, inline = 'border')
i_color_border = input.color(#000000, '', group = g_panel, inline = 'border')

i_showHeaders = input.bool(true, 'Show RSI Headers', group = g_panel)


i_color_header_bg = input.color(#5d606b, 'Headers Background', group = g_panel,
inline = 'header')
i_color_header_text = input.color(color.white, 'Text', group = g_panel, inline =
'header')

i_color_tf_bg = input.color(#2a2e39, 'Timeframe Background', group = g_panel,


inline = 'tf')
i_color_tf_text = input.color(color.white, 'Text', group = g_panel, inline = 'tf')

i_debug = input.bool(false, 'Display colors palette (debug)', group = g_panel)


// rsi bg colors
g_rsi = 'RSI Colors'
i_threshold_ob = input.int(61, 'Overbought Threshold', minval=55, maxval=100, group
= g_rsi)
i_color_ob = input.color(#128416, 'Overbought Background', inline = 'ob', group =
g_rsi)
i_tcolor_ob = input.color(color.white, 'Text', inline = 'ob', group = g_rsi)

i_threshold_uptrend = input.int(55, 'Uptrend Threshold', minval=45, maxval=100,


group = g_rsi)
i_color_uptrend = input.color(#2d472e, 'Uptrend Background', inline = 'up', group
= g_rsi)
i_tcolor_uptrend = input.color(color.white, 'Text', inline = 'up', group = g_rsi)

i_color_mid = input.color(#131722, 'No Trend Background', group = g_rsi, inline =


'mid')
i_tcolor_mid = input.color(#b2b5be, 'Text', group = g_rsi, inline = 'mid')

i_threshold_downtrend = input.int(40, 'Downtrend Threshold', group = g_rsi,


minval=0, maxval=49)
i_color_downtrend = input.color(#5b2e2e, 'Downtrend Background', group = g_rsi,
inline = 'down')
i_tcolor_downtrend = input.color(color.white, 'Text', group = g_rsi, inline =
'down')

i_threshold_os = input.int(30, 'Oversold Threshold', minval=0, maxval=49, group =


g_rsi)
i_color_os = input.color(#db3240, 'Oversold Background', group = g_rsi, inline =
'os')
i_tcolor_os = input.color(color.white, 'Text', group = g_rsi, inline = 'os')

g_rsi1 = 'RSI #1'


i_rsi1_enabled = input.bool(true, title = 'Enabled', group = g_rsi1)
i_rsi1_tf = input.timeframe('3', 'Timeframe', group = g_rsi1)
i_rsi1_len = input.int(14, 'Length', minval = 1, group = g_rsi1)
i_rsi1_src = input.source(close, 'Source', group = g_rsi1) * 10000
v_rsi1 = i_rsi1_enabled ? request.security(syminfo.tickerid, i_rsi1_tf,
ta.rsi(i_rsi1_src, i_rsi1_len)) : na

g_rsi2 = 'RSI #2'


i_rsi2_enabled = input.bool(true, title = 'Enabled', group = g_rsi2)
i_rsi2_tf = input.timeframe('5', 'Timeframe', group = g_rsi2)
i_rsi2_len = input.int(14, 'Length', minval = 1, group = g_rsi2)
i_rsi2_src = input.source(close, 'Source', group = g_rsi2) * 10000
v_rsi2 = i_rsi2_enabled ? request.security(syminfo.tickerid, i_rsi2_tf,
ta.rsi(i_rsi2_src, i_rsi2_len)) : na

g_rsi3 = 'RSI #3'


i_rsi3_enabled = input.bool(true, title = 'Enabled', group = g_rsi3)
i_rsi3_tf = input.timeframe('15', 'Timeframe', group = g_rsi3)
i_rsi3_len = input.int(14, 'Length', minval = 1, group = g_rsi3)
i_rsi3_src = input.source(close, 'Source', group = g_rsi3) * 10000
v_rsi3 = i_rsi3_enabled ? request.security(syminfo.tickerid, i_rsi3_tf,
ta.rsi(i_rsi3_src, i_rsi3_len)) : na

g_rsi4 = 'RSI #4'


i_rsi4_enabled = input.bool(true, title = 'Enabled', group = g_rsi4)
i_rsi4_tf = input.timeframe('60', 'Timeframe', group = g_rsi4)
i_rsi4_len = input.int(14, 'Length', minval = 1, group = g_rsi4)
i_rsi4_src = input.source(close, 'Source', group = g_rsi4) * 10000
v_rsi4 = i_rsi4_enabled ? request.security(syminfo.tickerid, i_rsi4_tf,
ta.rsi(i_rsi4_src, i_rsi4_len)) : na

g_rsi5 = 'RSI #5'


i_rsi5_enabled = input.bool(true, title = 'Enabled', group = g_rsi5)
i_rsi5_tf = input.timeframe('D', 'Timeframe', group = g_rsi5)
i_rsi5_len = input.int(14, 'Length', minval = 1, group = g_rsi5)
i_rsi5_src = input.source(close, 'Source', group = g_rsi5) * 10000
v_rsi5 = i_rsi5_enabled ? request.security(syminfo.tickerid, i_rsi5_tf,
ta.rsi(i_rsi5_src, i_rsi5_len)) : na

f_StrPositionToConst(_p) =>
switch _p
'Top Left' => position.top_left
'Top Right' => position.top_right
'Top Center' => position.top_center
'Middle Left' => position.middle_left
'Middle Right' => position.middle_right
'Middle Center' => position.middle_center
'Bottom Left' => position.bottom_left
'Bottom Right' => position.bottom_right
'Bottom Center' => position.bottom_center
=> position.bottom_right

f_timeframeToHuman(_tf) =>
seconds = timeframe.in_seconds(_tf)

if seconds < 60
_tf
else if seconds < 3600
str.tostring(seconds / 60) + 'm'
else if seconds < 86400
str.tostring(seconds / 60 / 60) + 'h'
else
switch _tf
"1D" => "D"
"1W" => "W"
"1M" => "M"
=> str.tostring(_tf)

type TPanel
table src = na
bool vertical_orientation = true
int row = 0
int col = 0

method incCol(TPanel _panel) =>


if _panel.vertical_orientation
_panel.col += 1
else
_panel.row += 1

method incRow(TPanel _panel) =>


if not _panel.vertical_orientation
_panel.col += 1
_panel.row := 0
else
_panel.row += 1
_panel.col := 0

method add(TPanel _panel, string _v1, color _bg1, color _ctext1, string _v2, color
_bg2, color _ctext2) =>
table.cell(_panel.src, _panel.col, _panel.row, _v1, text_color = _ctext1,
bgcolor = _bg1)
_panel.incCol()

table.cell(_panel.src, _panel.col, _panel.row, _v2, text_color = _ctext2,


bgcolor = _bg2)
_panel.incRow()

f_bg(_rsi) =>
c_line = na(_rsi) ? i_color_mid :
_rsi >= i_threshold_ob ? i_color_ob :
_rsi >= i_threshold_uptrend ? i_color_uptrend :

_rsi <= i_threshold_os ? i_color_os :


_rsi <= i_threshold_downtrend ? i_color_downtrend :

i_color_mid

f_rsi_text_color(_rsi) =>
c_line = na(_rsi) ? i_tcolor_mid :
_rsi >= i_threshold_ob ? i_tcolor_ob :
_rsi >= i_threshold_uptrend ? i_tcolor_uptrend :

_rsi <= i_threshold_os ? i_tcolor_os :


_rsi <= i_threshold_downtrend ? i_tcolor_downtrend :

i_tcolor_mid

f_formatRsi(_rsi) => na(_rsi) ? 'N/A' : str.tostring(_rsi, '0.00')

if barstate.islast

v_panel = TPanel.new(vertical_orientation = i_orientation == 'Vertical')


v_max_rows = 20
v_panel.src := table.new(f_StrPositionToConst(i_position), v_max_rows,
v_max_rows, border_width = i_border_width, border_color = i_color_border)

if i_showHeaders
v_panel.add('TF', i_color_header_bg, i_color_header_text, 'RSI',
i_color_header_bg, i_color_header_text)

if i_rsi1_enabled
v_panel.add(f_timeframeToHuman(i_rsi1_tf), i_color_tf_bg, i_color_tf_text,
f_formatRsi(v_rsi1), f_bg(v_rsi1), f_rsi_text_color(v_rsi1))
if i_rsi2_enabled
v_panel.add(f_timeframeToHuman(i_rsi2_tf), i_color_tf_bg, i_color_tf_text,
f_formatRsi(v_rsi2), f_bg(v_rsi2), f_rsi_text_color(v_rsi2))
if i_rsi3_enabled
v_panel.add(f_timeframeToHuman(i_rsi3_tf), i_color_tf_bg, i_color_tf_text,
f_formatRsi(v_rsi3), f_bg(v_rsi3), f_rsi_text_color(v_rsi3))
if i_rsi4_enabled
v_panel.add(f_timeframeToHuman(i_rsi4_tf), i_color_tf_bg, i_color_tf_text,
f_formatRsi(v_rsi4), f_bg(v_rsi4), f_rsi_text_color(v_rsi4))
if i_rsi5_enabled
v_panel.add(f_timeframeToHuman(i_rsi5_tf), i_color_tf_bg, i_color_tf_text,
f_formatRsi(v_rsi5), f_bg(v_rsi5), f_rsi_text_color(v_rsi5))

if i_debug
t = table.new(position.middle_center, 21, 20, border_width =
i_border_width, border_color = i_color_border)
v_panel2 = TPanel.new(t, vertical_orientation = i_orientation ==
'Vertical')

v_panel2.add('Debug', i_color_header_bg, i_color_header_text, 'Colors',


i_color_header_bg, i_color_header_text)

demo = map.new<string, float>()


map.put(demo, 'Overbought', i_threshold_ob)
map.put(demo, 'Uptrend', i_threshold_uptrend)
map.put(demo, 'No Trend', 50)
map.put(demo, 'Downtrend', i_threshold_downtrend)
map.put(demo, 'Oversold', i_threshold_os)

demoKeys = map.keys(demo)
for key in demoKeys
tf = key
rsi = map.get(demo, key)
v_panel2.add(tf, i_color_tf_bg, i_color_tf_text, f_formatRsi(rsi),
f_bg(rsi), f_rsi_text_color(rsi))

//IB

up15on = input(true, title='15 min IBH')


down15on = input(true, title='15 min IBL')

is_newbar(res) =>
ta.change(time(res)) != 0

adopt(r, s) =>
request.security(syminfo.tickerid, r, s)

high_range = ta.valuewhen(is_newbar('D'), high, 0)


low_range = ta.valuewhen(is_newbar('D'), low, 0)

high_rangeL = ta.valuewhen(is_newbar('D'), high, 0)


low_rangeL = ta.valuewhen(is_newbar('D'), low, 0)

adopt__1 = adopt('15', high_rangeL)


up15 = plot(up15on ? adopt__1 : na, title='IBH 15m', color=color.new(#009900, 0),
linewidth=4)
adopt__2 = adopt('15', low_rangeL)
down15 = plot(down15on ? adopt__2 : na, title='IBL 15m', color=color.new(#ff0000,
0), linewidth=4)

trans15 = up15on ? 97 : 100


fill(up15, down15, color=color.white, transp=trans15)

You might also like