Repaints Moxie for ThinkorSwim

Repaints
@SleppyZ, I have one more request. Can you modify the code to turn the lines on and off? Thanks again for your help.

Input show_lines now added

Screenshot-2022-11-05-105433.png
Ruby:
# Swing High and Swing Low
# tomsk
# 11.18.2019
# As requested by chillc15 I have modified [USER=1174]@RobertPayne[/USER] code to include SwingHigh
# points which are now plotted in CYAN with the swing high points painted in PINK.
# So now you have both swing high and low on your charts
# +------------------------------------------------------------+
# | Example: How to extend levels to the right of the chart |
# | Robert Payne |
# | https://funwiththinkscript.com |
# +------------------------------------------------------------+
# SWING LOW
# define swing low points
input show_lines = no;
input length = 10;
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);
def swingLow = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);
# identify the very last swing low point
def lowPointOneBarNumber = HighestAll(if swingLow then bn else 0);
def lowPointOneValue = if bn == lowPointOneBarNumber then low else lowPointOneValue[1];
plot low1 = if !show_lines or bn < lowPointOneBarNumber then Double.NaN else lowPointOneValue;
low1.SetDefaultColor(Color.LIGHT_RED);
# identify the 2nd to last swing low point
def lowPointTwoBarNumber = HighestAll(if swingLow and bn < lowPointOneBarNumber then bn else 0);
def lowPointTwoValue = if bn == lowPointTwoBarNumber then low else lowPointTwoValue[1];
plot low2 = if !show_lines or bn < lowPointTwoBarNumber then Double.NaN else lowPointTwoValue;
low2.SetDefaultColor(Color.LIGHT_RED);
# just keep doing ths for as many lines as you want to add to the chart
# identify then 3rd to last swingHigh point low
def lowPointThreeBarNumber = HighestAll(if swingLow and bn < lowPointTwoBarNumber then bn else 0);
def lowPointThreeValue = if bn == lowPointThreeBarNumber then low else lowPointThreeValue[1];
plot low3 = if !show_lines or bn < lowPointThreeBarNumber then Double.NaN else lowPointThreeValue;
low3.SetDefaultColor(Color.LIGHT_RED);
# identify then 4th to last swingHigh point low
def lowPointFourBarNumber = HighestAll(if swingLow and bn < lowPointThreeBarNumber then bn else 0);
def lowPointFourValue = if bn == lowPointFourBarNumber then low else lowPointFourValue[1];
plot low4 = if !show_lines or bn < lowPointFourBarNumber then Double.NaN else lowPointFourValue;
low4.SetDefaultColor(Color.LIGHT_RED);
# identify then 5th to last swingHigh point low
def lowPointFiveBarNumber = HighestAll(if swingLow and bn < lowPointFourBarNumber then bn else 0);
def lowPointFiveValue = if bn == lowPointFiveBarNumber then low else lowPointFiveValue[1];
plot low5 = if !show_lines or bn < lowPointFiveBarNumber then Double.NaN else lowPointFiveValue;
low5.SetDefaultColor(Color.LIGHT_RED);
# identify then 6th to last swingHigh point low
def lowPointSixBarNumber = HighestAll(if swingLow and bn < lowPointFiveBarNumber then bn else 0);
def lowPointSixValue = if bn == lowPointSixBarNumber then low else lowPointSixValue[1];
plot low6 = if !show_lines or bn < lowPointSixBarNumber then Double.NaN else lowPointSixValue;
low6.SetDefaultColor(Color.LIGHT_RED);
# identify then 7th to last swingHigh point low
def lowPointSevenBarNumber = HighestAll(if swingLow and bn < lowPointSixBarNumber then bn else 0);
def lowPointSevenValue = if bn == lowPointSevenBarNumber then low else lowPointSevenValue[1];
plot low7 = if !show_lines or bn < lowPointSevenBarNumber then Double.NaN else lowPointSevenValue;
low7.SetDefaultColor(Color.LIGHT_RED);
# identify then 8th to last swingHigh point low
def lowPointEightBarNumber = HighestAll(if swingLow and bn < lowPointSevenBarNumber then bn else 0);
def lowPointEightValue = if bn == lowPointEightBarNumber then low else lowPointEightValue[1];
plot low8 = if !show_lines or bn < lowPointEightBarNumber then Double.NaN else lowPointEightValue;
low8.SetDefaultColor(Color.LIGHT_RED);
# identify then 9th to last swingHigh point low
def lowPointNineBarNumber = HighestAll(if swingLow and bn < lowPointEightBarNumber then bn else 0);
def lowPointNineValue = if bn == lowPointNineBarNumber then low else lowPointNineValue[1];
plot low9 = if !show_lines or bn < lowPointNineBarNumber then Double.NaN else lowPointNineValue;
low9.SetDefaultColor(Color.LIGHT_RED);
# identify then 10th to last swingHigh point low
def lowPointTenBarNumber = HighestAll(if swingLow and bn < lowPointNineBarNumber then bn else 0);
def lowPointTenValue = if bn == lowPointTenBarNumber then low else lowPointTenValue[1];
plot low10 = if !show_lines or bn < lowPointTenBarNumber then Double.NaN else lowPointTenValue;
low10.SetDefaultColor(Color.LIGHT_RED);


# SWING HIGH
# define swing high points
def swingHigh = high > Highest(high[1], length - 1) and high == GetValue(Highest(high, length), -offset);
# identify the very last swing high point
def highPointOneBarNumber = HighestAll(if swingHigh then bn else 0);
def highPointOneValue = if bn == highPointOneBarNumber then high else highPointOneValue[1];
plot high1 = if !show_lines or bn < highPointOneBarNumber then Double.NaN else highPointOneValue;
high1.SetDefaultColor(Color.CYAN);
# identify the 2nd to last swing high point
def highPointTwoBarNumber = HighestAll(if swingHigh and bn < highPointOneBarNumber then bn else 0);
def highPointTwoValue = if bn == highPointTwoBarNumber then high else highPointTwoValue[1];
plot high2 = if !show_lines or bn < highPointTwoBarNumber then Double.NaN else highPointTwoValue;
high2.SetDefaultColor(Color.CYAN);
# just keep doing ths for as many lines as you want to add to the chart
def highPointThreeBarNumber = HighestAll(if swingHigh and bn < highPointTwoBarNumber then bn else 0);
def highPointThreeValue = if bn == highPointThreeBarNumber then high else highPointThreeValue[1];
plot high3 = if !show_lines or bn < highPointThreeBarNumber then Double.NaN else highPointThreeValue;
high3.SetDefaultColor(Color.CYAN);
def highPointFourBarNumber = HighestAll(if swingHigh and bn < highPointThreeBarNumber then bn else 0);
def highPointFourValue = if bn == highPointFourBarNumber then high else highPointFourValue[1];
plot high4 = if !show_lines or bn < highPointFourBarNumber then Double.NaN else highPointFourValue;
high4.SetDefaultColor(Color.CYAN);
def highPointFiveBarNumber = HighestAll(if swingHigh and bn < highPointFourBarNumber then bn else 0);
def highPointFiveValue = if bn == highPointFiveBarNumber then high else highPointFiveValue[1];
plot high5 = if !show_lines or bn < highPointFiveBarNumber then Double.NaN else highPointFiveValue;
high5.SetDefaultColor(Color.CYAN);
def highPointSixBarNumber = HighestAll(if swingHigh and bn < highPointFiveBarNumber then bn else 0);
def highPointSixValue = if bn == highPointSixBarNumber then high else highPointSixValue[1];
plot high6 = if !show_lines or bn < highPointSixBarNumber then Double.NaN else highPointSixValue;
high6.SetDefaultColor(Color.CYAN);
def highPointSevenBarNumber = HighestAll(if swingHigh and bn < highPointSixBarNumber then bn else 0);
def highPointSevenValue = if bn == highPointSevenBarNumber then high else highPointSevenValue[1];
plot high7 = if !show_lines or bn < highPointSevenBarNumber then Double.NaN else highPointSevenValue;
high7.SetDefaultColor(Color.CYAN);
def highPointEightBarNumber = HighestAll(if swingHigh and bn < highPointSevenBarNumber then bn else 0);
def highPointEightValue = if bn == highPointEightBarNumber then high else highPointEightValue[1];
plot high8 = if !show_lines or bn < highPointEightBarNumber then Double.NaN else highPointEightValue;
high4.SetDefaultColor(Color.CYAN);
def highPointNineBarNumber = HighestAll(if swingHigh and bn < highPointEightBarNumber then bn else 0);
def highPointNineValue = if bn == highPointNineBarNumber then high else highPointNineValue[1];
plot high9 = if !show_lines or bn < highPointNineBarNumber then Double.NaN else highPointNineValue;
high4.SetDefaultColor(Color.CYAN);
def highPointTenBarNumber = HighestAll(if swingHigh and bn < highPointNineBarNumber then bn else 0);
def highPointTenValue = if bn == highPointTenBarNumber then high else highPointTenValue[1];
plot high10 = if !show_lines or bn < highPointTenBarNumber then Double.NaN else highPointTenValue;
high4.SetDefaultColor(Color.CYAN);

