Power Root SuperTrend [AlgoAlpha] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
cnyQOHe.png


Author Message:

Power Root SuperTrend by AlgoAlpha, an advanced trading indicator that enhances the traditional SuperTrend by incorporating Root-Mean-Square (RMS) calculations for a more responsive and adaptive trend detection. This innovative tool is designed to help traders identify trend directions, potential take-profit levels, and optimize entry and exit points with greater accuracy, making it an excellent addition to your trading arsenal.

CODE:

CSS:
# // Indicator for TOS
#// © AlgoAlpha
#indicator("Power Root SuperTrend [AlgoAlpha]", "AlgoAlpha - Power Root", true, max_lines_count = 500)
# Conveted by Sam4Cok@Samer800     - 11/2024

input src  = close;
input atrFactor = 4.5;            # "Factor"
input atrLength = 12;             # "ATR Length"
input RootMeanSquareLength = 3;   # "Root-Mean-Square Length"
input rsiTakeProfitLength  = 14;  # "RSI Take-Profit Length"

def na = Double.NaN;
def last = IsNaN(close);
def mid = (open + close) / 2;
#// SuperTrend Function
script superTrendCalc {
    input multiplier = 4.5;
    input atrLength = 12;
    input source = hl2;
    def lowerLevel;
    def upperLevel;
    def atrValue1 = ATR(Length = atrLength);
    def upper = source + multiplier * atrValue1;
    def lower = source - multiplier * atrValue1;
    def  previousLowerLevel = CompoundValue(1, lowerLevel[1], 0);
    def previousUpperLevel = CompoundValue(1, upperLevel[1], 0);
    lowerLevel = if lower > previousLowerLevel or source[1] < previousLowerLevel then lower else previousLowerLevel;
    upperLevel = if upper < previousUpperLevel or source[1] > previousUpperLevel then upper else previousUpperLevel;
    def trendDirection;
    def trendValue;
    def previousTrend = CompoundValue(1, trendValue[1], 0);
    if (!atrValue1[1]) {
        trendDirection = 1;
    } else if previousTrend == previousUpperLevel {
        trendDirection = if source > upperLevel then -1 else 1;
    } else {
        trendDirection = if source < lowerLevel then 1 else -1;
    }
    trendValue = if trendDirection == -1 then lowerLevel else upperLevel;
    plot st = trendValue;
    plot dir = trendDirection;
}
def sumSrc = sum(Sqr(src), RootMeanSquareLength);
def rms = Sqrt(sumSrc / RootMeanSquareLength);

def superTrendValue = superTrendCalc(atrFactor, atrLength, rms). st;
def trendDirection  = superTrendCalc(atrFactor, atrLength, rms).dir;
def dist = AbsValue(src - superTrendValue);
def cross = trendDirection Crosses 0;
def col1 = if trendDirection[-1] > 0 then -1 else 1;
def col = if isNaN(col1) then col[1] else if col1 then col1 else col[1];

def chg; # = 0.0
def tp1; # = 0.0
def tp2; # = 0.0
def tp3; # = 0.0
def tp4; # = 0.0
def tp5; # = 0.0
def tp6; # = 0.0
def tp7; # = 0.0
def printedtp2; # = 0
def printedtp3; # = 0
def printedtp4; # = 0
def printedtp5; # = 0
def printedtp6; # = 0
def printedtp7; # = 0
def printedtp22; # = 0
def printedtp33; # = 0
def printedtp44; # = 0
def printedtp55; # = 0
def printedtp66; # = 0
def printedtp77; # = 0

