# // Indciator for TOS
#//@jayalekshmiaishu
#study(title="RAHUL ATR + Volume Spikes", overlay=true)
# Converted by Sam4Cok@Samer800 - 04/2025
input unusualVolumeBarColor = yes;
input atrPeriod = 20; #, "Period")
input atrMultiplier = 4.0; #, "Multiplier"
input Source = close; #, title="Source")
input movAvgLength = 20; #, minval=1, title="Smooth")
input VolumeSmaLength = 20; #, "Volume SMA length", minval=1)
def na = Double.NaN;
def last = IsNaN(close);
#vwma(source, length)
script VWMA {
input src = close;
input len = 14;
def vol = volume/1000;
def nom = Average(src * vol, len);
def den = Average(vol, len);
def VWMA = nom / den;
Plot result = VWMA;
}
def atr = ATR(Length = atrPeriod);
def nLoss = atrMultiplier * atr;
def xATRTrailingStop = If(close > xATRTrailingStop[1] and close[1] > xATRTrailingStop[1], Max(xATRTrailingStop[1], close - nLoss), If(close < xATRTrailingStop[1] and close[1] < xATRTrailingStop[1], Min(xATRTrailingStop[1], close + nLoss), If(close > xATRTrailingStop[1], close - nLoss, close + nLoss)));
def pos = If(close[1] < xATRTrailingStop[1] and close > xATRTrailingStop[1], 1, If(close[1] > xATRTrailingStop[1] and close < xATRTrailingStop[1], -1, pos[1]));
def isLong;
def isShort;
def LONG = !isLong[1] and pos == 1;
def SHORT = !isShort[1] and pos == -1;
if LONG {
isLong = yes;
isShort = no;
} else if SHORT {
isLong = no;
isShort = yes;
} else {
isLong = isLong[1];
isShort = isShort[1];
}
def vol = volume / 1000;
def vwma = vwma(Source, movAvgLength);
def avg1 = (vwma + xATRTrailingStop) / 2;
def vol_avg = Average(vol, VolumeSmaLength);
def unusual_vol_down = unusualVolumeBarColor and vol > vol_avg * 1.2 and close < open;
def unusual_vol_up = unusualVolumeBarColor and vol > vol_avg * 1.2 and close > open;
def barHighlightColor = if unusual_vol_down then -1 else
if unusual_vol_up then 1 else 0;
#-- Plot
plot avgLine = if avg1 then avg1 else na;
avgLine.SetLineWeight(2);
avgLine.SetDefaultColor(Color.LIGHT_ORANGE);
#-- Signals
AddChartBubble(SHORT, high, "S", Color.RED);
AddChartBubble(LONG, low, "B", Color.GREEN, no);
#-- Bar Color
AssignPriceColor(if barHighlightColor > 0 then Color.CYAN else
if barHighlightColor < 0 then Color.MAGENTA else Color.CURRENT);
#-- END of CODE