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

//@version=4
study("Volume Pressure Analysis", shorttitle = "VPA")
theme = input("RVOL Analysis", "Theme:", options=["Pressure Analysis","Support vs Resistance", "Directional Pressure", "RVOL Analysis", "RVOL"], group = "Main Settings", inline = "main")
widget = input("None", "Widget:", options=["None","Support vs Resistance", "Directional Pressure", "Support vs Resistance + Directional Pressure"], group = "Main Settings", inline = "main")
show = input(true, "Show Pressure Signals")
showentry = input(true, "Show Entry Signals?",group = "Entry Signals")
showvol = input(true, "Show Current Volume?",group = "General Settings")
showavg = input(true, title = "Show Average",group = "General Settings")
showrvol = input(true, "Show RVOL Calculation?",group = "General Settings")
showpercent = input(true, "Show Pressure Direction Percentage?", group = "General Settings")
showlsma21 = input(true,"Show LSMA 21 Entry Points", group = "Entry Signals")
showlsma6 = input(true,"Show LSMA 6 Entry Points",group = "Entry Signals")
showticker = input(false, "Show Ticker ID?",group = "General Settings")
showvola = input(false, "Show Relative Volatility? (RVOL Analysis theme only")
showlastvola = input(false, "Show Relative Volatility? (Widget only")
showlastsma = input(true, "Show SMA Line? (Widget only")
offset1 = 11

if theme == "Directional Pressure"
    show := false
if theme == "Pressure Analysis"
    show := false
if widget == "Support vs Resistance + Directional Pressure"
    offset1 := 22
x = "k"
vol = volume

if volume < 1000
	x := ""
if volume >= 1000
	vol := volume / 1000
if volume >= 1000000
	x := "M"
	vol := volume / 1000000
if volume >= 1000000000
	x := "B"
	vol := volume / 1000000000

spike = close - open <= 0 ? open - close  : close - open

avglength = input(30,"Lookback bars for Average",group = "General Settings")
avg = array.new_float(0)
for i = 1 to avglength
	array.push(avg, spike[i])

rvol = spike / array.avg(avg)

avg2 = array.new_float(0)
for i = 1 to avglength
	array.push(avg2, volume[i])

rvol2 = volume / array.avg(avg2)

y = "k"
avg1 = array.avg(avg2)
avg3 = avg1
if avg1 < 1000
	y := ""
if avg1 >= 1000
	avg3 := avg1 / 1000
if avg1 >= 1000000
	y := "M"
	avg3 := avg1 / 1000000
if avg1 >= 1000000000
	y := "B"
	avg3 := avg1 / 1000000000
	
rvol3 = rvol - rvol2 > 0 ? rvol - rvol2 : na
rvol4 = rvol - rvol2 < 0 ? rvol - rvol2 : na
rvoltot = rvol - rvol2 > 0 ? rvol - rvol2 : rvol - rvol2 < 0 ? (rvol - rvol2) * -1 : na
sma = sma(rvol2,20)
sma1 = sma(rvol3, 20)
sma2 = sma(rvol4, 20)
sma3 = sma(rvoltot,20)
lsma1 = linreg(rvol2, 50,0)
ma2 = linreg(rvol2,50,0)
ma3 = linreg(rvol2,21,0)
ma4 = linreg(rvol2,6,0)
cond1 = crossover(ma2,0)
clean = rvol2 - lsma1

float(rvol3)
float(rvol4)
transp1 = 0

highest = highest(rvol2,30) / 2

plot(showvola ? rvol : na, style = plot.style_columns, color = color.yellow,title = "Volatility")
plot(showlastvola ? rvol : na, style = plot.style_columns, color = color.yellow,title = "Volatility", show_last = 10, offset = 11)

