Complex issue BreakerBlock creation

silas7467

New member
My scripting isn't clean, I'm sorry
I'm trying to create and plot Breaker Block logic using two fundamental components
The first being a zigzag corresponding to parabolicSAR extremes to define small swings
the second being a keltner trailing ATR stop (basically a super trend) to define bigger picture swings

I'm trying to isolate the zigzag high prior to the lowest low in a bearish keltner leg before the keltner regime flips to a bullish state... and vice versa
What I do have is the logic to plot the breaker level AFTER the keltner regime flips, but I'm trying to anchor this horizontal level to the actual origin fractal low or high, I think the picture illustrates this much clearer.... Anyway, I'm not sure if this is even possible

Code:
declare weak_volume_dependency;

input showKelt = no;
input displace = 1;
input factor = 2.5;
input length = 34;
input price = close;
input averageType = AverageType.EXPONENTIAL;
input trueRangeAverageType = AverageType.SIMPLE;

def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);

def average = MovingAverage(averageType, price, length);
plot average1 = average[displace];
average1.SetDefaultColor(Color.RED);
average1.Hide();
average1.SetStyle(Curve.SHORT_DASH);

def Avg = average[displace];

def Upper_Band = average[displace] + shift[displace];

def Lower_Band = average[displace] - shift[displace];

def shortrisk = Upper_Band;
def longrisk = Lower_Band;

def pivot = Round((high - low) / 2 / TickSize(), 0) * TickSize() + low;

input break = {default CLOSE, HiLo};
def hii;
def loo;
switch (break){
case CLOSE:
    hii = close;
    loo = close;
case HiLo:
    hii = high;
    loo = low;
}

def rTrend = CompoundValue(1,
  # Trending Up
    if rTrend[1] > 0 then
      # If close is below trend then switch to downtrend (negative)
        if close < Max(longrisk[1], rTrend[1]) then -shortrisk
      # ...else adjust new uptrend value.   
        else Max(longrisk, rTrend[1])
  # Trending Down
    else if rTrend[1] < 0 then
      # If close is above trend then switch to uptrend (positive)
        if close > Min(shortrisk[1], AbsValue(rTrend[1])) then longrisk
      # ...else adjust new downtrend value.
        else -Min(shortrisk, AbsValue(rTrend[1]))
    else rTrend[1], pivot);

def trendy = AbsValue(rTrend);
plot trend = if Sign(rTrend) == Sign(rTrend[1]) then AbsValue(rTrend) else Double.NaN;
trend.SetDefaultColor(Color.VIOLET);

plot sUpper_Band = average[displace] + shift[displace];
sUpper_Band.SetDefaultColor(Color.GRAY);
sUpper_Band.HideBubble();
sUpper_Band.SetHiding(!showKelt);

plot sLower_Band = average[displace] - shift[displace];
sLower_Band.SetDefaultColor(Color.GRAY);
sLower_Band.HideBubble();
sLower_Band.SetHiding(!showKelt);

def keltbull = Sign(rTrend) > 0;
def keltbear = Sign(rTrend) < 0;
def keltflipbull = keltbear[1] and keltbull;
def keltflipbear = keltbull[1] and keltbear;

rec bullhi = if keltflipbull then high else if keltbull and high > bullhi[1] then high else bullhi[1];
rec bearlo = if keltflipbear then low else if keltbear and low < bearlo[1] then low else bearlo[1];


input displaced = 1;
input type = AverageType.EXPONENTIAL;

input Cloud = yes;
input hiLo = no;

plot hi = MovingAverage(type, high[displaced], length);
hi.SetDefaultColor(Color.RED);
hi.SetHiding(!hiLo);

plot lo = MovingAverage(type, low[displaced], length);
lo.SetDefaultColor(Color.RED);
lo.SetHiding(!hiLo);






DefineGlobalColor("Cloud", Color.DARK_RED);
AddCloud(if !Cloud then Double.NaN else hi, lo, GlobalColor("cloud"), GlobalColor("cloud"));

plot hiiloo = if IsNaN(trend) then Double.NaN else if rTrend > 0 then hi else lo;
hiiloo.SetDefaultColor(Color.RED);

input label = yes;
input maxRisk = 200;
input MicroDivide = 10.0;
def range = RoundUp(AbsValue(hiiloo - trend) / TickSize(), 0) * TickSize();
def dollars = range / TickSize() * TickValue() / MicroDivide;
def contracts = RoundDown(maxRisk / dollars, 0);
AddLabel(label, " " + dollars + " | " + contracts + " contract" + if contracts == 1 then " " else "s ");

plot sar = ParabolicSAR();
sar.SetDefaultColor(Color.WHITE);
sar.Hide();



#########################################################################################################################################




input priceH = high;
input priceL = low;

def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL < sar[1] {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }
} else {
    if priceH > sar[1] {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}

def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, barNumber));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - barNumber + 1;
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;

def lastH;
if highPoint and offset > 1 {
    lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH) do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH then Double.NaN else tH;
} else {
    lastH = Double.NaN;
}

def lastL;
if lowPoint and offset > 1 {
    lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL) do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL then Double.NaN else tL;
} else {
    lastL = Double.NaN;
}

plot ZZ;
if barNumber == 1 {
    ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP) do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH else Double.NaN;
} else if barNumber == barCount {
    ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL else Double.NaN;
} else {
    ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}
ZZ.SetDefaultColor(Color.RED);
ZZ.SetLineWeight(2);
ZZ.EnableApproximation();

rec hit = if !IsNaN(ZZ) and highPoint then high else hit[1];
#rec hi = if newstate then close else hi[1];
plot hiit = hit;
hiit.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hiit.SetDefaultColor(Color.DARK_GREEN);
hiit.Hide();
rec lot = if !IsNaN(ZZ) and lowPoint then low else lot[1];
plot loot = lot;
loot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
loot.SetDefaultColor(Color.DARK_RED);
loot.Hide();

def flipbull = state[1] == state.downtrend and state == state.uptrend;
def flipbear = state[1] == state.uptrend and state == state.downtrend;


def isperiodshort = bullhi != bullhi[1];



rec BreakerS = if isperiodshort then lot else BreakerS[1];


def isperiodlong = bearlo != bearlo[1];
rec BreakerL = if isperiodlong then hit else BreakerL[1];



plot BreakerShort = if keltbear then BreakerS else Double.NaN;
BreakerShort.SetLineWeight(3);
BreakerShort.SetDefaultColor(Color.RED);
BreakerShort.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot BreakerLong = if keltbull then BreakerL else Double.NaN;
BreakerLong.SetLineWeight(3);
BreakerLong.SetDefaultColor(Color.GREEN);
BreakerLong.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#################################################################################################################################

BB breaker.png



I don't know maybe I'm asking for something that's not possible, the AI s are useless. I'm thinking this is futile attempt, but I'll try one last resource here if anyone can help me extend and plot the current Breaker level and not the previous ones in a swing
 

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