Simple Trader - Swing Pivots
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SimpleTrader66



//@version=4

study(title="My Simple Trader - Swing Pivots", shorttitle="My Simple Trader - Swing Pivots", overlay=true, linktoseries=true)

bool inputShowWilliamsFractals = input(defval=true, title="Show Swing Pivots", type=input.bool)
int inputWilliamsLeftRange = input(defval=5, minval=1, maxval=50, title="No. of candles backward", group="Swing Settings")
int inputWilliamsRightRange = input(defval=5, minval=1, maxval=50, title="No. of candles foreward")
f_IsWilliamsFractal(_williamsLeftRange, _williamsRightRange, _type) =>
   
    _isWilliamsFractal = (_type == "high" and high[_williamsRightRange] >= highest(high,_williamsLeftRange+_williamsRightRange+1))
      or (_type == "low" and low[_williamsRightRange] <= lowest(low,_williamsLeftRange+_williamsRightRange+1))  // Passing in the _type allows us to use just one function to do both highs and lows
    _fractalValue = _isWilliamsFractal and _type == "high" ? high[_williamsRightRange] : _isWilliamsFractal and _type == "low" ? low[_williamsRightRange] : na
    [_isWilliamsFractal,_fractalValue]

[isWilliamsHigh,williamsHighPrice] = f_IsWilliamsFractal(inputWilliamsLeftRange, inputWilliamsRightRange, "high")
[isWilliamsLow,williamsLowPrice] = f_IsWilliamsFractal(inputWilliamsLeftRange, inputWilliamsRightRange, "low")

isWilliamsHigh := isWilliamsHigh[1] ? false : isWilliamsHigh
isWilliamsLow := isWilliamsLow[1] ? false : isWilliamsLow

williamsHighOffset = 0 - inputWilliamsRightRange 
color white10 = color.new(color.white,10)  
//plot(williamsHighPrice, title="Swing High PivotShape",   color=color.purple,  offset=williamsHighOffset, linewidth=2, style=plot.style_linebr)

plotshape(isWilliamsHigh and inputShowWilliamsFractals, title="Swing High PivotShape", style=shape.labelup, location=location.abovebar, color=white10, size=size.tiny, offset=williamsHighOffset)
williamsLowOffset = 0 - inputWilliamsRightRange

//plot(williamsLowPrice, title="Swing Low PivotShape",  color=color.green,  offset=williamsLowOffset, linewidth=1,  style=plot.style_linebr)

plotshape(isWilliamsLow and inputShowWilliamsFractals, title="Swing Low Pivot Shape", style=shape.labelup, location=location.belowbar, color=white10, size=size.tiny, offset=williamsLowOffset)


//plot(williamsHighPrice, "Line", color.green, 2, plot.style_line, true, show_last=1)
//plot(williamsLowPrice, "Line", color.red, 2, plot.style_line, true, show_last=1)

line.new(bar_index-inputWilliamsLeftRange, williamsHighPrice, bar_index+inputWilliamsRightRange, williamsHighPrice, extend=extend.none,width=1, color=color.white) 
line.new(bar_index-inputWilliamsLeftRange, williamsLowPrice, bar_index+inputWilliamsRightRange, williamsLowPrice, extend=extend.none,width=1, color=color.white)