# ADJUST CANDLE COLORS
# change candle colors just to make it easier to see what we are working with
#AssignPriceColor(if swingLow then Color.cyan else if swingHigh then Color.mageNTA else Color.current);

def maxhl = if highPointOneBarNumber == Max(highPointOneBarNumber, lowPointOneBarNumber) then 1 else 0;

input limit_developing_arrow_plot = yes;
input show_completed_arrows       = no;

plot HighArrow = if limit_developing_arrow_plot and maxhl == 1 and bn == highPointOneBarNumber then Double.NaN else if show_completed_arrows and swingHigh then high else Double.NaN;
HighArrow.SetLineWeight(5);
HighArrow.SetDefaultColor(Color.RED);
HighArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

plot LowArrow = if limit_developing_arrow_plot and maxhl == 0 and bn == lowPointOneBarNumber then Double.NaN else if show_completed_arrows and swingLow then low else Double.NaN;
LowArrow.SetLineWeight(5);
LowArrow.SetDefaultColor(Color.GREEN);
LowArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

input limit_developing_bubble_plot = no;
input show_completed_bubbles       = no;

AddChartBubble( if limit_developing_bubble_plot and maxhl == 1 and  bn == highPointOneBarNumber then Double.NaN else if show_completed_bubbles then  bn == highPointOneBarNumber else Double.NaN, high1, if high1 > high2 then "HH" else "LH", if high1 > high2 then Color.GREEN else Color.RED);
AddChartBubble(if limit_developing_bubble_plot and maxhl == 0 and  bn == lowPointOneBarNumber then Double.NaN else if show_completed_bubbles then bn == lowPointOneBarNumber else Double.NaN, low1, if low1 > low2 then "HL" else "LL", if low1 > low2 then Color.GREEN else Color.RED, no);

# End Swing High and Swing Low

#Test to view last high/low swings
input test = no;
plot Highdot = if bn == highPointOneBarNumber then swingHigh else Double.NaN;
Highdot.SetLineWeight(5);
Highdot.SetDefaultColor(Color.RED);
Highdot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Highdot.SetHiding(!test);

plot Lowdot = if bn == lowPointOneBarNumber then swingLow else Double.NaN;
Lowdot.SetLineWeight(5);
Lowdot.SetDefaultColor(Color.GREEN);
Lowdot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Lowdot.SetHiding(!test);

input show_bubbles_HHLL = no;

def swinghigh1 = if swingHigh then high else swinghigh1[1];
def swinghigh2 = if swinghigh1 != swinghigh1[1] then swinghigh1[1] else swinghigh2[1];
#addlabel(1,swinghigh1[1]+" "+swinghigh2[1]);
AddChartBubble(if show_bubbles_HHLL and swinghigh1 > swinghigh2 then swingHigh else Double.NaN, high, "HH", Color.GREEN);

def swinglow1 = if swingLow then low else swinglow1[1];
def swinglow2 = if swinglow1 != swinglow1[1] then swinglow1[1] else swinglow2[1];
#addlabel(1,swinglow1[1]+" "+swinglow2[1]);
AddChartBubble(if show_bubbles_HHLL and swinglow1 < swinglow2 then swingLow else Double.NaN, low, "LL", Color.RED, no);

input show_swing_Prices = yes;
plot swinghigh_price = if show_swing_Prices and swingHigh then high else Double.NaN;
swinghigh_price.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
swinghigh_price.SetDefaultColor(Color.CYAN);
plot swinglow_price  = if show_swing_Prices and swingLow then low else Double.NaN;
swinglow_price.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
swinglow_price.SetDefaultColor(Color.LIGHT_RED);
 
All inputs have been adjusted and new price plots at the swings added
Thanks SleepyZ for your help. I used addchartbubble(show_swing_Prices, swingHigh, "$"+round(high,2),color.magenta,yes); it did not give any price
swinghigh_price.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE); worked well.
can addchartbubble(show_swing_Prices, swingHigh, "$"+round(high,2),color.magenta,yes); be revised to add price at highs?
Thanks,
 
Thanks SleepyZ for your help. I used addchartbubble(show_swing_Prices, swingHigh, "$"+round(high,2),color.magenta,yes); it did not give any price
swinghigh_price.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE); worked well.
can addchartbubble(show_swing_Prices, swingHigh, "$"+round(high,2),color.magenta,yes); be revised to add price at highs?
Thanks,

Sure, see input show_swing_Prices = {default bubbles, value, none}; and choose bubbles

