// This source code is subject to the terms of the Mozilla Public License 2.0 at [Link]
0/
// © Fr3d0C0rl30n3
//@version=4
study("Fr3d0's Volume Profile Visible Range", "VPVR", overlay=true, max_boxes_count=500)
LINE_COLOR = [Link]([Link], 25)
BORDER_COLOR = [Link]([Link], 80)
BUY_COLOR = [Link]([Link], 25)
SELL_COLOR = [Link]([Link], 25)
TIME_UNIT = time - time[1]
MAX_BARS = 365
topBottomLineColor = input(LINE_COLOR, "Top/bottom Line Color", type = [Link])
buyBarColor = input(BUY_COLOR, "Buy Bar Color", type = [Link])
sellBarColor = input(SELL_COLOR, "Sell Bar Color", type = [Link])
numOfBars = input(90, 'Number of bars', minval=14, maxval=MAX_BARS)
distLastCandle = input(2, 'Distance from last candle', minval=2, maxval=20)
rangeHigh = highest(high, numOfBars)
rangeLow = lowest(low, numOfBars)
rangeHeight = rangeHigh - rangeLow
numOfHistograms = input(50, 'Number of histograms', minval=10, maxval=200)
widestHistogramWidth = input(50, 'Width of the PoC', minval=20, maxval=100)
histogramHeight = rangeHeight / numOfHistograms
histogramLowList = array.new_float(numOfHistograms, na)
histogramHighList = array.new_float(numOfHistograms, na)
histogramPriceList = array.new_float(numOfHistograms, 0.0)
histogramBuyVolumeList = array.new_float(numOfHistograms, 0.0)
histogramSellVolumeList = array.new_float(numOfHistograms, 0.0)
histogramVolumePercentageList = array.new_float(numOfHistograms, 0.0)
// Clean up drawings every tick
var buyBars = array.new_box(MAX_BARS, na)
for i=0 to MAX_BARS-1
[Link]([Link](buyBars, i))
var sellBars = array.new_box(MAX_BARS, na)
for i=0 to MAX_BARS-1
[Link]([Link](sellBars, i))
var line topLine = na
[Link](topLine)
var line bottomLine = na
[Link](bottomLine)
if [Link]
// Define lows and highs of the histograms
for i = 0 to numOfHistograms - 1
histogramLow = rangeLow + histogramHeight * i
histogramHigh = rangeLow + histogramHeight * (i + 1)
[Link](histogramLowList, i, histogramLow)
[Link](histogramHighList, i, histogramHigh)
[Link](histogramPriceList, i, (histogramLow + histogramHigh) / 2)
// Assign bar's volumes to histograms
for i = 0 to numOfBars - 1
currentBarHeight = high[i] - low[i]
currentBuyVolume = iff((high[i] == low[i]), 0, volume[i] * (close[i] - low[i]) / currentBarHeight)
currentSellVolume = iff((high[i] == low[i]), 0, volume[i] * (high[i] - close[i]) / currentBarHeight)
// Define the percentages of the current volume to give to histograms
for j = 0 to numOfHistograms - 1
histogramLow = [Link](histogramLowList, j)
histogramHigh = [Link](histogramHighList, j)
target = max(histogramHigh, high[i]) - min(histogramLow, low[i])
- (max(histogramHigh, high[i]) - min(histogramHigh, high[i]))
- (max(histogramLow, low[i]) - min(histogramLow, low[i]))
histogramVolumePercentage = target / currentBarHeight
histogramBuyVolume = [Link](histogramBuyVolumeList, j)
histogramSellVolume = [Link](histogramSellVolumeList, j)
// If there is at least one histogram affected
// then divide the current volume by the number of histograms affected
if histogramVolumePercentage > 0
[Link](histogramBuyVolumeList, j, histogramBuyVolume + currentBuyVolume *
histogramVolumePercentage)
[Link](histogramSellVolumeList, j, histogramSellVolume + currentSellVolume *
histogramVolumePercentage)
// Find the histogram with the highest volume
highestHistogramVolume = 0.0
for i = 0 to numOfHistograms - 1
histogramBuyVolume = [Link](histogramBuyVolumeList, i)
histogramSellVolume = [Link](histogramSellVolumeList, i)
histogramVolume = histogramBuyVolume + histogramSellVolume
highestHistogramVolume := max(highestHistogramVolume, histogramVolume)
timeUnit = time - time[1]
// Draw top and bottom of the range considered
topLine := [Link](time[numOfBars], rangeHigh, time_close + distLastCandle * timeUnit, rangeHigh,
xloc=xloc.bar_time, color=topBottomLineColor, width = 2)
bottomLine := [Link](time[numOfBars], rangeLow, time_close + distLastCandle * timeUnit, rangeLow,
xloc=xloc.bar_time, color=topBottomLineColor, width = 2)
// Draw histograms and highlight the Point of Control
for i = 0 to numOfHistograms - 1
histogramLow = [Link](histogramLowList, i)
histogramHigh = [Link](histogramHighList, i)
histogramBuyVolume = [Link](histogramBuyVolumeList, i)
histogramSellVolume = [Link](histogramSellVolumeList, i)
histogramVolume = histogramBuyVolume + histogramSellVolume
histogramWidth = widestHistogramWidth * histogramVolume / highestHistogramVolume
histogramBuyWidth = floor(histogramWidth * histogramBuyVolume / histogramVolume)
histogramSellWidth = floor(histogramWidth * histogramSellVolume / histogramVolume)
// Draw buy and sell histograms
[Link](buyBars, i, [Link](left=bar_index + distLastCandle, top=histogramHigh, right=bar_index +
distLastCandle + histogramBuyWidth, bottom=histogramLow, bgcolor=buyBarColor,
border_color=BORDER_COLOR))
[Link](sellBars, i, [Link](left=bar_index + distLastCandle + histogramBuyWidth, top=histogramHigh,
right=bar_index + distLastCandle + histogramBuyWidth + histogramSellWidth, bottom=histogramLow,
bgcolor=sellBarColor, border_color=BORDER_COLOR))