New High Low Breakout For ThinkOrSwim

John Snyder

New member
In an effort to learn how to use the Thinkscript "fold" command, I wrote this indicator which measures the number of bars taken to exceed the price since it was at this same level. I freely admit that I studied other indicator code and copied and modified the work of others to create this indicator!

tTxFPai.png


Code:
# NewHLBreakout (New High/Low Breakout) measures the number of bars taken to exceed the previous time price was at this level.

declare lower;

input LBlen = 100;  #Look back length
input Breakout = 20;

DefineGlobalColor("G0", CreateColor(0, 255, 0));  #Green
DefineGlobalColor("G1", CreateColor(0, 229, 0));  #Green
DefineGlobalColor("G2", CreateColor(0, 206, 0));  #Green
DefineGlobalColor("G3", CreateColor(0, 186, 0));  #Green
DefineGlobalColor("G4", CreateColor(0, 167, 0));  #Green
DefineGlobalColor("G5", CreateColor(0, 151, 0));  #Green
DefineGlobalColor("G6", CreateColor(0, 136, 0));  #Green
DefineGlobalColor("G7", CreateColor(0, 122, 0));  #Green
DefineGlobalColor("G8", CreateColor(0, 109, 0));  #Green
DefineGlobalColor("G9", CreateColor(0, 98, 0));  #Green

DefineGlobalColor("L0", CreateColor(128, 255, 0));  #Lime
DefineGlobalColor("L1", CreateColor(113, 224, 0));  #Lime  * 0.88
DefineGlobalColor("L2", CreateColor(99, 197, 0));  #Lime
DefineGlobalColor("L3", CreateColor(87, 174, 0));  #Lime
DefineGlobalColor("L4", CreateColor(77, 153, 0));  #Lime
DefineGlobalColor("L5", CreateColor(66, 135, 0));  #Lime
DefineGlobalColor("L6", CreateColor(59, 118, 0));  #Lime
DefineGlobalColor("L7", CreateColor(52, 104, 0));  #Lime
DefineGlobalColor("L8", CreateColor(46, 92, 0));  #Lime
DefineGlobalColor("L9", CreateColor(41, 81, 0));  #Lime

DefineGlobalColor("R0", CreateColor(255, 0, 0));  #Red
DefineGlobalColor("R1", CreateColor(224, 0, 0));  #Red
DefineGlobalColor("R2", CreateColor(197, 0, 0));  #Red
DefineGlobalColor("R3", CreateColor(174, 0, 0));  #Red
DefineGlobalColor("R4", CreateColor(153, 0, 0));  #Red
DefineGlobalColor("R5", CreateColor(135, 0, 0));  #Red
DefineGlobalColor("R6", CreateColor(118, 0, 0));  #Red
DefineGlobalColor("R7", CreateColor(104, 0, 0));  #Red
DefineGlobalColor("R8", CreateColor(92, 0, 0));  #Red
DefineGlobalColor("R9", CreateColor(81, 0, 0));  #Red

DefineGlobalColor("Pi0", CreateColor(255, 96, 128));  #Pink
DefineGlobalColor("Pi1", CreateColor(224, 84, 113));  #Pink
DefineGlobalColor("Pi2", CreateColor(197, 74, 99));  #Pink
DefineGlobalColor("Pi3", CreateColor(174, 65, 87));  #Pink
DefineGlobalColor("Pi4", CreateColor(153, 58, 77));  #Pink
DefineGlobalColor("Pi5", CreateColor(135, 51, 68));  #Pink
DefineGlobalColor("Pi6", CreateColor(118, 45, 59));  #Pink
DefineGlobalColor("Pi7", CreateColor(104, 39, 52));  #Pink
DefineGlobalColor("Pi8", CreateColor(92, 35, 46));  #Pink
DefineGlobalColor("Pi9", CreateColor(81, 30, 41));  #Pink

DefineGlobalColor("O0", CreateColor(255, 128, 0));  #Orange
DefineGlobalColor("O1", CreateColor(237, 106, 0));  #Orange  * 0.83
DefineGlobalColor("O2", CreateColor(221, 88, 0));  #Orange
DefineGlobalColor("O3", CreateColor(205, 73, 0));  #Orange
DefineGlobalColor("O4", CreateColor(191, 61, 0));  #Orange
DefineGlobalColor("O5", CreateColor(177, 50, 0));  #Orange
DefineGlobalColor("O6", CreateColor(165, 42, 0));  #Orange
DefineGlobalColor("O7", CreateColor(153, 33, 0));  #Orange
DefineGlobalColor("O8", CreateColor(143, 29, 0));  #Orange
DefineGlobalColor("O9", CreateColor(133, 24, 0));  #Orange