Screenshot-2022-11-05-125204.png
Ruby:
# Swing High and Swing Low
# tomsk
# 11.18.2019
# As requested by chillc15 I have modified [USER=1174]@RobertPayne[/USER] code to include SwingHigh
# points which are now plotted in CYAN with the swing high points painted in PINK.
# So now you have both swing high and low on your charts
# +------------------------------------------------------------+
# | Example: How to extend levels to the right of the chart |
# | Robert Payne |
# | https://funwiththinkscript.com |
# +------------------------------------------------------------+
# SWING LOW
# define swing low points
input show_lines = no;
input length = 10;
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);
def swingLow = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);
# identify the very last swing low point
def lowPointOneBarNumber = HighestAll(if swingLow then bn else 0);
def lowPointOneValue = if bn == lowPointOneBarNumber then low else lowPointOneValue[1];
plot low1 = if !show_lines or bn < lowPointOneBarNumber then Double.NaN else lowPointOneValue;
low1.SetDefaultColor(Color.LIGHT_RED);
# identify the 2nd to last swing low point
def lowPointTwoBarNumber = HighestAll(if swingLow and bn < lowPointOneBarNumber then bn else 0);
def lowPointTwoValue = if bn == lowPointTwoBarNumber then low else lowPointTwoValue[1];
plot low2 = if !show_lines or bn < lowPointTwoBarNumber then Double.NaN else lowPointTwoValue;
low2.SetDefaultColor(Color.LIGHT_RED);
# just keep doing ths for as many lines as you want to add to the chart
# identify then 3rd to last swingHigh point low
def lowPointThreeBarNumber = HighestAll(if swingLow and bn < lowPointTwoBarNumber then bn else 0);
def lowPointThreeValue = if bn == lowPointThreeBarNumber then low else lowPointThreeValue[1];
plot low3 = if !show_lines or bn < lowPointThreeBarNumber then Double.NaN else lowPointThreeValue;
low3.SetDefaultColor(Color.LIGHT_RED);
# identify then 4th to last swingHigh point low
def lowPointFourBarNumber = HighestAll(if swingLow and bn < lowPointThreeBarNumber then bn else 0);
def lowPointFourValue = if bn == lowPointFourBarNumber then low else lowPointFourValue[1];
plot low4 = if !show_lines or bn < lowPointFourBarNumber then Double.NaN else lowPointFourValue;
low4.SetDefaultColor(Color.LIGHT_RED);
# identify then 5th to last swingHigh point low
def lowPointFiveBarNumber = HighestAll(if swingLow and bn < lowPointFourBarNumber then bn else 0);
def lowPointFiveValue = if bn == lowPointFiveBarNumber then low else lowPointFiveValue[1];
plot low5 = if !show_lines or bn < lowPointFiveBarNumber then Double.NaN else lowPointFiveValue;
low5.SetDefaultColor(Color.LIGHT_RED);
# identify then 6th to last swingHigh point low
def lowPointSixBarNumber = HighestAll(if swingLow and bn < lowPointFiveBarNumber then bn else 0);
def lowPointSixValue = if bn == lowPointSixBarNumber then low else lowPointSixValue[1];
plot low6 = if !show_lines or bn < lowPointSixBarNumber then Double.NaN else lowPointSixValue;
low6.SetDefaultColor(Color.LIGHT_RED);
# identify then 7th to last swingHigh point low
def lowPointSevenBarNumber = HighestAll(if swingLow and bn < lowPointSixBarNumber then bn else 0);
def lowPointSevenValue = if bn == lowPointSevenBarNumber then low else lowPointSevenValue[1];
plot low7 = if !show_lines or bn < lowPointSevenBarNumber then Double.NaN else lowPointSevenValue;
low7.SetDefaultColor(Color.LIGHT_RED);
# identify then 8th to last swingHigh point low
def lowPointEightBarNumber = HighestAll(if swingLow and bn < lowPointSevenBarNumber then bn else 0);
def lowPointEightValue = if bn == lowPointEightBarNumber then low else lowPointEightValue[1];
plot low8 = if !show_lines or bn < lowPointEightBarNumber then Double.NaN else lowPointEightValue;
low8.SetDefaultColor(Color.LIGHT_RED);
# identify then 9th to last swingHigh point low
def lowPointNineBarNumber = HighestAll(if swingLow and bn < lowPointEightBarNumber then bn else 0);
def lowPointNineValue = if bn == lowPointNineBarNumber then low else lowPointNineValue[1];
plot low9 = if !show_lines or bn < lowPointNineBarNumber then Double.NaN else lowPointNineValue;
low9.SetDefaultColor(Color.LIGHT_RED);
# identify then 10th to last swingHigh point low
def lowPointTenBarNumber = HighestAll(if swingLow and bn < lowPointNineBarNumber then bn else 0);
def lowPointTenValue = if bn == lowPointTenBarNumber then low else lowPointTenValue[1];
plot low10 = if !show_lines or bn < lowPointTenBarNumber then Double.NaN else lowPointTenValue;
low10.SetDefaultColor(Color.LIGHT_RED);


# SWING HIGH
# define swing high points
def swingHigh = high > Highest(high[1], length - 1) and high == GetValue(Highest(high, length), -offset);
# identify the very last swing high point
def highPointOneBarNumber = HighestAll(if swingHigh then bn else 0);
def highPointOneValue = if bn == highPointOneBarNumber then high else highPointOneValue[1];
plot high1 = if !show_lines or bn < highPointOneBarNumber then Double.NaN else highPointOneValue;
high1.SetDefaultColor(Color.CYAN);
# identify the 2nd to last swing high point
def highPointTwoBarNumber = HighestAll(if swingHigh and bn < highPointOneBarNumber then bn else 0);
def highPointTwoValue = if bn == highPointTwoBarNumber then high else highPointTwoValue[1];
plot high2 = if !show_lines or bn < highPointTwoBarNumber then Double.NaN else highPointTwoValue;
high2.SetDefaultColor(Color.CYAN);
# just keep doing ths for as many lines as you want to add to the chart
def highPointThreeBarNumber = HighestAll(if swingHigh and bn < highPointTwoBarNumber then bn else 0);
def highPointThreeValue = if bn == highPointThreeBarNumber then high else highPointThreeValue[1];
plot high3 = if !show_lines or bn < highPointThreeBarNumber then Double.NaN else highPointThreeValue;
high3.SetDefaultColor(Color.CYAN);
def highPointFourBarNumber = HighestAll(if swingHigh and bn < highPointThreeBarNumber then bn else 0);
def highPointFourValue = if bn == highPointFourBarNumber then high else highPointFourValue[1];
plot high4 = if !show_lines or bn < highPointFourBarNumber then Double.NaN else highPointFourValue;
high4.SetDefaultColor(Color.CYAN);
def highPointFiveBarNumber = HighestAll(if swingHigh and bn < highPointFourBarNumber then bn else 0);
def highPointFiveValue = if bn == highPointFiveBarNumber then high else highPointFiveValue[1];
plot high5 = if !show_lines or bn < highPointFiveBarNumber then Double.NaN else highPointFiveValue;
high5.SetDefaultColor(Color.CYAN);
def highPointSixBarNumber = HighestAll(if swingHigh and bn < highPointFiveBarNumber then bn else 0);
def highPointSixValue = if bn == highPointSixBarNumber then high else highPointSixValue[1];
plot high6 = if !show_lines or bn < highPointSixBarNumber then Double.NaN else highPointSixValue;
high6.SetDefaultColor(Color.CYAN);
def highPointSevenBarNumber = HighestAll(if swingHigh and bn < highPointSixBarNumber then bn else 0);
def highPointSevenValue = if bn == highPointSevenBarNumber then high else highPointSevenValue[1];
plot high7 = if !show_lines or bn < highPointSevenBarNumber then Double.NaN else highPointSevenValue;
high7.SetDefaultColor(Color.CYAN);
def highPointEightBarNumber = HighestAll(if swingHigh and bn < highPointSevenBarNumber then bn else 0);
def highPointEightValue = if bn == highPointEightBarNumber then high else highPointEightValue[1];
plot high8 = if !show_lines or bn < highPointEightBarNumber then Double.NaN else highPointEightValue;
high4.SetDefaultColor(Color.CYAN);
def highPointNineBarNumber = HighestAll(if swingHigh and bn < highPointEightBarNumber then bn else 0);
def highPointNineValue = if bn == highPointNineBarNumber then high else highPointNineValue[1];
plot high9 = if !show_lines or bn < highPointNineBarNumber then Double.NaN else highPointNineValue;
high4.SetDefaultColor(Color.CYAN);
def highPointTenBarNumber = HighestAll(if swingHigh and bn < highPointNineBarNumber then bn else 0);
def highPointTenValue = if bn == highPointTenBarNumber then high else highPointTenValue[1];
plot high10 = if !show_lines or bn < highPointTenBarNumber then Double.NaN else highPointTenValue;
high4.SetDefaultColor(Color.CYAN);

# ADJUST CANDLE COLORS
# change candle colors just to make it easier to see what we are working with
#AssignPriceColor(if swingLow then Color.cyan else if swingHigh then Color.mageNTA else Color.current);

