(mab) Volume Index
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © happymab
//
// V 1.0
//
// The (mab) Volume Index measures volume momentum. The formula is somewhat similar to the formula of RSI but
// uses volume instead of price. The price is calculated as the average of open, high, low and close prices 
// and is used to determine if the volume is counted as up-volume or down-volume.
//

//@version=5
indicator("(mab) Volume Index", shorttitle="(mab)Mvi", overlay=false)

// (mab) Colors
col_none = color.new(color.white, 100)

// Green
col_darkSpringGreen 			= #137547
col_lightGreen 					= #8fe388
col_springGreen					= #61FF7E
col_malachite					= #4CE949
col_mediumAquamarine			= #69DC9E
col_Citron						= #8EA604
col_yellowGreen					= #97DB4F
// Red
col_rubyRed 					= #931621
col_redPigment 					= #ef1010
col_middleRed					= #E88873
col_radicalRed					= #FF1053
// Orange
col_outrageousOrange 			= #fc7753
col_orangeSoda					= #EB5133
col_tartOrange					= #F75550
col_orangeRedCrayola			= #FE5F55
// Pink
col_shockingPinkCrayola			= #FB62F6
col_congoPink					= #FE8E86
// Purple
col_mysticMaroon 				= #bc387d
col_darkBluePurple 				= #311b92
col_plumWeb						= #F9B9F2
// Blue
col_trurfsBlue 					= #0091eb
col_honoluluBlue 				= #176da3
col_GreenBlueCrayola 			= #1e91d6
col_vividSkyBlue 				= #42cafd
col_skyBlueCrayola				= #51E5FF
// Yellow
col_maximumYellowRed 			= #fec149
col_rajah						= #FFAA5A
col_honeyYellow					= #FBAF00
col_minionYellow				= #FDE74C
// White
col_navajo_white 				= #FEDB9B
col_seeshell 					= #fef4ec

// Blue-Purple
col_blue1 						= #CFEAF1
col_blue2 						= #BBD2E7
col_blue3 						= #A6BADC
col_blue4 						= #92A2D2
col_blue5 						= #7D8AC7
col_blue6 						= #6972BD
col_blue7 						= #545AB2
col_blue8 						= #4042A8
col_blue9 						= #2B2A9D


//////////////////////////////////
// MVI
//////////////////////////////////
src						= input.source(ohlc4, title="Source")
length					= input.int(14, title="Length", minval=1, maxval=2000)
emaLength				= input.int(13, title="EMA length", minval=1, maxval=2000)
oversold				= input.float(25.0, title="Oversold", minval=0.0)
overbought				= input.float(75.0, title="Overbought", minval=0.0)

midline = 50.0

// Index calculation function
_calcIndex(up, dn, len) =>
	rs = ta.rma(up, len) / ta.rma(dn, len)
    res = 100 - 100 / (1 + rs)
    res
	
// MVI
mviUp = src > src[1] ? volume : 0
mviDown = src[1] > src ? volume : 0
mvi = _calcIndex(mviUp, mviDown, length)
mviEma = ta.ema(mvi, emaLength)


// Plot band
band1 = plot(oversold, title="Oversold", color=color.new(col_lightGreen, 75), style=plot.style_cross, linewidth=1, join=true, display=display.none)
band2 = plot(overbought, title="Overbought", color=color.new(col_outrageousOrange, 75), style=plot.style_cross, linewidth=1, join=true, display=display.none)
plot(midline, title="Midline", color=color.new(col_seeshell, 75), style=plot.style_line, linewidth=1, join=true, display=display.none)
fill(band1, band2, color=color.new(col_blue3, 80), title="Background")

// Plot index
plot(mvi, title="MVI", color=color.new(col_blue3, 50), style=plot.style_area, histbase=50, linewidth=1)
plot(mviEma, title="MVI EMA", color=color.new(col_blue1, 50), style=plot.style_line, histbase=50, linewidth=1, display=display.none)
