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

//Sourced code from DepthHouse Trading Indicators by oh92 for Bull\Bear volume flow calculations. 

// This indicator integrates the Ma-over-MA crossover strategy in oh92's DepthHouse calculation with a volume-over-MA
//  calculation to further narrow down "Areas of Interest" levels for a potential re-test zone to the right of the chart.
// Added a Moving Average calculation for a multi-level cloud. Broke down more conditions to highlight both 
// volume flow crossover on the High and Extreme High MA and also higher than set average and extreme high volume spikes 
// without bull\bear conditions. Candle color changes for volume spikes. 
// Session backgrounds set for research purposes. 

//@version=5
indicator("Volume Analysis", shorttitle="VAS", format=format.volume)

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Session BG Shading

t = timestamp("01 Jan 2022 00:00")
OpenSession =  input.session(title="Open",  defval="0830-0845", tooltip="US(NYSE) Open",  group="Highlight Session Background", inline="open")
CloseSession = input.session(title="Close", defval="1445-1500", tooltip="US(NYSE) Close", group="Highlight Session Background", inline="close")

Show_Open_Session =  input.bool(title="Show Open Session?",  defval=true, group="Highlight Session Background", inline="show") // show open session ?
Show_Close_Session = input.bool(title="Show Close Session?", defval=false, group="Highlight Session Background", inline="show") // show close session ?

inSession(session, sessionTimeZone=syminfo.timezone) => 
    na(time(timeframe.period, session + ":1234567")) == false 

// Background plot
OpenColor =  input.color(color.rgb(153,153,153,95), title="Open",  group="Highlight Session Background", inline="open")
CloseColor = input.color(color.rgb(153,153,153,90), title="Close", group="Highlight Session Background", inline="close" )

bgcolor(inSession(OpenSession)  and Show_Open_Session  ? OpenColor  : na, editable=false) // Open
bgcolor(inSession(CloseSession) and Show_Close_Session ? CloseColor : na, editable=false) // Close

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Volume Flow Colors
// bias bull/bear colors
vf_color_bull     = color(color.rgb(  0, 176,151,00))
vf_color_bear     = color(color.rgb(102, 56, 150,00))
vf_ext_color_bull = color(color.rgb(  0, 245,245,00))
vf_ext_color_bear = color(color.rgb(182, 56, 231,00))

// non-bias high and extreme volume color
vol_ext_color = color(color.rgb(255,255,255,00))
vol_high_color = color(color.rgb(245,245,245,40))

// Cloud color fill color
fill_color1  = color(color.rgb( 76,204, 171,85))
fill_color2  = color(color.rgb(178,102,255,90))
fill_color3  = color(color.rgb(102,  0,204,90))

// Inputs for Volume Flow
vf_maType = input.string(title='Moving Average Type', options=['Simple', 'Exponential', 'Double Exponential'], defval='Simple', group="Volume Flow Settings")
vf_length = input(14, title='MA Length', group="Volume Flow Settings")
x         = input(3.1, title='Factor For Breakout Candle', group="Volume Flow Settings")

// Inputs for Cloud MA's
Avg_Type = input.string("SMA", title="Average Type", options=["SMA", "EMA", "HMA", "None"], tooltip="Selecting None removes cloud and all related High Volume Spikes from indicator", group="Volume MA Cloud Settings") 
Avg_Length = input(14, title="Average Length", group="Volume MA Cloud Settings")
ma_mult = input(3,title="Multiplier", tooltip="Multiplier to set Moving Average levels. ", group="Volume MA Cloud Settings")
Display_Volume = input(true,title="Show All Volume")

// Var Settings
vol = volume

bull = close > open ? vol : 0
bear = open > close ? vol : 0

Close = (close)
Open = (open)

// Moving Average Calcs
Avg  =  (Avg_Type == "SMA" ? ta.sma(volume, Avg_Length) : Avg_Type == "EMA" ? ta.ema(volume, Avg_Length) : 
     Avg_Type == "HMA" ? ta.hma(volume, Avg_Length)   : na)
     
Ma_mult_high = (Avg*ma_mult) //ma1
Ma_mult_mid  = (Ma_mult_high/2) //ma2
Ma_mult_low  = (Avg/2)  //ma3

// Double EMA Function 
dema(src, len) =>
    2 * ta.ema(src, len) - ta.ema(ta.ema(src, len), len)

