Normalized Elastic Volume Oscillator (MTF)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LeafAlgo

//@version=5
indicator('Normalized Elastic Volume Oscillator (MTF)', shorttitle='MTF N-EVO')

// Input parameters
emaLength = input(14, title='EMA Length')
scalingFactor = input(2.0, title='Scaling Factor')
volumeWeighting = input(true, title='Volume Weighting')
higherTF = input("240", title="Higher Timeframe")

// Calculate volume oscillator
volumeEMA = ta.ema(volume, emaLength)
volumeDeviation = volume - volumeEMA

// Calculate volume oscillator on higher timeframe
higherTFVolumeEMA = request.security(syminfo.tickerid, higherTF, ta.ema(volume, emaLength))
higherTFVolumeDeviation = request.security(syminfo.tickerid, higherTF, volume) - higherTFVolumeEMA

// Dynamic scaling and weighting
scaledVolumeDeviation = volumeDeviation * scalingFactor
weightedVolumeDeviation = volumeWeighting ? scaledVolumeDeviation : volumeDeviation

// Dynamic scaling and weighting on higher timeframe
scaledHigherTFVolumeDeviation = higherTFVolumeDeviation * scalingFactor
weightedHigherTFVolumeDeviation = volumeWeighting ? scaledHigherTFVolumeDeviation : higherTFVolumeDeviation


wvd_h = ta.max(weightedVolumeDeviation)
wvd_l = ta.min(weightedVolumeDeviation)
wvd_n = ((weightedVolumeDeviation - wvd_l) / (wvd_h - wvd_l)) - 0.25

whtf_h = ta.max(weightedHigherTFVolumeDeviation)
whtf_l = ta.min(weightedHigherTFVolumeDeviation)
whtf_n = ((weightedHigherTFVolumeDeviation - whtf_l) / (whtf_h - whtf_l)) - 0.25

// Color line based on positive and negative values
bColor = weightedVolumeDeviation >= 0 ? color.new(color.green, 70) : color.new(color.red, 70)

// Plotting
plot(wvd_n, title='Elastic Volume Oscillator', color=color.fuchsia, linewidth=2)
plot(whtf_n, title="Higher Timeframe Elastic Volume Oscillator", color=color.orange, linewidth=2)

bgcolor(bColor)



