Support & Resistance Dynamic [LuxAlgo] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
lfAw7w2.png

Author Message:

The Support & Resistance Dynamic indicator aims to return real-time predictive support and resistance zones that are relevant to a detected trend. This makes this indicator similar to our previously published Predictive Ranges indicator.

Users can additionally extend the most recent historical support and resistance zones.

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/
#// © LuxAlgo
#indicator("Support & Resistance Dynamic [LuxAlgo]"
# converted by Sam4Cok@Samer800    - 09 / 2023 - My not be typical conv.

#//Settings
input src = close;
input MultiplicativeFactor = 8.0;   # 'Multiplicative Factor'
input atrLength = 50;               # 'ATR Length'
input ExtendLevels = yes;           # 'Extend Levels'
input NoOfExtendedLines = 4;          # 'Extend Last'

def na = Double.NaN;

#//Calculation
def n = BarNumber();
def nATR = ATR(LENGTH = atrLength);
def breakout = nATR * MultiplicativeFactor;
def breakout_atr = if IsNaN(breakout) then 0 else breakout;

#def avg;
#def avg_ = if avg[1] then avg[1] else close;
def avg = if AbsValue(src - avg[1]) > breakout_atr then src else avg[1];
def lastAvg = avg ==src;
def hold_atr = if lastAvg then breakout_atr else hold_atr[1];
def os = if avg > avg[1] then 1 else if avg < avg[1] then 0 else os[1];

def avgUpRes = avg + hold_atr / MultiplicativeFactor;
def avgDnRes = avg + hold_atr / MultiplicativeFactor / 2;
def avgUpSup = avg - hold_atr / MultiplicativeFactor / 2;
def avgDnSup = avg - hold_atr / MultiplicativeFactor;

def upper_res = if os == 0 then avgUpRes else na;
def lower_res = if os == 0 then avgDnRes else na;
def upper_sup = if os == 1 then avgUpSup else na;
def lower_sup = if os == 1 then avgDnSup else na;

def uppSup = if !isNaN(upper_sup) then upper_sup else uppSup[1];
def uppRes = if !isNaN(upper_res) then upper_res else uppRes[1];

def change = (uppSup!=uppSup[1]) or (uppRes!=uppRes[1]);
def osDiff = change;#os != os[1];
def cnt = if change then cnt[1]+1 else cnt[1];
def cntHi = highestAll(cnt);
def cntCond = (cnt-1) > (cntHi) - NoOfExtendedLines ;
def plotCond = !lastAvg and cntCond;

script extend {
    input data = close;
    input plotCond = yes;
    def na = Double.NaN;
    def last = IsNaN(close);
    def cond = !IsNaN(data[1]) and IsNaN(data);
    def extend = if !plotCond then na else
                 if cond then data[1] else extend[1];
    plot out = if last then na else if extend then extend else na;
}
script extend2 {
    input data = close;
    input plotCond = yes;
    def na = Double.NaN;
    def last = IsNaN(close);
    def cond = data != data[1];
    def extend = if !plotCond then na else
                 if cond then data[1] else extend[1];
    plot out = if last then na else if extend then extend else na;
}
def upper_sup1 = extend(upper_sup, cntCond and ExtendLevels);
def lower_sup1 = extend(lower_sup, cntCond and ExtendLevels);
def upper_res1 = extend(upper_res, cntCond and ExtendLevels);
def lower_res1 = extend(lower_res, cntCond and ExtendLevels);

def upper_sup2 = extend2(upper_sup, cntCond and ExtendLevels);
def lower_sup2 = extend2(lower_sup, cntCond and ExtendLevels);
def upper_res2 = extend2(upper_res, cntCond and ExtendLevels);
def lower_res2 = extend2(lower_res, cntCond and ExtendLevels);

def upper_sup3 = extend(upper_sup2, cntCond and ExtendLevels);
def lower_sup3 = extend(lower_sup2, cntCond and ExtendLevels);
def upper_res3 = extend(upper_res2, cntCond and ExtendLevels);
def lower_res3 = extend(lower_res2, cntCond and ExtendLevels);

def upper_sup4 = extend2(upper_sup2, cntCond and ExtendLevels);
def lower_sup4 = extend2(lower_sup2, cntCond and ExtendLevels);
def upper_res4 = extend2(upper_res2, cntCond and ExtendLevels);
def lower_res4 = extend2(lower_res2, cntCond and ExtendLevels);

def upper_sup5 = extend(upper_sup4, cntCond and ExtendLevels);
def lower_sup5 = extend(lower_sup4, cntCond and ExtendLevels);
def upper_res5 = extend(upper_res4, cntCond and ExtendLevels);
def lower_res5 = extend(lower_res4, cntCond and ExtendLevels);

def upper_sup6 = extend2(upper_sup4, cntCond and ExtendLevels);
def lower_sup6 = extend2(lower_sup4, cntCond and ExtendLevels);
def upper_res6 = extend2(upper_res4, cntCond and ExtendLevels);
def lower_res6 = extend2(lower_res4, cntCond and ExtendLevels);


#//Plot supports
def upperSup1  = upper_sup1;
plot lowerSup1 = lower_sup1;#if !ExtendLevels or lastAvg then na else lower_sup1;
plot upperres1 = upper_res1;#if !ExtendLevels or lastAvg then na else upper_res1;
def lowerres1  = lower_res1;