def maxhl = if highPointOneBarNumber == Max(highPointOneBarNumber, lowPointOneBarNumber) then 1 else 0;

input limit_developing_arrow_plot = yes;
input show_completed_arrows       = no;

plot HighArrow = if limit_developing_arrow_plot and maxhl == 1 and bn == highPointOneBarNumber then Double.NaN else if show_completed_arrows and swingHigh then high else Double.NaN;
HighArrow.SetLineWeight(5);
HighArrow.SetDefaultColor(Color.RED);
HighArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

plot LowArrow = if limit_developing_arrow_plot and maxhl == 0 and bn == lowPointOneBarNumber then Double.NaN else if show_completed_arrows and swingLow then low else Double.NaN;
LowArrow.SetLineWeight(5);
LowArrow.SetDefaultColor(Color.GREEN);
LowArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

input limit_developing_bubble_plot = no;
input show_completed_bubbles       = no;

AddChartBubble( if limit_developing_bubble_plot and maxhl == 1 and  bn == highPointOneBarNumber then Double.NaN else if show_completed_bubbles then  bn == highPointOneBarNumber else Double.NaN, high1, if high1 > high2 then "HH" else "LH", if high1 > high2 then Color.GREEN else Color.RED);
AddChartBubble(if limit_developing_bubble_plot and maxhl == 0 and  bn == lowPointOneBarNumber then Double.NaN else if show_completed_bubbles then bn == lowPointOneBarNumber else Double.NaN, low1, if low1 > low2 then "HL" else "LL", if low1 > low2 then Color.GREEN else Color.RED, no);

# End Swing High and Swing Low

#Test to view last high/low swings
input test = no;
plot Highdot = if bn == highPointOneBarNumber then swingHigh else Double.NaN;
Highdot.SetLineWeight(5);
Highdot.SetDefaultColor(Color.RED);
Highdot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Highdot.SetHiding(!test);

plot Lowdot = if bn == lowPointOneBarNumber then swingLow else Double.NaN;
Lowdot.SetLineWeight(5);
Lowdot.SetDefaultColor(Color.GREEN);
Lowdot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
Lowdot.SetHiding(!test);

input show_bubbles_HHLL = no;

def swinghigh1 = if swingHigh then high else swinghigh1[1];
def swinghigh2 = if swinghigh1 != swinghigh1[1] then swinghigh1[1] else swinghigh2[1];
#addlabel(1,swinghigh1[1]+" "+swinghigh2[1]);
AddChartBubble(if show_bubbles_HHLL and swinghigh1 > swinghigh2 then swingHigh else Double.NaN, high, "HH", Color.GREEN);

def swinglow1 = if swingLow then low else swinglow1[1];
def swinglow2 = if swinglow1 != swinglow1[1] then swinglow1[1] else swinglow2[1];
#addlabel(1,swinglow1[1]+" "+swinglow2[1]);
AddChartBubble(if show_bubbles_HHLL and swinglow1 < swinglow2 then swingLow else Double.NaN, low, "LL", Color.RED, no);

input show_swing_Prices = {default bubbles, value, none};

plot swinghigh_price = if show_swing_Prices == show_swing_Prices.value and swingHigh then high else Double.NaN;
swinghigh_price.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
swinghigh_price.SetDefaultColor(Color.CYAN);
plot swinglow_price  = if show_swing_Prices == show_swing_Prices.value and swingLow then low else Double.NaN;
swinglow_price.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
swinglow_price.SetDefaultColor(Color.LIGHT_RED);
addchartbubble(show_swing_Prices == show_swing_Prices.bubbles and swingHigh, high, "$"+round(high,2),color.magenta,yes);
addchartbubble(show_swing_Prices == show_swing_Prices.bubbles and swinglow, low, "$"+round(low,2),color.cyan,no);
 
can a scan be created to show the Major Swing Low/High for this script
https://usethinkscript.com/threads/swing-high-low-indicator-for-thinkorswim.3109/#post-28726

# Trading Analysis Swing Waves - Trader
# Version 1.0
# 6/13/2016
#
# Authors: Brian Strong - Micro Quant ([email protected]) SwingWave detection
# Daniel Sinnig - Biiuse Trading ([email protected]) Ideal entry detection
#


input MajorSwingPeriod = 13;
input MinorSwingPeriod = 5;
input AlertsOn = yes;
input ExpansionFactor = 1.0;
input LookBackPeriod = 200;
input ADXPeriod = 21;
input ShowCounterTrendEntries = no;

def powerStopsOff = no;

def signalOffsetFactor = 0.20;
def offset = Average(TrueRange(high, close, low), 9) * signalOffsetFactor;

#Calculate Major Swings
def majorPivotH = if high > Highest(high[1], MajorSwingPeriod) and high >
Highest(high[-MajorSwingPeriod], MajorSwingPeriod) then 1 else 0;
def majorH = if majorPivotH then high+offset else Double.NaN;
def majorPivotL = if low < Lowest(low[1], MajorSwingPeriod) and low <
Lowest(low[-MajorSwingPeriod], MajorSwingPeriod) then 1 else 0;
def majorL = if majorPivotL then low-offset else Double.NaN;

#Calculate Minor Swings
def minorPivotH = if high > Highest(high[1], MinorSwingPeriod) and high >
Highest(high[-MinorSwingPeriod], MinorSwingPeriod) then 1 else 0;
def minorH = if minorPivotH then high+offset else Double.NaN;
def minorPivotL = if low < Lowest(low[1], MinorSwingPeriod) and low <
Lowest(low[-MinorSwingPeriod], MinorSwingPeriod) then 1 else 0;
def minorL = if minorPivotL then low-offset else Double.NaN;

Alert(AlertsOn and majorPivotH, GetSymbol() + " Major Swing High", Alert.BAR, Sound.Bell);
Alert(AlertsOn and majorPivotL, GetSymbol() + " Major Swing Low", Alert.BAR, Sound.Bell);
Alert(AlertsOn and minorPivotH, GetSymbol() + " Minor Swing High", Alert.BAR, Sound.Bell);
Alert(AlertsOn and minorPivotL, GetSymbol() + " MinorH Swing Low", Alert.BAR, Sound.Bell);


#Plot Major Swings
plot MajorSwHigh = majorH;
MajorSwHigh.setpaintingStrategy(paintingStrategy.POINTS);
MajorSwHigh.setLineWeight(5);
MajorSwHigh.setdefaultColor(Color.BLUE);
plot MajorSwLow = majorL;
MajorSwLow.setpaintingStrategy(paintingStrategy.POINTS);
MajorSwLow.setLineWeight(5);
MajorSwLow.setdefaultColor(Color.BLUE);

#Plot Minor Swings
plot MinorSwHigh = minorH;
MinorSwHigh.setpaintingStrategy(paintingStrategy.POINTS);
MinorSwHigh.setLineWeight(2);
MinorSwHigh.setdefaultColor(Color.MAGENTA);
plot MinorSwLow = minorL;
MinorSwLow.setpaintingStrategy(paintingStrategy.POINTS);
MinorSwLow.setLineWeight(2);
MinorSwLow.setdefaultColor(Color.MAGENTA);

#Ideal entry detection
#Find last minor and major High and Lows
def indexOfLastMinorHigh = fold minorHighIndex = -MajorSwingPeriod to lookBackPeriod - MajorSwingPeriod while IsNaN(GetValue(minorH, minorHighIndex)) do minorHighIndex + 1;
def indexOfLastMinorLow = fold minorLowIndex = -MajorSwingPeriod to lookBackPeriod - MajorSwingPeriod while IsNaN(GetValue(minorL, minorLowIndex)) do minorLowIndex + 1;
def indexOfLastMajorHigh = fold majorHighIndex = -MajorSwingPeriod to lookBackPeriod - MajorSwingPeriod while IsNaN(GetValue(majorH, majorHighIndex)) do majorHighIndex + 1;
def indexOfLastMajorLow = fold majorLowIndex = -MajorSwingPeriod to lookBackPeriod - MajorSwingPeriod while IsNaN(GetValue(majorL, majorLowIndex)) do majorLowIndex + 1;

