Advanced Volume Profile
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @version = 4
// @author  = Morogan
// © Morogan
//
// ADVANCED VOLUME PROFILE (for 24/7 and Futures only)
//
// This script plots volume relative to an asset's historical volume profile.
//
// Usage:
// As a companion to my "Unusual Time Frame Volume" (UTF Volume) script, this plots volume against the same historical volume profile used for UTF Volume.
// The same high volume (relative to historical) threshold alert is available (yellow bar).
// Likewise, if the volume exceeds the historical threshold, but is below the alert threshold, the bar color is orange.
// At the top of the chart is an indicator which is green if a bar has higher volume than the previous bar.
// You can also set a threshold for this such that if the volume of a bar exceeds the previous bar by a certain multiplier which will turn the indicator yellow.
// For example, if the threshold is set to "1.5", then the indicator will be yellow (instead of green) on an increase in volume over the previous bar of 1.5x.
//
// NOTES:
// THIS SCRIPT CURRENTLY ONLY WORKS FOR ASSETS THAT TRADE 24/7 OR CBOE FUTURES HOURS!
// Make sure you set the "Asset Mode" and "Time Frame (minutes)" to values that match your asset and chart setting.
// For example, if you are trading Futures on a 2m chart, set the Asset Mode to Futures and Time Frame to 2m.
// If you are trading crypto on a 5m chart, set the Asset Mode to 24/7 and Time Frame to 5m.
// If the settings are not set appropriately, the output will be incorrect/invalid.
//
// If you choose a "Look-back (Days)" setting that is too far back given the time frame, the script will produce an error.
// I suggest playing with settings from "1" (compares volume to the previous day's volume) to the highest number that doesn't break the script.
// For example, at a 2m time frame, the maximum look-back will be "6" or "7" depending on which mode you are using.
// Longer chart time settings allow larger look-back values.
// I find that the default value ("6") does a decent job in general.
//
// Please feel free to reuse or further develop this script.
// I would greatly appreciate it if you would send me a message below if you find it useful.

study("Advanced Volume Profile", format=format.price, precision=2, resolution="")

tf = input(title="Time Frame (minutes)", type=input.integer, defval=2, minval=1, maxval=240)
am = input(title="Asset Mode", defval="24/7", options=["24/7", "Futures"])
lb = input(title="Look-back (Days)", type=input.integer, defval=6, minval=1, maxval=30)
th = input(title="Alert Threshold - High Volume Relative to Historical Profile", type=input.float, defval=1.32, minval=0, maxval=7)
tp = input(title="Alert Threshold - High Volume Relative to Prior Bar", type=input.float, defval=2, minval=0, maxval=7)

period = 1440 / tf
if am == "24/7"
    period := 1440 / tf
if am == "Futures"
    period := 1380 / tf

time_frame_average(avg_len) =>
    ret_val = 0.0
    for i = 1 to avg_len
        ret_val := ret_val + volume[i*period]
    ret_val/avg_len

avgVol = time_frame_average(lb)
Delta = volume - (th * avgVol)

secolor = Delta > 0 ? color.yellow : (Delta < 0) and (volume > avgVol) ? color.orange : color.blue

plot(volume, title="Volume", style=plot.style_columns, color=secolor, linewidth=1)
plot(avgVol, title="Historical Volume Profile", color=color.white, style=plot.style_stepline, linewidth=1)

PAL = (volume > volume[1])
pacolor = volume > tp * volume[1] ? color.yellow : (volume < tp * volume[1]) and (volume > volume[1]) ? color.green : color.new(color.black, 25)

plotshape(PAL, title="Volume Increase", location=location.top, style=shape.square, size=size.auto, color=pacolor)
//plotshape(not(PAL), title="Volume Decrease", location=location.top, style=shape.xcross, size=size.auto, color=color.new(color.black, 25))