DefineGlobalColor("Y0", CreateColor(255, 255, 0));  #Yellow
DefineGlobalColor("Y1", CreateColor(234, 234, 0));  #Yellow *0.92
DefineGlobalColor("Y2", CreateColor(216, 216, 0));  #Yellow
DefineGlobalColor("Y3", CreateColor(198, 198, 0));  #Yellow
DefineGlobalColor("Y4", CreateColor(182, 182, 0));  #Yellow
DefineGlobalColor("Y5", CreateColor(168, 168, 0));  #Yellow
DefineGlobalColor("Y6", CreateColor(155, 155, 0));  #Yellow
DefineGlobalColor("Y7", CreateColor(142, 142, 0));  #Yellow
DefineGlobalColor("Y8", CreateColor(131, 131, 0));  #Yellow
DefineGlobalColor("Y9", CreateColor(120, 120, 0));  #Yellow

DefineGlobalColor("C0", CreateColor(0, 255, 255));  #Cyan
DefineGlobalColor("C1", CreateColor(0, 219, 219));  #Cyan *0.86
DefineGlobalColor("C2", CreateColor(0, 188, 189));  #Cyan
DefineGlobalColor("C3", CreateColor(0, 161, 162));  #Cyan
DefineGlobalColor("C4", CreateColor(0, 137, 139));  #Cyan
DefineGlobalColor("C5", CreateColor(0, 117, 119));  #Cyan
DefineGlobalColor("C6", CreateColor(0, 100, 103));  #Cyan
DefineGlobalColor("C7", CreateColor(0, 86, 89));  #Cyan
DefineGlobalColor("C8", CreateColor(0, 72, 76));  #Cyan
DefineGlobalColor("C9", CreateColor(0, 62, 66));  #Cyan

DefineGlobalColor("B0", CreateColor(0, 128, 255));  #Blue
DefineGlobalColor("B1", CreateColor(0, 117, 234));  #Blue *0.89
DefineGlobalColor("B2", CreateColor(0, 108, 216));  #Blue
DefineGlobalColor("B3", CreateColor(0, 99, 198));  #Blue
DefineGlobalColor("B4", CreateColor(0, 91, 182));  #Blue
DefineGlobalColor("B5", CreateColor(0, 84, 168));  #Blue
DefineGlobalColor("B6", CreateColor(0, 78, 155));  #Blue
DefineGlobalColor("B7", CreateColor(0, 71, 142));  #Blue
DefineGlobalColor("B8", CreateColor(0, 66, 131));  #Blue
DefineGlobalColor("B9", CreateColor(0, 60, 120));  #Blue
#DefineGlobalColor("B10", CreateColor(0, 60, 120));  #Blue

DefineGlobalColor("LP0", CreateColor(170, 149, 255));  #Light Purple
DefineGlobalColor("LP1", CreateColor(156, 137, 234));  #Light Purple *0.92
DefineGlobalColor("LP2", CreateColor(144, 126, 216));  #Light Purple
DefineGlobalColor("LP3", CreateColor(132, 116, 198));  #Light Purple
DefineGlobalColor("LP4", CreateColor(122, 107, 182));  #Light Purple
DefineGlobalColor("LP5", CreateColor(112, 98, 168));  #Light Purple
DefineGlobalColor("LP6", CreateColor(103, 90, 155));  #Light Purple
DefineGlobalColor("LP7", CreateColor(95, 83, 142));  #Light Purple
DefineGlobalColor("LP8", CreateColor(87, 76, 131));  #Light Purple
DefineGlobalColor("LP9", CreateColor(80, 70, 120));  #Light Purple

DefineGlobalColor("P0", CreateColor(128, 0, 255));  #Purple
DefineGlobalColor("P1", CreateColor(117, 0, 234));  #Purple
DefineGlobalColor("P2", CreateColor(108, 0, 216));  #Purple
DefineGlobalColor("P3", CreateColor(99, 0, 198));  #Purple
DefineGlobalColor("P4", CreateColor(91, 0, 182));  #Purple
DefineGlobalColor("P5", CreateColor(84, 0, 168));  #Purple
DefineGlobalColor("P6", CreateColor(78, 0, 155));  #Purple
DefineGlobalColor("P7", CreateColor(71, 0, 142));  #Purple
DefineGlobalColor("P8", CreateColor(66, 0, 131));  #Purple
DefineGlobalColor("P9", CreateColor(60, 0, 120));  #Purple

DefineGlobalColor("M0", CreateColor(255, 0, 255));  #Magenta
DefineGlobalColor("M1", CreateColor(224, 0, 224));  #Magenta
DefineGlobalColor("M2", CreateColor(197, 0, 197));  #Magenta
DefineGlobalColor("M3", CreateColor(174, 0, 174));  #Magenta
DefineGlobalColor("M4", CreateColor(153, 0, 153));  #Magenta
DefineGlobalColor("M5", CreateColor(135, 0, 135));  #Magenta
DefineGlobalColor("M6", CreateColor(118, 0, 118));  #Magenta
DefineGlobalColor("M7", CreateColor(104, 0, 104));  #Magenta
DefineGlobalColor("M8", CreateColor(92, 0, 92));  #Magenta
DefineGlobalColor("M9", CreateColor(81, 0, 81));  #Magenta

