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

//@version=5
// Created by Brett. Credit goes to Ripster for the original creation of the clouds. 
indicator("HDT Clouds","HDT Clouds", true)

matype = input.string(title="MA Type", defval="EMA", options=["EMA", "SMA"])

ma_len1 = input(title="Short EMA1 Length", defval=8)
ma_len2 = input(title="Long EMA1 Length", defval=9)
ma_len3 = input(title="Short EMA2 Length", defval=5)
ma_len4 = input(title="Long EMA2 Length",  defval=13)
ma_len5 = input(title="Short EMA3 Length",  defval=34)
ma_len6 = input(title="Long EMA3 Length",  defval=50)
ma_len7 = input(title="Short EMA4 Length",  defval=72)
ma_len8 = input(title="Long EMA4 Length",  defval=89)
ma_len9 = input(title="Short EMA5 Length",  defval=180)
ma_len10 = input(title="Long EMA5 Length", defval=200)

src = input(title="Source", defval=hl2)
ma_offset = input(title="Offset", defval=0)
//res = input(title="Resolution", type=resolution, defval="240")

f_ma(malen) =>
    float result = 0
    if (matype == "EMA")
        result := ta.ema(src, malen)
    if (matype == "SMA")
        result := ta.sma(src, malen)
    result
    
htf_ma1 = f_ma(ma_len1)
htf_ma2 = f_ma(ma_len2)
htf_ma3 = f_ma(ma_len3)
htf_ma4 = f_ma(ma_len4)
htf_ma5 = f_ma(ma_len5)
htf_ma6 = f_ma(ma_len6)
htf_ma7 = f_ma(ma_len7)
htf_ma8 = f_ma(ma_len8)
htf_ma9 = f_ma(ma_len9)
htf_ma10 = f_ma(ma_len10)

//plot(out1, color=green, offset=ma_offset)
//plot(out2, color=red, offset=ma_offset)

//lengthshort = input(8, minval = 1, title = "Short EMA Length")
//lengthlong = input(200, minval = 2, title = "Long EMA Length")
//emacloudleading = input(50, minval = 0, title = "Leading Period For EMA Cloud")
//src = input(hl2, title = "Source")
showlong = input(false, title="Show Long Alerts")
showshort = input(false, title="Show Short Alerts")
showLine = input(false, title="Display EMA Line")
ema1 = input(true, title="Show EMA Cloud-1")
ema2 = input(true, title="Show EMA Cloud-2")
ema3 = input(true, title="Show EMA Cloud-3")
ema4 = input(true, title="Show EMA Cloud-4")
ema5 = input(true, title="Show EMA Cloud-5")

emacloudleading = input.int(0, minval=0, title="Leading Period For EMA Cloud")
mashort1 = htf_ma1
malong1 = htf_ma2
mashort2 = htf_ma3
malong2 = htf_ma4
mashort3 = htf_ma5
malong3 = htf_ma6
mashort4 = htf_ma7
malong4 = htf_ma8
mashort5 = htf_ma9
malong5 = htf_ma10

cloudcolour1 = mashort1 >= malong1 ? #036103 : #880e4f
cloudcolour2 = mashort2 >= malong2 ? #4caf50 : #f44336
cloudcolour3 = mashort3 >= malong3 ? #2196f3 : #ffb74d
cloudcolour4 = mashort4 >= malong4 ? #009688 : #f06292
cloudcolour5 = mashort5 >= malong5 ? #05bed5 : #e65100
//03abc1

mashortcolor1 = mashort1 >= mashort1[1] ? color.olive : color.maroon
mashortcolor2 = mashort2 >= mashort2[1] ? color.olive : color.maroon
mashortcolor3 = mashort3 >= mashort3[1] ? color.olive : color.maroon
mashortcolor4 = mashort4 >= mashort4[1] ? color.olive : color.maroon
mashortcolor5 = mashort5 >= mashort5[1] ? color.olive : color.maroon


