Breakouts with Tests & Retests [LuxAlgo] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
B7oTudT.png


Author Message:

The Breakouts Tests & Retests indicator highlights tests and retests of levels constructed from detected swing points. A swing area of interest switches colors when a breakout occurs.

Users can control the sensitivity of the swing point detection and the width of the swing areas.

More Details can find here: https://www.tradingview.com/v/RTAaXDDr/

CODE:

CSS:
#// Indicator for TOS
#// © LuxAlgo
#indicator('Swing Breakouts Tests & Retests [LuxAlgo]', shorttitle='LuxAlgo - Swing Breakouts Tests & Retests',
# Converted by Sam4Cok@Samer800    - 07/2028 - Not Exact Conversion -

input timeframe   = {Default "Chart", "Custom Timeframe"};
input customTimeframe = AggregationPeriod.FIFTEEN_MIN;
input SwingType = {"Bullish OR Bearish", default "Bullish AND Bearish", "Bearish", "Bullish"}; #  'Display'
input Width     = 1.0; #   , 'Width'
input MaximumBarWithoutSignal = 500; # , 'Maximum Bars, Maximum bars without signal'
input DisplayOptions = {default "All", "Last"}; #'Display Test/Retest'
input MinimumBarBetweenSignals = 3; # 'Minimum Bars, Minimum required amount of bars between each labels'
input pivotLeftLookback  = 10; # 'Left'
input pivotRightLookback = 1; #, 'Right'

def na = Double.NaN;
def last = IsNaN(close);
def all = DisplayOptions==DisplayOptions."All";
def n = BarNumber();
def h;
def l;
def c;
def o;
Switch (timeframe) {
Case "Custom Timeframe" :
    h = if !last then high(Period = customTimeframe)  else h[1];
    l = if !last then low(Period = customTimeframe)   else l[1];
    c = if !last then close(Period = customTimeframe) else c[1];
    o = if !last then open(Period = customTimeframe)  else o[1];
Default :
    h = high;
    l = low;
    c = close;
    o = open;
}
script Pivots {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}
#//Execution
def nanATR = TotalSum(if n> 0 then h - l else 0) / (n + 1);
def tr = TrueRange(h, c, l);
def nATR = WildersAverage(tr, 200);
def atr  = if IsNaN(nATR) then nanATR * Width else nATR * Width;

def ph = pivots(h, pivotLeftLookback, pivotRightLookback, yes);
def pl = pivots(l, pivotLeftLookback, pivotRightLookback, no);

def bullBoxTop;
def bullBoxBot;
def bullLab1;
def bullLab2;
def bullCnt;
def bullL1Cnt;
def bullL2Cnt;
def bullCol;
def bullState = {default "0", "1", "2"};
def bearState = {default "0", "1", "2"};
def bullCond = SwingType == SwingType."Bullish" or SwingType == SwingType."Bullish AND Bearish" or
              (SwingType == SwingType."Bullish OR Bearish" and bearState[1]== bearState."0");
if bullCond {
switch (bullState[1]) {
case "0":
    if !IsNaN(pl) {
        bullState = bullState."1";
        bullCnt = 0;
        bullBoxTop = pl + atr;
        bullBoxBot = pl;
        bullLab1  = 0;
        bullLab2  = 0;
        bullL1Cnt = 0;
        bullL2Cnt = 0;
        bullCol   = 0;
    } else {
        bullState = bullState."0";
        bullCnt = 0;
        bullBoxTop = na;
        bullBoxBot = na;
        bullLab1  = 0;
        bullLab2  = 0;
        bullL1Cnt = 0;
        bullL2Cnt = 0;
        bullCol   = 0;
    }
case "1":
    if bullCnt[1] >= MaximumBarWithoutSignal {
        bullState = bullState."0";
        bullCnt = 0;
        bullBoxTop = na;
        bullBoxBot = na;
        bullLab1  = 0;
        bullLab2  = 0;
        bullL1Cnt = 0;
        bullL2Cnt = 0;
        bullCol   = 0;
    } else {
        bullState = if c < bullBoxBot[1] then bullState."2" else bullState."1";
        bullCnt = bullCnt[1] + 1;
        bullBoxTop = bullBoxTop[1];
        bullBoxBot = bullBoxBot[1];
        bullLab2  = 0;
        bullL2Cnt = 0;
    if c > bullBoxTop and o < bullBoxTop and
        (if (!bullLab1[1] or !all) then yes else (all and (bullL1Cnt[1] > MinimumBarBetweenSignals))) {
            bullLab1  = bullLab1[1] + 1;
            bullL1Cnt = 0;
        } else {
            bullLab1  = bullLab1[1];
            bullL1Cnt = if bullLab1 > 0 then bullL1Cnt[1] + 1 else 0;
    }
        bullCol   = if bullLab1 > 0 then 1 else 0;
}
case "2":
    if bullCnt[1] >= MaximumBarWithoutSignal {
        bullState = bullState."0";
        bullCnt = 0;
        bullBoxTop = na;
        bullBoxBot = na;
        bullLab1  = 0;
        bullLab2  = 0;
        bullL1Cnt = 0;
        bullL2Cnt = 0;
        bullCol   = 0;
    } else {
        bullState = if c > bullBoxTop[1] then bullState."0" else bullState."2";
        bullCnt = bullCnt[1] + 1;
        bullBoxTop = bullBoxTop[1];
        bullBoxBot = bullBoxBot[1];
        bullLab1  = bullLab1[1];
        bullL1Cnt = if bullLab1 > 0 then bullL1Cnt[1] + 1 else 0;
    if c < bullBoxBot and o > bullBoxBot and
        (if (!bullLab2[1] or !all) then yes else (all and (bullL2Cnt[1] > MinimumBarBetweenSignals))) {
            bullLab2  = bullLab2[1] + 1;
            bullL2Cnt = 0;
    } else {
            bullLab2  = bullLab2[1];
            bullL2Cnt = if bullLab2 > 0 then bullL2Cnt[1] + 1 else 0;
        }
        bullCol   = if (bullLab1 or bullLab2) then -1 else 0;
    }
}
    } else {
        bullState = bullState."0";
        bullCnt = 0;
        bullBoxTop = na;
        bullBoxBot = na;
        bullLab1  = 0;
        bullLab2  = 0;
        bullL1Cnt = 0;
        bullL2Cnt = 0;
        bullCol   = 0;
    }

