Day Trading [VB]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bhosalevishal

//@version=5
indicator('Day Trading [VB]', shorttitle='DTrade [VB]', overlay=true) 


// USER INPUTS
showRoundNo         = input(group="Round Number Levels", inline="1", title='Show Round Number Levels', defval=true)
roundNo             = input.int(group="Round Number Levels", inline="2", title='Round With', defval=100, tooltip="🛑 Plots expanded horizontal lines.")
candlestickPattern  = input.bool(group="Candlestick Patterns", inline="1", title='Show Candlestick Patterns', defval=true, tooltip="🛑 Plots candlestick pattern name.")


showTomorrowCPR     = input(group="CPR", inline="1", title='Show tomorrow\'s CPR', defval=false)
showHistoricalCPR   = input(group="CPR", inline="2", title='Show historical CPR', defval=false)

showReversal        = input(group="Reversal", inline="1", title='Show Reversals', defval=true)

_wma                = input(group="WMA", inline="1", title='Show WMA', defval=false)
wma_len             = input.int(9, minval=1, title="Length")
wma_src             = input(close, title="Source")
wma_offset          = input.int(title="Offset", defval=0, minval=-500, maxval=500)

fastEMA             = input(group="EMA(fast)", inline="1", title='Show EMA(fast)', defval=true)
fastEMA_len         = input.int(defval=3, minval=1, title='Length')
fastEMA_src         = input(defval=close, title='Source')
fastEMA_offset      = input.int(title='Offset', defval=0, minval=-500, maxval=500)

medEMA              = input(group="EMA(med)", inline="1", title='Show EMA(med)', defval=true)
medEMA_len          = input.int(defval=13, minval=1, title='Length')
medEMA_src          = input(defval=close, title='Source')
medEMA_offset       = input.int(title='Offset', defval=0, minval=-500, maxval=500)

slowEMA             = input(group="EMA(slow)", inline="1", title='Show EMA(slow)', defval=false)
slowEMA_len         = input.int(defval=20, minval=1, title='Length')
slowEMA_src         = input(defval=close, title='Source')
slowEMA_offset      = input.int(title='Offset', defval=0, minval=-500, maxval=500)


// CONSTANTS
var lTransp             = 30
var color blue          = #0000FF
var color cprColor      = color.new(blue, lTransp)
var color sColor        = color.new(color.green, lTransp)
var color rColor        = color.new(color.red, lTransp)
var color pdHLColor     = color.new(color.purple, lTransp)
var color labelColor    = color.new(color.fuchsia, lTransp)
var color doColor       = color.new(color.lime, lTransp)
var color grayColor    = color.new(color.blue, lTransp)
var color limeColor    = color.new(color.lime, lTransp)


// Today's Start and End time 
start   = timestamp(year(time), month(time), dayofmonth(time), 09, 00)
end     = timestamp(year(time), month(time), dayofmonth(time), 15, 40)

// Tomorrow's Start and End time 
tom_start   = start + 86400000
tom_end     = end + 86400000

// WMA
wma_out     = ta.wma(wma_src, wma_len)

// EMAs
fastEMA_out = ta.ema(fastEMA_src, fastEMA_len)
medEMA_out  = ta.ema(medEMA_src, medEMA_len)
slowEMA_out = ta.ema(slowEMA_src, slowEMA_len)

// CCI
// cci = ta.cci(hlc3,7)