DefineGlobalColor("Gray", CreateColor(28, 28, 28));  #Gray

def na = Double.NaN;
def bn = BarNumber();
def lastbar = if !IsNaN(close) then bn else na;


def HH = fold i = 1 to LBlen + 1
         with p = 0
         while GetValue(high, bn - lastbar) > GetValue(high, (bn - (lastbar - i)))
         do i;

def LL = fold j = 1 to LBlen + 1
         with q = 0
         while GetValue(low, bn - lastbar) < GetValue(low, (bn - (lastbar - j)))
         do -j;

def HLsum = HH + LL;
def HLavg = TEMA(HLsum, 150);
def highestAvg = HighestAll(HLavg);
def lowestAvg = LowestAll(HLavg);
def RangeAvg = highestAvg + AbsValue(lowestAvg);
def scHLAvg = (HLavg / (RangeAvg * 0.5)) * LBlen;


plot Tr_HH = HH;
Tr_HH.AssignValueColor(Color.GREEN);
AddCloud(HH, 0, Color.GREEN);

plot Tr_LL = LL;
Tr_LL.AssignValueColor(Color.RED);
AddCloud(0, LL, Color.RED);

plot Tr_Avg = scHLAvg;
Tr_Avg.hide();
Tr_Avg.SetLineWeight(3);
Tr_Avg.AssignValueColor(
if HLSum > 0
then
    if HLavg >= HLavg[1]
    then
        if (HLavg - HLavg[1]) >= (HLavg[1] - HLavg[2])
        then GlobalColor("G0")
        else GlobalColor("G5")
    else
        if (HLavg - HLavg[1]) <= (HLavg[1] - HLavg[2])
        then GlobalColor("B0")
        else GlobalColor("B5")
else
    if HLavg <= HLavg[1]
    then
        if (HLavg - HLavg[1]) <= (HLavg[1] - HLavg[2])
        then GlobalColor("R0")
        else GlobalColor("R5")
    else
        if (HLavg - HLavg[1]) >= (HLavg[1] - HLavg[2])
        then GlobalColor("Y0")
        else GlobalColor("Y3")
);

plot UpperBO = Breakout;
UpperBO.AssignValueColor(Color.LIGHT_GRAY);

plot UpperCO = Breakout * 0.5;
UpperCO.AssignValueColor(Color.GRAY);

plot LowerBO = -Breakout;
LowerBO.AssignValueColor(Color.LIGHT_GRAY);

plot LowerCO = -Breakout * 0.5;
LowerCO.AssignValueColor(Color.GRAY);
 
Last edited by a moderator:

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

NewHighLowBreakout Indicator

In an effort to learn how to use the Thinkscript "fold" command, I wrote this indicator which measures the number of bars taken to exceed the price since it was at this same level. I freely admit that I studied other indicator code and copied and modified the work of others to create this indicator!

Code:
# NewHLBreakout (New High/Low Breakout) measures the number of bars taken to exceed the previous time price was at this level.

declare lower;

input LBlen = 100;  #Look back length
input Breakout = 20;

DefineGlobalColor("G0", CreateColor(0, 255, 0));  #Green
DefineGlobalColor("G1", CreateColor(0, 229, 0));  #Green
DefineGlobalColor("G2", CreateColor(0, 206, 0));  #Green
DefineGlobalColor("G3", CreateColor(0, 186, 0));  #Green
DefineGlobalColor("G4", CreateColor(0, 167, 0));  #Green
DefineGlobalColor("G5", CreateColor(0, 151, 0));  #Green
DefineGlobalColor("G6", CreateColor(0, 136, 0));  #Green
DefineGlobalColor("G7", CreateColor(0, 122, 0));  #Green
DefineGlobalColor("G8", CreateColor(0, 109, 0));  #Green
DefineGlobalColor("G9", CreateColor(0, 98, 0));  #Green

DefineGlobalColor("L0", CreateColor(128, 255, 0));  #Lime
DefineGlobalColor("L1", CreateColor(113, 224, 0));  #Lime  * 0.88
DefineGlobalColor("L2", CreateColor(99, 197, 0));  #Lime
DefineGlobalColor("L3", CreateColor(87, 174, 0));  #Lime
DefineGlobalColor("L4", CreateColor(77, 153, 0));  #Lime
DefineGlobalColor("L5", CreateColor(66, 135, 0));  #Lime
DefineGlobalColor("L6", CreateColor(59, 118, 0));  #Lime
DefineGlobalColor("L7", CreateColor(52, 104, 0));  #Lime
DefineGlobalColor("L8", CreateColor(46, 92, 0));  #Lime
DefineGlobalColor("L9", CreateColor(41, 81, 0));  #Lime

