I am trying to write a short script to plot 2% swing values with a binary indicator. If the close rises 2% from a swing low Show View would be +1. If the close falls 2% from a swing high, Show View = -1.
I tried working with the below script, but getting +1 with no change.
// 2% Swing
PctSwing = PERCENTSWING(PERCENT=2, BARS=1);
Start = SWINGSTART(PctSwing);
End = SWINGEND(PctSwing);
// +1 if Close > Swing Low by 2%
// -1 if Close < Swing High by 2%
Binary = If(Close >= Start * 1.02,1,If( Close <= End * 0.98, -1, 0);
Another way to look at it would be when the swing turns up, which by definition it must be >2% from the low. So this will show +1 for upward swings, and -1 for downward swings. Note that it has to wait for the +/-2% move to happen before the direction changes (highlighted by the Show Bar arrow).
We also have SWINGSTAT() which may be useful when looking for average swing lengths, eg average up swing length for the last 5 2% swings on AGG is 31 trading days: