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

//   __    __            __       __  __                  __       __                        __      __    __              __      _______             __    __                                          __ 
//  /  |  /  |          /  |  _  /  |/  |                /  \     /  |                      /  |    /  \  /  |            /  |    /       \           /  \  /  |                                        /  |
//  $$ |  $$ |  ______  $$ | / \ $$ |$$ |____    ______  $$  \   /$$ | __    __   _______  _$$ |_   $$  \ $$ |  ______   _$$ |_   $$$$$$$  |  ______  $$  \ $$ |  ______   _____  ____    ______    ____$$ |
//  $$ |__$$ | /      \ $$ |/$  \$$ |$$      \  /      \ $$$  \ /$$$ |/  |  /  | /       |/ $$   |  $$$  \$$ | /      \ / $$   |  $$ |__$$ | /      \ $$$  \$$ | /      \ /     \/    \  /      \  /    $$ |
//  $$    $$ |/$$$$$$  |$$ /$$$  $$ |$$$$$$$  |/$$$$$$  |$$$$  /$$$$ |$$ |  $$ |/$$$$$$$/ $$$$$$/   $$$$  $$ |/$$$$$$  |$$$$$$/   $$    $$< /$$$$$$  |$$$$  $$ | $$$$$$  |$$$$$$ $$$$  |/$$$$$$  |/$$$$$$$ |
//  $$$$$$$$ |$$    $$ |$$ $$/$$ $$ |$$ |  $$ |$$ |  $$ |$$ $$ $$/$$ |$$ |  $$ |$$      \   $$ | __ $$ $$ $$ |$$ |  $$ |  $$ | __ $$$$$$$  |$$    $$ |$$ $$ $$ | /    $$ |$$ | $$ | $$ |$$    $$ |$$ |  $$ |
//  $$ |  $$ |$$$$$$$$/ $$$$/  $$$$ |$$ |  $$ |$$ \__$$ |$$ |$$$/ $$ |$$ \__$$ | $$$$$$  |  $$ |/  |$$ |$$$$ |$$ \__$$ |  $$ |/  |$$ |__$$ |$$$$$$$$/ $$ |$$$$ |/$$$$$$$ |$$ | $$ | $$ |$$$$$$$$/ $$ \__$$ |
//  $$ |  $$ |$$       |$$$/    $$$ |$$ |  $$ |$$    $$/ $$ | $/  $$ |$$    $$/ /     $$/   $$  $$/ $$ | $$$ |$$    $$/   $$  $$/ $$    $$/ $$       |$$ | $$$ |$$    $$ |$$ | $$ | $$ |$$       |$$    $$ |
//  $$/   $$/  $$$$$$$/ $$/      $$/ $$/   $$/  $$$$$$/  $$/      $$/  $$$$$$/  $$$$$$$/     $$$$/  $$/   $$/  $$$$$$/     $$$$/  $$$$$$$/   $$$$$$$/ $$/   $$/  $$$$$$$/ $$/  $$/  $$/  $$$$$$$/  $$$$$$$/ 
//                                                                                                                                                                                                          
//                                                                                                                                                                                                          
//
//@version=5
indicator("Volatility/Volume Impact", overlay=false)
import HeWhoMustNotBeNamed/enhanced_ta/13 as eta

atrMaType = input.string(title='', defval='rma', options=['ema', 'sma', 'hma', 'rma', 'vwma', 'wma', 'swma'], group='ATR', inline='a')
atrLength = input.int(22, '', group='ATR', inline='a')

maType = input.string(title='', defval='median', options=['ema', 'sma', 'hma', 'rma', 'vwma', 'wma', 'swma', 'median', 'linreg'], group='High/Low Reference', inline="ma")
length = input.int(100, '', step=10, group='High/Low Reference', inline='ma')

usePercentage = input.bool(false, 'Percentage', group='Correlation')
rangeLength = input.int(500, 'Range', step=100, group='Correlation')

getCorrelation(source)=>
    var float pvd = 0
    var float nvd = 0
    ref = eta.ma(source, maType, length)
    diff = nz(close - close[1], 0)
    diffPercent = diff/close[1]
    
    diffSource = usePercentage? diffPercent : diff
    pvd := source > ref? pvd + diffSource : pvd
    nvd := source < ref? nvd + diffSource : nvd

    pvdCorrelation = ta.correlation(pvd, close, rangeLength)
    nvdCorrelation = ta.correlation(nvd, close, rangeLength)
    [pvd, nvd, pvdCorrelation, nvdCorrelation]

tradedVolume = volume*ohlc4
volatility = eta.atr(atrMaType, atrLength)*100/close

[pVolumeD, nVolumeD, pVolumeDCorrelation, nVolumeDCorrelation] = getCorrelation(tradedVolume)
[pVolatilityD, nVolatilityD, pVolatilityDCorrelation, nVolatilityDCorrelation] = getCorrelation(volatility)

plot(pVolumeD, "High Volume Impact", color=pVolumeDCorrelation>nVolumeDCorrelation? color.green: color.silver)
plot(nVolumeD, "Low Volume Impact", color=nVolumeDCorrelation>pVolumeDCorrelation? color.red: color.silver)

plot(pVolatilityD, "High Volatility Impact", color=pVolatilityDCorrelation>nVolatilityDCorrelation? color.lime: color.silver)
plot(nVolatilityD, "Low Volatility Impact", color=nVolatilityDCorrelation>pVolatilityDCorrelation? color.orange: color.silver)

