3c Indicator

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

3c indicator

// This source code is subject to the terms of the Mozilla Public License 2.0 at
https://mozilla.org/MPL/2.0/
// © chartcommandos

//@version=4
study("Chart Commando Combo", shorttitle="3C Indicator", overlay=true)

// User inputs
showTomorrowCPR = input(title="Show tomorrow's CPR", type=input.bool, defval=true)
showHistoricalCPR = input(title="Show historical CPR", type=input.bool,
defval=false)
showR3S3 = input(title="Show R3 & S3", type=input.bool, defval=false)

showBB = input(title="Show BB", type=input.bool, defval=true)


showVWAP = input(title="Show VWAP", type=input.bool, defval=false)
showEMA9 = input(title="Show EMA9", type=input.bool, defval=false)
showEMA20 = input(title="Show EMA20", type=input.bool, defval=true)
showEMA200 = input(title="Show EMA200", type=input.bool, defval=false)
showPHPL = input(title="Show PHPL", type=input.bool, defval=false)

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

// Get user input


vwaplength= input(title="VWAP Length", type=input.integer, defval=1)
cvwap = ema(vwap,vwaplength)
plotvwap = plot(showVWAP ? cvwap : na,title="VWAP",color=color.orange, transp=0,
linewidth=2)

//////////////////
// BB //
//////////////////
length = input(20, minval=1)
src = input(close, title="BB_Source")
mult = input(2.0, minval=0.001, maxval=50, title="BB_StdDev")
basis = sma(src, length)
dev = mult * stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input(0, "Offset", type = input.integer, minval = -500, maxval = 500)
plot(showBB ? basis : na, "Basis", color=#872323, offset = offset)
p1 = plot(showBB ? upper : na, "Upper", color=color.teal, offset = offset)
p2 = plot(showBB ? lower: na, "Lower", color=color.teal, offset = offset)
fill(p1, p2, title = "Background", color=#198787, transp=95)

//////////////////
// EMA20 //
//////////////////
EMA20_Len = input(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 = ema(EMA20_src, EMA20_Len)
plot(showEMA20 ? EMA20_out:na, title="EMA20", color=color.yellow,
offset=EMA20_offset)

//////////////////
// EMA9 //
//////////////////
EMA9_Len = input(9, minval=1, title="EMA9_Length")
EMA9_src = input(close, title="EMA9_Source")
EMA9_offset = input(title="EMA9_Offset", type=input.integer, defval=0, minval=-500,
maxval=500)
EMA9_out = ema(EMA9_src, EMA9_Len)
plot(showEMA9 ? EMA9_out:na, title="EMA9", color=color.blue, offset=EMA9_offset)

//////////////////
// EMA200 //
//////////////////
EMA200_Len = input(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 = ema(EMA200_src, EMA200_Len)
plot(showEMA200 ? EMA200_out:na, title="EMA200", color=color.purple,
offset=EMA200_offset)

//////////////////
// PL && PH //
//////////////////

//study(title="previous Day high & low", shorttitle="Prev day H-L", overlay=true,


precision=8)
prevDH = input(true, title="Show previous Day high?")
prevDL = input(true, title="show previous Day low?")

//previous day
prevDayHigh = security(syminfo.tickerid, 'D', high[1], lookahead=true)
prevDayLow = security(syminfo.tickerid, 'D', low[1], lookahead=true)

//previous day Plots


plot(showPHPL ? prevDH and prevDayHigh ? prevDayHigh : na : na, title="Prev Day
High", style=plot.style_stepline, linewidth=1, color=color.red, transp=20)
plot(showPHPL ? prevDL and prevDayLow ? prevDayLow : na : na, title="Prev Day
Low", style=plot.style_stepline, linewidth=1, color=color.green, transp=20)

// Defaults
// CPR Colors
cprColor = color.yellow
rColor = color.fuchsia
sColor = color.aqua

// Line style & Transparency


lStyle = plot.style_line
lTransp = 35
//Fill Transparency
fTransp = 95

// Global Variables & Flags


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

// 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(_date == timestamp(2020, 02, 21, 09, 15), true,
iff(_date == timestamp(2020, 03, 10, 09, 15), true,
iff(_date == timestamp(2020, 04, 02, 09, 15), true,
iff(_date == timestamp(2020, 04, 06, 09, 15), true,
iff(_date == timestamp(2020, 04, 10, 09, 15), true,
iff(_date == timestamp(2020, 04, 14, 09, 15), true,
iff(_date == timestamp(2020, 05, 01, 09, 15), true,
iff(_date == timestamp(2020, 05, 25, 09, 15), true,
iff(_date == timestamp(2020, 10, 02, 09, 15), true,
iff(_date == timestamp(2020, 11, 16, 09, 15), true,
iff(_date == timestamp(2020, 11, 30, 09, 15), true,
iff(_date == timestamp(2020, 12, 25, 09, 15), true,
false))))))))))))

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

// Get series
getSeries(e, timeFrame) => 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
R3 = H + 2*(P - L)
R2 = P + (H - L)
R1 = (P * 2) - L

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

// Plot Today's CPR


if not(IsHoliday(start)) and not(IsWeekend(start)) and shouldPlotToday
if showR3S3
_r3 = line.new(start, R3, end, R3, xloc.bar_time, color=color.new(rColor,
lTransp))
line.delete(_r3[1])
_r2 = line.new(start, R2, end, R2, xloc.bar_time, color=color.new(rColor,
lTransp))
line.delete(_r2[1])
_r1 = line.new(start, R1, end, R1, xloc.bar_time, color=color.new(rColor,
lTransp))
line.delete(_r1[1])

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


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

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


lTransp))
line.delete(_s1[1])
_s2 = line.new(start, S2, end, S2, xloc.bar_time, color=color.new(sColor,
lTransp))
line.delete(_s2[1])
if showR3S3
_s3 = line.new(start, S3, end, S3, xloc.bar_time, color=color.new(sColor,
lTransp))
line.delete(_s3[1])

// Plot Today's Labels


if not(IsHoliday(start)) and not(IsWeekend(start)) and shouldPlotToday
if showR3S3
l_r3 = label.new(start, R3, text="CR3", xloc=xloc.bar_time,
textcolor=rColor, style=label.style_none)
label.delete(l_r3[1])
l_r2 = label.new(start, R2, text="CR2", xloc=xloc.bar_time, textcolor=rColor,
style=label.style_none)
label.delete(l_r2[1])
l_r1 = label.new(start, R1, text="CR1", xloc=xloc.bar_time, textcolor=rColor,
style=label.style_none)
label.delete(l_r1[1])

l_tc = label.new(start, TC, text="CTC", xloc=xloc.bar_time,


textcolor=cprColor, style=label.style_none)
label.delete(l_tc[1])
l_p = label.new(start, P, text="CP", xloc=xloc.bar_time, textcolor=cprColor,
style=label.style_none)
label.delete(l_p[1])
l_bc = label.new(start, BC, text="CBC", xloc=xloc.bar_time,
textcolor=cprColor, style=label.style_none)
label.delete(l_bc[1])

l_s1 = label.new(start, S1, text="CS1", xloc=xloc.bar_time, textcolor=sColor,


style=label.style_none)
label.delete(l_s1[1])
l_s2 = label.new(start, S2, text="CS2", xloc=xloc.bar_time, textcolor=sColor,
style=label.style_none)
label.delete(l_s2[1])
if showR3S3
l_s3 = label.new(start, S3, text="CS3", xloc=xloc.bar_time,
textcolor=sColor, style=label.style_none)
label.delete(l_s3[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
if showR3S3
_t_r3 = line.new(tom_start, tR3, tom_end, tR3, xloc.bar_time,
color=color.new(rColor, lTransp))
line.delete(_t_r3[1])
_t_r2 = line.new(tom_start, tR2, tom_end, tR2, xloc.bar_time,
color=color.new(rColor, lTransp))
line.delete(_t_r2[1])
_t_r1 = line.new(tom_start, tR1, tom_end, tR1, xloc.bar_time,
color=color.new(rColor, lTransp))
line.delete(_t_r1[1])

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


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

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


color=color.new(sColor, lTransp))
line.delete(_t_s1[1])
_t_s2 = line.new(tom_start, tS2, tom_end, tS2, xloc.bar_time,
color=color.new(sColor, lTransp))
line.delete(_t_s2[1])
if showR3S3
_t_s3 = line.new(tom_start, tS3, tom_end, tS3, xloc.bar_time,
color=color.new(sColor, lTransp))
line.delete(_t_s3[1])

// Plot Tomorrow's Labels


if showTomorrowCPR
if showR3S3
l_t_r3 = label.new(tom_start, tR3, text="CR3", 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="CR2", 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="CR1", 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="CTC", 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="CP", xloc=xloc.bar_time,
textcolor=cprColor, style=label.style_none)
label.delete(l_t_p[1])
l_t_bc = label.new(tom_start, tBC, text="CBC", 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="CS1", 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="CS2", xloc=xloc.bar_time,
textcolor=sColor, style=label.style_none)
label.delete(l_t_s2[1])
if showR3S3
l_t_s3 = label.new(tom_start, tS3, text="CS3", xloc=xloc.bar_time,
textcolor=sColor, style=label.style_none)
label.delete(l_t_s3[1])

//Plot Historical CPR


p_r3 = plot(showHistoricalCPR ? showR3S3 ? R3 : na : na, title=' CR3',
color=rColor, transp=lTransp, style=lStyle)
p_r2 = plot(showHistoricalCPR ? R2 : na, title=' CR2', color=rColor,
transp=lTransp, style=lStyle)
p_r1 = plot(showHistoricalCPR ? R1 : na, title=' CR1', color=rColor,
transp=lTransp, style=lStyle)

p_cprTC = plot(showHistoricalCPR ? TC : na, title=' CTC', color=cprColor,


transp=lTransp, style=lStyle)
p_cprP = plot(showHistoricalCPR ? P : na, title=' CP', color=cprColor,
transp=lTransp, style=lStyle)
p_cprBC = plot(showHistoricalCPR ? BC : na, title=' CBC', color=cprColor,
transp=lTransp, style=lStyle)

s1 = plot(showHistoricalCPR ? S1 : na, title=' CS1', color=sColor, transp=lTransp,


style=lStyle)
s2 = plot(showHistoricalCPR ? S2 : na, title=' CS2', color=sColor, transp=lTransp,
style=lStyle)
s3 = plot(showHistoricalCPR ? showR3S3 ? S3 : na : na, title=' CS3', color=sColor,
transp=lTransp, style=lStyle)

fill(p_cprTC, p_cprBC, color=color.purple, transp=fTransp)

You might also like