DefineGlobalColor("R0", CreateColor(255, 0, 0));  #Red
DefineGlobalColor("R1", CreateColor(224, 0, 0));  #Red
DefineGlobalColor("R2", CreateColor(197, 0, 0));  #Red
DefineGlobalColor("R3", CreateColor(174, 0, 0));  #Red
DefineGlobalColor("R4", CreateColor(153, 0, 0));  #Red
DefineGlobalColor("R5", CreateColor(135, 0, 0));  #Red
DefineGlobalColor("R6", CreateColor(118, 0, 0));  #Red
DefineGlobalColor("R7", CreateColor(104, 0, 0));  #Red
DefineGlobalColor("R8", CreateColor(92, 0, 0));  #Red
DefineGlobalColor("R9", CreateColor(81, 0, 0));  #Red

DefineGlobalColor("Pi0", CreateColor(255, 96, 128));  #Pink
DefineGlobalColor("Pi1", CreateColor(224, 84, 113));  #Pink
DefineGlobalColor("Pi2", CreateColor(197, 74, 99));  #Pink
DefineGlobalColor("Pi3", CreateColor(174, 65, 87));  #Pink
DefineGlobalColor("Pi4", CreateColor(153, 58, 77));  #Pink
DefineGlobalColor("Pi5", CreateColor(135, 51, 68));  #Pink
DefineGlobalColor("Pi6", CreateColor(118, 45, 59));  #Pink
DefineGlobalColor("Pi7", CreateColor(104, 39, 52));  #Pink
DefineGlobalColor("Pi8", CreateColor(92, 35, 46));  #Pink
DefineGlobalColor("Pi9", CreateColor(81, 30, 41));  #Pink

DefineGlobalColor("O0", CreateColor(255, 128, 0));  #Orange
DefineGlobalColor("O1", CreateColor(237, 106, 0));  #Orange  * 0.83
DefineGlobalColor("O2", CreateColor(221, 88, 0));  #Orange
DefineGlobalColor("O3", CreateColor(205, 73, 0));  #Orange
DefineGlobalColor("O4", CreateColor(191, 61, 0));  #Orange
DefineGlobalColor("O5", CreateColor(177, 50, 0));  #Orange
DefineGlobalColor("O6", CreateColor(165, 42, 0));  #Orange
DefineGlobalColor("O7", CreateColor(153, 33, 0));  #Orange
DefineGlobalColor("O8", CreateColor(143, 29, 0));  #Orange
DefineGlobalColor("O9", CreateColor(133, 24, 0));  #Orange

DefineGlobalColor("Y0", CreateColor(255, 255, 0));  #Yellow
DefineGlobalColor("Y1", CreateColor(234, 234, 0));  #Yellow *0.92
DefineGlobalColor("Y2", CreateColor(216, 216, 0));  #Yellow
DefineGlobalColor("Y3", CreateColor(198, 198, 0));  #Yellow
DefineGlobalColor("Y4", CreateColor(182, 182, 0));  #Yellow
DefineGlobalColor("Y5", CreateColor(168, 168, 0));  #Yellow
DefineGlobalColor("Y6", CreateColor(155, 155, 0));  #Yellow
DefineGlobalColor("Y7", CreateColor(142, 142, 0));  #Yellow
DefineGlobalColor("Y8", CreateColor(131, 131, 0));  #Yellow
DefineGlobalColor("Y9", CreateColor(120, 120, 0));  #Yellow

DefineGlobalColor("C0", CreateColor(0, 255, 255));  #Cyan
DefineGlobalColor("C1", CreateColor(0, 219, 219));  #Cyan *0.86
DefineGlobalColor("C2", CreateColor(0, 188, 189));  #Cyan
DefineGlobalColor("C3", CreateColor(0, 161, 162));  #Cyan
DefineGlobalColor("C4", CreateColor(0, 137, 139));  #Cyan
DefineGlobalColor("C5", CreateColor(0, 117, 119));  #Cyan
DefineGlobalColor("C6", CreateColor(0, 100, 103));  #Cyan
DefineGlobalColor("C7", CreateColor(0, 86, 89));  #Cyan
DefineGlobalColor("C8", CreateColor(0, 72, 76));  #Cyan
DefineGlobalColor("C9", CreateColor(0, 62, 66));  #Cyan

DefineGlobalColor("B0", CreateColor(0, 128, 255));  #Blue
DefineGlobalColor("B1", CreateColor(0, 117, 234));  #Blue *0.89
DefineGlobalColor("B2", CreateColor(0, 108, 216));  #Blue
DefineGlobalColor("B3", CreateColor(0, 99, 198));  #Blue
DefineGlobalColor("B4", CreateColor(0, 91, 182));  #Blue
DefineGlobalColor("B5", CreateColor(0, 84, 168));  #Blue
DefineGlobalColor("B6", CreateColor(0, 78, 155));  #Blue
DefineGlobalColor("B7", CreateColor(0, 71, 142));  #Blue
DefineGlobalColor("B8", CreateColor(0, 66, 131));  #Blue
DefineGlobalColor("B9", CreateColor(0, 60, 120));  #Blue
#DefineGlobalColor("B10", CreateColor(0, 60, 120));  #Blue

