Fractal Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © roaffix
// @version=4
// Fractal Candles | Toxic QR

study("Fractal Candles", shorttitle="Fractals", overlay=true)
x = input(title="Calculate Fractal by X candles", defval=5, minval=3)

index = if x%2
    ceil(x/2)-1
else
    round(x/2)-1

higher_fractal = if high[index] == highest(high, x)
    high[index]
else
    na

lower_fractal = if low[index] == lowest(low, x)
    low[index]
else
    na

plotshape(higher_fractal, style=shape.triangledown, location=location.abovebar, offset=-index, color=color.black)
plotshape(lower_fractal, style=shape.triangleup, location=location.belowbar, offset=-index, color=color.black)