Volume Delta (Hapharmonic)
Volume Delta (Hapharmonic)
0
at https://mozilla.org/MPL/2.0/
// © hapharmonic
//@version=5
FV = format.volume,FP = format.percent
indicator('Volume Delta [hapharmonic]',format=FV, max_bars_back=5000,
max_labels_count=500)
//------------------------------------------
// Settings |
//------------------------------------------
// Inputs Table
bool usecandle = input.bool(true,title = 'Volume on Candles')
position.middle_left,position.bottom_center,position.middle_center,
position.bottom_left,position.bottom_right,position.top_left],
group =
"Style")
string sL = input.string(size.small , 'Size Label' , options =[size.tiny,
size.small, size.normal, size.large],group = "Style")
string sT = input.string(size.normal , 'Size Table', options =[size.tiny,
size.small, size.normal, size.large],group = "Style")
bool Label = input.bool(false,inline = 'l') , History = input.bool(true,inline =
'l')
// Inputs for EMA lengths and volume confirmation
bool MAV = input.bool(true,title = 'EMA',group = "EMA")
string volumeOption = input.string("Use Volume Confirmation", title="Volume
Option", options=["none", "Use Volume Confirmation"], group="EMA")
bool useVolumeConfirmation = volumeOption == 'none' ?false:true
int emaFastLength = input(12, title="Fast EMA Length",group = "EMA")
int emaSlowLength = input(26, title="Slow EMA Length",group = "EMA")
int volumeConfirmationLength = input(6, title="Volume Confirmation Length",group =
"EMA")
string alert_freq = input.string(alert.freq_once_per_bar_close, title="Alert
Frequency",
options=[alert.freq_once_per_bar,
alert.freq_once_per_bar_close],group = "EMA",
tooltip='If you choose once_per_bar, you
will receive immediate notifications (but this may cause interference or indicator
repainting).
\n However, if you choose
once_per_bar_close, it will wait for the candle to confirm the signal before
notifying.')
//------------------------------------------
// UDT_identifier |
//------------------------------------------
// Define User Defined Type (UDT) for OHLCV data with default values
type OHLCV
float O = open
float H = high
float L = low
float C = close
float V = volume
//------------------------------------------
// Calculate volumes and percentages |
//------------------------------------------
calcVolumes(OHLCV ohlcv) =>
var VolumeData data = VolumeData.new()
data.buyVol := ohlcv.V * (ohlcv.C - ohlcv.L) / (ohlcv.H - ohlcv.L) //
Calculate buy volume using the formula: volume * (close - low) / (high - low)
data.sellVol := ohlcv.V - data.buyVol //
Calculate sell volume by subtracting buy volume from total volume
data.pcBuy := data.buyVol / ohlcv.V * 100 //
Calculate the percentage of buy volume
data.pcSell := 100 - data.pcBuy //
Calculate the percentage of sell volume (100% - buy percentage)
data.isBuyGreater := data.buyVol > data.sellVol //
Determine if buy volume is greater than sell volume
data.higherVol := data.isBuyGreater ? data.buyVol : data.sellVol //
Assign the higher volume value based on the comparison
data.lowerVol := data.isBuyGreater ? data.sellVol : data.buyVol //
Assign the lower volume value based on the comparison
data.higherCol := data.isBuyGreater ? C_Up : C_Down //
Assign the color for the higher volume bar based on the comparison
data.lowerCol := data.isBuyGreater ? C_Down : C_Up //
Assign the color for the lower volume bar based on the comparison
data
//------------------------------------------
// Get volume data |
//------------------------------------------
// Instantiate OHLCV without explicit values (uses defaults)
ohlcv = OHLCV.new()
volData = calcVolumes(ohlcv)
if sup
TC = text.align_center,CW = color.white
// Create table and set header
var table tb = table.new(P_, 6, 6, bgcolor = na, frame_width = 2, frame_color =
chart.fg_color,border_width = 1, border_color = CW)
// Title
tb.cell(0, 0, text = "Volume Candles",text_color = #FFBF00, bgcolor = #0E2841,
text_halign = TC, text_valign = TC,text_size = sT)
tb.merge_cells( 0, 0, 5, 0)
//Fill the cell with color, setting 20 cells to 100% area. Then, loop according
to the % volume of all trades.
cellCount = 20 // 4 columns x 5 rows = 20 cells
filledCells = 0
for r = 5 to 1
for c = 2 to 5
if filledCells < cellCount * (totalbuyPercentC_ / 100)
tb.cell(c, r, text = "", bgcolor = C_Up)
else
tb.cell(c, r, text = "", bgcolor = C_Down)
filledCells := filledCells + 1
if Label
sp = ' '
l = label.new(bar_index, ohlcv.V,
text=str.format('Net: {0}\nBuy: {1} ({2})\nSell: {3} ({4})\
n{5}/\\\n {5}l\n {5}l',
volStr, buyVolStr, buyPercentStr, sellVolStr,
sellPercentStr, sp),
style=label.style_none, textcolor=volData.higherCol, size=sL,
textalign=text.align_left)
if not History
(l[1]).delete()
//------------------------------------------
// Draw volume levels on the candlesticks |
//------------------------------------------
// Define base and value for gradient candles
float base = na,float value = na
bool uc = usecandle and sup
// Calculate base and value based on buy/sell volume and open/close relationship
if volData.isBuyGreater
base := math.min(ohlcv.O, ohlcv.C) // Start from the lower of open/close for
buy
value := base + math.abs(ohlcv.O - ohlcv.C) * (volData.pcBuy / 100) // Extend
to the percentage of buy volume
else
base := math.max(ohlcv.O, ohlcv.C) // Start from the higher of open/close for
sell
value := base - math.abs(ohlcv.O - ohlcv.C) * (volData.pcSell / 100) // Extend
to the percentage of sell volume