WVF - Oscillator
// 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('WVF - Oscillator', overlay=false)

import HeWhoMustNotBeNamed/enhanced_ta/12 as eta
period = input.int(100, "Loopback", step=10, group="WVF")
maType = input.string(title='MA Type', defval='hma', options=['ema', 'sma', 'hma', 'rma', 'vwma', 'wma', 'swma'], group='WVF')
useHistoricalRange = input.bool(false, group='WVF')
getWvfMomentum(source, maType, pd)=>
    //WVF formula/concept taken from @ChrisMoody
    wvf = (ta.highest(source, pd) - source)*100 / ta.highest(source, pd)
    ma = eta.ma(wvf, maType, pd)
    wvfMomentum = ta.linreg(wvf-ma, pd, 0)
    wvfMomentum
    
o = getWvfMomentum(open, maType, period)
h = getWvfMomentum(high, maType, period)
l = getWvfMomentum(low, maType, period)
c = getWvfMomentum(close, maType, period)

barcolor = o<c and c[1] < c? color.green : o > c and c[1] > c? color.red : color.silver
plotcandle(o, h, l, c, "WVF Candles", barcolor, barcolor, bordercolor=barcolor)

var wvfMomentumArray = array.new_float()
var wvfHighArray = array.new_float()
var wvfLowArray = array.new_float()

array.push(wvfMomentumArray, c)
array.push(wvfHighArray, ta.highest(h, period))
array.push(wvfLowArray, ta.lowest(l, period))

wvfMedian = useHistoricalRange ? array.median(wvfMomentumArray) : ta.median(c, period)
wvfHigh = useHistoricalRange ? array.median(wvfHighArray) : ta.median(ta.highest(h, period), period)
wvfLow = useHistoricalRange ? array.median(wvfLowArray) : ta.median(ta.lowest(h, period), period)

PMEDIAN = plot(wvfMedian, title="Median", color=color.blue)
PHIGH = plot(wvfHigh, title="High", color=color.red)
PLOW = plot(wvfLow, title="Low", color=color.green)
