Moving Averages Proximity Oscillator [LUX] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
Gd4WTrQ.png


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
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
446 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top