DefineGlobalColor("LP0", CreateColor(170, 149, 255));  #Light Purple
DefineGlobalColor("LP1", CreateColor(156, 137, 234));  #Light Purple *0.92
DefineGlobalColor("LP2", CreateColor(144, 126, 216));  #Light Purple
DefineGlobalColor("LP3", CreateColor(132, 116, 198));  #Light Purple
DefineGlobalColor("LP4", CreateColor(122, 107, 182));  #Light Purple
DefineGlobalColor("LP5", CreateColor(112, 98, 168));  #Light Purple
DefineGlobalColor("LP6", CreateColor(103, 90, 155));  #Light Purple
DefineGlobalColor("LP7", CreateColor(95, 83, 142));  #Light Purple
DefineGlobalColor("LP8", CreateColor(87, 76, 131));  #Light Purple
DefineGlobalColor("LP9", CreateColor(80, 70, 120));  #Light Purple

DefineGlobalColor("P0", CreateColor(128, 0, 255));  #Purple
DefineGlobalColor("P1", CreateColor(117, 0, 234));  #Purple
DefineGlobalColor("P2", CreateColor(108, 0, 216));  #Purple
DefineGlobalColor("P3", CreateColor(99, 0, 198));  #Purple
DefineGlobalColor("P4", CreateColor(91, 0, 182));  #Purple
DefineGlobalColor("P5", CreateColor(84, 0, 168));  #Purple
DefineGlobalColor("P6", CreateColor(78, 0, 155));  #Purple
DefineGlobalColor("P7", CreateColor(71, 0, 142));  #Purple
DefineGlobalColor("P8", CreateColor(66, 0, 131));  #Purple
DefineGlobalColor("P9", CreateColor(60, 0, 120));  #Purple

DefineGlobalColor("M0", CreateColor(255, 0, 255));  #Magenta
DefineGlobalColor("M1", CreateColor(224, 0, 224));  #Magenta
DefineGlobalColor("M2", CreateColor(197, 0, 197));  #Magenta
DefineGlobalColor("M3", CreateColor(174, 0, 174));  #Magenta
DefineGlobalColor("M4", CreateColor(153, 0, 153));  #Magenta
DefineGlobalColor("M5", CreateColor(135, 0, 135));  #Magenta
DefineGlobalColor("M6", CreateColor(118, 0, 118));  #Magenta
DefineGlobalColor("M7", CreateColor(104, 0, 104));  #Magenta
DefineGlobalColor("M8", CreateColor(92, 0, 92));  #Magenta
DefineGlobalColor("M9", CreateColor(81, 0, 81));  #Magenta

DefineGlobalColor("Gray", CreateColor(28, 28, 28));  #Gray

def na = Double.NaN;
def bn = BarNumber();
def lastbar = if !IsNaN(close) then bn else na;


def HH = fold i = 1 to LBlen + 1
         with p = 0
         while GetValue(high, bn - lastbar) > GetValue(high, (bn - (lastbar - i)))
         do i;

def LL = fold j = 1 to LBlen + 1
         with q = 0
         while GetValue(low, bn - lastbar) < GetValue(low, (bn - (lastbar - j)))
         do -j;

def HLsum = HH + LL;
def HLavg = TEMA(HLsum, 150);
def highestAvg = HighestAll(HLavg);
def lowestAvg = LowestAll(HLavg);
def RangeAvg = highestAvg + AbsValue(lowestAvg);
def scHLAvg = (HLavg / (RangeAvg * 0.5)) * LBlen;


plot Tr_HH = HH;
Tr_HH.AssignValueColor(Color.GREEN);
AddCloud(HH, 0, Color.GREEN);

plot Tr_LL = LL;
Tr_LL.AssignValueColor(Color.RED);
AddCloud(0, LL, Color.RED);

plot Tr_Avg = scHLAvg;
Tr_Avg.hide();
Tr_Avg.SetLineWeight(3);
Tr_Avg.AssignValueColor(
if HLSum > 0
then
    if HLavg >= HLavg[1]
    then
        if (HLavg - HLavg[1]) >= (HLavg[1] - HLavg[2])
        then GlobalColor("G0")
        else GlobalColor("G5")
    else
        if (HLavg - HLavg[1]) <= (HLavg[1] - HLavg[2])
        then GlobalColor("B0")
        else GlobalColor("B5")
else
    if HLavg <= HLavg[1]
    then
        if (HLavg - HLavg[1]) <= (HLavg[1] - HLavg[2])
        then GlobalColor("R0")
        else GlobalColor("R5")
    else
        if (HLavg - HLavg[1]) >= (HLavg[1] - HLavg[2])
        then GlobalColor("Y0")
        else GlobalColor("Y3")
);

plot UpperBO = Breakout;
UpperBO.AssignValueColor(Color.LIGHT_GRAY);

plot UpperCO = Breakout * 0.5;
UpperCO.AssignValueColor(Color.GRAY);