mashortline1 = plot(ema1 ? mashort1 : na, color=showLine ? mashortcolor1 : na, linewidth=1, offset=emacloudleading, title="Short Leading EMA1")
mashortline2 = plot(ema2 ? mashort2 : na, color=showLine ? mashortcolor2 : na, linewidth=1, offset=emacloudleading, title="Short Leading EMA2")
mashortline3 = plot(ema3 ?mashort3 : na, color=showLine ? mashortcolor3 : na, linewidth=1, offset=emacloudleading, title="Short Leading EMA3")
mashortline4 = plot(ema4 ? mashort4 :na , color=showLine ? mashortcolor4 : na, linewidth=1, offset=emacloudleading, title="Short Leading EMA4")
mashortline5 = plot(ema5 ? mashort5 : na, color=showLine ? mashortcolor5 : na, linewidth=1, offset=emacloudleading, title="Short Leading EMA5")

malongcolor1 = malong1 >= malong1[1] ? color.green : color.red
malongcolor2 = malong2 >= malong2[1] ? color.green : color.red
malongcolor3 = malong3 >= malong3[1] ? color.green : color.red
malongcolor4 = malong4 >= malong4[1] ? color.green : color.red
malongcolor5 = malong5 >= malong5[1] ? color.green : color.red

malongline1 = plot(ema1 ? malong1 : na, color=showLine ? malongcolor1 : na, linewidth=3, offset=emacloudleading, title="Long Leading EMA1")
malongline2 = plot(ema2 ? malong2 : na, color=showLine ? malongcolor2 : na, linewidth=3, offset=emacloudleading, title="Long Leading EMA2")
malongline3 = plot(ema3 ? malong3 : na, color=showLine ? malongcolor3 : na, linewidth=3, offset=emacloudleading, title="Long Leading EMA3")
malongline4 = plot(ema4 ? malong4 : na, color=showLine ? malongcolor4 : na, linewidth=3, offset=emacloudleading, title="Long Leading EMA4")
malongline5 = plot(ema5 ? malong5 : na, color=showLine ? malongcolor5 : na, linewidth=3, offset=emacloudleading, title="Long Leading EMA5")

emaBand = input.int(200, title="EMA cloud band")
smaBand = input.int(200, title="SMA cloud band")
showSmaAndEmaCloud = input(false, title="Show EMA/MA cloud with above values")
emaBandColor = #EE0000
smaBandColor = #00EE00
emaAndSmaCloudColor = #b5b5f5
emaBandPlot = plot(showSmaAndEmaCloud ? ta.ema(src, emaBand) : na, color=emaBandColor, offset=emacloudleading, title="EMA cloud band length")
smaBandPlot = plot(showSmaAndEmaCloud ? ta.sma(src, smaBand) : na, color=smaBandColor, offset=emacloudleading, title="SMA cloud band length")
fill(emaBandPlot, smaBandPlot, color=emaAndSmaCloudColor, transp=23, title="EMA and SMA Cloud")

fill(mashortline1, malongline1, color=cloudcolour1, transp=45, title="MA Cloud1")
fill(mashortline2, malongline2, color=cloudcolour2, transp=65, title="MA Cloud2")
fill(mashortline3, malongline3, color=cloudcolour3, transp=70, title="MA Cloud3")
fill(mashortline4, malongline4, color=cloudcolour4, transp=65, title="MA Cloud4")
fill(mashortline5, malongline5, color=cloudcolour5, transp=65, title="MA Cloud5")

var cumVol = 0.
cumVol += nz(volume)
if barstate.islast and cumVol == 0
    runtime.error("No volume is provided by the data vendor.")

computeVWAP(src, isNewPeriod) =>
	var float sumSrcVol = na
	var float sumVol = na
    var float sumSrcSrcVol = na

	sumSrcVol := isNewPeriod ? src * volume : src * volume + sumSrcVol[1]
	sumVol := isNewPeriod ? volume : volume + sumVol[1]
	// sumSrcSrcVol calculates the dividend of the equation that is later used to calculate the standard deviation
	sumSrcSrcVol := isNewPeriod ? volume * math.pow(src, 2) : volume * math.pow(src, 2) + sumSrcSrcVol[1]

	_vwap = sumSrcVol / sumVol
	variance = sumSrcSrcVol / sumVol - math.pow(_vwap, 2)
	variance := variance < 0 ? 0 : variance
	stDev = math.sqrt(variance)

	[_vwap, stDev]

