# Indicator for TOS
#// @tommyf1001
#//indicator('Volume-based Support & Resistance Zones V2', shorttitle='Vol S/R Zones V2'
# Converted by Sam4Cok@Samer800 - 08/2024
input extendLastLines = yes;
input selectTimeframe = AggregationPeriod.FIVE_MIN;
input volumeMovAvgLength = 6; #(title='Volume MA - Threshold', d
def na = Double.NaN;
def last = isNaN(close);
def current = GetAggregationPeriod();
def tf1 = Max(selectTimeframe, Current);
def ext = if extendLastLines then yes else if last then no else yes;
#-- Color
DefineGlobalColor("dn", CreateColor(255, 82, 82));
DefineGlobalColor("up", CreateColor(0, 230, 118));
#// TFUp and TFDown Calculations
Script f_tfUp {
input _TF_High = high;
input _TF_Vol = volume;
input _TF_VolMA = volume;
def f_tfUp = _TF_High[3] > _TF_High[4] and _TF_High[4] > _TF_High[5] and
_TF_High[2] < _TF_High[3] and _TF_High[1] < _TF_High[2] and _TF_Vol[3] > _TF_VolMA[3];
plot out = if isNaN(f_tfUp) then no else f_tfUp;
}
Script f_tfDown {
input _TF_Low = low;
input _TF_Vol = volume;
input _TF_VolMA = volume;
def f_tfDown = _TF_Low[3] < _TF_Low[4] and _TF_Low[4] < _TF_Low[5] and
_TF_Low[2] > _TF_Low[3] and _TF_Low[1] > _TF_Low[2] and _TF_Vol[3] > _TF_VolMA[3];
plot out = if isNaN(f_tfDown) then 0 else f_tfDown;
}
#// S/R = Time Frame 1 = TF1
def TF1_Vol = CompoundValue(1,if(volume(Period = tf1), volume(Period = tf1), TF1_Vol[1]), Volume(Period = tf1));
def TF1_VolMA = Average(TF1_Vol, volumeMovAvgLength);
def TF1_High = CompoundValue(1, if(high(Period = tf1), high(Period = tf1), TF1_High[1]), high(Period = tf1));
def TF1_Low = CompoundValue(1, if(low(Period = tf1), low(Period = tf1), TF1_Low[1]), low(Period = tf1));
def TF1_Open = CompoundValue(1, if(open(Period = tf1), open(Period = tf1), TF1_Open[1]), open(Period = tf1));
def TF1_Close = CompoundValue(1, if(close(Period = tf1), close(Period = tf1), TF1_Close[1]), close(Period = tf1));
def TF1_Up = f_tfUp(TF1_High, TF1_Vol, TF1_VolMA);
def TF1_Down = f_tfDown(TF1_Low, TF1_Vol, TF1_VolMA);
def TF1_FractalUp = CompoundValue(1, if TF1_Up then TF1_High[3] else TF1_FractalUp[1], TF1_High);
def TF1_FractalDn = CompoundValue(1, if TF1_Down then TF1_Low[3] else TF1_FractalDn[1], TF1_Low);
def TF1_ResZone = CompoundValue(1, if TF1_Up and TF1_Close[3] >= TF1_Open[3] then TF1_Close[3] else
if TF1_Up and TF1_Close[3] < TF1_Open[3] then TF1_Open[3] else TF1_ResZone[1], TF1_Close[3]);
def TF1_SupZone = CompoundValue(1, if TF1_Down and TF1_Close[3] >= TF1_Open[3] then TF1_Open[3] else
if TF1_Down and TF1_Close[3] < TF1_Open[3] then TF1_Close[3] else TF1_SupZone[1], TF1_Close[3]);
def ResTop1 = CompoundValue(1,if(TF1_FractalUp!=TF1_FractalUp[1],TF1_FractalUp,ResTop1[1]),TF1_FractalUp);
def ResBot1 = CompoundValue(1,if TF1_FractalUp!=TF1_FractalUp[1] then TF1_ResZone else ResBot1[1], TF1_ResZone);
def SupTop1 = CompoundValue(1,if TF1_FractalDn!=TF1_FractalDn[1] then TF1_SupZone else SupTop1[1], TF1_SupZone);
def SupBot1 = CompoundValue(1,if(TF1_FractalDn!=TF1_FractalDn[1],TF1_FractalDn,SupBot1[1]), TF1_FractalDn);
def ResTop = if last then ResTop1[1] else ResTop1;
def ResBot = if last then ResBot1[1] else ResBot1;
def SupTop = if last then SupTop1[1] else SupTop1;
def SupBot = if last then SupBot1[1] else SupBot1;
def n = -((tf1 / current) * 2);
plot ResTopLine = if !ext then na else if ResTop[n] then ResTop[n] else na;
plot ResBotLine = if !ext then na else if ResBot[n] then ResBot[n] else na;
plot SupTopLine = if !ext then na else if SupTop[n] then SupTop[n] else na;
plot SupBotLine = if !ext then na else if SupBot[n] then SupBot[n] else na;
ResTopLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ResBotLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
SupTopLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
SupBotLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ResTopLine.SetDefaultColor(GlobalColor("dn"));
ResBotLine.SetDefaultColor(GlobalColor("dn"));
SupTopLine.SetDefaultColor(GlobalColor("up"));
SupBotLine.SetDefaultColor(GlobalColor("up"));
#-- Cloud
AddCloud(if ResTopLine==ResTopLine[1] then ResTopLine else na, ResBotLine, Color.DARK_RED);
AddCloud(if SupTopLine==SupTopLine[1] then SupTopLine else na, SupBotLine, Color.DARK_GREEN);
def timeInMin = if tf1 < AggregationPeriod.HOUR then tf1 / AggregationPeriod.MIN else
if tf1 < AggregationPeriod.DAY then tf1 / AggregationPeriod.HOUR else
if tf1 < AggregationPeriod.WEEK then tf1 / AggregationPeriod.DAY else
if tf1 < AggregationPeriod.MONTH then tf1 / AggregationPeriod.WEEK else
tf1 / AggregationPeriod.MONTH;
def txt = if tf1 < AggregationPeriod.HOUR then 1 else
if tf1 < AggregationPeriod.DAY then 2 else
if tf1 < AggregationPeriod.WEEK then 3 else
if tf1 < AggregationPeriod.MONTH then 4 else 5;
AddLabel(1, Round(timeInMin, 0) + (if txt == 1 then "Min" else
if txt == 2 then "H" else
if txt == 3 then "D" else
if txt == 4 then "W" else "M"), Color.WHITE);
#-- END of CODE