#Find High / Low between current bar and last minor low / high (check that it should only minor for major stops as well)/
def indexOfLastHigh_sinceMinorHigh = fold i20 = -MajorSwingPeriod to indexOfLastMinorHigh + 1 with _tempHigh = Double.NEGATIVE_INFINITY do if GetValue(high, i20) > _tempHigh then GetValue(high, i20) else _tempHigh;
def indexOfLastLow_sinceMinorLow = fold i21 = -MajorSwingPeriod to indexOfLastMinorLow + 1 with _tempLow = Double.POSITIVE_INFINITY do if GetValue(low, i21) < _tempLow then GetValue(low, i21) else _tempLow;

def DiPlusDiMinusDiff = DIPlus(ADXPeriod) - DIMinus(ADXPeriod);

#Calculate and set individual differences

#---------------------------------------------
# (1) Major High to major Low
def differenceMajorH_to_MajorL = if IsNaN(majorL)
then 0 #if we are not at a major low then it's 0
else #otherwise find the corresponding major High and calculate the difference
fold i1 = 1 to lookBackPeriod
with diffMajorHighToMajorLow = 0
while (diffMajorHighToMajorLow == 0) do
if (!IsNaN(GetValue(majorL, i1))) then Double.NaN else #if another major low is found then cancel
if (!IsNaN(GetValue(majorH, i1)) and (GetValue(DiPlusDiMinusDiff, i1) < 0)) then Double.NaN #if DI+ is less than DI- then flag as NaN so that loop will stop and it'll be ignored later on
else if (!IsNaN(GetValue(majorH, i1))) then high[i1] - low
else 0;
def sum_AllDifferences_MajorH_to_MajorL = fold i5 = -MajorSwingPeriod to lookBackPeriod - MajorSwingPeriod
with _tempSum_MajorH_to_MajorL = 0 do
if !IsNaN(differenceMajorH_to_MajorL[i5]) then _tempSum_MajorH_to_MajorL + differenceMajorH_to_MajorL[i5] else _tempSum_MajorH_to_MajorL;

def count_AllDifferences_MajorH_to_MajorL = fold i9 = -MajorSwingPeriod to lookBackPeriod - MajorSwingPeriod with _tempCount_MajorH_to_MajorL = 0 do if
!IsNaN(differenceMajorH_to_MajorL[i9]) then
if (differenceMajorH_to_MajorL[i9] <> 0) then _tempCount_MajorH_to_MajorL + 1 else _tempCount_MajorH_to_MajorL
else _tempCount_MajorH_to_MajorL;

def average_MajorH_to_MajorL = if (count_AllDifferences_MajorH_to_MajorL <> 0) then sum_AllDifferences_MajorH_to_MajorL / count_AllDifferences_MajorH_to_MajorL else 0;

#FINAL
plot majorLongEntryLine = HighestAll(if IsNaN(close[-MajorSwingPeriod - 1]) and !IsNaN(close) then
if (!powerStopsOff and (ShowCounterTrendEntries or GetValue(DiPlusDiMinusDiff, -MajorSwingPeriod) >= 0)) then indexOfLastHigh_sinceMinorHigh - average_MajorH_to_MajorL * ExpansionFactor else Double.NaN
else Double.NaN);

majorLongEntryLine.SetLineWeight(2);
majorLongEntryLine.SetDefaultColor(Color.GREEN);



#------------------------------------------------
# (2) Major Low to major High

def differenceMajorL_to_MajorH = if IsNaN(majorH)
then 0 #if we are not at a minor high then it's 0
else #otherwise find the corresponding major Low and calculate the difference
fold i2 = 1 to lookBackPeriod
with diffMajorLowToMajorHigh = 0
while (diffMajorLowToMajorHigh == 0) do
if (!IsNaN(GetValue(majorH, i2))) then Double.NaN else #if another major high is found then cancel
if (!IsNaN(GetValue(majorL, i2)) and (GetValue(DiPlusDiMinusDiff, i2) > 0)) then Double.NaN #if DI+ is greater than DI- then flag as NaN so that loop will stop and it'll be ignored later on
else if (!IsNaN(GetValue(majorL, i2))) then high - low[i2]
else 0;
def sum_AllDifferences_MajorL_to_MajorH = fold i6 = -MajorSwingPeriod to lookBackPeriod - MajorSwingPeriod
with _tempSum_MajorL_to_MajorH = 0 do
if !IsNaN(differenceMajorL_to_MajorH[i6]) then _tempSum_MajorL_to_MajorH + differenceMajorL_to_MajorH[i6] else _tempSum_MajorL_to_MajorH;

def count_AllDifferences_MajorL_to_Major_H = fold i10 = -MajorSwingPeriod to lookBackPeriod - MajorSwingPeriod with _tempCount_MajorL_to_MajorH = 0 do if
!IsNaN(differenceMajorL_to_MajorH[i10]) then
if (differenceMajorL_to_MajorH[i10] <> 0) then _tempCount_MajorL_to_MajorH + 1 else _tempCount_MajorL_to_MajorH
else _tempCount_MajorL_to_MajorH;

def average_MajorL_to_MajorH = if (count_AllDifferences_MajorL_to_Major_H <> 0) then sum_AllDifferences_MajorL_to_MajorH / count_AllDifferences_MajorL_to_Major_H else 0;

#FINAL
plot majorShortEntryLine = HighestAll(if IsNaN(close[-MajorSwingPeriod - 1]) and !IsNaN(close) then
if (!powerStopsOff and (ShowCounterTrendEntries or GetValue(DiPlusDiMinusDiff, -MajorSwingPeriod) <= 0)) then indexOfLastLow_sinceMinorLow + average_MajorL_to_MajorH * ExpansionFactor else Double.NaN
else Double.NaN);

majorShortEntryLine.SetLineWeight(2);
majorShortEntryLine.SetDefaultColor(Color.RED);

#------------------------------------------------
# (3) Any high to any low (except major to major)

def differenceMinorH_to_MinorL = if IsNaN(minorL)
then 0 #if we are not at a minor low (that is not also a major low) then it's 0
else #otherwise find the corresponding major High and calculate the difference
fold i3 = 1 to lookBackPeriod
with diffMinorHighToMinorLow = 0
while (diffMinorHighToMinorLow == 0) do
if (!IsNaN(GetValue(minorL, i3))) then Double.NaN else #if another low is found then cancel
if !IsNaN(majorL) and !IsNaN(GetValue(majorH, i3)) then Double.NaN else #exclude major to major
if (!IsNaN(GetValue(minorH, i3)) and (GetValue(DiPlusDiMinusDiff, i3) < 0)) then Double.NaN #if DI+ is less than DI- then flag as NaN so that loop will stop and it'll be ignored later on
else if (!IsNaN(GetValue(minorH, i3))) then high[i3] - low
else 0;

def sum_AllDifferences_MinorH_to_Minor_L = fold i7 = -MajorSwingPeriod to lookBackPeriod - MajorSwingPeriod
with _tempSum_MinorH_to_MinorL = 0 do
if !IsNaN(differenceMinorH_to_MinorL[i7]) then _tempSum_MinorH_to_MinorL + differenceMinorH_to_MinorL[i7] else _tempSum_MinorH_to_MinorL;

