# https://www.tradingview.com/v/S2ClgBm5/
#/@tommyf1001
#// Original script is thanks to synapticex and additional modifications is thanks to Lij_MC. Credit to both of them for most of the logic behind this script. Since then I have made many changes to this script as noted below.
#indicator('Volume-based Support & Resistance Zones V2', shorttitle='Vol S/R Zones V2'
# Converted by Sam4Cok@Samer800 - 07/2023
# Update - fix MTF issue and added volume Calc Option - 10/2023
#// Inputs
input extendLines = yes;
input showLabel = yes;
input ShowTodayOnly = no;
input showLines = {"High/Low Line", "Open/Close Line", default "Show Zone"};
input Timeframe = {default "Chart", "Custom"};
input cusotmTimeframe = AggregationPeriod.FIFTEEN_MIN;
input volumeCalcMethod = {"Average Volume", default "Green/Red Bars"};
input volumeLookback = 6; # 'Volume MA - Threshold'
def na = Double.NaN;
def last = IsNaN(close);
def extend = if extendLines then no else last;
def mtf = cusotmTimeframe;
def Current = GetAggregationPeriod();
def today = GetDay() == GetLastDay();
def chart = Timeframe == Timeframe."Chart";
def zone = showLines==showLines."Show Zone";
#--- Color
DefineGlobalColor("bull", Color.CYAN);#CreateColor(11, 113, 195));
DefineGlobalColor("bear", Color.MAGENTA);#CreateColor(195, 175, 0));
DefineGlobalColor("dbull", CreateColor(6, 56, 99));
DefineGlobalColor("dbear", Color.PLUM);#CreateColor(68, 59, 0));
#-- MTF
def MTFclose = Fundamental(fundamentalType = FundamentalType.CLOSE, Period = mtf);
def MTFhigh = Fundamental(fundamentalType = FundamentalType.HIGH, Period = mtf);
def MTFlow = Fundamental(fundamentalType = FundamentalType.LOW, Period = mtf);
def MTFopen = Fundamental(fundamentalType = FundamentalType.OPEN, Period = mtf);
def MTFvolume = Fundamental(fundamentalType = FundamentalType.VOLUME, Period = mtf);
def TFTF = if chart then Current else mtf;
#-- Script
#// TFUp and TFDown Calculations
script f_tfUp {
input _TF_High = high;
input _TF_Vol = volume;
input _TF_Close = close;
input _TF_Open = open;
input Bars = 6;
input method = "Average Volume";
def TF_High = _TF_High;
def TF_Vol = _TF_Vol;
def TF_Close = _TF_Close;
def TF_Open = _TF_Open;
def TF_VolMA = Average(TF_Vol, Bars);
def Green = Sum(if TF_Close > TF_Open then TF_Vol else 0, Bars);
def Total = Sum(TF_Vol, Bars);
def GreenRatio = Green / Total * 100;
def cond1 = (TF_High[3]) > (TF_High[4]);
def cond2 = (TF_High[4]) > (TF_High[5]);
def cond3 = (TF_High[2]) < (TF_High[3]);
def cond4 = (TF_High[1]) < (TF_High[2]);
def cond5 = (TF_Vol[3]) > (TF_VolMA[3]);
def VolTh = if method == "Average Volume" then cond5 else GreenRatio > 55;
def f_tfUp = cond1 and cond2 and cond3 and cond4 and VolTh;
plot out = if IsNaN(f_tfUp) then 0 else f_tfUp;
}
script f_tfDn {
input _TF_Low = low;
input _TF_Vol = volume;
input _TF_Close = close;
input _TF_Open = open;
input Bars = 6;
input method = "Average Volume";
def TF_Low = _TF_Low;
def TF_Vol = _TF_Vol;
def TF_Close = _TF_Close;
def TF_Open = _TF_Open;
def TF_VolMA = Average(TF_Vol, Bars);
def Red = Sum(if TF_Close < TF_Open then TF_Vol else 0, Bars);
def Total = Sum(TF_Vol, Bars);
def RedRatio = Red / Total * 100;
def cond1 = (TF_Low[3]) < (TF_Low[4]);
def cond2 = (TF_Low[4]) < (TF_Low[5]);
def cond3 = (TF_Low[2]) > (TF_Low[3]);
def cond4 = (TF_Low[1]) > (TF_Low[2]);
def cond5 = (TF_Vol[3]) > (TF_VolMA[3]);
def volTh = if method == "Average Volume" then cond5 else RedRatio > 55;
def f_tfDn = cond1 and cond2 and cond3 and cond4 and volTh;
plot out = if IsNaN(f_tfDn) then 0 else f_tfDn;
}
def TF_TF = TFTF;
def TF_Vol = volume;
def TF_High = high;
def TF_Low = low;
def TF_Open = open;
def TF_Close = close;
def TF_Up1 = f_tfUp(TF_High, TF_Vol, TF_Close, TF_Open, volumeLookback, volumeCalcMethod);
def TF_Dn1 = f_tfDn(TF_Low, TF_Vol, TF_Close, TF_Open, volumeLookback, volumeCalcMethod);
def TF1_Vol = MTFvolume;
def TF1_High = MTFhigh;
def TF1_Low = MTFlow;
def TF1_Open = MTFopen;
def TF1_Close = MTFclose;
def TF1_Up1 = f_tfUp(TF1_High, TF1_Vol, TF1_Close, TF1_Open, volumeLookback, volumeCalcMethod);
def TF1_Dn1 = f_tfDn(TF1_Low, TF1_Vol, TF1_Close, TF1_Open, volumeLookback, volumeCalcMethod);
#-- Label Cal
def TFInMinutes = TF_TF / 60000;
#def currentTFInMinutes = current;
#// Compare current TF to higher TF to make sure it is smaller, otherwise our plots don't make sense.
def chartOnLowerTF = Current > TF_TF;
def errorInAggregation = chartOnLowerTF;
Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
def TF_inH = TFInMinutes / 60;
def hiLabel = TF_TF / 60 / 60 / 24 / 1000;
def TF_text = if TFInMinutes >= 60 and TFInMinutes < 1440 then 1 else
if TFInMinutes < 60 then 2 else 3;
AddLabel(showLabel and TF_text == 1, TF_inH + "H" , Color.WHITE);
AddLabel(showLabel and TF_text == 2, TFInMinutes + "M" , Color.WHITE);
AddLabel(showLabel and TF_text == 3, hiLabel + "D" , Color.WHITE);
#// S/R = Time Frame 1 = TF1
def TF_Up = TF_Up1;
def TF_Dn = TF_Dn1;
def TF1_Up = TF1_Up1;
def TF1_Dn = TF1_Dn1;
def TF_CalcFractalUp = if TF_Up then TF_High[3] else TF_CalcFractalUp[1];
def TF_CalcFractalDn = if TF_Dn then TF_Low[3] else TF_CalcFractalDn[1];
def TF_FractalUp = TF_CalcFractalUp;
def TF_FractalDn = TF_CalcFractalDn;
def TF1_CalcFractalUp = if TF1_Up then TF1_High[3] else TF1_CalcFractalUp[1];
def TF1_CalcFractalDn = if TF1_Dn then TF1_Low[3] else TF1_CalcFractalDn[1];
def TF1_FractalUp = TF1_CalcFractalUp;
def TF1_FractalDn = TF1_CalcFractalDn;
#// Zones - Current Time Frame = Time Frame 1 = TF1
#// Fractal Up Zones
def maxUp = Max(TF_Close[3], TF_Open[3]);
def TF_CalcFractalUpZone = if TF_Up then maxUp else TF_CalcFractalUpZone[1];
def TF_ResZone = TF_CalcFractalUpZone;
def max1Up = Max(TF1_Close[3], TF1_Open[3]);
def TF1_CalcFractalUpZone = if TF1_Up then max1Up else TF1_CalcFractalUpZone[1];
def TF1_ResZone = TF1_CalcFractalUpZone;
#// Fractal Down Zones
def MinDn = Min(TF_Close[3], TF_Open[3]);
def TF_CalcFractalDnZone = if TF_Dn then MinDn else TF_CalcFractalDnZone[1];
def TF_SupZone = TF_CalcFractalDnZone;
def Min1Dn = Min(TF1_Close[3], TF1_Open[3]);
def TF1_CalcFractalDnZone = if TF1_Dn then Min1Dn else TF1_CalcFractalDnZone[1];
def TF1_SupZone = TF1_CalcFractalDnZone;
def FractalUp = if chart then TF_FractalUp else TF1_FractalUp;
def FractalDn = if chart then TF_FractalDn else TF1_FractalDn;
def ResZone = if chart then TF_ResZone else TF1_ResZone;
def SupZone = if chart then TF_SupZone else TF1_SupZone;
def TF_ResistanceLineAup;
def TF_ResistanceLineAdn;
def TF_SupportLineAup;
def TF_SupportLineAdn;
switch (showLines) {
case "High/Low Line" :
TF_ResistanceLineAup = FractalUp;
TF_ResistanceLineAdn = na;
TF_SupportLineAup = FractalDn;
TF_SupportLineAdn = na;
case "Open/Close Line" :
TF_ResistanceLineAup = na;
TF_ResistanceLineAdn = ResZone;
TF_SupportLineAup = na;
TF_SupportLineAdn = SupZone;
default :
TF_ResistanceLineAup = FractalUp;
TF_ResistanceLineAdn = ResZone;
TF_SupportLineAup = FractalDn;
TF_SupportLineAdn = SupZone;
}
def tfResAup = if !ShowTodayOnly then TF_ResistanceLineAup else
if !today then na else TF_ResistanceLineAup;
#if FractalUp != FractalUp[1] then TF_ResistanceLineAup else tfResAup[1];
def tfResAdn = if !ShowTodayOnly then TF_ResistanceLineAdn else
if !today then na else TF_ResistanceLineAdn;
#if FractalUp != FractalUp[1] then TF_ResistanceLineAdn else tfResAdn[1];
def tfSupAup = if !ShowTodayOnly then TF_SupportLineAup else
if !today then na else TF_SupportLineAup;
#if FractalDn != FractalDn[1] then TF_SupportLineAup else tfSupAup[1];
def tfSupAdn = if !ShowTodayOnly then TF_SupportLineAdn else
if !today then na else TF_SupportLineAdn;
#if FractalDn != FractalDn[1] then TF_SupportLineAdn else tfSupAdn[1];
plot tfResUpLine = if !tfResAup or extend or zone then na else tfResAup;
plot tfResDnLine = if !tfResAdn or extend or zone then na else tfResAdn;
tfResUpLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tfResDnLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tfResUpLine.SetDefaultColor(GlobalColor("bear"));
tfResDnLine.SetDefaultColor(GlobalColor("bear"));
plot tfSupUpLine = if !tfSupAup or extend or zone then na else tfSupAup;
plot tfSupDnLine = if !tfSupAdn or extend or zone then na else tfSupAdn;
tfSupUpLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tfSupDnLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tfSupUpLine.SetDefaultColor(GlobalColor("bull"));
tfSupDnLine.SetDefaultColor(GlobalColor("bull"));
#-- Cloud
def resUp = if tfResAup then tfResAup else na;
def resDn = if tfResAdn then tfResAdn else na;
def supUp = if tfSupAup then tfSupAup else na;
def supDn = if tfSupAdn then tfSupAdn else na;
def cloudRes = !zone or extend or (resUp - resUp[1]);
def cloudSup = !zone or extend or (supUp - supUp[1]);
AddCloud(if cloudRes then na else resUp, resDn, GlobalColor("dbear"), GlobalColor("dbear"), yes);
AddCloud(if cloudSup then na else supUp, supDn, GlobalColor("dbull"), GlobalColor("dbull"), yes);
#-- END of CODE