TrendRange for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
0U1QxnN.png


trend range indicator based on ATR and CCI indicator.

CODE:
CSS:
# This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#//@version=4
#study("TrendRange", overlay=true, precision=2)
# Converted by Sam4Cok@Samer800 - 02/2023

input showInfoLabel = yes;
input BarColor     = yes;
input ShowLines    = yes;
input ShowBand     = yes;
input ShowCloud    = yes;
input Price = close;
input trendMovAvg   = AverageType.SIMPLE;
input trendAtrMulti = 5;
input tradeAtrMulti = 2;
input trendLength = 63;#, "Trend Period"
input tradeLength = 15;#, "Trade Period"
input Long1 = 10;
input Short1 = 2;
input Ratio1 = 10;
input AtrAverageType = AverageType.WILDERS;
input atr_length = 15;
input AtrMulti   = 2;

def na = Double.NaN;

#f_TrendMagic(PeriodCCI, PeriodATR, CoeffATR) =>
script f_TrendMagic {
input MovAvg    = AverageType.SIMPLE;
input Price     = close;
input PeriodCCI = 63;
input PeriodATR = 15;
input CoeffATR  = 2;
    def tr    = TrueRange(High, Close, Low);
    def nATR  = MovingAverage(MovAvg, tr, PeriodATR) * CoeffATR;
    def upT  = low  - nATR;
    def dnT  = high + nATR;
    def linDev = lindev(price, PeriodCCI);
    def src = Price;
    def ma = MovingAverage(MovAvg, src, PeriodCCI);
    def cci = (src - ma) / (0.015 * linDev(src, PeriodCCI));
    def cciT = CCI;
    def level;
    level = CompoundValue(1, if cciT >= 0 then (if upT > level[1] then upT else level[1]) else
                                               (if dnT < level[1] then dnT else level[1]), ohlc4);
    plot return = level;
}
def trend = f_TrendMagic(trendMovAvg, Price, trendLength, trendLength, trendAtrMulti);
def trade = f_TrendMagic(trendMovAvg, Price, tradeLength, tradeLength, tradeAtrMulti);

plot TrendLevel = trend;    # "Trend level"
TrendLevel.SetLineWeight(2);
TrendLevel.SetDefaultColor(Color.MAGENTA);
TrendLevel.SetHiding(!ShowLines);
plot TradeLevel = trade;    # "Trade level"
TradeLevel.SetDefaultColor(Color.ORANGE);
TradeLevel.SetHiding(!ShowLines);

AddCloud(if ShowCloud then TradeLevel else na, TrendLevel, Color.DARK_GREEN, Color.DARK_RED);

def xvnoise1 = AbsValue(Price - Price[1]);
def nAMA1;
def nfastend1 = 2 / (Short1 + 1);
def nslowend1 = 2 / (Long1 + 1);
def nsignal1 = AbsValue(Price - Price[Ratio1]);
def nnoise1 = sum(xvnoise1, Ratio1);
def nefratio1 = if(nnoise1 != 0, nsignal1 / nnoise1, 0);
def nsmooth1 = power(nefratio1 * (nfastend1 - nslowend1) + nslowend1, 2);
nAMA1 = CompoundValue(1, nAMA1[1] + nsmooth1 * (Price - nAMA1[1]), 0);

def truRange = TrueRange(High, Close, Low);

def vol = MovingAverage(AtrAverageType, truRange, atr_length);
#def vol = ATR(LENGTH=atr_length);

def smoothed = ExpAverage(truRange, 7);
def pctAdj = (smoothed / vol);
def Adj = if(pctAdj > 1.5, 1.5, pctAdj);

def upper = if((trade<trend and (close[2]<trend[2] and close[1]<trend[1] and close<trend)),
                nAMA1 + (vol * AtrMulti), nAMA1 + (vol * AtrMulti * Adj));
def lower = if((trade>trend and (close[2]>trend[2] and close[1]>trend[1] and close>trend)),
                nAMA1 - (vol * AtrMulti), nAMA1 - (vol * AtrMulti * Adj));

plot UpperBand = if ShowBand then upper else na;#, title="Upper", color=color.red)
UpperBand.SetDefaultColor(Color.RED);
plot LowerBand = if ShowBand then lower else na;#, title="Lower", color=color.green)
LowerBand.SetDefaultColor(Color.GREEN);

#//decide if price is bullish, bearish or range
def action = if((trade>trend and (close[2]>trend[2] and close[1]>trend[1] and close>trend)), 1,
             if((trade<trend and (close[2]<trend[2] and close[1]<trend[1] and close<trend)),-1, 0));

AddLabel(showInfoLabel, if action>0 then "BULLISH" else if action<0 then "BEARISH" else "NEUTRAL",
                        if action>0 then Color.GREEN else if action<0 then Color.RED else Color.GRAY);

AssignPriceColor(if !BarColor then Color.CURRENT else
                 if action>0 then if Price>trade then Color.GREEN else color.DARK_GREEN else
                 if action<0 then if Price<trade then color.RED else Color.DARK_RED else Color.GRAY);

#/pct_range
def pct_range = Round((close - lower) / (upper - lower) *100, 2);
#def upside    = Round(((upper / close) - 1) *100, 2);
#def downside  = Round(((lower / close) - 1) *100, 2);

AddLabel(showInfoLabel , pct_range + " %", if pct_range>85 then Color.PINK else if pct_range<15 then Color.LIGHT_GREEN else Color.WHITE);
#AddLabel(showInfoLabel , upside    + " % Up", Color.WHITE);
#AddLabel(showInfoLabel , downside  + " % Dn", Color.WHITE);


#--- END 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
487 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