def count_AllDifferences_MinorH_to_MinorL = fold i11 = -MajorSwingPeriod to lookBackPeriod - MajorSwingPeriod with _tempCount_MinorH_to_MinorL = 0 do if
!IsNaN(differenceMinorH_to_MinorL[i11]) then
if (differenceMinorH_to_MinorL[i11] <> 0) then _tempCount_MinorH_to_MinorL + 1 else _tempCount_MinorH_to_MinorL
else _tempCount_MinorH_to_MinorL;

def average_MinorH_to_MinorL = if (count_AllDifferences_MinorH_to_MinorL <> 0) then sum_AllDifferences_MinorH_to_Minor_L / count_AllDifferences_MinorH_to_MinorL else 0;

#FINAL
plot minorLongEntryLine = HighestAll(if IsNaN(close[-MajorSwingPeriod - 1]) and !IsNaN(close) then
if (!powerStopsOff and (ShowCounterTrendEntries or GetValue(DiPlusDiMinusDiff, -MajorSwingPeriod) >= 0)) then indexOfLastHigh_sinceMinorHigh - average_MinorH_to_MinorL * ExpansionFactor else Double.NaN
else Double.NaN);

minorLongEntryLine.SetDefaultColor(Color.GREEN);

#------------------------------------------------
# (4) Minor Low (not major) to Minor High (not major)

def differenceMinorL_to_MinorH = if IsNaN(minorH)
then 0 #if we are not at a minor low (that is not also a major low) then it's 0
else #otherwise find the corresponding major High and calculate the difference
fold i4 = 1 to lookBackPeriod
with diffMinorLowToMinorHigh = 0
while (diffMinorLowToMinorHigh == 0) do
if (!IsNaN(GetValue(minorH, i4))) then Double.NaN else #if another high is found then cancel
if !IsNaN(majorH) and !IsNaN(GetValue(majorL, i4)) then Double.NaN else #exclude major to major
if (!IsNaN(GetValue(minorL, i4)) and (GetValue(DiPlusDiMinusDiff, i4) > 0)) then Double.NaN #if DI+ is greater than DI- then flag as NaN so that loop will stop and it'll be ignored later on
else if (!IsNaN(GetValue(minorL, i4))) then high - low [i4]
else 0;

def sum_AllDifferences_MinorL_to_MinorH = fold i8 = -MajorSwingPeriod to lookBackPeriod - MajorSwingPeriod
with _tempSum_MinorL_to_MinorH = 0 do
if !IsNaN(differenceMinorL_to_MinorH[i8]) then _tempSum_MinorL_to_MinorH + differenceMinorL_to_MinorH[i8] else _tempSum_MinorL_to_MinorH;

def count_AllDifferences_MinorL_to_MinorH = fold i12 = -MajorSwingPeriod to lookBackPeriod - MajorSwingPeriod with _tempCount_MinorL_to_MinorH = 0 do if
!IsNaN(differenceMinorL_to_MinorH[i12]) then
if (differenceMinorL_to_MinorH[i12] <> 0) then _tempCount_MinorL_to_MinorH + 1 else _tempCount_MinorL_to_MinorH
else _tempCount_MinorL_to_MinorH;

def average_MinorL_to_MinorH = if (count_AllDifferences_MinorL_to_MinorH <> 0) then sum_AllDifferences_MinorL_to_MinorH / count_AllDifferences_MinorL_to_MinorH else 0;

#FINAL
plot minorShortEntryLine = HighestAll(if IsNaN(close[-MajorSwingPeriod - 1]) and !IsNaN(close) then
if (!powerStopsOff and (ShowCounterTrendEntries or GetValue(DiPlusDiMinusDiff, -MajorSwingPeriod) <= 0)) then indexOfLastLow_sinceMinorLow + average_MinorL_to_MinorH * ExpansionFactor else Double.NaN
else Double.NaN);

minorShortEntryLine.SetDefaultColor(Color.RED);
 
Last edited by a moderator:
Is it possible to count and display the number of candles between the high and following low instead of displaying their values?
 
can a scan be created to show the Major Swing Low/High for this script
https://usethinkscript.com/threads/swing-high-low-indicator-for-thinkorswim.3109/#post-28726

No, it can not be used in a scan.
When reviewing threads found on the forum. Make note of the ones that have a "repaints" prefix.
Repainters for the most part are too complex to use in scans, watchlists and conditional orders.

Read more about repainters:
https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-57833
 
Last edited:
Is it possible to count and display the number of candles between the high and following low instead of displaying their values?

Here is a swing high/low pivot indicator with the barcount between changes as well as other options:

Screenshot-2023-04-03-143719.png

Code:
#Swing_HL_Pivots_Stats

input pivotlength = 5;
input showhorizontals = yes;
input HHLLlabels = yes;
input showbubbles = yes;
input showHHLL_in_bubbles = yes;
input showprice_in_bubbles = yes;
input showpercentpricechange_in_bubbles = yes;
input showbarcount_in_bubbles = yes;

def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;

# Simple Swing Pivots

def HH1 = h >= Highest(h, pivotlength) and h >= Highest(h, pivotlength)[-pivotlength];
def High1 = if HH1 then h else Double.NaN;
def LL1 = l <= Lowest(l, pivotlength) and l <= Lowest(l, pivotlength)[-pivotlength];
def Low1 = if LL1 then l else Double.NaN;
def PointCount = if BarNumber() == 1 then 0 else
                 if IsNaN(c) then PointCount[1] else
                 if !IsNaN(High1) then Max(1, PointCount[1] + 1) else
                 if !IsNaN(Low1) then Min(-1, PointCount[1] - 1)
                 else PointCount[1];

#Horizontal Lines at Pivots1
def RangeHI = if !IsNaN(High1) and PointCount == 1
then h else RangeHI[1];
plot Rhi = RangeHI;
Rhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rhi.SetLineWeight(1);
Rhi.SetDefaultColor(Color.RED);
Rhi.sethiding(!showhorizontals);
def RangeLO = if !IsNaN(Low1) and PointCount == -1
then l else RangeLO[1];
plot Rlo = RangeLO;
Rlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rlo.SetLineWeight(1);
Rlo.SetDefaultColor(Color.GREEN);
Rlo.sethiding(!showhorizontals);

#HH/LH & LH/LL1 Labels

def rh1 = if !IsNaN(Rhi) then Rhi else rh1[1];
def rh2 = if rh1 != rh1[1] then rh1[1] else rh2[1];

AddLabel(HHLLlabels, if rh1 > rh2
              then "Higher Highs"
              else "Lower Highs",
              if rh1 > rh2
              then Color.GREEN
              else Color.RED);

def rl1 = if !IsNaN(Rlo) then Rlo else rl1[1];
def rl2 = if rl1 != rl1[1] then rl1[1] else rl2[1];
AddLabel(HHLLlabels, if  rl1 > rl2
              then "Higher Lows"
              else "Lower Lows",
              if rl1 > rl2
              then Color.GREEN
              else Color.RED);

#ZigZags
def zHI;
def zLO;

zHI = if !IsNaN(High1) and PointCount == 1 then h else Double.NaN;
zLO = if !IsNaN(Low1) and PointCount == -1 then l else Double.NaN;

def bn = BarNumber();
def lastzhi = if !IsNaN(zHI) then bn else lastzhi[1];
def lastzlo = if !IsNaN(zLO) then bn else lastzlo[1];

#ZigZag Line
plot hilowline = if !IsNaN(zHI) then zHI else zLO;
hilowline.EnableApproximation();
hilowline.SetLineWeight(3);
hilowline.SetDefaultColor(Color.WHITE);

