# ZigZag Fibonacci

> ZigZag Fibonacci is an indicator drawn over the price chart.

Source: https://algobarsx.com/docs/ex-35-zigzag-fibonacci/

```algobarsx
indicator "ZigZag Fibonacci"
    pane: price

input deviation = 3%

swings = zigzag(deviation: deviation, depth: 12)

if swings.len >= 2:
    last_swing = swings.last()
    previous_swing = swings.get(swings.len - 2)
    fib from: (previous_swing.bar, previous_swing.price), to: (last_swing.bar, last_swing.price), levels: [0.382, 0.5, 0.618, 0.786]
    polyline points: swings.map(s => (s.bar, s.price)), color: gray, width: 1
```
What this script says

ZigZag Fibonacci is an indicator drawn over the price chart.

You can change one input: `deviation` (default 3%).

It calculates `swings` as the zigzag swings (deviation `deviation`, depth 12).

On each bar, it checks whether the number of `swings` is at or above 2 and, if so, sets `last_swing` to `swings.last()`, sets `previous_swing` to `swings.get(swings.len - 2)`, draws Fibonacci levels and draws a polyline.
