Enio_SPX_Accumulation/Distribution
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Point-Blank-Trading

//@version=5
indicator("Enio SPX Accumulation/Distribution")
lengthAD = input.int(3, "AccDist_MA_length", minval=1, maxval=200, step=1)
//input.string("Histogram", "Plot style", ["Histogram", "Candlesticks"]) 

aVol_open = request.security("USI:UVOL",timeframe.period , open)
aVol_high = request.security("USI:UVOL", timeframe.period , high)
aVol_low = request.security("USI:UVOL", timeframe.period , low)
aVol_close = request.security("USI:UVOL",timeframe.period ,close)

aIs_open = request.security("USI:ADV",timeframe.period , open)
aIs_high = request.security("USI:ADV", timeframe.period , high)
aIs_low = request.security("USI:ADV", timeframe.period , low)
aIs_close = request.security("USI:ADV", timeframe.period , close)

dVol_open = request.security("USI:DVOL", timeframe.period ,  open)
dVol_high = request.security("USI:DVOL", timeframe.period , high)
dVol_low = request.security("USI:DVOL", timeframe.period , low)
dVol_close = request.security("USI:DVOL", timeframe.period , close)

dIs_open = request.security("USI:DECL",  timeframe.period , open)
dIs_high = request.security("USI:DECL",  timeframe.period , high)
dIs_low = request.security("USI:DECL",  timeframe.period , low)
dIs_close = request.security("USI:DECL", timeframe.period , close)

ind_open = request.security("SP:SPX", timeframe.period , open)
ind_high = request.security("SP:SPX", timeframe.period , high)
ind_low = request.security("SP:SPX", timeframe.period ,low)
ind_close = request.security("SP:SPX", timeframe.period , close)

AccDis_open = (aVol_open * aIs_open - dVol_open * dIs_open) / ind_open
AccDis_high = (aVol_open *aIs_high - dVol_high * dIs_high) / ind_high
AccDis_low = (aVol_open * aIs_low - dVol_low * dIs_low) / ind_low
AccDis_close = (aVol_close * aIs_close - dVol_close * dIs_close) / ind_close

AD_MA = ta.sma(AccDis_close, lengthAD)

hist_color = if AccDis_close > 0
    AccDis_close > AD_MA ? color.rgb(3, 119, 7) : color.rgb(51, 172, 113)
else 
    AccDis_close <AD_MA ? color.rgb(250, 1, 1, 5) : color.rgb(224, 138, 138, 20)


//candle_color = if(AccDis_close > AccDis_open) 
	//color.green
//else 
    //if(AccDis_close < AccDis_open)
        //color.red
    //else
	    //color.white


plot(AccDis_close , style=plot.style_histogram, linewidth=4, color=hist_color)

    