def last = if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN;
def lastbar   = if BarNumber() == HighestAll(last) then 1 else 0;
def lastvalue = if lastbar then close else Double.NaN;

plot hilowline1 = if bn < Max(lastzhi, lastzlo)
                  then Double.NaN
                  else if lastzlo > lastzhi and !IsNaN(zLO)
                  then zLO
                  else if lastzlo < lastzhi and !IsNaN(zHI)
                  then zHI else lastvalue;
hilowline1.EnableApproximation();
hilowline1.SetLineWeight(3);
hilowline1.SetDefaultColor(Color.WHITE);

def xxhigh  = if rh1 == high then high else xxhigh[1];
def xxlow   = if rl1 == low then low else xxlow[1];

#Percent Price Change between ZigZags
def chghigh = (rh1 - xxlow[1]) / xxlow[1] * 100;
def chglow  = (rl1 - xxhigh[1]) / xxhigh[1] * 100;

#Bar Count between ZigZags
def barcountchange = AbsValue(lastzhi - lastzlo);


AddChartBubble(showbubbles and !IsNaN(High1) and PointCount == 1, high,
(if showHHLL_in_bubbles then
(if rh1 > rh2 then "HH\n" else "LH\n")
else "\n") +
(if showprice_in_bubbles then
AsText(high) + "\n" else "\n") +
(if showpercentpricechange_in_bubbles
then Round(chghigh) + "%\n" else "\n") +
if showbarcount_in_bubbles then
"" + barcountchange else "",
if xxhigh > xxhigh[1] then Color.GREEN else Color.RED);

AddChartBubble(showbubbles and !IsNaN(Low1) and PointCount == -1, low,
(if showHHLL_in_bubbles then
(if rl1 > rl2 then "HL\n" else "LL\n")
else "\n") +
(if showprice_in_bubbles then
AsText(low) + "\n" else "\n") +
(if showpercentpricechange_in_bubbles
then Round(chglow) + "%\n" else "\n") +
if showbarcount_in_bubbles then
"" + barcountchange else "",
if xxlow > xxlow[1] then Color.GREEN else Color.RED, no);
 
Last edited:
Is this related to Elliott wave by any chance? See if this can be any help to ya (but I doubt it)

Code:
## Mobius' swing high/low script.
## Added Fib retracements and extensions

# The number of bars to look back.
input n = 20;

# Set minDollarRange > 0 to turn off 0.382 and 0.618 Fibs,
# when dollar value of HighLine - LowLine range is less than
# chosen minDollarRange. Set to zero to ignore setting.
input minDollarRange = 200.0;

# Allow lines to be drawn in expansion area (Yes).
input rightExt = Yes;

def H = high;
def L = low;
def cBar = barNumber();
def PH;
def PL;

def isH = fold i = 1 to n + 1 with p = 1
while p do H > getValue(H, -i);

PH = if (cBar > n and H == Highest(H, n) and isH) then H
else double.NaN;

def isL = fold j = 1 to n + 1 with q = 1
while q do L < getValue(L, -j);

PL = if (cBar > n and L == Lowest(L, n) and isL) then l
else double.NaN;

rec PHL = if !IsNaN(PH) then PH else PHL[1];
rec PLL = if !IsNaN(PL) then PL else PLL[1];

def bOk = !IsNaN(h) or rightExt;

plot HighLine = if bOk and PHL > 0 then PHL else double.NaN;
HighLine.SetDefaultColor(Color.Light_Red);
HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HighLine.SetLineWeight(1);

plot LowLine = if bOk and PLL > 0 then PLL else double.NaN;
LowLine.SetDefaultColor(Color.Light_Green);
LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LowLine.SetLineWeight(1);

########################################
## Fib Retracements

def Ext0_382 = 0.382;
def Ext0_618 = 0.618;
def Ext0_2 = 0.2;
def Ext0_8 = 0.8;

def rHi = HighLine;
def rLo = LowLine;

def tmidP = (rHi + rLo) / 2;
def range = rHi - rLo;
def F0_382 = rHi - (range * Ext0_382);
def F0_618 = rHi - (range * Ext0_618);
def F0_2 = rHi - (range * Ext0_2);
def F0_8 = rHi - (range * Ext0_8);

def rangeValue = (AbsValue(range) / tickSize()) * tickValue();

def rangeValueOK = minDollarRange <= rangeValue;

plot FibMid = tmidP;
plot Fib0_382 = if !rangeValueOK then Double.NaN else F0_382;
plot Fib0_618 = if !rangeValueOK then Double.NaN else F0_618;
plot Fib0_2 = F0_2;
plot Fib0_8 = F0_8;

FibMid.HideTitle();
FibMid.SetDefaultColor(Color.White);
FibMid.SetPaintingStrategy(PaintingStrategy.DASHES);
FibMid.SetLineWeight(1);

Fib0_382.HideTitle();
Fib0_382.SetDefaultColor(Color.Gray);
Fib0_382.SetPaintingStrategy(PaintingStrategy.DASHES);

Fib0_618.HideTitle();
Fib0_618.SetDefaultColor(Color.Gray);
Fib0_618.SetPaintingStrategy(PaintingStrategy.DASHES);

Fib0_2.HideTitle();
Fib0_2.SetDefaultColor(Color.Gray);
Fib0_2.SetPaintingStrategy(PaintingStrategy.DASHES);

Fib0_8.HideTitle();
Fib0_8.SetDefaultColor(Color.Gray);
Fib0_8.SetPaintingStrategy(PaintingStrategy.DASHES);

########################################
## Fib Extensions

def FH_382 = range + if !IsNaN(h) then F0_382 else F0_382[1];
;

plot FibH_382 = FH_382;

FibH_382.HideTitle();
FibH_382.SetDefaultColor(Color.Cyan);
FibH_382.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FibH_382.SetLineWeight(1);

def FL_618 = (if !IsNaN(h) then F0_618 else F0_618[1]) - range;

plot FibL_618 = FL_618;

FibL_618.HideTitle();
FibL_618.SetDefaultColor(Color.Magenta);
FibL_618.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FibL_618.SetLineWeight(1);
Is it possible to turn this into a MTF? I watch the HOUR timeframe and take my trades off the 5m. I would love to this script on the HOUR TF with the same levels appearing on the 5m. Thank you
 
Is it possible to turn this into a MTF? I watch the HOUR timeframe and take my trades off the 5m. I would love to this script on the HOUR TF with the same levels appearing on the 5m. Thank you
Put repainting MTF code on a repainting indicator, no.

The redeeming value of repainting higher highs and lower lows indicators is that there is no lag.
read more: https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-57833

However, putting higher timeframes on a lower timeframe means having to wait for the higher timeframe to close. The wait time, wipes out the only value that the repainting indicator had.

For this reason, you will not find a multiple timeframe (MTF) repainting higher highs and lower lows indicators on this forum.
 
@samer800 Can you please provide a custom WL label to show for Swing High (SH) in red and Swing Low (SL) in green the WL ?
It is not possible to create watchlists for repainting indiacators as the Swing High (SH) and Swing Low (SL) are determined after-the-fact not in real time,
 
Is there a way to paint a line from swing high to swing high and paint a separate line from swing low to swing low? Paint line green if direction from swing high/low to swing high/low is upward, red if direction is sloping downward? Possible?
 
Last edited:
Is there a way to paint a line from swing high to swing high and paint a separate line from swing low to swing low? Paint line green if direction from swing high/low to swing high/low is upward, red if direction is sloping downward? Possible?

