## Wave Oscillator inspired by LazyBear and Zeiierman on TradingView
## Ported over by Chemmy for usethinkscript.com
declare lower;
input Channel_Length = 21; #10
input Average_Length = 35; #10
input over_bought_1 = 60;
input over_bought_2 = 53;
input over_sold_1 = -60;
input over_sold_2 = -53;
input show_bubbles = yes;
input show_sec_bbls = no;
input show_alerts = yes;
input show_lines = no;
input term = 60;
input calcavgtype = averagetype.EXPONENTIAL;
input band_avgtype = averagetype.WILDERS;
input showdiv = Yes;
input show_impulse = Yes;
def ap = hlc3;
def esa = MovingAverage(calcavgtype, ap, Channel_Length);
def d = MovingAverage(calcavgtype, AbsValue(ap - esa), Channel_Length);
def ci = (ap - esa) / (0.015 * d);
def tci = MovingAverage(calcavgtype, ci, Average_Length);
def wt1 = tci;
def wt2 = MovingAverage(band_avgtype, wt1, 4);
#def zero = 0;
plot zero = 0;
zero.SetDefaultColor( Color.GRAY );
#plot obLevel1 = over_bought_1;
#obLevel1.SetDefaultColor(Color.RED);
#plot osLevel1 = over_sold_1;
#osLevel1.SetDefaultColor(Color.GREEN);
#plot obLevel2 = over_bought_2;
#obLevel2.SetDefaultColor(Color.RED);
#obLevel2.SetStyle(Curve.SHORT_DASH);
#plot osLevel2 = over_sold_2;
#osLevel2.SetDefaultColor(Color.GREEN);
#osLevel2.SetStyle(Curve.SHORT_DASH);
plot wt1_1 = wt1;
wt1_1.SetDefaultColor(Color.GREEN);
wt1_1.SetHiding(!show_lines);
plot wt2_1 = wt2;
wt2_1.SetDefaultColor(Color.RED);
wt2_1.SetStyle(Curve.POINTS);
wt2_1.SetHiding(!show_lines);
plot wt3 = (wt1 - wt2);
wt3.AssignValueColor(if wt3>0 and wt3>wt3[1] then color.Green else if wt3>0 and wt3 <= wt3[1] then color.dark_green
else if wt3<0 and wt3<wt3[1] then color.Red else if wt3<0 and wt3>=wt3[1] then color.dark_red else color.Gray);
wt3.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
def dyna1 = highest(wt3, term);
def dyna2 = lowest(wt3, term);
plot dynaup = dyna1;
plot dynadn = dyna2;
def signal1 = wt1 crosses above wt2 and wt1 < over_sold_2;
plot Signal = if signal1 then (signal1 * dyna2) else Double.NaN;
Signal.SetDefaultColor(Color.GREEN);
Signal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Signal.SetLineWeight(3);
Signal.HideTitle();
def signal2 = wt1 crosses below wt2 and wt1 > over_bought_2;
plot Signal2_ = if signal2 then (signal2 * dyna1) else Double.NaN;
Signal2_.SetDefaultColor(Color.RED);
Signal2_.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Signal2_.SetLineWeight(3);
Signal2_.HideTitle();
def impdown = wt1 crosses below dynadn;
plot impulsedn = if impdown then wt3 else double.NaN;
impulsedn.SetPaintingStrategy(PaintingStrategy.Points);
impulsedn.SetDefaultColor(color.Red);
impulsedn.setlineweight(3);
impulsedn.SetHiding(!show_impulse);
def impup = wt1 crosses below dynadn;
plot impulseup = if impup then wt3 else double.NaN;
impulseup.SetPaintingStrategy(PaintingStrategy.Points);
impulseup.SetDefaultColor(color.Green);
impulseup.setlineweight(3);
impulseup.SetHiding(!show_impulse);
## Divergence Sections
##### DIVERGANCE
def bar = BarNumber();
def n = channel_Length;
def CurrH = if wt1 > dynaup
then fold i = 1 to Floor(n / 2)
with p = 1
while p
do wt1 > GetValue(wt1, -i)
else 0;
def CurrPivotH = if (bar > n and
wt1 == Highest(wt1, Floor(n / 2)) and CurrH) then wt3 else double.NaN;
def CurrL = if wt1 < dynadn
then fold j = 1 to Floor(n / 2)
with q = 1
while q
do wt1 < GetValue(wt1, -j)
else 0;
def CurrPivotL = if (bar > n and
wt1 == Lowest(wt1, Floor(n / 2)) and CurrL) then wt3 else double.NaN;
def CurrPHBar = if !IsNaN(CurrPivotH) then bar else CurrPHBar[1];
def CurrPLBar = if !IsNaN(CurrPivotL) then bar else CurrPLBar[1];
def PHpoint = if !IsNaN(CurrPivotH) then CurrPivotH else PHpoint[1];
def PLpoint = if !IsNaN(CurrPivotL) then CurrPivotL else PLpoint[1];
def priorPHBar = if PHpoint != PHpoint[1] then CurrPHBar[1] else priorPHBar[1];
def priorPLBar = if PLpoint != PLpoint[1] then CurrPLBar[1] else priorPLBar[1];
def HighPivots = bar >= HighestAll(priorPHBar);
def LowPivots = bar >= HighestAll(priorPLBar);
def pivotHigh = if HighPivots then CurrPivotH else double.Nan;
def pivotLow = if LowPivots then CurrPivotL else double.Nan;
plot PlotHline = pivotHigh;
PlotHline.EnableApproximation();
PlotHline.SetDefaultColor(Color.RED);
PlotHline.SetStyle(Curve.SHORT_DASH);
PlotHline.SetHiding(!ShowDiv);
plot PlotLline = pivotLow;
PlotLline.EnableApproximation();
PlotLline.SetDefaultColor(Color.LIME);
PlotLline.SetStyle(Curve.SHORT_DASH);
PlotLline.SetHiding(!ShowDiv);
plot PivotDot = if !IsNaN(pivotHigh) then pivotHigh else
if !IsNaN(pivotLow) then pivotLow else double.NaN;
PivotDot.SetDefaultColor(Color.YELLOW);
PivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
PivotDot.SetLineWeight(3);
PivotDot.SetHiding(!ShowDiv);