plot LowerBO = -Breakout;
LowerBO.AssignValueColor(Color.LIGHT_GRAY);

plot LowerCO = -Breakout * 0.5;
LowerCO.AssignValueColor(Color.GRAY);
Can you clarify how you use this?
 
Can you clarify how you use this?
Good question!! As I said, I wrote this as an exercise in an effort to figure out how to use the "fold" command. However ... I know there are some individuals who trade using breakouts ... and with this you can easily determine when you have reached a 20 day high or low, or 50 day, or whatever you select. In some ways it would provide information similar to what one would obtain drawing support and resistance lines.
 
In an effort to learn how to use the Thinkscript "fold" command, I wrote this indicator which measures the number of bars taken to exceed the price since it was at this same level. I freely admit that I studied other indicator code and copied and modified the work of others to create this indicator!
Code:
# NewHLBreakout (New High/Low Breakout) measures the number of bars taken to exceed the previous time price was at this level.

declare lower;

input LBlen = 100;  #Look back length
input Breakout = 20;

DefineGlobalColor("G0", CreateColor(0, 255, 0));  #Green
DefineGlobalColor("G1", CreateColor(0, 229, 0));  #Green
DefineGlobalColor("G2", CreateColor(0, 206, 0));  #Green
DefineGlobalColor("G3", CreateColor(0, 186, 0));  #Green
DefineGlobalColor("G4", CreateColor(0, 167, 0));  #Green
DefineGlobalColor("G5", CreateColor(0, 151, 0));  #Green
DefineGlobalColor("G6", CreateColor(0, 136, 0));  #Green
DefineGlobalColor("G7", CreateColor(0, 122, 0));  #Green
DefineGlobalColor("G8", CreateColor(0, 109, 0));  #Green
DefineGlobalColor("G9", CreateColor(0, 98, 0));  #Green

DefineGlobalColor("L0", CreateColor(128, 255, 0));  #Lime
DefineGlobalColor("L1", CreateColor(113, 224, 0));  #Lime  * 0.88
DefineGlobalColor("L2", CreateColor(99, 197, 0));  #Lime
DefineGlobalColor("L3", CreateColor(87, 174, 0));  #Lime
DefineGlobalColor("L4", CreateColor(77, 153, 0));  #Lime
DefineGlobalColor("L5", CreateColor(66, 135, 0));  #Lime
DefineGlobalColor("L6", CreateColor(59, 118, 0));  #Lime
DefineGlobalColor("L7", CreateColor(52, 104, 0));  #Lime
DefineGlobalColor("L8", CreateColor(46, 92, 0));  #Lime
DefineGlobalColor("L9", CreateColor(41, 81, 0));  #Lime

DefineGlobalColor("R0", CreateColor(255, 0, 0));  #Red
DefineGlobalColor("R1", CreateColor(224, 0, 0));  #Red
DefineGlobalColor("R2", CreateColor(197, 0, 0));  #Red
DefineGlobalColor("R3", CreateColor(174, 0, 0));  #Red
DefineGlobalColor("R4", CreateColor(153, 0, 0));  #Red
DefineGlobalColor("R5", CreateColor(135, 0, 0));  #Red
DefineGlobalColor("R6", CreateColor(118, 0, 0));  #Red
DefineGlobalColor("R7", CreateColor(104, 0, 0));  #Red
DefineGlobalColor("R8", CreateColor(92, 0, 0));  #Red
DefineGlobalColor("R9", CreateColor(81, 0, 0));  #Red

DefineGlobalColor("Pi0", CreateColor(255, 96, 128));  #Pink
DefineGlobalColor("Pi1", CreateColor(224, 84, 113));  #Pink
DefineGlobalColor("Pi2", CreateColor(197, 74, 99));  #Pink
DefineGlobalColor("Pi3", CreateColor(174, 65, 87));  #Pink
DefineGlobalColor("Pi4", CreateColor(153, 58, 77));  #Pink
DefineGlobalColor("Pi5", CreateColor(135, 51, 68));  #Pink
DefineGlobalColor("Pi6", CreateColor(118, 45, 59));  #Pink
DefineGlobalColor("Pi7", CreateColor(104, 39, 52));  #Pink
DefineGlobalColor("Pi8", CreateColor(92, 35, 46));  #Pink
DefineGlobalColor("Pi9", CreateColor(81, 30, 41));  #Pink

DefineGlobalColor("O0", CreateColor(255, 128, 0));  #Orange
DefineGlobalColor("O1", CreateColor(237, 106, 0));  #Orange  * 0.83
DefineGlobalColor("O2", CreateColor(221, 88, 0));  #Orange
DefineGlobalColor("O3", CreateColor(205, 73, 0));  #Orange
DefineGlobalColor("O4", CreateColor(191, 61, 0));  #Orange
DefineGlobalColor("O5", CreateColor(177, 50, 0));  #Orange
DefineGlobalColor("O6", CreateColor(165, 42, 0));  #Orange
DefineGlobalColor("O7", CreateColor(153, 33, 0));  #Orange
DefineGlobalColor("O8", CreateColor(143, 29, 0));  #Orange
DefineGlobalColor("O9", CreateColor(133, 24, 0));  #Orange

