# USER INPUTS
input show_points = no;
input dot_size = 1;
input dot_distance = 2;
input show_lines = yes;
input price_type = {default High_Low, Open_Close, Close_Only};
input swing_back = 9;
input swing_forward = 9;
input maxbars = 500;
input show_labels = no;
input limit_plot = yes;
input plot_limited_to_last_x = 1;
# GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(192, 192, 192));
# CALCULATIONS
def sb = swing_back;
def sf = swing_forward;
def na = Double.NaN;
def PriceHigh =
if price_type == price_type.High_Low then high
else if price_type == price_type.Open_Close then Max(open, close)
else if price_type == price_type.Close_Only then close
else close
;
def PriceLow =
if price_type == price_type.High_Low then low
else if price_type == price_type.Open_Close then Min(open, close)
else if price_type == price_type.Close_Only then close
else close
;
def lfor = Lowest(PriceLow, sf)[-sf];
def lback = Lowest(PriceLow, sb)[1];
def hfor = Highest(PriceHigh, sf)[-sf];
def hback = Highest(PriceHigh, sb)[1];
def swinglow = if PriceLow < lfor and PriceLow <= lback then yes else no;
def swinghigh = if PriceHigh > hfor and PriceHigh >= hback then yes else no;
def lsl = if IsNaN(close[-sf]) then lsl[1] else if swinglow then PriceLow else lsl[1];
def lsh = if swinghigh then PriceHigh else lsh[1];
def lcount = if swinglow then 1 else lcount[1] + 1;
def hcount = if swinghigh then 1 else hcount[1] + 1;
def slcount = if swinglow then slcount[1] + 1 else slcount[1];
def shcount = if swinghigh then shcount[1] + 1 else shcount[1];
def onlylastslplotbar = slcount == HighestAll(slcount);
def onlylastshplotbar = shcount == HighestAll(shcount);
def lastlowdata =
if lcount <= maxbars and IsNaN(close[-sf]) then lsl[1]
else if lcount > maxbars then na
else if lcount < 2 then na
else lsl;
def lasthighdata =
if hcount <= maxbars and IsNaN(close[-sf]) then lsh[1]
else if hcount > maxbars then na
else if hcount < 2 then na
else lsh;
# PLOTS
plot sl = if swinglow then PriceLow - (dot_distance * TickSize()) else na;
plot sh = if swinghigh then PriceHigh + (dot_distance * TickSize()) else na;
def cond_h = if !IsNaN(sh) then 1 else Double.NaN;
def dataCount_h = CompoundValue(1, if !IsNaN(cond_h) then dataCount_h[1] + 1 else dataCount_h[1], 0);
def cond_l = if !IsNaN(sl) then 1 else Double.NaN;
def dataCount_l = CompoundValue(1, if !IsNaN(cond_l) then dataCount_l[1] + 1 else dataCount_l[1], 0);
plot lastlow =
if limit_plot then
if HighestAll(dataCount_l) - dataCount_l <= plot_limited_to_last_x - 1 then
lastlowdata
else na
else lsl;
plot lasthigh =
if limit_plot then
if HighestAll(dataCount_h) - dataCount_h <= plot_limited_to_last_x - 1 then
lasthighdata
else na
else
lasthighdata;
# FORMATTING
sh.SetStyle(Curve.POINTS);
sh.SetLineWeight(dot_size);
sh.AssignValueColor(if sh > lasthigh[1] then GlobalColor("Green") else GlobalColor("Red"));
sh.SetHiding(show_points == no);
sh.HideBubble();
sh.HideTitle();
sl.SetStyle(Curve.POINTS);
sl.SetLineWeight(dot_size);
sl.AssignValueColor(if sl < lastlow[1] then GlobalColor("Red") else GlobalColor("Green"));
sl.SetHiding(show_points == no);
sl.HideBubble();
sl.HideTitle();
lasthigh.SetStyle(Curve.SHORT_DASH);
lasthigh.AssignValueColor(GlobalColor("Gray"));
lasthigh.SetHiding(show_lines == no);
lasthigh.HideBubble();
lasthigh.HideTitle();
lastlow.SetStyle(Curve.SHORT_DASH);
lastlow.AssignValueColor(GlobalColor("Gray"));
lastlow.SetHiding(show_lines == no);
lastlow.HideBubble();
lastlow.HideTitle();