Braid Filter with BarColor[Mod Sam4Cok] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
Pf0UZYa.png

the below added to TV script:
- MovAvg trend line - Zero Level - .
-ADX customized filter.
-Bar color based on the braid filter and trendline.
-Signal as trigger point with your own strategy.

You can disable all option and use the indicator as original Braid filter.

CSS:
#// Copyright © Robert Hill, 2006
#//@version=4
#https://www.tradingview.com/script/PfpFNXyI-Braid-Filter/
#study("Braid Filter")
# Converted and modified by Sam4Cok@Samer800 - 08/2022

#//-- Inputs
declare lower;
input ColorBar   = yes;
input ShowSignal = yes;
input maType = {default "EMA", "DEMA", "TEMA", "WMA", "VWMA", "SMA", "SMMA", "HMA", "LSMA", "Kijun", "McGinley", "RMA"};
input BraidPeriod1 = 3;
input BraidPeriod2 = 7;
input BraidPeriod3 = 14;
input PipsMinSepPercent = 40;
input ShowMALine = yes;  # "Show MovAvg Trend Line?"
input MovAvgLen  = 34;   # "MovingAvg Period"
input ADXLen     = 14;
input ADXthreshold = 20;

def na = Double.NaN;
script nz {
    input data = 0;
    input replacement  = 0;
    def ret_val = if IsNaN(data) then replacement else data;
    plot return = ret_val;
}

script RMA {
    input src = close;
    input length = 14;
    def   alpha = 1 / length;
    def sum;  
sum = if IsNaN(sum[1]) then simpleMovingAvg(src, length) else alpha * src + (1 - alpha) * nz(sum[1]);
    Plot  Return = sum;
}

#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    def VWMA = SimpleMovingAvg(x * volume, y) / SimpleMovingAvg(volume, y);
    plot result = VWMA;
}
#ma(type, src, len) =>
script ma {
    input type   = "EMA";
    input src = close;
    input len = 0;
    def e = ExpAverage(src, len);
    def w = WMA(src, len);
    def Mcg = CompoundValue(1, Mcg[1] + ((src - Mcg[1]) / (len * Power(src / Mcg[1], 4))), src);
    def ma;
    ma = if type == "SMA" then SimpleMovingAvg(src, len) else
     if type == "EMA" then ExpAverage(src, len) else
     if type == "DEMA" then 2 * e - ExpAverage(e, len) else
     if type == "TEMA" then 3 * (e - ExpAverage(e, len)) + ExpAverage(ExpAverage(e, len), len) else
     if type == "WMA" then WMA(src, len) else
     if type == "VWMA" then vwma(src, len) else
     if type == "SMMA" then if isNaN(w[1]) then SimpleMovingAvg(src, len) else
                                           (w[1] * (len - 1) + src) / len else
     if type == "HMA" then wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) else
     if type == "RMA" then RMA(src, len) else
     if type == "Kijun" then (Lowest(low, len) + Highest(high, len)) / 2 else
     if type == "LSMA" then Inertia(src, len) else
     if type == "McGinley" then if IsNaN(ma[1]) then ExpAverage(src, len) else
                Mcg else Double.NaN;
    plot result = ma;
}

#//-- Braid Filter  
def ma01 = ma(maType, close,BraidPeriod1);
def ma02 = ma(maType, open, BraidPeriod2);
def ma03 = ma(maType, close,BraidPeriod3);

def max  = max(max(ma01, ma02), ma03);
def min  = min(min(ma01, ma02), ma03);
def dif  = max - min;

def filter = atr(14) * PipsMinSepPercent / 100;

#//-- Plots
def BraidColor = if ma01 > ma02 and dif > filter then 1 else
                 if ma02 > ma01 and dif > filter then -1 else 0;

plot FilterLine = filter; #, "Filter", color.blue, 2, plot.style_line)
FilterLine.SetDefaultColor(CreateColor(33,150,243));
FilterLine.SetLineWeight(2);

plot Braid = dif; #, "Braid", BraidColor, 5, plot.style_columns)
Braid.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Braid.AssignValueColor( if BraidColor > 0 then CreateColor(76,175,80) else
                        if BraidColor < 0 then CreateColor(255,82,82) else Color.GRAY);
Braid.SetLineWeight(2);


AddCloud(if BraidColor > 0 then Double.POSITIVE_INFINITY else Double.NEGATIVE_INFINITY,
         if BraidColor < 0 then Double.POSITIVE_INFINITY else Double.NEGATIVE_INFINITY, Color.DARK_GREEN, Color.DARK_RED);

#### Moving Average
def MovAvg = nz(ma(maType, hlc3, MovAvgLen), MovAvg[1]);

plot MovLine = if ShowMALine then -0.01 else na;
MovLine.SetLineWeight(4);
MovLine.AssignValueColor(if hlc3  >= MovAvg then color.GREEN else
                         if hlc3  < MovAvg then color.LIGHT_RED else color.white);
 
#// This source code is subject to the terms of the Mozilla Public License 2.0 at #https://mozilla.org/MPL/2.0/
#// © BeikabuOyaji
#study("ADX and DI for v4")