DefineGlobalColor("Y0", CreateColor(255, 255, 0));  #Yellow
DefineGlobalColor("Y1", CreateColor(234, 234, 0));  #Yellow *0.92
DefineGlobalColor("Y2", CreateColor(216, 216, 0));  #Yellow
DefineGlobalColor("Y3", CreateColor(198, 198, 0));  #Yellow
DefineGlobalColor("Y4", CreateColor(182, 182, 0));  #Yellow
DefineGlobalColor("Y5", CreateColor(168, 168, 0));  #Yellow
DefineGlobalColor("Y6", CreateColor(155, 155, 0));  #Yellow
DefineGlobalColor("Y7", CreateColor(142, 142, 0));  #Yellow
DefineGlobalColor("Y8", CreateColor(131, 131, 0));  #Yellow
DefineGlobalColor("Y9", CreateColor(120, 120, 0));  #Yellow

DefineGlobalColor("C0", CreateColor(0, 255, 255));  #Cyan
DefineGlobalColor("C1", CreateColor(0, 219, 219));  #Cyan *0.86
DefineGlobalColor("C2", CreateColor(0, 188, 189));  #Cyan
DefineGlobalColor("C3", CreateColor(0, 161, 162));  #Cyan
DefineGlobalColor("C4", CreateColor(0, 137, 139));  #Cyan
DefineGlobalColor("C5", CreateColor(0, 117, 119));  #Cyan
DefineGlobalColor("C6", CreateColor(0, 100, 103));  #Cyan
DefineGlobalColor("C7", CreateColor(0, 86, 89));  #Cyan
DefineGlobalColor("C8", CreateColor(0, 72, 76));  #Cyan
DefineGlobalColor("C9", CreateColor(0, 62, 66));  #Cyan

DefineGlobalColor("B0", CreateColor(0, 128, 255));  #Blue
DefineGlobalColor("B1", CreateColor(0, 117, 234));  #Blue *0.89
DefineGlobalColor("B2", CreateColor(0, 108, 216));  #Blue
DefineGlobalColor("B3", CreateColor(0, 99, 198));  #Blue
DefineGlobalColor("B4", CreateColor(0, 91, 182));  #Blue
DefineGlobalColor("B5", CreateColor(0, 84, 168));  #Blue
DefineGlobalColor("B6", CreateColor(0, 78, 155));  #Blue
DefineGlobalColor("B7", CreateColor(0, 71, 142));  #Blue
DefineGlobalColor("B8", CreateColor(0, 66, 131));  #Blue
DefineGlobalColor("B9", CreateColor(0, 60, 120));  #Blue
#DefineGlobalColor("B10", CreateColor(0, 60, 120));  #Blue

DefineGlobalColor("LP0", CreateColor(170, 149, 255));  #Light Purple
DefineGlobalColor("LP1", CreateColor(156, 137, 234));  #Light Purple *0.92
DefineGlobalColor("LP2", CreateColor(144, 126, 216));  #Light Purple
DefineGlobalColor("LP3", CreateColor(132, 116, 198));  #Light Purple
DefineGlobalColor("LP4", CreateColor(122, 107, 182));  #Light Purple
DefineGlobalColor("LP5", CreateColor(112, 98, 168));  #Light Purple
DefineGlobalColor("LP6", CreateColor(103, 90, 155));  #Light Purple
DefineGlobalColor("LP7", CreateColor(95, 83, 142));  #Light Purple
DefineGlobalColor("LP8", CreateColor(87, 76, 131));  #Light Purple
DefineGlobalColor("LP9", CreateColor(80, 70, 120));  #Light Purple

DefineGlobalColor("P0", CreateColor(128, 0, 255));  #Purple
DefineGlobalColor("P1", CreateColor(117, 0, 234));  #Purple
DefineGlobalColor("P2", CreateColor(108, 0, 216));  #Purple
DefineGlobalColor("P3", CreateColor(99, 0, 198));  #Purple
DefineGlobalColor("P4", CreateColor(91, 0, 182));  #Purple
DefineGlobalColor("P5", CreateColor(84, 0, 168));  #Purple
DefineGlobalColor("P6", CreateColor(78, 0, 155));  #Purple
DefineGlobalColor("P7", CreateColor(71, 0, 142));  #Purple
DefineGlobalColor("P8", CreateColor(66, 0, 131));  #Purple
DefineGlobalColor("P9", CreateColor(60, 0, 120));  #Purple

