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