Wolfe wave ithinkscript 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 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 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 m1 = if peak then (high - prevHigh) / cpp else m1[1]; def m2 = if valley then (low - prevLow) / cpv else m2[1]; def pp4 = if IsNaN(close) then 0 else peak and (high < prevHigh) and (prevLow < prevLow2) and (high >= prevLow2) and m1 < m2; def p4 = if peak and BarNumber() < lastPeakBar then pp4 and !GetValue(pp4, -cnp) and !GetValue(pp4, -(cnp + GetValue(cnp, -cnp))) else pp4; def p5 = if IsNaN(close) then 0 else (valley and GetValue(p4, cpp)); def p3 = if IsNaN(close) then 0 else (valley and GetValue(p4, -cnp)); def p2 = if IsNaN(close) then 0 else (peak and GetValue(p3, -cnv)); def p1 = if IsNaN(close) then 0 else (valley and GetValue(p2, -cnp)); def ap5 = CompoundValue(1, if p1 then 0 else if p5 then 1 else ap5[1], 0); def lp5 = if !ap5 then 0 else if p5 then low else lp5[1]; def pv4 = if IsNaN(close) then 0 else valley and (low > prevLow) and (prevHigh > prevHigh2) and (low <= prevHigh2) and m1 < m2; def v4 = if valley and BarNumber() < lastValleyBar then pv4 and !GetValue(pv4, -cnv) and !GetValue(pv4, -(cnv + GetValue(cnv, -cnv))) else pv4; def v5 = if IsNaN(close) then 0 else (peak and GetValue(v4, cpv)); def v3 = if IsNaN(close) then 0 else (peak and GetValue(v4, -cnv)); def v2 = if IsNaN(close) then 0 else (valley and GetValue(v3, -cnp)); def v1 = if IsNaN(close) then 0 else (peak and GetValue(v2, -cnv)); def av5 = CompoundValue(1, if v1 then 0 else if v5 then 1 else av5[1], 0); def lv5 = if !av5 then 0 else if v5 then high else lv5[1]; def bullc = if p1 then 1 else if p2 then 2 else if p3 then 3 else if p4 then 4 else if p5 then 5 else 0; def bearc = if v1 then 1 else if v2 then 2 else if v3 then 3 else if v4 then 4 else if v5 then 5 else 0; AddChartBubble(labelPoints and bullc > 0, if peak then high else low, bullc, Color.ORANGE, if peak then 1 else 0); AddChartBubble(labelPoints and bearc > 0, if peak then high else low, bearc, Color.PINK, if peak then 1 else 0); def pStopped = CompoundValue(1, if !ap5 or IsNaN(close) then 0 else if close crosses below lp5 then pStopped[1] + 1 else pStopped[1], 0); def vStopped = CompoundValue(1, if !av5 or IsNaN(close) then 0 else if close crosses above lv5 then vStopped[1] + 1 else vStopped[1], 0); 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 aBullWave = CompoundValue(1, if IsNaN(close) then 0 else if p1 then 1 else if p5[1] or (BarNumber() > lastPeakBar and BarNumber() <> lastValleyBar and !cnv) then 0 else aBullWave[1] , 0); def aBearWave = CompoundValue(1, if IsNaN(close) then 0 else if v1 then 1 else if v5[1] or (BarNumber() > lastValleyBar and BarNumber() <> lastPeakBar and !cnp) then 0 else aBearWave[1] , 0); 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 cp1 = if p1 then 0 else cp1[1] + 1; def cp2 = if p2 then 0 else cp2[1] + 1; def cp5 = if p5 then 0 else cp5[1] + 1; def mp13 = if p1 then ((nextLow - low) / cnv) else Double.NaN; def mp24 = if p2 then ((nextHigh - high) / cnp) else Double.NaN; def mp14 = if p1 then ((nextHigh2 - low) / (cnp + GetValue(cnp, -cnp))) else Double.NaN; def pl13 = if p1 then low else GetValue(low, cp1) + (cp1 * (GetValue(mp13, cp1))); def pl24 = if p2 then high else GetValue(high, cp2) + (cp2 * (GetValue(mp24, cp2))); def pl14 = if p1 then low else GetValue(low, cp1) + (cp1 * (GetValue(mp14, cp1))); def lp = if IsNaN(pl24) then 0 else pl24 > pl13; def lpx = !p1 and !lp and lp[1]; def cpx = if lpx then 0 else cpx[1] + 1; def above14 = if IsNaN(close) then 0 else ap5 and close > pl14; def ap13 = CompoundValue(1, if p1 then 1 else if lpx or p1[-1] or pStopped or (above14) then 0 else ap13[1], 0); def ap24 = CompoundValue(1, if p2 then 1 else if lpx or p1[-1] or pStopped or (above14) then 0 else ap24[1], 0); def ap14 = CompoundValue(1, if ap13 then 1 else if p1[-1] or pStopped then 0 else ap14[1], 0); plot bull13 = if !p1[-1] and ap13 then pl13 else Double.NaN; bull13.SetDefaultColor(Color.ORANGE); plot bull24 = if !p1[-1] and ap24 then pl24 else Double.NaN; bull24.SetDefaultColor(Color.ORANGE); plot bull14 = if !p1[-1] and ap14 and cp1 < 12 * waveLength then pl14 else Double.NaN; bull14.SetDefaultColor(Color.DARK_GREEN); bull14.SetLineWeight(2); bull14.SetStyle(Curve.SHORT_DASH); def cv1 = if v1 then 0 else cv1[1] + 1; def cv2 = if v2 then 0 else cv2[1] + 1; def cv5 = if v5 then 0 else cv5[1] + 1; def mv13 = if v1 then ((nextHigh - high) / cnp) else Double.NaN; def mv24 = if v2 then ((nextLow - low) / cnv) else Double.NaN; def mv14 = if v1 then ((nextLow2 - high) / (cnv + GetValue(cnv, -cnv))) else Double.NaN; def vl13 = if v1 then high else GetValue(high, cv1) + (cv1 * (GetValue(mv13, cv1))); def vl24 = if v2 then low else GetValue(low, cv2) + (cv2 * (GetValue(mv24, cv2))); def vl14 = if v1 then high else GetValue(high, cv1) + (cv1 * (GetValue(mv14, cv1))); def lv = if IsNaN(vl24) then 0 else vl24 < vl13; def lvx = if IsNaN(lv) then 0 else !v1 and !lv and lv[1]; def cvx = if lvx then 0 else cvx[1] + 1; def below14 = if IsNaN(close) then 0 else av5 and close < vl14; def av13 = CompoundValue(1, if v1 then 1 else if lvx or v1[-1] or vStopped or (below14) then 0 else av13[1], 0); def av24 = CompoundValue(1, if v2 then 1 else if lvx or v1[-1] or vStopped or (below14) then 0 else av24[1], 0); def av14 = CompoundValue(1, if av13 then 1 else if v1[-1] or vStopped then 0 else av14[1], 0); plot bear13 = if !v1[-1] and av13 then vl13 else Double.NaN; bear13.SetDefaultColor(Color.PINK); plot bear24 = if !v1[-1] and av24 then vl24 else Double.NaN; bear24.SetDefaultColor(Color.PINK); plot bear14 = if !v1[-1] and av14 and cv1 < 12 * waveLength then vl14 else Double.NaN; bear14.SetDefaultColor(Color.DARK_RED); bear14.SetLineWeight(2); bear14.SetStyle(Curve.SHORT_DASH); def bls = CompoundValue(1, if p5 then 1 else if p1 or pStopped or close crosses above pl14 then 0 else bls[1], 0); def ap3 = CompoundValue(1, if p1 then 0 else if p3 then 1 else ap3[1], 0); def lp3 = if !ap3 then 0 else if p3 then low else lp3[1]; def ppe = (close crosses above pl13 or close crosses above lp3 or close crosses above pl24 or (cp5 >= 2 and close > GetValue(high, cp5))); def pe = if p5[-1] then 0 else if close > Max(close[1], open[1]) and ppe then 1 else pe[1]; plot bullEntry = bls and !pe[1] and pe; bullEntry.SetDefaultColor(Color.CYAN); bullEntry.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP); bullEntry.SetLineWeight(5); def brs = CompoundValue(1, if v5 then 1 else if v1 or vStopped or close crosses below vl14 then 0 else brs[1], 0); def av3 = CompoundValue(1, if v1 then 0 else if v3 then 1 else av3[1], 0); def lv3 = if !av3 then 0 else if v3 then high else lv3[1]; def pve = (close crosses below vl13 or close crosses below lv3 or close crosses below vl24 or (cv5 >= 2 and close < GetValue(low, cv5))); def ve = if v5[-1] then 0 else if close < Min(close[1], open[1]) and pve then 1 else ve[1]; plot bearEntry = brs and ve and !ve[1]; bearEntry.SetDefaultColor(Color.MAGENTA); bearEntry.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN); bearEntry.SetLineWeight(5); AssignPriceColor(if bullEntry then Color.CYAN else Color.CURRENT); AssignPriceColor(if bearEntry then Color.MAGENTA else Color.CURRENT); plot bullstop = if pe and bls then lp5 else Double.NaN; bullstop.SetDefaultColor(Color.CYAN); plot bearStop = if ve and brs then lv5 else Double.NaN; bearStop.SetDefaultColor(Color.MAGENTA);