def bearBoxTop;
def bearBoxBot;
def bearLab1;
def bearLab2;
def bearCnt;
def bearL1Cnt;
def bearL2Cnt;
def bearCol;
def bearCond = SwingType == SwingType."Bearish" or SwingType == SwingType."Bullish AND Bearish" or
              (SwingType == SwingType."Bullish OR Bearish" and bullState==bullState."0");

if bearCond {
switch (bearState[1]) {
case "0":
    if !IsNaN(ph) {
        bearState = bearState."1";
        bearCnt = 0;
        bearBoxTop = ph;
        bearBoxBot = ph - atr;
        bearLab1  = 0;
        bearLab2  = 0;
        bearL1Cnt = 0;
        bearL2Cnt = 0;
        bearCol = 0;
    } else {
        bearState = bearState."0";
        bearCnt = 0;
        bearBoxTop = na;
        bearBoxBot = na;
        bearLab1  = 0;
        bearLab2  = 0;
        bearL1Cnt = 0;
        bearL2Cnt = 0;
        bearCol = 0;

}
case "1":
    if bearCnt[1] >= MaximumBarWithoutSignal {
        bearState = bearState."0";
        bearCnt = 0;
        bearBoxTop = bearBoxTop[1];
        bearBoxBot = bearBoxBot[1];
        bearLab1  = 0;
        bearLab2  = 0;
        bearL1Cnt = 0;
        bearL2Cnt = 0;
        bearCol = 0;
    } else {
        bearState = if c > bearBoxTop[1] then bearState."2" else bearState."1";
        bearCnt = bearCnt[1] + 1;
        bearBoxTop = bearBoxTop[1];
        bearBoxBot = bearBoxBot[1];
        bearLab2  = 0;
        bearL2Cnt = 0;
    if c < bearBoxBot and o > bearBoxBot and
        (if !bearLab1[1] or !all then yes else (all and (bearL1Cnt[1] > MinimumBarBetweenSignals))) {
            bearLab1  = bearLab1[1] + 1;
            bearL1Cnt = 0;
        } else {
            bearLab1  = bearLab1[1];
            bearL1Cnt = if bearLab1 > 0 then bearL1Cnt[1] + 1 else 0;
        }
        bearCol = if bearLab1 > 0 then -1 else 0;
    }
case "2":
    if bearCnt[1] >= MaximumBarWithoutSignal {
        bearState = bearState."0";
        bearCnt = 0;
        bearBoxTop = na;
        bearBoxBot = na;
        bearLab1  = 0;
        bearLab2  = 0;
        bearL1Cnt = 0;
        bearL2Cnt = 0;
        bearCol = 0;
    } else {
        bearState  = if c < bearBoxBot[1] then bearState."0" else  bearState."2";
        bearCnt = bearCnt[1] + 1;
        bearBoxTop = bearBoxTop[1];
        bearBoxBot = bearBoxBot[1];
        bearLab1  = bearLab1[1];
        bearL1Cnt = if bearLab1 > 0 then bearL1Cnt[1] + 1 else 0;
    if c > bearBoxTop and o < bearBoxTop and
        (if (!bearLab2[1] or !all) then yes else (all and (bearL2Cnt[1] > MinimumBarBetweenSignals))) {
            bearLab2  = bearLab2[1] + 1;
            bearL2Cnt = 0;
        } else {
            bearLab2  = bearLab2[1];
            bearL2Cnt = if bearLab2 > 0 then bearL2Cnt[1] + 1 else 0;
        }
        bearCol   = if (bearLab1 or bearLab2) then 1 else 0;
    }
}
    } else {
    bearState = bearState."0";
    bearCnt = 0;
    bearBoxTop = na;
    bearBoxBot = na;
    bearLab1  = 0;
    bearLab2  = 0;
    bearL1Cnt = 0;
    bearL2Cnt = 0;
    bearCol = 0;

}
def stateB = bullState == bullState."0";
def stateS = bearState == bearState."0";
def bullBG = bullCol;
def bearBG = bearCol;

