#// This Pine Scriptโข code is subject to the terms of the Mozilla Public License 2.0 at https:/
#// ยฉ AlgoAlpha
#indicator("Standardized Median Proximity [AlgoAlpha]", "AlgoAlpha - ๐ข๐ฝ๐ช๐ท๐ญ๐ช๐ป๐ญ๐ฒ๐๐ฎ๐ญ ๐๐ฎ๐ญ๐ฒ๐ช๐ท ๐๐ป๐ธ๐๐ฒ๐ถ๐ฒ๐ฝ๐"
# Converted by Sam4Cok@Samer800 - 05/2024
declare lower;
input ColoredBars = yes; #(true, "Colored Bars?")
input timeframe = {default "Chart TF", "Custom TF"};
input customTimeframe = AggregationPeriod.FIFTEEN_MIN;
input displayOptions = {default "Standardized Median Proximity", "Median Proximity Percentile"};
input Source = FundamentalType.CLOSE; #(close, "Source")
input lookbackLength = 21; # "Lookback Length"
input NormlizedMovAvgType = AverageType.HULL;
input NormlizedMovAvgLength = 20; #, minval = 1, title = "HMA Lookback Length")
input stdDevMultiplier = 1.0; #, title = "Standard Deviation Multiplier")
input thresholdForStandardizedMp = 1.5; #, title = "Upper Boundary of Deviation Bands")
input ShowNeutralTrendForStandardizedMp = yes; #(true, "Show Neutral Trend?")
input noiseScatterplotForPercentileMp = yes; #, "Noise Scatterplot")
def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def last = IsNaN(close);
def Perc = displayOptions == displayOptions."Median Proximity Percentile";
def neut = ShowNeutralTrendForStandardizedMp;
def priceSource;
switch (timeframe) {
case "Custom TF" :
priceSource = Fundamental(FundamentalType = Source, Period = customTimeframe);
default :
priceSource = Fundamental(FundamentalType = Source);
}
DefineGlobalColor("extUp", CreateColor(255,172,50));
DefineGlobalColor("extDn", Color.MAGENTA);
DefineGlobalColor("Up", CreateColor(156, 93, 0));
DefineGlobalColor("Dn", Color.PLUM);
DefineGlobalColor("Gr", CreateColor(0, 255, 107)); #Color.GREEN);
DefineGlobalColor("dGr", CreateColor(0, 104, 44)); #Color.DARK_GREEN);
DefineGlobalColor("Re", CreateColor(254, 0, 82)); #Color.RED);
DefineGlobalColor("dRe", CreateColor(130, 0, 42)); #Color.DARK_RED);
#colorUp = input.color(#00ffbb, title = "Up Color")
#colorDown = input.color(#ff1100, title = "Down Color")
#// Calculations
def medianValue = Median(priceSource, lookbackLength);
def priceDeviation = (priceSource - medianValue);
def standardDeviation = StDev(priceDeviation, 45);
def normVal = priceDeviation / (standardDeviation + standardDeviation);
def positiveValues = if normVal > 0 then normVal else 0;
def negativeValues = if normVal < 0 then normVal else 0;
def posBandStdv = StDev(positiveValues, lookbackLength) * stdDevMultiplier;
def negBandStdv = StDev(negativeValues, lookbackLength) * stdDevMultiplier;
def upperBoundary = ExpAverage(positiveValues, lookbackLength) + posBandStdv;
def lowerBoundary = ExpAverage(negativeValues, lookbackLength) - negBandStdv;
def emaValue = MovingAverage(NormLizedMovAvgType, normVal, NormLizedMovAvgLength);
#// Color Conditions
def ColNorm = if normVal > upperBoundary then 2 else
if normVal < lowerBoundary then -2 else
if (normVal<0 and normVal < upperBoundary and emaValue > lowerBoundary and normVal > emaValue and neut) or
(normVal>0 and normVal > lowerBoundary and emaValue < upperBoundary and normVal < emaValue and neut) then 0 else
if normVal>0 then 1 else -1;
#// Plots
plot UpBand = if Perc then na else upperBoundary; # "Upper Boundary"
plot LoBand = if Perc then na else lowerBoundary; # "Lower Boundary"
plot normalizedValue = if Perc then na else normVal;
plot SigLine = if Perc then na else emaValue; # "Hull MA"
UpBand.SetDefaultColor(GlobalColor("Re")); #GlobalColor("extDn"));
LoBand.SetDefaultColor(GlobalColor("Gr")); #GlobalColor("extUp"));
SigLine.SetDefaultColor(Color.WHITE);
#normalizedValue.SetLineWeight(3);
normalizedValue.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
normalizedValue.AssignValueColor(if ColNorm == 2 then GlobalColor("extUp") else
if ColNorm == 1 then GlobalColor("Up") else
if ColNorm == -2 then GlobalColor("extDn") else
if ColNorm == -1 then GlobalColor("Dn") else Color.GRAY);
#-- OB/OS
AddCloud(2, thresholdForStandardizedMp, GlobalColor("dRe"), GlobalColor("dRe"));
AddCloud(-thresholdForStandardizedMp, -2, GlobalColor("dGr"), GlobalColor("dGr"));
#// ยฉ AlgoAlpha
#indicator("Median Proximity Percentile [AlgoAlpha]", "AlgoAlpha - % Median Proximity Percentile"
def perValue = if !Perc then na else
100 * (normVal - lowerBoundary) / (upperBoundary - lowerBoundary) - 50;
def emaPer = MovingAverage(NormLizedMovAvgType, perValue, NormLizedMovAvgLength);
#// Color Conditions
def PerCol = if perValue > 0 then 2 else
if perValue < 0 then -2 else 0;
#// Plots
def perCrossUp = (emaPer crosses below emaPer[1]);
def perCrossDn = (emaPer[1] crosses below emaPer);
plot bear = if perCrossUp and emaPer[1] > 90 then emaPer + 15 else na; # "Bearish Reversal"
plot bull = if perCrossDn and emaPer[1] < -90 then emaPer - 15 else na; # "Bullish Reversal"
bear.SetLineWeight(2);
bull.SetLineWeight(2);
bear.SetPaintingStrategy(PaintingStrategy.SQUARES);
bull.SetPaintingStrategy(PaintingStrategy.SQUARES);
bear.SetDefaultColor(GlobalColor("Re"));
bull.SetDefaultColor(GlobalColor("Gr"));
plot noiseLine = if noiseScatterplotForPercentileMp and PerCol then perValue else na;
plot zeroLine = if last then na else 0; #, color = plotColor, title = "Zero Line")
zeroLine.SetDefaultColor(Color.DARK_GRAY);
def hmaPlot = emaPer; # "Hull MA")
def hmaPlot1 = emaPer[1];
zeroLine.SetDefaultColor(Color.GRAY);
noiseLine.SetPaintingStrategy(PaintingStrategy.POINTS);
noiseLine.AssignValueColor(if PerCol > 0 then GlobalColor("Up") else GlobalColor("Dn"));
AddCloud(hmaPlot, hmaPlot1, GlobalColor("extUp"), GlobalColor("extDn"), yes);
AddCloud(hmaPlot, hmaPlot1, GlobalColor("extUp"), GlobalColor("extDn"), yes);
AddCloud(if !last and Perc then 150 else na, 120, Color.DARK_RED);
AddCloud(if !last and Perc then 150 else na, 90, Color.DARK_RED);
AddCloud(if !last and Perc then -120 else na, -150, Color.DARK_GREEN);
AddCloud(if !last and Perc then -90 else na, -150, Color.DARK_GREEN);
#-- bar color
def col = if Perc then PerCol else ColNorm;
AssignPriceColor(if !ColoredBars then Color.CURRENT else
if col == 2 then GlobalColor("extUp") else
if col == 1 then GlobalColor("Up") else
if col == -2 then GlobalColor("extDn") else
if col == -1 then GlobalColor("Dn") else Color.GRAY);
#-- END of CODE