I suspect you problem starts with your use of SWINGSTART to find Peaks and Troughs.
Every trend change producing a Top and Bottom on the chart will be a SWINGSTART, except the last bar on the chart. Similarly, every Top and Bottom will be a SWINGEND, except for the first top/bottom turn on the left of the chart. You can easily prove this by applying SWINGSTART(GS1) to a SHOWBAR.
Because of this, to define a Peak you need to show that the previous and succeeding SWINGSTARTs are lower in price that the SWINGSTART at the Peak, thus to identify Peaks you need something like this:
Peak = (SWINGSTART(GS1)[1] < SWINGSTART(GS1)[0]) and (SWINGSTART(GS1)[0] > SWINGSTART(GS1)[-1]);
and for Troughs you could use:
Trough = (SWINGSTART(GS1)[1] > SWINGSTART(GS1)[0]) and (SWINGSTART(GS1)[0] < SWINGSTART(GS1)[-1]);
That’s the starting point for making your script work. I haven’t gone any further, that I’ll leave to you to it for the moment.
I would also note that there are a number of other foibles in using the GANNSWING, especially that a SWINGSTART/SWINGEND can only be confirmed and highlighted once the change in trend is confirmed and this can take a number of bars, especially if there are outside and/or inside bars around the turn. Been burned by that many times.
I would also suggest that every time you add a new variable to your scripts you test it with a SHOWBAR or SHOWVIEW for Boolean values and if you are calculating a value, use SHOWPLOT/SHOWVIEW or a CHART ELEMENT.
Thanks Trevor, I knew it had to be something to do with the SWINGSTART but would never have thought of the solution you’ve provided!!! Very much appreciated.
The best advice was to build it slowly using a Show Bar to check - I do tend to rush in a bit!
I’ll keep playing with the rest of it and hopefully get there.