#The Official Savage Bands
#https://usethinkscript.com/threads/savage-oscillator.4214/
#Assembled by FateOwnzYou on UseThinkscript.com
###MTF###
input Sensitivity = 1;
input Distance = 5;
input Smoothness = 7;
input MTF = yes;
input currentTF = no;
input TimeFrame = aggregationPeriod.DAY;
input HighVolume = no;
input volumeAveragingLength = 20;
input volumePercentThreshold = 50;
def S1_MTF = Max(-100, Min(100, (StochasticFull(KPeriod = 8, slowing_period = 5, averageType = AverageType.EXPONENTIAL, priceh = high(period=TimeFrame), pricel = low(period=TimeFrame), pricec = close(period=TimeFrame)))) - 50) / 50.01;
def S2_MTF = Max(-100, Min(100, (StochasticFull(KPeriod = 17, slowing_period = 5, averageType = AverageType.EXPONENTIAL, priceh = high(period=TimeFrame), pricel = low(period=TimeFrame), pricec = close(period=TimeFrame)))) - 50) / 50.01;
def S3_MTF = Max(-100, Min(100, reference RSI(2, price = close(period=timeframe)))- 50) / 50.01;
def savage_MTF = (ExpAverage((S1_MTF + S2_MTF + S3_MTF) / 3, Sensitivity)) * 10;
def bottom_MTF = Min(close(period=TimeFrame)[1], low(period=TimeFrame));
def tr_MTF = TrueRange(high(period=TimeFrame), close(period=TimeFrame), low(period=TimeFrame));
def ptr_MTF = tr_MTF / (bottom_MTF + tr_MTF / 2) * 100;
def APTR_MTF = (MovingAverage(AverageType.WILDERS, ptr_MTF, 14))*Distance;
def LowLine_MTF = ((0.0625*(((APTR_MTF*close(period=TimeFrame))/100)*-1))*(savage_MTF+7.75))+close(period=TimeFrame);
def HighLine_MTF = ((0.01*((APTR_MTF*close(period=TimeFrame))/100))*((-6.25*savage_MTF)+51.5625))+close(period=TimeFrame);
plot SmoothedLow_MTF = if MTF then (movingaverage(data = LowLine_MTF, averageType = AverageType.WEIGHTED, length = Smoothness)) else double.nan;
smoothedlow_MTF.setdefaultColor(color.red);
plot SmoothedHigh_MTF = if MTF then (movingaverage(data = HighLine_MTF, averageType = AverageType.WEIGHTED, length = Smoothness)) else double.nan;
smoothedhigh_MTF.setdefaultColor(color.red);
def aVol_MTF = Average(volume(period = TimeFrame), volumeAveragingLength);
def pVol_MTF = 100 * ((volume(period = TimeFrame) - aVol_MTF[1]) /aVol_MTF[1]);
def pDot_MTF = pVol_MTF >= volumePercentThreshold;
#plot volumeStrength = if pDot and HighVolume then hl2 else Double.NaN;
plot MidLine_MTF = ((SmoothedHigh_MTF - SmoothedLow_MTF)/2)+SmoothedLow_MTF;
MidLine_MTF.assignvalueColor(if pdot_MTF and HighVolume and MTF then color.yellow else color.red);
### End MTF
#Stochastics
def S1 = Max(-100, Min(100, (StochasticFull(KPeriod = 8, slowing_period = 5, averageType = AverageType.EXPONENTIAL, priceh = high, pricel = low, pricec = close))) - 50) / 50.01;
def S2 = Max(-100, Min(100, (StochasticFull(KPeriod = 17, slowing_period = 5, averageType = AverageType.EXPONENTIAL, priceh = high, pricel = low, pricec = close))) - 50) / 50.01;
def S3 = Max(-100, Min(100, reference RSI(2, price = close))- 50) / 50.01;
def savage = (ExpAverage((S1 + S2 + S3) / 3, Sensitivity)) * 10;
def bottom = Min(close[1], low);
def tr = TrueRange(high, close, low);
def ptr = tr / (bottom + tr / 2) * 100;
def APTR = (MovingAverage(AverageType.WILDERS, ptr, 14))*Distance;
def LowLine = ((0.0625*(((APTR*close)/100)*-1))*(savage+7.75))+close;
def HighLine = ((0.01*((APTR*close)/100))*((-6.25*savage)+51.5625))+close;
plot SmoothedLow = if CurrentTF then (movingaverage(data = LowLine, averageType = AverageType.WEIGHTED, length = Smoothness)) else double.nan;
smoothedlow.setdefaultColor(color.cyan);
plot SmoothedHigh = if CurrentTF then (movingaverage(data = HighLine, averageType = AverageType.WEIGHTED, length = Smoothness)) else double.nan;
smoothedhigh.setdefaultColor(color.cyan);
def aVol = Average(volume, volumeAveragingLength);
def pVol = 100 * ((volume - aVol[1]) /aVol[1]);
def pDot = pVol >= volumePercentThreshold;
plot MidLine = ((SmoothedHigh - SmoothedLow)/2)+SmoothedLow;
MidLine.assignvaluecolor(if pdot and HighVolume then color.yellow else color.cyan);