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

//@version=5
indicator(title="Projected Volume", overlay=false, format = format.volume, max_bars_back=5000)

// 1. Input
volumeTypeInput = input.string(defval="Financial", title="Volume Type", options=["Financial", "Quantity"], group="Settings")
maTypeInput =   input.string(defval="SMA", title="Type", options=["SMA", "EMA", "WMA", "RMA", "HMA", "VWMA", "SWMA"], group="Moving Average")
maLengthInput =   input.int(defval=20, title="Length", minval=1, step=1, group="Moving Average")
maShowInput = input.bool(defval=true, title="Show", group="Moving Average")
tradingHoursInput = input.float(defval=24.0, title="Trading Hours", minval=0.5, maxval=24, step=0.5, tooltip="Affects only the daily timeframe", group="1-day timeframe (only)")

// 2. Variables
color realColor = close > open ? color.rgb(146, 210, 204) : color.rgb(247, 169, 167)
color projectedColor = color.rgb(181, 182, 187)

float realVol = 0.0
float cumVol = 0.0
float projectedVol = 0.0
float tf = 0
float tradingMinutes = tradingHoursInput * 60
float dayInMinutes = 1440


// 3 . Functions
getMA(maType, maLen) => 
    switch maType
        "SMA" => ta.sma(realVol, maLen)
        "EMA" => ta.ema(realVol, maLen)
        "WMA" => ta.wma(realVol, maLen)
        "RMA" => ta.rma(realVol, maLen)
        "HMA" => ta.hma(realVol, maLen)
        "VWMA" => ta.vwma(realVol, maLen)
        "SWMA" => ta.swma(realVol)


// 4. Calculations
if timeframe.isdaily
    tf := timeframe.in_seconds(timeframe.period) * tradingMinutes / dayInMinutes
else
    tf := timeframe.in_seconds(timeframe.period)

elapsedTime = (timenow - time) / 1000

if volumeTypeInput == "Financial"
    projectedVol := volume * close * tf / elapsedTime 
    realVol := volume * close
else
    projectedVol := volume * tf / elapsedTime
    realVol := volume

// Check for error
cumVol += nz(volume)
if barstate.islast and cumVol == 0
    runtime.error("No volume is provided by the data vendor.")

ma = getMA(maTypeInput, maLengthInput)

// 5. Drawings
plot(barstate.islast ? projectedVol : na, title="Projected", color=barstate.islast ?  projectedColor: realColor, style = plot.style_columns)
plot(realVol, color=realColor, title="Real",  style = plot.style_columns)
plot(maShowInput ? ma : na, title="MA", color=color.blue, linewidth=1)