computeStdevBands(value, stdev, bandMult) =>
    float upperBand = value + stdev * bandMult
    float lowerBand = value - stdev * bandMult
    [upperBand, lowerBand]

hideonDWM = input(false, title="Hide VWAP on 1D or Above", group="VWAP Settings")
var anchor = input.string(defval = "Session", title="Anchor Period",
 options=["Session", "Week", "Month", "Quarter", "Year", "Decade", "Century", "Earnings", "Dividends", "Splits"], group="VWAP Settings")
srcVmap = input(title = "Source", defval = hlc3, group="VWAP Settings")
offset = input(0, title="Offset", group="VWAP Settings")

showBand_1 = input(true, title="", group="Standard Deviation Bands Settings", inline="band_1")
stdevMult_1 = input(1.0, title="Bands Multiplier #1", group="Standard Deviation Bands Settings", inline="band_1")
showBand_2 = input(false, title="", group="Standard Deviation Bands Settings", inline="band_2")
stdevMult_2 = input(2.0, title="Bands Multiplier #2", group="Standard Deviation Bands Settings", inline="band_2")
showBand_3 = input(false, title="", group="Standard Deviation Bands Settings", inline="band_3")
stdevMult_3 = input(3.0, title="Bands Multiplier #3", group="Standard Deviation Bands Settings", inline="band_3")

timeChange(period) =>
	ta.change(time(period))

new_earnings = request.earnings(syminfo.tickerid, earnings.actual, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
new_dividends = request.dividends(syminfo.tickerid, dividends.gross, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
new_split = request.splits(syminfo.tickerid, splits.denominator, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)

isNewPeriod = switch anchor
	"Earnings" => not na(new_earnings)
	"Dividends" => not na(new_dividends)
	"Splits" => not na(new_split)
	"Session" => timeChange("D")
	"Week" => timeChange("W")
	"Month" => timeChange("M")
	"Quarter" => timeChange("3M")
	"Year" => timeChange("12M")
	"Decade" => timeChange("12M") and year % 10 == 0
	"Century" => timeChange("12M") and year % 100 == 0
	=> false

isEsdAnchor = anchor == "Earnings" or anchor == "Dividends" or anchor == "Splits"
if na(srcVmap[1]) and not isEsdAnchor
	isNewPeriod := true

float vwapValue = na
float stdev = na
float upperBandValue1 = na
float lowerBandValue1 = na
float upperBandValue2 = na
float lowerBandValue2 = na
float upperBandValue3 = na
float lowerBandValue3 = na

if not (hideonDWM and timeframe.isdwm)
    [_vwap, _stdev] = computeVWAP(srcVmap, isNewPeriod)
    vwapValue := _vwap
    stdev := _stdev
	[upBV1, loBV1] = computeStdevBands(vwapValue, stdev, stdevMult_1)
	upperBandValue1 := showBand_1 ? upBV1 : na
	lowerBandValue1 := showBand_1 ? loBV1 : na
	[upBV2, loBV2] = computeStdevBands(vwapValue, stdev, stdevMult_2)
	upperBandValue2 := showBand_2 ? upBV2 : na
	lowerBandValue2 := showBand_2 ? loBV2 : na
	[upBV3, loBV3] = computeStdevBands(vwapValue, stdev, stdevMult_3)
	upperBandValue3 := showBand_3 ? upBV3 : na
	lowerBandValue3 := showBand_3 ? loBV3 : na

plot(vwapValue, title="VWAP", color=#2962FF, offset=offset)

ma200Show = input(false, title="Show the 200MA line")
ma100Show = input(false, title="Show the 100MA line")

plot(ma200Show ? ta.sma(src, 200) : na, color=color.red, linewidth=1, title="MA 200")
plot(ma100Show ? ta.sma(src, 100) : na, color=color.green, linewidth=1, title="MA 100")