def TrueRange = max(max(high-low, AbsValue(high-nz(close[1]))), AbsValue(low-nz(close[1])));
def DirPlus = if high-nz(high[1]) > nz(low[1])-low then max(high-nz(high[1]), 0) else 0;
def DirMinus = if nz(low[1])-low > high-nz(high[1]) then max(nz(low[1])-low, 0) else 0;
def SmoothedTrueRange = nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/ADXLen) + TrueRange;
def SmoothedDiPlus = nz(SmoothedDiPlus[1]) - (nz(SmoothedDiPlus[1])/ADXLen) + DirPlus;
def SmoothedDiMinus = nz(SmoothedDiMinus[1]) - (nz(SmoothedDiMinus[1])/ADXLen) + DirMinus;
def DIPlus = SmoothedDiPlus / SmoothedTrueRange * 100;
def DIMinus = SmoothedDiMinus / SmoothedTrueRange * 100;
def DX = AbsValue(DIPlus-DIMinus) / (DIPlus+DIMinus)*100;
def ADX = SimpleMovingAvg(DX, ADXLen);

########### Signals
def MovUp = if hlc3 >= MovAvg then MovAvg else na;
def MovDn = if hlc3 < MovAvg then MovAvg else na;
def AdxSignal = ADX >= ADXthreshold and ADX > ADX[1];
def BraidUp = BraidColor > 0 and BraidColor[1] <=0;
def BraidDN = BraidColor < 0 and BraidColor[1] >=0;
def SignalUp = close > MovUp and AdxSignal and BraidUp;
def SignalDn = close < MovDn and AdxSignal and BraidDn;

plot PointUp = if SignalUp then dif + dif * 0.4 else na;
PointUp.SetPaintingStrategy(PaintingStrategy.SQUARES);
PointUp.SetDefaultColor(Color.GREEN);
PointUp.SetLineWeight(3);
PointUp.SetHiding(!ShowSignal);

plot PointDn = if SignalDn then dif + dif * 0.4 else na;
PointDn.SetPaintingStrategy(PaintingStrategy.SQUARES);
PointDn.SetDefaultColor(Color.RED);
PointDn.SetLineWeight(3);
PointDn.SetHiding(!ShowSignal);

AssignPriceColor(if colorBar then if BraidColor > 0 and hlc3 >= MovAvg then color.GREEN else
                                  if BraidColor > 0 and hlc3  < MovAvg then Color.LIGHT_GREEN else
                                  if BraidColor < 0 and hlc3  < MovAvg then Color.RED else
                                  if BraidColor < 0 and hlc3 >= MovAvg then Color.PINK else
                                    Color.GRAY else Color.CURRENT);

#### End

Copy and paste below for the signal scan. Remove the "#" for up or down signals.

CSS:
# Braid Scanner - Sam4Cok@Samer800
input BraidPeriod1 = 3;
input BraidPeriod2 = 7;
input BraidPeriod3 = 14;
input PipsMinSepPercent = 40;
input ShowMALine = yes;  # "Show MovAvg Trend Line?"
input MovAvgLen  = 34;   # "MovingAvg Period"
input ADXLen     = 14;
input ADXthreshold = 20;

def na = Double.NaN;
script nz {
    input data = 0;
    input replacement  = 0;
    def ret_val = if IsNaN(data) then replacement else data;
    plot return = ret_val;
}

#//-- Braid Filter   
def ma01 = ExpAverage(close,BraidPeriod1);
def ma02 = ExpAverage(open, BraidPeriod2);
def ma03 = ExpAverage(close,BraidPeriod3);

def max  = max(max(ma01, ma02), ma03);
def min  = min(min(ma01, ma02), ma03);
def dif  = max - min;

def filter = atr(14) * PipsMinSepPercent / 100;

#//-- Plots
def BraidColor = if ma01 > ma02 and dif > filter then 1 else
                 if ma02 > ma01 and dif > filter then -1 else 0;


#### Moving Average
def MovAvg = nz(ExpAverage(hlc3, MovAvgLen), MovAvg[1]);


#// This source code is subject to the terms of the Mozilla Public License 2.0 at #https://mozilla.org/MPL/2.0/
#// © BeikabuOyaji
#study("ADX and DI for v4")

def TrueRange = max(max(high-low, AbsValue(high-nz(close[1]))), AbsValue(low-nz(close[1])));
def DirPlus = if high-nz(high[1]) > nz(low[1])-low then max(high-nz(high[1]), 0) else 0;
def DirMinus = if nz(low[1])-low > high-nz(high[1]) then max(nz(low[1])-low, 0) else 0;
def SmoothedTrueRange = nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/ADXLen) + TrueRange;
def SmoothedDiPlus = nz(SmoothedDiPlus[1]) - (nz(SmoothedDiPlus[1])/ADXLen) + DirPlus;
def SmoothedDiMinus = nz(SmoothedDiMinus[1]) - (nz(SmoothedDiMinus[1])/ADXLen) + DirMinus;
def DIPlus = SmoothedDiPlus / SmoothedTrueRange * 100;
def DIMinus = SmoothedDiMinus / SmoothedTrueRange * 100;
def DX = AbsValue(DIPlus-DIMinus) / (DIPlus+DIMinus)*100;
def ADX = SimpleMovingAvg(DX, ADXLen);

########### Signals
def MovUp = if hlc3 >= MovAvg then MovAvg else na;
def MovDn = if hlc3 < MovAvg then MovAvg else na;
def AdxSignal = ADX >= ADXthreshold and ADX > ADX[1];
def BraidUp = BraidColor > 0 and BraidColor[1] <=0;
def BraidDN = BraidColor < 0 and BraidColor[1] >=0;
def SignalUp = close > MovUp and AdxSignal and BraidUp;
def SignalDn = close < MovDn and AdxSignal and BraidDn;

plot PointUp = SignalUp within 3 bars;
#plot PointDn = SignalDn within 3 bars; # remove the "#" for down signal and hash the up signal
 
Last edited:

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