This indicator returns the percentage or count of prices greater than simple moving averages with periods in a user set range, as well as the moving average period that is the closest to price values.
The candles gave correct indication as per original script while the MovAvg line not exact output (Anyone can help on this ). Still; it can help to identify the start of new trends.
CODE:
CSS:
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
#https://www.tradingview.com/script/HGlMiFRz-Moving-Averages-Proximity-Oscillator-LUX
#// © LuxAlgo
#//@version=5
#indicator("Moving Averages Proximity Oscillator [LUX]", "MAPO [LUX]")
# Converted by Sam4Cok@Samer800 - 09-2022 (MovAvgLine not exact output)
#//------------------------------------------------------------------------------
#//Settings
#//-----------------------------------------------------------------------------{
input VolatilityLine = yes;
input minLength = 10; #, "Minimum Length"
input maxLength = 100; # "Maximum Length"
input smooth = 9;
input normalized = yes;
input src = close;
declare lower;
#//----------------------------------------------------------------------------}
#//Calculations
#//----------------------------------------------------------------------------{
def na = Double.NaN;
def csum = totalSum(src);
def max_min = absValue(src - (csum - csum[minLength]) / minLength);
def lvl = if normalized then 50 else (maxLength + minLength + 1)/2;
plot ob = if normalized then 80 else 0.8 * maxLength + 0.2 * minLength;
plot os = if normalized then 20 else 0.8 * minLength + 0.2 * maxLength;
ob.SetStyle(Curve.SHORT_DASH);
os.SetStyle(Curve.SHORT_DASH);
ob.SetDefaultColor(Color.GRAY);
os.SetDefaultColor(Color.GRAY);
def ma = fold k = minLength to maxLength with r do
(csum - csum[k])/k;
def per = fold i = minLength to maxLength with p do
p + (if src > (csum - csum[i])/i then 1 else 0);
def per1 = fold j = minLength to maxLength with q do
if normalized then Average(per, smooth) / (maxLength - minLength + 1) * 100 else
Average(per, smooth) + minLength;
def maxmin;
maxmin = if isNaN(maxmin[1]) then 0 else
fold o = minLength to maxLength with v do
min(AbsValue(src - (csum - csum[o])/o),max_min);
def len;
len = if isNaN(len[1]) then 0 else
fold m = minLength to maxLength with t do
if AbsValue(src - (csum - csum[m])/m) == maxmin
then m else Average(AbsValue(src - (csum - csum[m])/m), smooth);
def len1 = if isNaN(len1[1]) then 0 else
fold n = minLength to maxLength with u do
if normalized then (Average(len, smooth) - minLength) / (maxLength - minLength + 1) * 100 else
Average(len, smooth);
plot AvgLen = if len1 > 100 then 100 else if len1 < 0 then 0 else len1;
AvgLen.SetHiding(!VolatilityLine);
AvgLen.SetDefaultColor(Color.WHITE);
AddChart(high = if per1 > lvl then per1 else na,
low = lvl,
open = per1,
close = lvl,
type = ChartType.CANDLE, growcolor = CreateColor(0,110,98));
AddChart(high = lvl,
low = per1,
open = lvl,
close = if per1 <= lvl then per1 else na,
type = ChartType.CANDLE, growcolor = CreateColor(204,66,66));
#### End