Easy Swing Question

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);

I am clearly doing something wrong, but not sure what.

Many thanks,

Eric

Hi Eric,

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).

Pct=PERCENTSWING(PERCENT=2.0);
V1=SwingUp(Pct);
IF(V1, 1, -1)

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:

Pct=PERCENTSWING(PERCENT=2.0);
SWINGSTAT(Pct,DEFAULT=AvgBars, SWINGS=5, TYPE=Up)

Finally, here’s a post by Mathew explaining the complexities of using swing scripting formulas.

This is very helpful Darren.

1 Like