Hi Karen,

I’m assuming—by looking at your script—that negative divergence is when the PPO indicator is going down and the price is going up. My comments below are based on that assumption. If you are looking for something else, please let me know where I can read the theory behind it. I agree with Trevor that the best way to approach any issue is to output each line and check that it is what you expect.

**Comment 1** - Pivot should be backwards-looking only.

P1 = PIVOT(PPOPlot1, MIN=15, TYPE=High, DIR=Backwards);

We pass through the data in order and if we consider both directions then we are adding in a forward-looking bias.

**Comment 2** - ValueWhen() holds the value until the condition is next true. Here is the value of V1. In this image the lines are your event of

P1 <> 0

and the shaded area is

V1 = VALUEWHEN(PPOPlot1, P1 <> 0);

Note how the level stays until the next time P1 <> 0.

This means that the following line is actually looking for a decrease in the value of the PPO when the signals happened

//Is PPO high < previous?
Sig1 = V1 < V1[1]*0.96;

I don’t think that is what you are looking for. The actual solution to this is not trivial and is beyond what I can do here right now. If you want us to solve this, we’d need you to organise a Scripting consult at optuma.com/consults

An alternative is to use the Vergence function. (See KB https://help.optuma.com/kb/faq.php?id=960)

//Negative PPO divergence
// Calculating PPO
PPOMA1 = MA(BARS=5, STYLE=Exponential, CALC=Close);
PPOMA2 = MA(BARS=60, STYLE=Exponential, CALC=Close);
PPOPlot1 = ((PPOMA1 - PPOMA2) / PPOMA2)*100;
VERGENCE(High(), PPOPlot1, TYPE=Divergence)

All the best

Mathew