#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © PuguForex 2021
#indicator('Directional Volatility and Volume')
# Converrted and mod by Sam4Cok@Samer800 - 12/2022
declare lower;
input EnableBackgroundColor = yes;
input ColorBar = no;
input src = close;
input VolatilityPeriod = 4; # 'Volatility period'
input VolatilityMethod = AverageType.HULL; # "Volatility smoothing"
input VolumePeriod = 14; # 'Volume period'
input VolumeMethod = AverageType.HULL; # "Volume smoothing"
input ZonePeriod = 14; # 'Zone period'
input ZoneMethod = AverageType.HULL; # "Zone smoothing"
def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
#--- Color
DefineGlobalColor("exUp" , CreateColor(0,179,0));
DefineGlobalColor("up" , CreateColor(0,104,0));
DefineGlobalColor("exDn" , CreateColor(255,5,68));
DefineGlobalColor("dn" , CreateColor(133,0,33));
DefineGlobalColor("volUp" , createcolor(173,216,230));
DefineGlobalColor("volDn" , CreateColor(230,173,188));
def zeroLine = MovingAverage(ZoneMethod, hl2 , ZonePeriod);
def val = MovingAverage(VolatilityMethod, close, VolatilityPeriod) - zeroLine;
def znUp = MovingAverage(ZoneMethod, high , ZonePeriod) - zeroLine;
def znDn = MovingAverage(ZoneMethod, low , ZonePeriod) - zeroLine;
def vol = if close > open then volume else -volume;
def volSm = MovingAverage(VolumeMethod, vol, VolumePeriod);
def upBr = Crosses(val, znUp, CrossingDirection.ABOVE);
def dnBr = Crosses(val, znDn, CrossingDirection.BELOW);
def movAvg = MovingAverage(ZoneMethod, val, ZonePeriod);
def volatColor = if val > znUp then 1 else
if val < znDn then -1 else 0;# color.green : color.red
def ZoneColor = if volSm > 0 then 1 else if volSm < 0 then -1 else 0;
#--- PLots
plot VolumeUp = if volatColor>0 then na else znUp; # 'Volume Up'
VolumeUp.AssignValueColor(if ZoneColor>0 then GlobalColor("volUp") else
if ZoneColor<0 then GlobalColor("volDn") else Color.GRAY);
VolumeUp.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
VolumeUp.SetLineWeight(4);
plot VolumeDn = if volatColor<0 then na else znDn; # 'Volume Dn'
VolumeDn.AssignValueColor(if ZoneColor>0 then GlobalColor("volUp") else
if ZoneColor<0 then GlobalColor("volDn") else Color.GRAY);
VolumeDn.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
VolumeDn.SetLineWeight(4);
plot Volatility = val; # 'Volatility'
Volatility.AssignValueColor(if volatColor>0 then
if val>val[1] then GlobalColor("exUp") else GlobalColor("up") else
if volatColor<0 then
if val<val[1] then GlobalColor("exDn") else GlobalColor("dn") else Color.GRAY);
Volatility.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#--- Signal
AddChart(high = if upBr and src>movAvg then pos else na,
low = neg,
open = pos,
close = neg,
type = ChartType.CANDLE, growcolor = CreateColor(0,78,0));
AddChart(high = if dnBr and src<movAvg then pos else na,
low = neg,
open = pos,
close = neg,
type = ChartType.CANDLE, growcolor = CreateColor(78,0,0));
#--- Bar Color
AssignPriceColor(if !ColorBar then Color.CURRENT else if volatColor>0 then
if val>val[1] and src>src[1] then GlobalColor("exUp") else GlobalColor("up") else
if val<val[1] and src<src[1] then GlobalColor("exDn") else GlobalColor("dn"));
# Background color
AssignBackgroundColor(if !EnableBackgroundColor then Color.CURRENT else if volatColor>0 and src>movAvg then CreateColor(1,25,16) else if volatColor<0 and src<movAvg then CreateColor(38,0,0) else Color.CURRENT);
#-- END CODE