DefineGlobalColor("M0", CreateColor(255, 0, 255));  #Magenta
DefineGlobalColor("M1", CreateColor(224, 0, 224));  #Magenta
DefineGlobalColor("M2", CreateColor(197, 0, 197));  #Magenta
DefineGlobalColor("M3", CreateColor(174, 0, 174));  #Magenta
DefineGlobalColor("M4", CreateColor(153, 0, 153));  #Magenta
DefineGlobalColor("M5", CreateColor(135, 0, 135));  #Magenta
DefineGlobalColor("M6", CreateColor(118, 0, 118));  #Magenta
DefineGlobalColor("M7", CreateColor(104, 0, 104));  #Magenta
DefineGlobalColor("M8", CreateColor(92, 0, 92));  #Magenta
DefineGlobalColor("M9", CreateColor(81, 0, 81));  #Magenta

DefineGlobalColor("Gray", CreateColor(28, 28, 28));  #Gray

def na = Double.NaN;
def bn = BarNumber();
def lastbar = if !IsNaN(close) then bn else na;


def HH = fold i = 1 to LBlen + 1
         with p = 0
         while GetValue(high, bn - lastbar) > GetValue(high, (bn - (lastbar - i)))
         do i;

def LL = fold j = 1 to LBlen + 1
         with q = 0
         while GetValue(low, bn - lastbar) < GetValue(low, (bn - (lastbar - j)))
         do -j;

def HLsum = HH + LL;
def HLavg = TEMA(HLsum, 150);
def highestAvg = HighestAll(HLavg);
def lowestAvg = LowestAll(HLavg);
def RangeAvg = highestAvg + AbsValue(lowestAvg);
def scHLAvg = (HLavg / (RangeAvg * 0.5)) * LBlen;


plot Tr_HH = HH;
Tr_HH.AssignValueColor(Color.GREEN);
AddCloud(HH, 0, Color.GREEN);

plot Tr_LL = LL;
Tr_LL.AssignValueColor(Color.RED);
AddCloud(0, LL, Color.RED);

plot Tr_Avg = scHLAvg;
Tr_Avg.hide();
Tr_Avg.SetLineWeight(3);
Tr_Avg.AssignValueColor(
if HLSum > 0
then
    if HLavg >= HLavg[1]
    then
        if (HLavg - HLavg[1]) >= (HLavg[1] - HLavg[2])
        then GlobalColor("G0")
        else GlobalColor("G5")
    else
        if (HLavg - HLavg[1]) <= (HLavg[1] - HLavg[2])
        then GlobalColor("B0")
        else GlobalColor("B5")
else
    if HLavg <= HLavg[1]
    then
        if (HLavg - HLavg[1]) <= (HLavg[1] - HLavg[2])
        then GlobalColor("R0")
        else GlobalColor("R5")
    else
        if (HLavg - HLavg[1]) >= (HLavg[1] - HLavg[2])
        then GlobalColor("Y0")
        else GlobalColor("Y3")
);

plot UpperBO = Breakout;
UpperBO.AssignValueColor(Color.LIGHT_GRAY);

plot UpperCO = Breakout * 0.5;
UpperCO.AssignValueColor(Color.GRAY);

plot LowerBO = -Breakout;
LowerBO.AssignValueColor(Color.LIGHT_GRAY);

plot LowerCO = -Breakout * 0.5;
LowerCO.AssignValueColor(Color.GRAY);


congrats on writing a working fold code. fold can be a confusing function to learn. (took me awhile)


i was looking at your fold formulas and was wondering what the values would be for the offsets.
the , bn - lastbar , in the getvalue,
GetValue(high, bn - lastbar)

at first , it seems like the offset would be negative and point to some future bar.
current barnumber minus the last barnumber.

then i noticed the lastbar formula doesn't include highestall().
it is just the current barnumber.
so lastbar will not be the highest barnumber on the chart.

so bn - lastbar , will always be (current bn - current bn) , which will always be 0.

so the equivelent code line to this,
while GetValue(high, bn - lastbar) > GetValue(high, (bn - (lastbar - i)))

will be,
while GetValue(high, 0) > GetValue(high, - i)

or,
while high > GetValue(high, - i)

-----------------------------

i wrote this to test the offsets,


Code:
# test_neg_offsets_00

#https://usethinkscript.com/threads/new-high-low-breakout-for-thinkorswim.15142/

def na = Double.NaN;
def bn = BarNumber();
def lastbar = if !IsNaN(close) then bn else na;


#def HH = fold i = 1 to LBlen + 1
#         with p = 0
#         while GetValue(high, bn - lastbar) > GetValue(high, (bn - (lastbar - i)))
#         do i;

#def LL = fold j = 1 to LBlen + 1
#         with q = 0
#         while GetValue(low, bn - lastbar) < GetValue(low, (bn - (lastbar - j)))
#         do -j;

plot z = 0;
addchartbubble(1, low,
 bn + "  bn\n" +
 lastbar + "  last\n" +
 (bn - lastbar) + "  b - l"
, color.yellow, no);
#

test bubble, that shows bn is the same as lastbar
nDCSiZO.jpg
 
Yes, I know. That was code that I simply copied from another indicator. I assume the author had a reason for doing it that way, but I never took the time to try to understand exactly why doing it that way might be necessary.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
431 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