#converted from mobisu macd to ppo
# this is actually macd and sqz indicator in one
# added pivots
declare lower;
input fastPeriod = 12; #9/18/6 for 5m
input slowPeriod = 26;
input signalPeriod = 9;
input price = close;
input show = yes;
input agg1 = AggregationPeriod.FIVE_MIN;
def fastEma = ExpAverage( price, fastPeriod );
def slowEma = ExpAverage( price, slowPeriod );
def periodOK = fastPeriod < slowPeriod;
AddLabel( !periodOK, "ERROR: fastPeriod MUST be less than slowPeriod" );
def _ppo = if periodOK then ((fastEma - slowEma) / slowEma) * 100 else 0;
def _signal = ExpAverage( _ppo, signalPeriod );
# generic plots wiht colors
plot pmain = _ppo;
pmain.SetDefaultColor( Color.BLUE );
pmain.SetLineWeight(2);
pmain.AssignValueColor(if pmain < 0 and pmain < pmain[1] then Color.RED
else if pmain < 0 and pmain > pmain[1] then Color.DARK_GREEN
else if pmain > 0 and pmain > pmain[1] then Color.GREEN
else Color.DARK_RED);
pmain.HideBubble();
plot mainEma = _signal;
mainEma.SetDefaultColor( Color.RED );
mainEma.HideBubble();
AddCloud(pmain, mainEma, Color.Green, Color.RED);
#diff as histogram
plot diff = _ppo - _signal;
diff.SetDefaultColor(GetColor(5));
diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
diff.SetLineWeight(3);
diff.DefineColor("Positive and Up", Color.GREEN);
diff.DefineColor("Positive and Down", Color.DARK_GREEN);
diff.DefineColor("Negative and Down", Color.RED);
diff.DefineColor("Negative and Up", Color.DARK_RED);
diff.AssignValueColor(if diff >= 0 then if diff > diff[1] then diff.Color("Positive and Up") else diff.Color("Positive and Down") else if diff < diff[1] then diff.Color("Negative and Down") else diff.Color("Negative and Up"));
#mobius divergence calc
def pmainh = CompoundValue(1,
if pmain < 0 then Double.NaN
else if pmain crosses above 0 then pmain
else if pmain > 0 and pmain > pmainh[1]
then pmain else pmainh[1], 0);
def Valueh = CompoundValue(1,
if pmain < 0 then Double.NaN
else if pmain crosses above 0 then high
else if pmain > 0 and high > Valueh[1] then high
else Valueh[1], 0);
plot divLowSignal = if pmain > 0 and high > Valueh[1] and pmain < pmainh[1] then 0
else Double.NaN;
divLowSignal.SetPaintingStrategy(PaintingStrategy.SQUARES);
divLowSignal.SetLineWeight(5);
divLowSignal.SetDefaultColor(Color.dark_orange);
def pmainL = CompoundValue(1, if pmain > 0 then Double.NaN
else if pmain crosses below 0 then pmain
else if pmain < 0 and pmain < pmainl[1] then pmain
else pmainl[1], 0);
def ValueL = CompoundValue(1, if pmain > 0 then Double.NaN
else if pmain crosses below 0 then low
else if pmain < 0 and low < Valuel[1] then low
else Valuel[1], 0);
plot divUpSignall = if pmain < 0 and low < Valuel[1] and pmain > pmainl[1] then 0
else Double.NaN;
divupSignall.SetPaintingStrategy(PaintingStrategy.SQUARES);
divUpSignall.SetLineWeight(5);
divUPSignall.SetDefaultColor(Color.blue);
#zeroline
plot zeroLine = 0;
zeroLine.SetPaintingStrategy(PaintingStrategy.LINE);
zeroLine.SetLineWeight(1);
zeroLine.SetDefaultColor(Color.Gray);
#pivots
def ptrend = pmain;
def prange = 2;
def pivotHigh = if IsNaN(ptrend[-1]) then 0 else Lowest(ptrend, prange)[1] > ptrend and Lowest(ptrend, prange)[-prange] > ptrend ;
def pivotLow = if IsNaN(ptrend[-1]) then 0 else Highest(ptrend, prange)[1] < ptrend and Highest(ptrend, prange)[-prange] < ptrend ;
#study
plot plotPH = if pivotHigh and show then ptrend else Double.NaN;
plotPH.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
plotPH.SetDefaultColor(Color.GREEN);
plot phline = if pivotHigh then pmain else Double.NaN;
phline.SetDefaultColor(Color.DARK_GREEN);
phline.EnableApproximation();phline.Hide();
#
plot plotPL = if pivotLow and show then ptrend else Double.NaN;
plotPL.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
plotPL.SetDefaultColor(Color.RED);
plot plline = if pivotLow then pmain else Double.NaN;
plline.SetDefaultColor(Color.RED);
plline.EnableApproximation(); plline.hide();