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

//@version=5

indicator(title='', shorttitle='SD Levels', overlay=true)

//Inputs
src = input(ohlc4, title='Levels source')
stddev1 = input(true, title='Standard Deviation Levels')
sHiLo = input(false, title='Previous Day\'s High Low Levels')
sHiLoNow = input(false, title='Current Day\'s Running High Low Levels')

m=color.gray
n=66
//Emas
out0 = ta.ema(close, 25 )
out1 = ta.ema(close, 100 )
out2 = ta.ema(close, 400 )
plot(out0, color=m, transp=n)
plot(out1, color=m, transp=n)
plot(out2, color=m, transp=n)

//VWAP
timeflag = timeframe.period == '15S' or timeframe.period == '1' or timeframe.period == '3' or timeframe.period == '5' or timeframe.period == '15' or timeframe.period == '60' or timeframe.period == '240' or timeframe.period == 'D' ? true : false
src1 = hlc3
t = time('D')
start0 = na(t[1]) or t > t[1]
sumSrc = src1 * volume
sumVol = volume
sumSrc := start0 ? sumSrc : sumSrc + sumSrc[1]
sumVol := start0 ? sumVol : sumVol + sumVol[1]
Value = timeflag ? sumSrc / sumVol : na
plot(Value, title="Vwap", style=plot.style_circles, linewidth=1, color=m, transp=n)

//SD Levels
sLength = timeframe.isintraday ? 1 : 1
nstart = request.security(syminfo.tickerid, 'D', bar_index, barmerge.gaps_off, barmerge.lookahead_on)
start = request.security(syminfo.tickerid, 'D', time, barmerge.gaps_off, barmerge.lookahead_on)
first = request.security(syminfo.tickerid, 'D', ta.valuewhen(barstate.islast, time, 0), barmerge.gaps_off, barmerge.lookahead_on)

nohist = nstart <= math.max(sLength, 20) + 1
change_1 = ta.change(start)
newDay = nohist ? false : timeframe.isintraday ? change_1 : dayofmonth(time) < dayofmonth(time[1])


//Annualised Volatility
hv = 0.0
stdev_1 = ta.stdev(math.log(src / src[1]), 20)
security_1 = request.security(syminfo.tickerid, 'D', stdev_1 * math.sqrt(250), barmerge.gaps_off, barmerge.lookahead_on)
stdev_2 = ta.stdev(math.log(src / src[1]), 20)
hv_ = true ? security_1 : stdev_2 * math.sqrt(250)
hv := newDay ? hv_ : nz(hv[1], hv_)
hinow = high
hinow := newDay ? high : high > hinow[1] ? high : hinow[1]
lonow = low
lonow := newDay ? low : low < lonow[1] ? low : lonow[1]

prevhi = ta.valuewhen(newDay, hinow[1], 0)
prevlo = ta.valuewhen(newDay, lonow[1], 0)

//Daily Settlement 
valuewhen_1 = ta.valuewhen(start[1] <= time, src, 0)
vwma_1 = ta.vwma(src[1], sLength)
settlement = sLength == 1 ? valuewhen_1 : vwma_1
settlement := newDay ? settlement : nz(settlement[1], open[1])

firstDay = dayofmonth(start) == dayofmonth(first) and month(start) == month(first) and year(start) == year(first)
stdhv = 0.0
DaystoExpire = 0 == 0 ? timeframe.isintraday ? true ? 1 : math.min(250, 1440 / timeframe.multiplier) : 20 : 0
stdhv := newDay ? settlement * hv * math.sqrt(DaystoExpire / 250) : nz(stdhv[1])

// SD Ratios.
stdhv05 = stdhv * 0.33
stdhv07 = stdhv * 0.66
stdhv1 = stdhv

// Plot
SettleM = plot(not nohist and stddev1 and (not false or firstDay) and not newDay ? settlement : na, color=color.new(color.gray, 40), title='Settlement', linewidth=2, style=plot.style_linebr)
Stdhv05u = plot(not nohist and stddev1 and (not false or firstDay) and not newDay ? settlement + stdhv05 : na, color=color.new(color.red, 20), title='+0.33 SD', linewidth=1, style=plot.style_linebr)
Stdhv05d = plot(not nohist and stddev1 and (not false or firstDay) and not newDay ? settlement - stdhv05 : na, color=color.new(color.red, 20), title='-0.33 SD', linewidth=1, style=plot.style_linebr)
Stdhv07u = plot(not nohist and stddev1 and (not false or firstDay) and not newDay ? settlement + stdhv07 : na, color=color.new(color.lime, 20), title='+0.66 SD', linewidth=1, style=plot.style_linebr)
Stdhv07d = plot(not nohist and stddev1 and (not false or firstDay) and not newDay ? settlement - stdhv07 : na, color=color.new(color.lime, 20), title='-0.66 SD', linewidth=1, style=plot.style_linebr)
Stdhv1u = plot(not nohist and stddev1 and (not false or firstDay) and not newDay ? settlement + stdhv1 : na, color=color.new(color.orange, 20), title='+1 SD', linewidth=1, style=plot.style_linebr)
Stdhv1d = plot(not nohist and stddev1 and (not false or firstDay) and not newDay ? settlement - stdhv1 : na, color=color.new(color.orange, 20), title='-1 SD', linewidth=1, style=plot.style_linebr)
//
plot((not false or firstDay) and not nohist and sHiLo and not newDay ? prevhi : na, color=color.new(color.teal, 20), title='PREV HI', linewidth=1, style=plot.style_cross, join=false)
plot((not false or firstDay) and not nohist and sHiLo and not newDay ? prevlo : na, color=color.new(color.teal, 20), title='PREV LO', linewidth=1, style=plot.style_cross, join=false)
plot((not false or firstDay) and not nohist and sHiLoNow and not newDay ? lonow : na, color=color.new(color.blue, 20), title='D Lo', linewidth=1, trackprice=false, offset=0, style=plot.style_circles, join=false)
plot((not false or firstDay) and not nohist and sHiLoNow and not newDay ? hinow : na, color=color.new(color.blue, 20), title='D Hi', linewidth=1, trackprice=false, offset=0, style=plot.style_circles, join=false)