def bullCnt1 = if !stateB and stateB[1] then n else bullCnt1[1];
def bullCnt2 = if !stateB and stateB[1] then n else bullCnt2[1];
def bearCnt1 = if !stateS and stateS[1] then n else bearCnt1[1];
def bearCnt2 = if !stateS and stateS[1] then n else bearCnt2[1];

def pltBull1 = if all then yes else n >=highestAll(bullCnt1);
def pltBull2 = if all then yes else n >=highestAll(bullCnt2);
def pltBear1 = if all then yes else n >=highestAll(bearCnt1);
def pltBear2 = if all then yes else n >=highestAll(bearCnt2);

#-- retest
def bullChg1 = pltBull1 and bullCnt > 1 and bullLab1!=bullLab1[1];
def bullChg2 = pltBull2 and bullCnt > 1 and bullLab2!=bullLab2[1];
def bearChg1 = pltBear1 and bearCnt > 1 and bearLab1!=bearLab1[1];
def bearChg2 = pltBear1 and bearCnt > 1 and bearLab2!=bearLab2[1];


plot bull1 = if !last and bullChg1  then bullBoxBot else na;
plot bull2 = if !last and bullChg2  then bullBoxTop else na;
plot bear1 = if !last and bearChg1 then bearBoxTop else na;
plot bear2 = if !last and bearChg2 then bearBoxBot else na;
bull1.SetDefaultColor(Color.CYAN);
bull2.SetDefaultColor(Color.MAGENTA);
bear1.SetDefaultColor(Color.MAGENTA);
bear2.SetDefaultColor(Color.CYAN);
bull1.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
bull2.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
bear1.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
bear2.SetPaintingStrategy(PaintingStrategy.TRIANGLES);

#-- Swing Lines

plot bullTop = if !last and pltBull1 and bullBoxTop then bullBoxTop else na;
plot bullBot = if !last and pltBull2 and bullBoxBot then bullBoxBot else na;

plot bearTop = if !last and pltBear1 and bearBoxTop then bearBoxTop else na;
plot bearBot = if !last and pltBear2 and bearBoxBot then bearBoxBot else na;

bullTop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bullBot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bearTop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bearBot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bullTop.AssignValueColor(if bullCol>0 then Color.DARK_GREEN else
                         if bullCol<0 then Color.DARK_RED else Color.GRAY);
bullBot.AssignValueColor(if bullCol>0 then Color.DARK_GREEN else
                         if bullCol<0 then Color.DARK_RED else Color.GRAY);
bearTop.AssignValueColor(if bearCol>0 then Color.DARK_GREEN else
                         if bearCol<0 then Color.DARK_RED else Color.GRAY);
bearBot.AssignValueColor(if bearCol>0 then Color.DARK_GREEN else
                         if bearCol<0 then Color.DARK_RED else Color.GRAY);

AddCloud(if bullBG > 0 then bullTop else na,
         if bullBot==bullBot[1] then bullBot else na, Color.DARK_GREEN, Color.DARK_GREEN);
AddCloud(if bullBG < 0 then bullTop else na,
         if bullBot==bullBot[1] then bullBot else na, Color.DARK_RED, Color.DARK_RED);

AddCloud(if bearBG > 0 then bearTop else na,
         if bearBot==bearBot[1] then bearBot else na, Color.DARK_GREEN, Color.DARK_GREEN);
AddCloud(if bearBG < 0 then bearTop else na,
         if bearBot==bearBot[1] then bearBot else na, Color.DARK_RED, Color.DARK_RED);

AddCloud(if !bullBG then bullTop else na,
         if bullBot==bullBot[1] then bullBot else na, Color.DARK_GRAY, Color.DARK_GRAY);
AddCloud(if !bearBG then bearTop else na,
         if bearBot==bearBot[1] then bearBot else na, Color.DARK_GRAY, Color.DARK_GRAY);


# 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
407 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