// ********** FUNCTION ********** //
plotLinesAndLabels(value,_label, _color, start, end) =>
    __line = line.new(start, value, end, value, xloc.bar_time, color=_color, style=line.style_solid, width=1)
    line.delete(__line[1])
    __label = label.new(start, value, text=_label+'‏  ‏  ‏  ‏  ‏', xloc=xloc.bar_time, textcolor=labelColor, style=label.style_label_center, color=#00000000)
    label.delete(__label[1])
    
plotExtendedLines(value) =>
    ex_line = line.new(start, value, end, value, xloc.bar_time, color=limeColor, extend=extend.right, width=2, style=line.style_dotted)
    line.delete(ex_line[1])
    
//Pivot = (High + Low + Close)/3
//Bottom CPR = (High + Low)/ 2
//Top CPR = (Pivot – BC) + Pivot
    
calCPRSR(H, L, C) =>
    Pivot = (H + L + C) / 3
    BC = (H + L) / 2
    TC = (Pivot - BC) + Pivot

    R3 = H + 2 * (Pivot - L)
    R2 = Pivot + H - L
    R1 = Pivot * 2 - L

    S1 = Pivot * 2 - H
    S2 = Pivot - (H - L)
    S3 = L - 2 * (H - Pivot)
    
    [Pivot, BC, TC, R3, R2, R1, S1, S2, S3]   
    
    
// calculate camarrila SRs    
calCamSR(H, L, C) =>
    Pivot_Median = (H + L + C) / 3
    Pivot_Range = H - L
    
    R1 = C + Pivot_Range  * 1.1 / 12.0
    S1 = C - Pivot_Range  * 1.1 / 12.0
    
    R2 = C + Pivot_Range  * 1.1 / 6.0
    S2 = C - Pivot_Range  * 1.1 / 6.0
    
    R3 = C + Pivot_Range  * 1.1 / 4.0
    S3 = C - Pivot_Range  * 1.1 / 4.0
    
    R4 = C + Pivot_Range  * 1.1 / 2.0
    S4 = C - Pivot_Range  * 1.1 / 2.0
    
    [R4, R3, R2, R1, S1, S2, S3, S4]    
    

// Todays HLOC
[dayO, dayH, dayL, dayC] = request.security(syminfo.tickerid, 'D', [open, high, low, close], barmerge.gaps_off, barmerge.lookahead_on)

// Previous Day HLOC
[pDayO, pDayH, pDayL, pDayC] = request.security(syminfo.tickerid, 'D', [open[1], high[1], low[1], close[1]], barmerge.gaps_off, barmerge.lookahead_on)


// TODAYS CPR + SR CALCULATIONS 
[dayP, dayBC, dayTC, dayR3, dayR2, dayR1, dayS1, dayS2, dayS3] = calCPRSR(pDayH, pDayL, pDayC)

// TOMORROW CPR + SR CALCULATIONS 
[tomP, tomBC, tomTC, tomR3, tomR2, tomR1, tomS1, tomS2, tomS3] = calCPRSR(dayH, dayL, dayC)

// TODAYS CAMARRILA SR CALCULATIONS 
[camR4, camR3, camR2, camR1, camS1, camS2, camS3, camS4] = calCamSR(pDayH, pDayL, pDayC)

// Weekly Pivot + SR
[WP, WS1, WS2, WS3, WR1, WR2, WR3] = request.security(syminfo.tickerid, 'W', [dayP[1], dayS1[1], dayS2[1], dayS3[1], dayR1[1], dayR2[1], dayR3[1]], barmerge.gaps_off, barmerge.lookahead_on)

// Monthly Pivot + SR
[MP, MS1, MS2, MS3, MR1, MR2, MR3] = request.security(syminfo.tickerid, 'M', [dayP[1], dayS1[1], dayS2[1], dayS3[1], dayR1[1], dayR2[1], dayR3[1]], barmerge.gaps_off, barmerge.lookahead_on)

getRoundUp() =>
    _round = math.round(dayO/roundNo)*roundNo
    _roundUp_1 = _round + roundNo
    _roundUp_2 = _roundUp_1 + roundNo
    _roundDwn_1 = _round - roundNo
    _roundDwn_2 = _roundDwn_1 - roundNo
    [_round, _roundUp_1, _roundUp_2, _roundDwn_1, _roundDwn_2]


// ********** PREVIOUS DAY ********** //
plotLinesAndLabels(pDayO, 'PDO', grayColor, start, end)
plotLinesAndLabels(pDayH, 'PDH', grayColor, start, end)
plotLinesAndLabels(pDayL, 'PDL', grayColor, start, end)
plotLinesAndLabels(pDayC, 'PDC', grayColor, start, end)

// ********** TODAY OPEN ********** //
plotLinesAndLabels(dayO,  'DO', doColor, start, end)


// ********** 1st CANDLE HIGH LOW ********** //
if (session.ismarket) and not session.ismarket[1]
    plotLinesAndLabels(high,  '1stH', pdHLColor, start, end)
    plotLinesAndLabels(low,  '1stL', pdHLColor, start, end)
    

// ********** TODAYS CPR + SR ********** //
plotLinesAndLabels(dayTC,    'TC', cprColor, start, end)
plotLinesAndLabels(dayP,     'P',  cprColor, start, end)
plotLinesAndLabels(dayBC,    'BC', cprColor, start, end)
plotLinesAndLabels(dayS1,    'S1', sColor, start, end)
plotLinesAndLabels(dayS2,    'S2', sColor, start, end)
plotLinesAndLabels(dayS3,    'S3', sColor, start, end)
plotLinesAndLabels(dayR1,    'R1', rColor, start, end)
plotLinesAndLabels(dayR2,    'R2', rColor, start, end)
plotLinesAndLabels(dayR3,    'R3', rColor, start, end)

__p_label = label.new(end, dayP, text='‏  ‏  ‏  ‏  ‏'+'P', xloc=xloc.bar_time, textcolor=labelColor, style=label.style_label_center, color=#00000000)
label.delete(__p_label[1])
__tc_label = label.new(end, dayTC, text='‏  ‏  ‏  ‏  ‏'+'TC', xloc=xloc.bar_time, textcolor=labelColor, style=label.style_label_center, color=#00000000)
label.delete(__tc_label[1])
__bc_label = label.new(end, dayBC, text='‏  ‏  ‏  ‏  ‏'+'BC', xloc=xloc.bar_time, textcolor=labelColor, style=label.style_label_center, color=#00000000)
label.delete(__bc_label[1])


// ********** Weekly CPR + SR ********** //
plotLinesAndLabels(WP,  'WP',  cprColor, start, end)
plotLinesAndLabels(WS1, 'WS1', sColor, start, end)
plotLinesAndLabels(WS2, 'WS2', sColor, start, end)
plotLinesAndLabels(WS3, 'WS3', sColor, start, end)
plotLinesAndLabels(WR1, 'WR1', rColor, start, end)
plotLinesAndLabels(WR2, 'WR2', rColor, start, end)
plotLinesAndLabels(WR3, 'WR3', rColor, start, end)

// ********** Monthly CPR + SR ********** //
plotLinesAndLabels(MP,  'MP',  cprColor, start, end)
plotLinesAndLabels(MS1, 'MS1', sColor, start, end)
plotLinesAndLabels(MS2, 'MS2', sColor, start, end)
plotLinesAndLabels(MS3, 'MS3', sColor, start, end)
plotLinesAndLabels(MR1, 'MR1', rColor, start, end)
plotLinesAndLabels(MR2, 'MR2', rColor, start, end)
plotLinesAndLabels(MR3, 'MR3', rColor, start, end)

// ********** TOMORROWS CPR + SR ********** //
if showTomorrowCPR
    plotLinesAndLabels(tomP,     'P',  cprColor, tom_start, tom_end)
    plotLinesAndLabels(tomTC,    'TC', cprColor, tom_start, tom_end)
    plotLinesAndLabels(tomBC,    'BC', cprColor, tom_start, tom_end)
    plotLinesAndLabels(tomS1,    'S1', sColor,   tom_start, tom_end)
    plotLinesAndLabels(tomS2,    'S2', sColor,   tom_start, tom_end)
    plotLinesAndLabels(tomS3,    'S3', sColor,   tom_start, tom_end)
    plotLinesAndLabels(tomR1,    'R1', rColor,   tom_start, tom_end)
    plotLinesAndLabels(tomR2,    'R2', rColor,   tom_start, tom_end)
    plotLinesAndLabels(tomR3,    'R3', rColor,   tom_start, tom_end)

// ********** EMAs ********** //
plot(_wma    ? wma_out      : na, title='WMA',       color=color.new(color.blue, 0),     offset=fastEMA_offset, style=plot.style_line, linewidth=2)
plot(fastEMA ? fastEMA_out  : na, title='EMA(fast)', color=color.new(color.fuchsia, 0),    offset=fastEMA_offset, style=plot.style_line, linewidth=2)
plot(medEMA  ? medEMA_out   : na, title='EMA(med)',  color=color.new(color.green, 0),    offset=medEMA_offset,  style=plot.style_line, linewidth=2)
plot(slowEMA ? slowEMA_out  : na, title='EMA(slow)', color=color.new(color.yellow, 0),   offset=slowEMA_offset, style=plot.style_line, linewidth=2)


// ********** Buy Sell Signals ********** //
longCond = ta.crossover(fastEMA_out, medEMA_out) 
shortCond = ta.crossunder(fastEMA_out, medEMA_out)

plotshape(longCond, title='Buy Signal', text='B', textcolor=color.new(color.white, 0), style=shape.labelup, size=size.tiny, location=location.belowbar, color=color.new(color.green, 0))
plotshape(shortCond, title='Short Signal', text='S', textcolor=color.new(color.white, 0), style=shape.labeldown, size=size.tiny, location=location.abovebar, color=color.new(color.red, 0))

//strategy.entry("Long",  strategy.long, when=longCond and strategy.position_size <= 0)
//strategy.close(id="Long", when = shortCond)

//strategy.entry("Short", strategy.short, when=shortCond and strategy.position_size > 0)
//strategy.close(id="Short", when = longCond)

line hiline = na
line loline = na
float currH = 0.0
float currL = 0.0
if longCond
    currH := high
    hiline := line.new(bar_index, high, bar_index+5, high, xloc.bar_index, extend.none, color=pdHLColor, style=line.style_dotted, width=2)
    
if shortCond
    currL := low
    loline := line.new(bar_index, low, bar_index+5, low, xloc.bar_index, extend.none, color=pdHLColor, style=line.style_dotted, width=2)
    
for i = 1 to 5
    if not na(hiline[i]) and high >= high[i]
        line.delete(hiline[i])

    if not na(loline[i]) and low <= low[i]
        line.delete(loline[i]) 


// ********** Round Number Lines ********** //
if (showRoundNo)
    [_round, _roundUp_1, _roundUp_2, _roundDwn_1, _roundDwn_2] = getRoundUp()
    plotExtendedLines(_round)
    plotExtendedLines(_roundUp_1)
    plotExtendedLines(_roundUp_2)
    plotExtendedLines(_roundDwn_1)
    plotExtendedLines(_roundDwn_2)
    
    
    
////////////////////////
//   Historical CPR   //     
//////////////////////
lStyle = plot.style_circles
p_r3 = plot(showHistoricalCPR ? dayR3 : na, color=rColor, style=lStyle, title="R3")
p_r2 = plot(showHistoricalCPR ? dayR2 : na, color=rColor, style=lStyle, linewidth=1, title="R2")
p_r1 = plot(showHistoricalCPR ? dayR1 : na, color=rColor, style=lStyle, linewidth=1, title="R1")

p_cprTC = plot(showHistoricalCPR ? dayTC : na, color=cprColor, style=lStyle, linewidth=1, title="TC")
p_cprP = plot(showHistoricalCPR  ? dayP  : na, color=cprColor, style=lStyle, linewidth=1, title="P")
p_cprBC = plot(showHistoricalCPR ? dayBC : na, color=cprColor, style=lStyle, linewidth=1, title="BC")

s1 = plot(showHistoricalCPR ? dayS1 : na, color=sColor, style=lStyle, linewidth=1, title="S1")
s2 = plot(showHistoricalCPR ? dayS2 : na, color=sColor, style=lStyle, linewidth=1, title="S2")
s3 = plot(showHistoricalCPR ? dayS3 : na, color=sColor, style=lStyle, linewidth=1, title="S3")

fill(p_cprTC, p_cprBC, color=color.new(pdHLColor,80))



// ********** Camarilla SR ********** //
line.new(start, camR4, time, camR4, xloc.bar_time, color=pdHLColor, style=line.style_dashed, width=1)
line.new(start, camR3, time, camR3, xloc.bar_time, color=pdHLColor, style=line.style_dashed, width=1)
line.new(start, camR2, time, camR2, xloc.bar_time, color=pdHLColor, style=line.style_dashed, width=1)
line.new(start, camR1, time, camR1, xloc.bar_time, color=pdHLColor, style=line.style_dashed, width=1)

line.new(start, camS1, time, camS1, xloc.bar_time, color=pdHLColor, style=line.style_dashed, width=1)
line.new(start, camS2, time, camS2, xloc.bar_time, color=pdHLColor, style=line.style_dashed, width=1)
line.new(start, camS3, time, camS3, xloc.bar_time, color=pdHLColor, style=line.style_dashed, width=1)
line.new(start, camS4, time, camS4, xloc.bar_time, color=pdHLColor, style=line.style_dashed, width=1)


//Candle Stick Patterns

DJ1 = math.abs(open - close) < (high - low) * 0.1 and high - low > ta.atr(14)
plotshape(candlestickPattern? DJ1 : na, title='Doji', text='D', location=location.abovebar, color=color.new(color.blue, 0), style=shape.xcross)

OR1 = open[1] > close[1] and open < close and low[1] > low and close > high[1] and high - low > ta.atr(14) * 1.25  // or close[1] > open
plotshape(candlestickPattern? OR1 : na, title='Bullish Engulfing', text='BE', style=shape.labelup, color=color.new(color.black, 0), location=location.belowbar, textcolor=color.new(color.white, 0))

OR2 = open[1] < close[1] and open > close and high[1] < high and close < low[1] and high - low > ta.atr(14) * 1.25  // or close[1] < open
plotshape(candlestickPattern? OR2 : na, title='Bearish Engulfing', text='BE', style=shape.labeldown, color=color.new(color.black, 0), textcolor=color.new(color.white, 0))

WR11 = low < low[1] and math.abs(low - math.min(open, close)) > math.abs(open - close) * 2 and math.abs(high - close) < (high - low) * 0.35 and high - low > ta.atr(14)
plotshape(candlestickPattern? WR11 : na, title='Hammer', text='H', location=location.belowbar, color=color.new(color.black, 0), style=shape.labelup, textcolor=color.new(color.white, 0))

WR21 = high > high[1] and high - math.max(open, close) > math.abs(open - close) * 2 and math.abs(close - low) < (high - low) * 0.35 and high - low > ta.atr(14)
plotshape(candlestickPattern? WR21 : na, title='Shooting Star', text='SS', color=color.new(color.black, 0), style=shape.labeldown, textcolor=color.new(color.white, 0))

ER1 = high[1] - low[1] > ta.atr(14) * 2 and math.abs(open[1] - close[1]) > (high[1] - low[1]) * 0.5 and open[1] > close[1] and open < close  //and abs(open[1] - close[1]) < (high[1]-low[1]) * 0.85
plotshape(candlestickPattern? ER1 : na, title='Bullish E.Reversal', text='BR', location=location.belowbar, color=color.new(color.black, 0), style=shape.labelup, textcolor=color.new(color.white, 0))  // E denotes Extreme

ER2 = high[1] - low[1] > ta.atr(14) * 2 and math.abs(open[1] - close[1]) > (high[1] - low[1]) * 0.5 and open[1] < close[1] and open > close  //and abs(open[1] - close[1]) < (high[1]-low[1]) * 0.85
plotshape(candlestickPattern? ER2 : na, title='Bearish E.Reversal', text='BR', location=location.abovebar, color=color.new(color.black, 0), style=shape.labeldown, textcolor=color.new(color.white, 0))


bool bullReversal = fastEMA_out > high and fastEMA_out < medEMA_out and (fastEMA_out - high >= 2)
plotshape(bullReversal and showReversal, title='Bullish Reversal', text='R', textcolor=color.new(color.white, 0), style=shape.labelup, size=size.tiny, location=location.belowbar, color=color.new(color.purple, 0))

bool bearReversal = fastEMA_out < low and fastEMA_out > medEMA_out and (low - fastEMA_out >= 2)
plotshape(bearReversal and showReversal, title='Bearish Reversal', text='R', textcolor=color.new(color.white, 0), style=shape.labeldown, size=size.tiny, location=location.abovebar, color=color.new(color.purple, 0))

