# +--------------------------------------------------+
script InRange {
input start = 0;
input end = 0;
input lower = 0;
input upper = 0;
def f0 = 0.264;
def f1 = 0.382;
def f2 = 0.500;
def f3 = 0.618;
def f4 = 0.707;
def f5 = 0.786;
def f6 = 0.886;
def f7 = 1.000;
def range = end - start;
def p1 = if lower == f6 then f5 else
if lower == f5 then f4 else
if lower == f4 then f3 else
if lower == f3 then f2 else
if lower == f2 then f1 else
f0;
def p2 = if upper == f1 then f2 else
if upper == f2 then f3 else
if upper == f3 then f4 else
if upper == f4 then f5 else
if upper == f5 then f6 else
f7;
def n1 = end - range * p1;
def n2 = end - range * p2;
def upperRange = Max(n1, n2);
def lowerRange = Min(n1, n2);
plot inRange = if range > 0 then low < upperRange and low > lowerRange
else high < upperRange and high > lowerRange;
}
script PRZ {
input x = 0;
input a = 0;
input b = 0;
input c = 0;
input abcd = 0;
input xd1 = 0;
input xd2 = 0;
input cd1 = 0;
input cd2 = 0;
def XArng = (a - x);
def ABrng = (a - b);
def BCrng = (c - b);
def Cr = AbsValue(BCrng) / AbsValue(ABrng);
def Cret = if Cr >= 0.836 then 0.886 else
if Cr >= 0.747 then 0.786 else
if Cr >= 0.663 then 0.707 else
if Cr >= 0.559 then 0.618 else
if Cr >= 0.441 then 0.500 else
0.382;
def Cex = if 1 / Cret < cd1 then cd1 else if 1 / Cret > cd2 then cd2 else 1 / Cret;
plot prz1 = c - (ABrng * abcd);
plot prz2 = c - (BCrng * Cex);
plot prz3 = a - (XArng * xd1);
plot prz4 = a - (XArng * xd2);
plot przMax = Max(Max(prz1, prz2), Max(prz3, prz4));
plot przMin = Min(Min(prz1, prz2), Min(prz3, prz4));
}
input waveLength = 13;
input labelPoints = yes;
def lastBar = HighestAll(if !IsNaN(close) then BarNumber() else 0);
def offset = Min(waveLength - 1, lastBar - BarNumber());
def pHigh = high == Highest(high, waveLength) and high == GetValue(Highest(high, waveLength), -offset);
def pLow = low == Lowest(low, waveLength) and low == GetValue(Lowest(low, waveLength), -offset);
def cph = if pHigh[1] then 1 else cph[1] + 1;
def cpl = if pLow[1] then 1 else cpl[1] + 1;
def wHigh = ((pHigh and !pLow) or (pHigh and pLow and cpl < cph)) and cph >= waveLength;
def wLow = ((pLow and !pHigh) or (pLow and pHigh and cph < cpl)) and cpl >= waveLength;
def spHigh = if wLow[1] then high else if high > spHigh[1] then high else spHigh[1];
def bHigh = if wHigh then 0 else if wLow[1] then BarNumber() else if high >= spHigh[1] then BarNumber() else bHigh[1];
def spLow = if wHigh[1] then low else if low < spLow[1] then low else spLow[1];
def bLow = if wLow then 0 else if wHigh[1] then BarNumber() else if low <= spLow[1] then BarNumber() else bLow[1];
def cpwh = if wHigh[1] then 1 else cpwh[1] + 1;
def cpwl = if wLow[1] then 1 else cpwl[1] + 1;
def cnwh = fold i = 1 to lastBar with a = Double.NaN while IsNaN(a) do if GetValue(wHigh, -i) then i else Double.NaN;
def cnwl = fold j = 1 to lastBar with b = Double.NaN while IsNaN(b) do if GetValue(wLow, -j) then j else Double.NaN;
def mwHigh = if !wHigh and (cpwl < cpwh) and BarNumber() == GetValue(bHigh[1], -cnwl) then 1 else Double.NaN;
def mwLow = if !wLow and (cpwh < cpwl) and BarNumber() == GetValue(bLow[1], -cnwh) then 1 else Double.NaN;
def peak = if IsNaN(close) then 0 else wHigh or !IsNaN(mwHigh);
def valley = if IsNaN(close) then 0 else wLow or !IsNaN(mwLow);
def lastPeakBar = HighestAll(if peak then BarNumber() else 0);
def lastPeak = BarNumber() == lastPeakBar;
def lastValleyBar = HighestAll(if valley then BarNumber() else 0);
def lastValley = BarNumber() == lastValleyBar;
def cpp = if peak[1] then 1 else cpp[1] + 1;
def cpv = if valley[1] then 1 else cpv[1] + 1;
def prevHigh = GetValue(high, cpp);
def prevLow = GetValue(low, cpv);
def prevHigh2 = GetValue(prevHigh, cpp);
def prevLow2 = GetValue(prevLow, cpv);
def cnp = if BarNumber() >= lastPeakBar then 0 else fold k = 1 to lastBar with c = 0 while c == 0 do if GetValue(peak, -k) then k else 0;
def cnp2 = cnp + GetValue(cnp, -cnp);
def cnv = if BarNumber() >= lastValleyBar then 0 else fold l = 1 to lastBar with d = 0 while d == 0 do if GetValue(valley, -l) then l else 0;
def cnv2 = cnv + GetValue(cnv, -cnv);
def nextHigh = if BarNumber() >= lastPeakBar then Double.NaN else GetValue(high, -cnp);
def nextLow = if BarNumber() >= lastValleyBar then Double.NaN else GetValue(low, -cnv);
def nextHigh2 = if IsNaN(GetValue(nextHigh, -cnp)) then nextHigh2[1] else GetValue(nextHigh, -cnp);
def nextLow2 = if IsNaN(GetValue(nextLow, -cnv)) then nextLow2[1] else GetValue(nextLow, -cnv);
def hf0 = 0.618;
def hf1 = 0.618;
def hf2 = 0.382;
def hf3 = 0.886;
def hf4 = 1.000;
def hf5 = 0.786;
def hf6 = 0.786;
def hf7 = 1.129;
def hf8 = 1.618;
input FudgeFactor = 0.8;
def FF = ATR()[1] * FudgeFactor;
def Lprz = waveLength * 2;
def blg1 = valley and inrange(prevLow, prevHigh, hf0, hf1);
def blg2 = peak and GetValue(blg1, cpv) and inrange(prevHigh, prevLow, hf2, hf3);
def blg3 = valley and GetValue(blg2, cpp);
def cv1 = if blg2 then 0 else cv1[1] + 1;
def blgz1 = CompoundValue(1, if blg2 then prz(prevLow2, prevHigh, prevLow, high, hf4, hf5, hf6, hf7, hf8).przMax else if (peak or valley) and cv1 > Lprz and !GetValue(blg2, cpp) then Double.NaN else blgz1[1], Double.NaN);
def blgz2 = CompoundValue(1, if blg2 then prz(prevLow2, prevHigh, prevLow, high, hf4, hf5, hf6, hf7, hf8).przMin else if (peak or valley) and cv1 > Lprz and !GetValue(blg2, cpp) then Double.NaN else blgz2[1], Double.NaN);
def blg4 = (low <= prevLow + ATR() * 2 and blg3 and !cnp and high >= blgz2 - FF) or (low <= prevLow and blg3 and cnp and (high >= (blgz2 - FF) and low <= (blgz1 + FF)));
def blg5 = if IsNaN(blg4) then 0 else blg4;
def blgX = valley and GetValue(blg5, -cnv2) and cnv2 > cnv;
def blgA = peak and GetValue(blgX, cpv);
def blgB = valley and GetValue(blgA, cpp);
def blgC = peak and GetValue(blgB, cpv);
def blgD = valley and GetValue(blgC, cpp);
def aBullWave = CompoundValue(1, if blgX and GetValue(blgD, -cnv2) then 1 else if blgD[1] and !blgX[1] then 0 else aBullWave[1], 0);
DefineGlobalColor("Bullish Labels", Color.LIGHT_GREEN);
AddChartBubble(blgX and labelPoints, low, "X", GlobalColor("Bullish Labels"), 0);
AddChartBubble(blgA and labelPoints, high, "A", GlobalColor("Bullish Labels"));
AddChartBubble(blgB and labelPoints, low, "B", GlobalColor("Bullish Labels"), 0);
AddChartBubble(blgC and labelPoints, high, "C", GlobalColor("Bullish Labels"));
AddChartBubble(blgD and labelPoints, low, "D", GlobalColor("Bullish Labels"), 0);
def abl = CompoundValue(1, if aBullWave and !aBullWave[1] then 0 else if aBullWave and blgC then 1 else abl[1], 0);
def blgPRZ1 = if IsNaN(close) then blgPRZ1[1] else if GetValue(abl, cv1) and GetValue(aBullWave, cv1) then blgz1 else Double.NaN;
def blgPRZ2 = if IsNaN(close) then blgPRZ2[1] else if GetValue(abl, cv1) and GetValue(aBullWave, cv1) then blgz2 else Double.NaN;
plot PRZlmax = blgPRZ1;
PRZlmax.SetDefaultColor(Color.DARK_GRAY);
plot PRZlmin = blgPRZ2;
PRZlmin.SetDefaultColor(Color.DARK_GRAY);
DefineGlobalColor("Bullish PRZ", CreateColor(163, 221, 255));
AddCloud(PRZlmax, PRZlmin, GlobalColor("Bullish PRZ"));
def brg1 = peak and inrange(prevHigh, prevLow, hf0, hf1);
def brg2 = valley and GetValue(brg1, cpp) and inrange(prevLow, prevHigh, hf2, hf3);
def brg3 = peak and GetValue(brg2, cpv);
def cr1 = if brg2 then 0 else cr1[1] + 1;
def brgz1 = CompoundValue(1, if brg2 then prz(prevHigh2, prevLow, prevHigh, low, hf4, hf5, hf6, hf7, hf8).przMax else if (peak or valley) and cr1 > Lprz and !GetValue(brg2, cpv) then Double.NaN else brgz1[1], Double.NaN);
def brgz2 = CompoundValue(1, if brg2 then prz(prevHigh2, prevLow, prevHigh, low, hf4, hf5, hf6, hf7, hf8).przMin else if (peak or valley) and cr1 > Lprz and !GetValue(brg2, cpv) then Double.NaN else brgz2[1], Double.NaN);
def brg4 = (high >= prevHigh - ATR() * 2 and brg3 and !cnv and low <= brgz1 + FF) or (high >= prevHigh and brg3 and cnv and (low <= (brgz1 + FF) and high >= (brgz2 - FF)));
def brg5 = if IsNaN(brg4) then 0 else brg4;
def brgX = peak and GetValue(brg5, -cnp2) and cnp2 > cnp;
def brgA = valley and GetValue(brgX, cpp);
def brgB = peak and GetValue(brgA, cpv);
def brgC = valley and GetValue(brgB, cpp);
def brgD = peak and GetValue(brgC, cpv);
def aBearWave = if brgX and GetValue(brgD, -cnp2) then 1 else if brgD[1] and !brgX[1] then 0 else aBearWave[1];
DefineGlobalColor("Bearish Labels", Color.PINK);
AddChartBubble(brgX and labelPoints, high, "X", GlobalColor("Bearish Labels"));
AddChartBubble(brgA and labelPoints, low, "A", GlobalColor("Bearish Labels"), 0);
AddChartBubble(brgB and labelPoints, high, "B", GlobalColor("Bearish Labels"));
AddChartBubble(brgC and labelPoints, low, "C", GlobalColor("Bearish Labels"), 0);
AddChartBubble(brgD and labelPoints, high, "D", GlobalColor("Bearish Labels"));
def abr = CompoundValue(1, if aBearWave and !aBearWave[1] then 0 else if aBearWave and brgC then 1 else abr[1], 0);
def brgPRZ1 = if IsNaN(close) then brgPRZ1[1] else if GetValue(abr, cr1) and GetValue(aBearWave, cr1) then brgz1 else Double.NaN;
def brgPRZ2 = if IsNaN(close) then brgPRZ2[1] else if GetValue(abr, cr1) and GetValue(aBearWave, cr1) then brgz2 else Double.NaN;
plot PRZrmax = brgPRZ1;
PRZrmax.SetDefaultColor(Color.DARK_GRAY);
plot PRZrmin = brgPRZ2;
PRZrmin.SetDefaultColor(Color.DARK_GRAY);
DefineGlobalColor("Bearish PRZ", CreateColor(226, 163, 255));
AddCloud(PRZrmax, PRZrmin, GlobalColor("Bearish PRZ"));
def mWave = if BarNumber() < lastPeakBar and valley then ((nextHigh - low) / cnp) else if BarNumber() < lastValleyBar and peak then ((nextLow - high) / cnv) else mWave[1];
def waveLine = if valley then low else if peak then high else waveLine[1] + mWave;
plot bullWave = if aBullWave then waveLine else Double.NaN;
bullWave.SetDefaultColor(Color.WHITE);
bullWave.SetLineWeight(2);
plot bearWave = if aBearWave then waveLine else Double.NaN;
bearWave.SetDefaultColor(Color.WHITE);
bearWave.SetLineWeight(2);
def mPeak = if BarNumber() < lastPeakBar and peak then ((nextHigh - high) / cnp) else mPeak[1];
def lPeak = if peak then high else lPeak[1] + mPeak;
def peakLine = if aBearWave then lPeak else Double.NaN;
AddCloud(peakLine, bearWave, Color.PINK);
def mValley = if BarNumber() < lastValleyBar and valley then ((nextLow - low) / cnv) else mValley[1];
def lvalley = if valley then low else lvalley[1] + mValley;
def valleyLine = if aBullWave then lvalley else Double.NaN;
AddCloud(bullWave, valleyLine, Color.LIGHT_GREEN);
#=============
input ShowTargetZones = No;
def brS = if IsNaN(nextHigh) then 0 else if nextHigh > high then 1 else 0;
def brT1 = CompoundValue(1, if (brgA or blgA) then Double.NaN else if brgD and !brS then (prevLow2 - prevHigh2) * 0.382 + high else brT1[1], Double.NaN);
def brT2 = CompoundValue(1, if (brgA or blgA) then Double.NaN else if brgD and !brS then (prevLow2 - prevHigh2) * 0.618 + high else brT2[1], Double.NaN);
def brT3 = CompoundValue(1, if (brgA or blgA) then Double.NaN else if brgD and !brS then (prevLow2 - prevHigh2) * 0.786 + high else brT3[1], Double.NaN);
def brT4 = CompoundValue(1, if (brgA or blgA) then Double.NaN else if brgD and !brS then (prevLow2 - prevHigh2) * 1.000 + high else brT4[1], Double.NaN);
def brT5 = CompoundValue(1, if (brgA or blgA) then Double.NaN else if brgD and !brS then (prevLow2 - prevHigh2) * 1.27 + high else brT5[1], Double.NaN);
def brT6 = CompoundValue(1, if (brgA or blgA) then Double.NaN else if brgD and !brS then (prevLow2 - prevHigh2) * 1.618 + high else brT6[1], Double.NaN);
plot bearT1 = if !ShowTargetZones then Double.NaN else brT1;
bearT1.SetDefaultColor(Color.DARK_RED);
bearT1.SetLineWeight(2);
plot bearT2 = if !ShowTargetZones then Double.NaN else brT2;
bearT2.SetDefaultColor(Color.DARK_RED);
bearT2.SetLineWeight(2);
plot bearT3 = if !ShowTargetZones then Double.NaN else brT3;
bearT3.SetDefaultColor(Color.DARK_RED);
bearT3.SetLineWeight(2);
plot bearT4 = if !ShowTargetZones then Double.NaN else brT4;
bearT4.SetDefaultColor(Color.DARK_RED);
bearT4.SetLineWeight(2);
plot bearT5 = if !ShowTargetZones then Double.NaN else brT5;
bearT5.SetDefaultColor(Color.DARK_RED);
bearT5.SetLineWeight(2);
plot bearT6 = if !ShowTargetZones then Double.NaN else brT6;
bearT6.SetDefaultColor(Color.DARK_RED);
bearT6.SetLineWeight(2);
AddCloud(bearT2, bearT3, Color.YELLOW);
AddCloud(bearT5, bearT6, Color.YELLOW);
def blS = if IsNaN(nextLow) then 0 else if nextLow < low then 1 else 0;
def blT1 = CompoundValue(1, if (brgA or blgA) then Double.NaN else if blgD and !blS then (prevHigh2 - prevLow2) * 0.382 + low else blT1[1], Double.NaN);
def blT2 = CompoundValue(1, if (brgA or blgA) then Double.NaN else if blgD and !blS then (prevHigh2 - prevLow2) * 0.618 + low else blT2[1], Double.NaN);
def blT3 = CompoundValue(1, if (brgA or blgA) then Double.NaN else if blgD and !blS then (prevHigh2 - prevLow2) * 0.786 + low else blT3[1], Double.NaN);
def blT4 = CompoundValue(1, if (brgA or blgA) then Double.NaN else if blgD and !blS then (prevHigh2 - prevLow2) * 1.000 + low else blT4[1], Double.NaN);
def blT5 = CompoundValue(1, if (brgA or blgA) then Double.NaN else if blgD and !blS then (prevHigh2 - prevLow2) * 1.27 + low else blT5[1], Double.NaN);
def blT6 = CompoundValue(1, if (brgA or blgA) then Double.NaN else if blgD and !blS then (prevHigh2 - prevLow2) * 1.618 + low else blT6[1], Double.NaN);
plot bullT1 = if !ShowTargetZones then Double.NaN else blT1;
bullT1.SetDefaultColor(Color.DARK_GREEN);
bullT1.SetLineWeight(2);
plot bullT2 = if !ShowTargetZones then Double.NaN else blT2;
bullT2.SetDefaultColor(Color.DARK_GREEN);
bullT2.SetLineWeight(2);
plot bullT3 = if !ShowTargetZones then Double.NaN else blT3;
bullT3.SetDefaultColor(Color.DARK_GREEN);
bullT3.SetLineWeight(2);
plot bullT4 = if !ShowTargetZones then Double.NaN else blT4;
bullT4.SetDefaultColor(Color.DARK_GREEN);
bullT4.SetLineWeight(2);
plot bullT5 = if !ShowTargetZones then Double.NaN else blT5;
bullT5.SetDefaultColor(Color.DARK_GREEN);
bullT5.SetLineWeight(2);
plot bullT6 = if !ShowTargetZones then Double.NaN else blT6;
bullT6.SetDefaultColor(Color.DARK_GREEN);
bullT6.SetLineWeight(2);
AddCloud(bullT3, bullT2, Color.YELLOW);
AddCloud(bullT6, bullT5, Color.YELLOW);