// Bull Moving Average Calculation
bullma = vf_maType == 'Exponential' ? ta.ema(bull, vf_length) : vf_maType == 'Double Exponential' ? dema(bull, vf_length) : ta.sma(bull, vf_length)

// Bear Moving Average Calculation //
bearma = vf_maType == 'Exponential' ? ta.ema(bear, vf_length) : vf_maType == 'Double Exponential' ? dema(bear, vf_length) : ta.sma(bear, vf_length)


// Volume Spikes //
vol_ext = ta.crossover(volume, Ma_mult_high) ? vol : na
vol_high = ta.crossover(volume, Ma_mult_mid) ? vol : na
gsig = ta.crossover(bull, bullma * x) ? vol : na
rsig = ta.crossover(bear, bearma * x) ? vol : na
gsig_high = ta.crossover(bull, bullma * x) and vol_high ? vol : na
rsig_high = ta.crossover(bear, bearma * x) and vol_high ? vol : na
gsig_ext = ta.crossover(bull, bullma * x) and vol_ext ? vol : na
rsig_ext = ta.crossover(bear, bearma * x) and vol_ext ? vol : na


// Color Calcs //
vf_dif = bullma - bearma
vdClr = vf_dif > 0 ?  vf_color_bull :  vf_color_bear
vClr = close > open ? vf_color_bull : vf_color_bear
//volume up/down color calc
Volume_Color = Close > Open ? color(color.rgb(224,224,224,90)) : Close < Open ? color(color.rgb(96,96,96,90)) : na 


// PLOTS // 
//volume
plot(Display_Volume ? volume : na,  title="Volume", color=Volume_Color,  style=plot.style_histogram,  linewidth=3)

// Moving average lines
plot_avg     = plot(Avg,          title="Average", color=color(color.rgb(96,96,96,80)), style=plot.style_line,  linewidth=1) //plot ma1
plot_ma_high = plot(Ma_mult_high, title="High",    color=color(color.rgb(96,96,96,80)), style=plot.style_line,  linewidth=1) //plot ma2
plot_ma_mid  = plot(Ma_mult_mid,  title="Mid",     color=color(color.rgb(96,96,96,80)), style=plot.style_line,  linewidth=1) //plot ma4
plot_ma_low  = plot(Ma_mult_low,  title="Low",     color=color(color.rgb(96,96,96,80)), style=plot.style_line,  linewidth=1) //plot ma3

// Moving average cloud
fill(plot_ma_mid,plot_ma_high, title="Upper Cloud",  color=fill_color1)
fill(plot_avg,plot_ma_mid,     title="Middle Cloud", color=fill_color2)
fill(plot_ma_low,plot_avg,     title="Lower Cloud",  color=fill_color3)

// Volume spike without directional bias
plot(vol_ext,  style=plot.style_histogram, linewidth=4, color=(vol_ext_color), title='Extreme Volume Spike', display=display.pane)
plot(vol_high, style=plot.style_histogram, linewidth=4, color=(vol_high_color), title='High Volume Spike', display=display.pane)

// Volume spikes with bull/bear bias
plot(gsig,      style=plot.style_histogram, linewidth=4, color=(vf_color_bull),     title='Bull Vol Spike', display=display.pane) 
plot(rsig,      style=plot.style_histogram, linewidth=4, color=(vf_color_bear),     title='Bear Vol Spike', display=display.pane)
plot(gsig_high, style=plot.style_histogram, linewidth=4, color=(vf_color_bull),     title='High Bull Vol Spike', display=display.pane) 
plot(rsig_high, style=plot.style_histogram, linewidth=4, color=(vf_color_bear),     title='High Bear Vol Spike', display=display.pane)
plot(gsig_ext,  style=plot.style_histogram, linewidth=4, color=(vf_ext_color_bull), title='Extreme Bull Vol Spike', display=display.pane) 
plot(rsig_ext,  style=plot.style_histogram, linewidth=4, color=(vf_ext_color_bear), title='Extreme Bear Vol Spike', display=display.pane)


// Color candles based on volume conditions
barcolor(gsig ? vf_color_bull: rsig ? vf_color_bear: gsig_high ? vf_color_bull : rsig_high ? vf_color_bear : gsig_ext ? vf_ext_color_bull : rsig_ext ? vf_ext_color_bear :  na)
barcolor(vol_high ? vol_high_color : vol_ext ? vol_ext_color : na)