The code in the following link plots a bubble at the swing highs/lows that is green if the swing high/low is greater than the previous swing high/low and red otherwise

https://usethinkscript.com/threads/swing-high-low-indicator-for-thinkorswim.3109/post-123038
 
help me please. How can I add an average volume between these swing high / swing low pivot points?
https://usethinkscript.com/threads/swing-high-low-indicator-for-thinkorswim.3109/page-7#post-123038

Added Volume and Average Volume options at ZigZags

Screenshot 2023-05-19 095637.png
Code:
#Swing_HL_Pivots_Stats

input pivotlength = 5;
input showhorizontals = yes;
input HHLLlabels = yes;
input showbubbles = yes;
input showHHLL_in_bubbles = yes;
input showprice_in_bubbles = yes;
input showpercentpricechange_in_bubbles = yes;
input showbarcount_in_bubbles = yes;
input showvolume_in_bubbles = yes;
input showavgvolume_in_bubbles = yes;

def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;

# Simple Swing Pivots

def HH1 = h >= Highest(h, pivotlength) and h >= Highest(h, pivotlength)[-pivotlength];
def High1 = if HH1 then h else Double.NaN;
def LL1 = l <= Lowest(l, pivotlength) and l <= Lowest(l, pivotlength)[-pivotlength];
def Low1 = if LL1 then l else Double.NaN;
def PointCount = if BarNumber() == 1 then 0 else
                 if IsNaN(c) then PointCount[1] else
                 if !IsNaN(High1) then Max(1, PointCount[1] + 1) else
                 if !IsNaN(Low1) then Min(-1, PointCount[1] - 1)
                 else PointCount[1];

#Horizontal Lines at Pivots1
def RangeHI = if !IsNaN(High1) and PointCount == 1
then h else RangeHI[1];
plot Rhi = if !showhorizontals then Double.NaN else RangeHI;
Rhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rhi.SetLineWeight(1);
Rhi.SetDefaultColor(Color.RED);
def RangeLO = if !IsNaN(Low1) and PointCount == -1
then l else RangeLO[1];
plot Rlo = if !showhorizontals then Double.NaN else RangeLO;
Rlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rlo.SetLineWeight(1);
Rlo.SetDefaultColor(Color.GREEN);

#HH/LH & LH/LL1 Labels

def rh1 = if !IsNaN(Rhi) then Rhi else rh1[1];
def rh2 = if rh1 != rh1[1] then rh1[1] else rh2[1];

AddLabel(HHLLlabels, if rh1 > rh2
              then "Higher Highs"
              else "Lower Highs",
              if rh1 > rh2
              then Color.GREEN
              else Color.RED);

def rl1 = if !IsNaN(Rlo) then Rlo else rl1[1];
def rl2 = if rl1 != rl1[1] then rl1[1] else rl2[1];
AddLabel(HHLLlabels, if  rl1 > rl2
              then "Higher Lows"
              else "Lower Lows",
              if rl1 > rl2
              then Color.GREEN
              else Color.RED);

#ZigZags
def zHI;
def zLO;

zHI = if !IsNaN(High1) and PointCount == 1 then h else Double.NaN;
zLO = if !IsNaN(Low1) and PointCount == -1 then l else Double.NaN;

def bn = BarNumber();
def lastzhi = if !IsNaN(zHI) then bn else lastzhi[1];
def lastzlo = if !IsNaN(zLO) then bn else lastzlo[1];

#ZigZag Line
plot hilowline = if !IsNaN(zHI) then zHI else zLO;
hilowline.EnableApproximation();
hilowline.SetLineWeight(3);
hilowline.SetDefaultColor(Color.WHITE);

def last = if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN;
def lastbar   = if BarNumber() == HighestAll(last) then 1 else 0;
def lastvalue = if lastbar then close else Double.NaN;

plot hilowline1 = if bn < Max(lastzhi, lastzlo)
                  then Double.NaN
                  else if lastzlo > lastzhi and !IsNaN(zLO)
                  then zLO
                  else if lastzlo < lastzhi and !IsNaN(zHI)
                  then zHI else lastvalue;
hilowline1.EnableApproximation();
hilowline1.SetLineWeight(3);
hilowline1.SetDefaultColor(Color.WHITE);

def xxhigh  = if rh1 == high then high else xxhigh[1];
def xxlow   = if rl1 == low then low else xxlow[1];

#Percent Price Change between ZigZags
def chghigh = (rh1 - xxlow[1]) / xxlow[1] * 100;
def chglow  = (rl1 - xxhigh[1]) / xxhigh[1] * 100;

#Bar Count between ZigZags
def barcountchange = AbsValue(lastzhi - lastzlo);

#Volume and Avg Volume between ZigZags
def swinghivolume = if !IsNaN(zLO) then volume else if IsNaN(zHI) then swinghivolume[1] + volume else swinghivolume[1];

def swinglovolume = if !IsNaN(zHI) then volume else if IsNaN(zLO) then swinglovolume[1] + volume else swinglovolume[1];


AddChartBubble(showbubbles and !IsNaN(High1) and PointCount == 1, high,
(if showHHLL_in_bubbles then
(if rh1 > rh2 then "HH\n" else "LH\n")
else "\n") +
(if showprice_in_bubbles then
AsText(high) + "\n" else "\n") +
(if showpercentpricechange_in_bubbles
then Round(chghigh) + "%\n" else "\n") +
(if showbarcount_in_bubbles then
"" + barcountchange else "") +
(if showvolume_in_bubbles then
"\n" + swinghivolume else "\n") +
(if showavgvolume_in_bubbles then
"\n" + Round(swinghivolume / barcountchange, 0) else ""),
if xxhigh > xxhigh[1] then Color.GREEN else Color.RED);

AddChartBubble(showbubbles and !IsNaN(Low1) and PointCount == -1, low,
(if showHHLL_in_bubbles then
(if rl1 > rl2 then "HL\n" else "LL\n")
else "\n") +
(if showprice_in_bubbles then
AsText(low) + "\n" else "\n") +
(if showpercentpricechange_in_bubbles
then Round(chglow) + "%\n" else "\n") +
(if showbarcount_in_bubbles then
"" + barcountchange else "\n") +
(if showvolume_in_bubbles then
"\n" + swinglovolume else "\n") +
(if showavgvolume_in_bubbles then
"\n" + Round(swinglovolume / barcountchange, 0) else ""),
if xxlow > xxlow[1] then Color.GREEN else Color.RED, no);
 
Hello, I have trying to set up an alert using the HH, HL, LL, LH from the scripts in this forum but have not been able to figure it out. Does anyone know if that is possible? can anyone tell me which expression is the LH or HL? both scripts show the HH and LL then say else for the LH and HL and I can't seem to get the formula correct. any help is much appreciated.
 
Hello, I have trying to set up an alert using the HH, HL, LL, LH from the scripts in this forum but have not been able to figure it out. Does anyone know if that is possible? can anyone tell me which expression is the LH or HL? both scripts show the HH and LL then say else for the LH and HL and I can't seem to get the formula correct. any help is much appreciated.

The studies in this thread repaint.
They do not just repaint the last candle. It is NOT a case of waiting for the last candle to close.
These studies can go back and repaint candles from long past data points. They have to, otherwise, those perfect swings and zigzags wouldn't be perfect. That is how they look lovely on a chart.

In other words, the points that you are looking for do not happen in real time, they are repainted on points long past.

ToS platform does not provide for alerts on repainted signals.

Read more here --> https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-57833
 
Hello. Any chance you know which formula is the HL and LH from the script? I wanted to also add these from a higher timeframe onto a lower timeframe.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
296 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top