RicardoSantos

[RS]Swing Charts V0

EXPERIMENTAL:
Swing Charts with a personal touch(has slight modifications) :p
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?
//@version=2
study(title='[RS]Swing Charts V0', shorttitle='SC', overlay=true)

//  Swing charts as described here: http://www.investopedia.com/articles/technical/04/080404.asp
f_up_bar(_n_bars_back)=>
    _return = high[_n_bars_back] > high[_n_bars_back + 1] and low[_n_bars_back] > low[_n_bars_back + 1]

f_down_bar(_n_bars_back)=>
    _return = low[_n_bars_back] < low[_n_bars_back + 1] and high[_n_bars_back] < high[_n_bars_back + 1]

f_inside_bar(_n_bars_back)=>
    _return = high[_n_bars_back] <= high[_n_bars_back + 1] and low[_n_bars_back] >= low[_n_bars_back + 1]

f_outside_bar(_n_bars_back)=>
    _return = high[_n_bars_back] >= high[_n_bars_back + 1] and low[_n_bars_back] <= low[_n_bars_back + 1]

//--
f_swing_high(_n_bars_back)=>
    _condition_00 = f_up_bar(_n_bars_back + 1) and f_down_bar(_n_bars_back)
    _condition_01 = f_outside_bar(_n_bars_back + 1) and f_down_bar(_n_bars_back)
    _condition_02 = f_inside_bar(_n_bars_back + 1) and f_down_bar(_n_bars_back)
    _condition_03 = f_up_bar(_n_bars_back + 1) and f_inside_bar(_n_bars_back) and close[_n_bars_back] < hl2[_n_bars_back + 1]
    _condition_04 = f_outside_bar(_n_bars_back) and close < hl2
    _condition_05 = false
    _return = _condition_00 or _condition_01 or _condition_02 or _condition_03 or _condition_04 or _condition_05

f_swing_low(_n_bars_back)=>
    _condition_00 = f_down_bar(_n_bars_back + 1) and f_up_bar(_n_bars_back)
    _condition_01 = f_outside_bar(_n_bars_back + 1) and f_up_bar(_n_bars_back)
    _condition_02 = f_inside_bar(_n_bars_back + 1) and f_up_bar(_n_bars_back)
    _condition_03 = f_down_bar(_n_bars_back + 1) and f_inside_bar(_n_bars_back) and close[_n_bars_back] > hl2[_n_bars_back + 1]
    _condition_04 = f_outside_bar(_n_bars_back) and close > hl2
    _condition_05 = false
    _return = _condition_00 or _condition_01 or _condition_02 or _condition_03 or _condition_04 or _condition_05
//--
f_swingchart(_swings_high, _swings_low)=>
    _trend = na(_trend[1]) ? 1 : _trend[1] > 0 and _swings_low ? -1 : _trend[1] < 0 and _swings_high ? 1 : _trend[1]
    _return = na(_return[1]) ? 0 : change(_trend) > 0 ? nz(_swings_high, high[1]) : change(_trend) < 0 ? nz(_swings_low, low[1]) : _return[1]

swings_high = f_swing_high(0) ? highest(3) : na
swings_low = f_swing_low(0) ? lowest(3) : na

swing_chart = f_swingchart(swings_high, swings_low)
zigzag = change(swing_chart) != 0 ? swing_chart : na

plot(title='Swing High', series=swings_high, style=circles, color=red, transp=0, linewidth=4, offset=-1)
plot(title='Swing Low', series=swings_low, style=circles, color=lime, transp=0, linewidth=4, offset=-1)
plot(title='Swing Chart', series=swing_chart, color=change(swing_chart) != 0 ? na : black, transp=0, offset=-1)
plot(title='ZigZag', series=zigzag, color=black, transp=0, linewidth=2, offset=-1)

barcolor(title='Up Bar', color=f_up_bar(0) ? lime : na)
barcolor(title='Down Bar', color=f_down_bar(0) ? red : na)
barcolor(title='Inside Bar', color=f_inside_bar(0) ? blue : na)
barcolor(title='Outside Bar', color=f_outside_bar(0) ? aqua : na)