def upperSup2  = upper_sup2;
plot lowerSup2 = lower_sup2;#if !ExtendLevels or lastAvg then na else lower_sup2;
plot upperres2 = upper_res2;#if !ExtendLevels or lastAvg then na else upper_res2;
def lowerres2  = lower_res2;

def upperSup3  = upper_sup3;
plot lowerSup3 = lower_sup3;#if !ExtendLevels or lastAvg then na else lower_sup3;
plot upperres3 = upper_res3;#if !ExtendLevels or lastAvg then na else upper_res3;
def lowerres3  = lower_res3;

def upperSup4  = upper_sup4;
plot lowerSup4 = lower_sup4;#if !ExtendLevels or lastAvg then na else lower_sup4;
plot upperres4 = upper_res4;#if !ExtendLevels or lastAvg then na else upper_res4;
def lowerres4  = lower_res4;

def upperSup5  = upper_sup5;
plot lowerSup5 = lower_sup5;#if !ExtendLevels or lastAvg then na else lower_sup5;
plot upperres5 = upper_res5;#if !ExtendLevels or lastAvg then na else upper_res5;
def lowerres5  = lower_res5;

def upperSup6  = upper_sup6;
plot lowerSup6 = lower_sup6;#if !ExtendLevels or lastAvg then na else lower_sup6;
plot upperres6 = upper_res6;#if !ExtendLevels or lastAvg then na else upper_res6;
def lowerres6  = lower_res6;

lowerSup1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
upperres1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowerSup2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
upperres2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowerSup3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
upperres3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowerSup4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
upperres4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowerSup5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
upperres5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowerSup6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
upperres6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);


def plot_upper_sup = if lastAvg then na else upper_sup;    # 'Upper Support'
plot plot_lower_sup = if lastAvg then na else lower_sup;   # 'Lower Support'
plot_lower_sup.SetDefaultColor(Color.GREEN);
#//Plot resistances
def plot_lower_res = if lastAvg then na else lower_res;
plot plot_upper_res = if lastAvg then na else upper_res;#, 'Upper Resistance'
plot_upper_res.SetDefaultColor(Color.RED);
#//Fills
AddCloud(plot_upper_sup,  plot_lower_sup,  Color.GREEN);    # 'Support Area')
AddCloud(plot_upper_res, plot_lower_res, Color.RED);        # 'Resistance Area')


lowerSup1.SetDefaultColor(Color.DARK_GREEN);
upperres1.SetDefaultColor(Color.DARK_RED);
lowerSup1.SetStyle(Curve.MEDIUM_DASH);
upperres1.SetStyle(Curve.MEDIUM_DASH);

lowerSup2.SetDefaultColor(Color.DARK_GREEN);
upperres2.SetDefaultColor(Color.DARK_RED);
lowerSup2.SetStyle(Curve.MEDIUM_DASH);
upperres2.SetStyle(Curve.MEDIUM_DASH);

lowerSup3.SetDefaultColor(Color.DARK_GREEN);
upperres3.SetDefaultColor(Color.DARK_RED);
lowerSup3.SetStyle(Curve.MEDIUM_DASH);
upperres3.SetStyle(Curve.MEDIUM_DASH);

lowerSup4.SetDefaultColor(Color.DARK_GREEN);
upperres4.SetDefaultColor(Color.DARK_RED);
lowerSup4.SetStyle(Curve.MEDIUM_DASH);
upperres4.SetStyle(Curve.MEDIUM_DASH);

lowerSup5.SetDefaultColor(Color.DARK_GREEN);
upperres5.SetDefaultColor(Color.DARK_RED);

upperres5.SetStyle(Curve.MEDIUM_DASH);

lowerSup6.SetDefaultColor(Color.DARK_GREEN);
upperres6.SetDefaultColor(Color.DARK_RED);
lowerSup6.SetStyle(Curve.MEDIUM_DASH);
upperres6.SetStyle(Curve.MEDIUM_DASH);


AddCloud(upper_Sup1,  lower_Sup1,  Color.DARK_GREEN);#, 'Support Area')
AddCloud(upper_res1, lower_res1, Color.DARK_RED);#, 'Resistance Area')

AddCloud(upper_Sup2,  lower_Sup2,  Color.DARK_GREEN);#, 'Support Area')
AddCloud(upper_res2, lower_res2, Color.DARK_RED);#, 'Resistance Area')

AddCloud(upper_Sup3,  lower_Sup3,  Color.DARK_GREEN);#, 'Support Area')
AddCloud(upper_res3, lower_res3, Color.DARK_RED);#, 'Resistance Area')

AddCloud(upperSup4,  lowerSup4,  Color.DARK_GREEN);#, 'Support Area')
AddCloud(upperres4, lowerres4, Color.DARK_RED);#, 'Resistance Area')

AddCloud(upperSup5,  lowerSup5,  Color.DARK_GREEN);#, 'Support Area')
AddCloud(upperres5, lowerres5, Color.DARK_RED);#, 'Resistance Area')

AddCloud(upperSup6,  lowerSup6,  Color.DARK_GREEN);#, 'Support Area')
AddCloud(upperres6, lowerres6, Color.DARK_RED);#, 'Resistance Area')



#--- END of CODE
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
362 Online
Create Post

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