if cross {
    printedtp2 = 0;
    printedtp3 = 0;
    printedtp4 = 0;
    printedtp5 = 0;
    printedtp6 = 0;
    printedtp7 = 0;
    chg = AbsValue(superTrendValue - superTrendValue[1]);
    tp1 = superTrendValue[1] + (if trendDirection > 0 then -chg else chg);
    tp2 = superTrendValue[1] + (if trendDirection > 0 then -chg * 2 else chg * 2);
    tp3 = superTrendValue[1] + (if trendDirection > 0 then -chg * 3 else chg * 3);
    tp4 = superTrendValue[1] + (if trendDirection > 0 then -chg * 4 else chg * 4);
    tp5 = superTrendValue[1] + (if trendDirection > 0 then -chg * 5 else chg * 5);
    tp6 = superTrendValue[1] + (if trendDirection > 0 then -chg * 6 else chg * 6);
    tp7 = superTrendValue[1] + (if trendDirection > 0 then -chg * 7 else chg * 7);
    } else {
    printedtp2 = printedtp22[1];
    printedtp3 = printedtp33[1];
    printedtp4 = printedtp44[1];
    printedtp5 = printedtp55[1];
    printedtp6 = printedtp66[1];
    printedtp7 = printedtp77[1];
    chg = chg[1];
    tp1 = tp1[1];
    tp2 = tp2[1];
    tp3 = tp3[1];
    tp4 = tp4[1];
    tp5 = tp5[1];
    tp6 = tp6[1];
    tp7 = tp7[1];
}
def nRSI = rsi(Price = dist, Length = rsiTakeProfitLength);
def tp = nRSI Crosses Below 60;

def extreme = if trendDirection > 0 then low else high;
def extreme_tp1_dist = AbsValue(extreme - tp1);
def extreme_tp2_dist = AbsValue(extreme - tp2);
def extreme_tp3_dist = AbsValue(extreme - tp3);
def extreme_tp4_dist = AbsValue(extreme - tp4);
def extreme_tp5_dist = AbsValue(extreme - tp5);
def extreme_tp6_dist = AbsValue(extreme - tp6);
def extreme_tp7_dist = AbsValue(extreme - tp7);

    printedtp22 = if printedtp2 == 0 and extreme_tp2_dist < extreme_tp1_dist then 1 else printedtp2;
    printedtp33 = if printedtp3 == 0 and extreme_tp3_dist < extreme_tp2_dist then 1 else printedtp3;
    printedtp44 = if printedtp4 == 0 and extreme_tp4_dist < extreme_tp3_dist then 1 else printedtp4;
    printedtp55 = if printedtp5 == 0 and extreme_tp5_dist < extreme_tp4_dist then 1 else printedtp5;
    printedtp66 = if printedtp6 == 0 and extreme_tp6_dist < extreme_tp5_dist then 1 else printedtp6;
    printedtp77 = if printedtp7 == 0 and extreme_tp7_dist < extreme_tp6_dist then 1 else printedtp7;

plot sTP = if tp and trendDirection > 0 then low else na;  # "RSI-Based Shorts TP"
plot lTP = if tp and trendDirection < 0 then high else na; # "RSI-Based Longs TP"

sTP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
lTP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
sTP.SetDefaultColor(Color.MAGENTA);
lTP.SetDefaultColor(Color.CYAN);

#-- TP Lines
plot tpl1 = if !last and tp1[-1] then tp1[-1] else na;
plot tpl2= if !last and printedtp22[-1] then tp2[-1] else na;
plot tpl3= if !last and printedtp33[-1] then tp3[-1] else na;
plot tpl4= if !last and printedtp44[-1] then tp4[-1] else na;
plot tpl5= if !last and printedtp55[-1] then tp5[-1] else na;
plot tpl6= if !last and printedtp66[-1] then tp6[-1] else na;
plot tpl7= if !last and printedtp77[-1] then tp7[-1] else na;
plot stLine = if superTrendValue then superTrendValue else na;

AddCloud(stLine, mid, Color.DARK_RED, Color.DARK_GREEN);

stLine.AssignValueColor(if trendDirection > 0 then Color.DARK_RED else Color.DARK_GREEN);
tpl1.SetLineWeight(2);
tpl1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tpl2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tpl3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tpl4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tpl5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tpl6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tpl7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
tpl1.AssignValueColor(if col>0 then Color.CYAN else Color.MAGENTA);
tpl2.AssignValueColor(if col>0 then Color.CYAN else Color.MAGENTA);
tpl3.AssignValueColor(if col>0 then Color.CYAN else Color.MAGENTA);
tpl4.AssignValueColor(if col>0 then Color.CYAN else Color.MAGENTA);
tpl5.AssignValueColor(if col>0 then Color.CYAN else Color.MAGENTA);
tpl6.AssignValueColor(if col>0 then Color.CYAN else Color.MAGENTA);
tpl7.AssignValueColor(if col>0 then Color.CYAN else Color.MAGENTA);


#-- END of CODE
 

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
364 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