plot(theme == "RVOL" and rvol - rvol2 > rvol2 ? (rvol - rvol2) : na, style = plot.style_columns, color = close > open ? #80deea : #ffcdd2, transp = 0)
plot(theme == "RVOL" ? rvol2 : na, style = plot.style_columns, color = close > open ? #26a69a : color.red, transp = 60)
plot(theme == "RVOL" and rvol - rvol2 < 0 ? (rvol - rvol2) * -1 : na, style = plot.style_columns, color = close > open ? #FF5252 : #26a69a, transp = 60)

plot(theme == "RVOL" and clean > stdev(rvol2,21) or theme == "RVOL" and rvol - rvol2 > rvol2 ? rvol2 : na, style = plot.style_columns , color = close > open ? #26a69a : color.red, title = "Anomalies")
plot(theme == "RVOL" and clean > stdev(rvol2,21) and rvol - rvol2 < 0 ? (rvol - rvol2) * -1 : na, style = plot.style_columns, color = close > open ? #FF5252 : #26a69a, title = "Anomalies")

plot(theme == "RVOL" ? rvol2 : na, style = plot.style_columns, color = close > open ? #26a69a : color.red, show_last = 1)
plot(theme == "RVOL" and rvol - rvol2 < 0 ? (rvol - rvol2) * -1 : na, style = plot.style_columns, color = close > open ? #FF5252 : #26a69a, show_last = 1)
plot(theme == "RVOL" and rvol - rvol2 < rvol2 and rvol - rvol2 > 0 ? (rvol - rvol2): na, style = plot.style_columns, color = close > open ? #80deea : #ffcdd2, transp = 0, show_last = 1)
plot(theme == "RVOL" and clean > stdev(rvol2,21) and rvol - rvol2 < rvol2 and rvol - rvol2 > 0 ? (rvol - rvol2): na, style = plot.style_columns, color = close > open ? #80deea : #ffcdd2, transp = 0)

plot(theme == "RVOL Analysis" and rvol - rvol2 > rvol2 ? (rvol - rvol2) : na, style = plot.style_columns, color = close > open ? #80deea : #ffcdd2, transp = 0)

plot(theme == "RVOL Analysis" ? rvol2 : na, style = plot.style_columns, color = close > open ? #26a69a : color.red)
plot(theme == "RVOL Analysis" and rvol - rvol2 < 0 ? (rvol - rvol2) * -1 : na, style = plot.style_columns, color = close > open ? #FF5252 : #26a69a)
plot(theme == "RVOL Analysis" and rvol - rvol2 < rvol2 and rvol - rvol2 > 0 ? (rvol - rvol2): na, style = plot.style_columns, color = close > open ? #80deea : #ffcdd2, transp = 0)

plot(widget == "Support vs Resistance" and (rvol - rvol2) > 0 or widget == "Support vs Resistance + Directional Pressure" and (rvol - rvol2) > 0  ? (rvol - rvol2) : na, style = plot.style_columns, color = #26a69a, show_last = 10, offset = 11,title = "pressure")
plot(widget == "Support vs Resistance" and (rvol - rvol2) < 0 or widget == "Support vs Resistance + Directional Pressure" and (rvol - rvol2) < 0 ? (rvol - rvol2) * -1 : na, style = plot.style_columns, color = color.red, show_last = 10, offset = 11,title = "pressure")

plot(theme == "Support vs Resistance" and (rvol - rvol2) > 0 ? (rvol - rvol2) : na, style = plot.style_columns, color =  rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #4db6ac : rvol - rvol2 > 0 ? #00796b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #ef5350 : #9a2b2b,title = "SR")
plot(theme == "Support vs Resistance" and (rvol - rvol2) < 0 ? (rvol - rvol2) * -1 : na, style = plot.style_columns, color =  rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #4db6ac : rvol - rvol2 > 0 ? #00796b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #ef5350 : #9a2b2b,title = "SR")
 
plot(theme == "Directional Pressure" and close < open and (rvol - rvol2) < 0  ? (rvol - rvol2) * -1 : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #4db6ac : rvol - rvol2 > 0 ? #00796b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #4db6ac : #00796b)
plot(theme == "Directional Pressure" and close > open and (rvol - rvol2) < 0  ? rvol - rvol2 : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #ef5350 : rvol - rvol2 > 0 ? #9a2b2b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #ef5350 : #9a2b2b)
plot(theme == "Directional Pressure" and close > open and (rvol - rvol2) > 0  ? (rvol - rvol2) : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #4db6ac : rvol - rvol2 > 0 ? #00796b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #4db6ac : #00796b)
plot(theme == "Directional Pressure" and close < open and (rvol - rvol2) > 0  ? (rvol - rvol2) * -1 : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #ef5350 : rvol - rvol2 > 0 ? #9a2b2b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #ef5350 : #9a2b2b)

//plot(theme == "Directional Pressure 2" and close < open and (rvol - rvol2) < 0  ? (rvol - rvol2) * -1 : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #4db6ac : rvol - rvol2 > 0 ? #00796b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #4db6ac : #00796b)
//plot(theme == "Directional Pressure 2" and close > open and (rvol - rvol2) < 0  ? rvol - rvol2 * -1 : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #ef5350 : rvol - rvol2 > 0 ? #9a2b2b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #ef5350 : #9a2b2b)
//plot(theme == "Directional Pressure 2" and close > open and (rvol - rvol2) > 0  ? (rvol - rvol2) : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #4db6ac : rvol - rvol2 > 0 ? #00796b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #4db6ac : #00796b)
//plot(theme == "Directional Pressure 2" and close < open and (rvol - rvol2) > 0  ? (rvol - rvol2) : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #ef5350 : rvol - rvol2 > 0 ? #9a2b2b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #ef5350 : #9a2b2b)

plot(widget == "Directional Pressure" and close < open and (rvol - rvol2) < 0 or widget == "Support vs Resistance + Directional Pressure" and close < open and (rvol - rvol2) < 0  ? (rvol - rvol2) * -1 : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #4db6ac : rvol - rvol2 > 0 ? #00796b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #4db6ac : #00796b, show_last = 10, offset = offset1)
plot(widget == "Directional Pressure" and close > open and (rvol - rvol2) < 0 or widget == "Support vs Resistance + Directional Pressure" and close > open and (rvol - rvol2) < 0  ? rvol - rvol2 * -1 : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #ef5350 : rvol - rvol2 > 0 ? #9a2b2b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #ef5350 : #9a2b2b, show_last = 10, offset = offset1)
plot(widget == "Directional Pressure" and close > open and (rvol - rvol2) > 0 or widget == "Support vs Resistance + Directional Pressure" and close > open and (rvol - rvol2) > 0  ? (rvol - rvol2) : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #4db6ac : rvol - rvol2 > 0 ? #00796b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #4db6ac : #00796b, show_last = 10, offset = offset1)
plot(widget == "Directional Pressure" and close < open and (rvol - rvol2) > 0 or widget == "Support vs Resistance + Directional Pressure" and close < open and (rvol - rvol2) > 0  ? (rvol - rvol2) : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #ef5350 : rvol - rvol2 > 0 ? #9a2b2b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #ef5350 : #9a2b2b, show_last = 10, offset = offset1)

tot1 = close < open and (rvol - rvol2) < 0  ? (rvol - rvol2) * -1 : close > open and (rvol - rvol2) < 0  ? rvol - rvol2 * -1 : close > open and (rvol - rvol2) > 0  ? (rvol - rvol2) : close < open and (rvol - rvol2) > 0  ? (rvol - rvol2) : na
totup = close < open and (rvol - rvol2) < 0  ? (rvol - rvol2) * -1 :  close > open and (rvol - rvol2) > 0  ? (rvol - rvol2) : na
totdown = close > open and (rvol - rvol2) < 0  ? rvol - rvol2 * -1 : close < open and (rvol - rvol2) > 0  ? (rvol - rvol2) : na
totlength = input(10,"Lookback bars for Directional Pressure Percentage",group = "General Settings")

sumu = array.new_float(0)
for i = 0 to totlength
	array.push(sumu, totup[i])
	
sumd = array.new_float(0)
for i = 0 to totlength
	array.push(sumd, totdown[i])
	
tot = array.new_float(0)
for i = 0 to totlength
	array.push(tot, tot1[i])

sumup = array.sum(sumu) 
sumdown = array.sum(sumd) 
sum = array.sum(tot) 

percentup = (sumup / sum) * 100
percentdown = (sumdown / sum) * 100
percent = percentup > percentdown ? percentup : percentdown > percentup ? percentdown : na

plotshape(show and rvol4 < sma2 and open < close , color = color.yellow, style = shape.triangledown, location = location.bottom)
plotshape(show and rvol4 < sma2 and open > close , color = color.yellow, style = shape.triangleup, location = location.bottom)
plotshape(show and rvol3 > sma1 and open < close , color = #00796b , style = shape.triangleup, location = location.bottom)
plotshape(show and rvol3 > sma1 and open > close , color = #b71c1c , style = shape.triangledown, location = location.bottom)

plot(theme == "RVOL Analysis" or theme == "RVOL" ? sma : na,color = color.white) 

plot(widget == "Support vs Resistance + Directional Pressure" and showlastsma ? sma3 : na,color = color.white, show_last = 10, offset = 11) 

plot(theme == "Pressure Analysis" ? rvol - rvol2 : na, style = plot.style_columns, color = rvol - rvol2 > 0 and rvol - rvol2 > sma1 ? #4db6ac : rvol - rvol2 > 0 ? #00796b : rvol - rvol2 < 0 and rvol - rvol2 < sma2 ? #ef5350 : #9a2b2b)

if showlsma21 and not showlsma6
    cond1 := crossover(ma2,0) or crossover(ma3,0)
if showlsma6 and not showlsma21
    cond1 := crossover(ma2,0) or crossover(ma4,0)
if showlsma6 and showlsma21
    cond1 := crossover(ma2,0) or crossover(ma3,0) or crossover(ma4,0)   
plotshape(showentry? cond1 : na, title="Entry Signal", location=location.top, style=shape.circle, size=size.tiny, color=color.yellow, transp=0)

var table perfTable = table.new(position.top_right, 3, 2, border_width = 0)
f_fillCell(_table, _column, _row, _value) =>
	_cellText = tostring(_value, "#.##") + x
	table.cell(_table, _column, _row, _cellText, text_color = close > open ? color.teal : color.red)
	
f_fillCell4(_table, _column, _row, _value) =>
	_cellText = tostring(_value, "#.##") + y
	table.cell(_table, _column, _row, _cellText, text_color = color.blue)

f_fillCell3(_table, _column, _row, _value) =>
	_cellText = tostring(_value, "#.##")
	table.cell(_table, _column, _row, _cellText, text_color = color.yellow)

f_fillCell5(_table, _column, _row, _value) =>
	_cellText = tostring(_value, "#.##") + "%"
	table.cell(_table, _column, _row, _cellText, text_color = percentup > percentdown ? color.teal : percentdown > percentup ? color.red : na)
    table.cell_set_text_halign(perfTable, 2, 1, text.align_right)
if barstate.islast and showvol
	f_fillCell(perfTable, 2, 0, vol)
if barstate.islast and showpercent
	f_fillCell5(perfTable, 2, 1, percent)
if barstate.islast and showavg
	f_fillCell4(perfTable, 1, 0, avg3)
if barstate.islast and showrvol
	f_fillCell3(perfTable, 0, 0, rvol2)

var table perfTable2 = table.new(position.top_center, 1, 2, border_width = 0)

f_fillCell2(_table, _column, _row, _value) =>
	table.cell(_table, _column, _row, text_color = color.blue, text= syminfo.tickerid + " " + timeframe.period)
if barstate.islast and showticker
	f_fillCell2(perfTable2, 0, 0, syminfo.description)
	
	
