Developing a Range Study... Cannot store previous high/low values permanently, how to compare?

RangeBound1

New member
Hello,

#====Main Issue====#
I want to identify a range high/low then compare every candle after that to those values.
If there is a close outside of the high/low, the drawing ends.
If there is a new high/low, but the candle closes within the prev high/low of the range, expand the drawing to include the new high/low.
I don't have a way to store the high/low value and compare them. The best I could find was https://stackoverflow.com/questions/56518594/how-to-create-a-variable-that-retains-its-value , but nothing seems to make sense.
I do not want to use a Highest/Lowest (high/low, input), as I want to compare to the previous range instead of manually entering a lookback value.

Thank you in advance for any assistance you can provide on this.


#====Summary of Project====#

Here is what I am trying to accomplish:
s9LjvMw.png


I hope the image works. If not, here is the web link .

The concept is to determine when candles are in a range, mark the high and low of the range as a horizontal line.
As the chart progresses, expand the drawing.
If a new candle breaks the high or low, but closes within the range, redraw the range to include the new high or low.
Paint candles that close within the range as grey (discretionary), paint candles that close outside of the range as blue (discretionary).

Example, a range is created with a high of 10.00 and a low of 9.75.
The next candle made a new high of 10.05 but closed at 10.00, within the range.
The range should be redrawn to include the high of 10.05.
The new redrawn range will then be 10.05 to 9.75.

If a candle closes outside of the marked range, it is considered a trending candle. The drawing then ceases until the next range is formed.

The definitions are as follows:
A range begins when 1 bar closes within the high and low of the previous.
The range high is initiated as the highest value of the current bar and previous.
The range low is initiated as the lowest value of the current bar and previous.
The range ends when a bar closes outside of the range high or low.

If a bar makes a new high above the range, but closes within, the range expands to include the new high and the current candle is considered part of the range.
(and vice versa for the low)


#====Current Code====#

Here is the code I have so far, along with an image of its current display.

khD3D9i.png


Rich (BB code):
#========


plot insidebar = close < high[1] and close > low[1];
plot trendbar = !insidebar;


AssignPriceColor(if insidebar then Color.RED else Color.BLUE);


plot prevbarinside = close[1] < high[2] and close[1] > low[2];
plot testlow;
plot testhigh;


if (insidebar is true and prevbarinside is false)
{
    testlow = lowest(low,2);
    testhigh = highest(high,2);
}
else
{
    testlow = double.NaN;
    testhigh = double.NaN;
}


testlow.setPaintingStrategy(paintingStrategy.HORIZONTAL);
testlow.setDefaultColor(color.WHITE);


testhigh.setPaintingStrategy(paintingStrategy.HORIZONTAL);
testhigh.setDefaultColor(color.WHITE);
 
Last edited:
I have gotten here, but seemingly everything is set to N/A.
If someone could figure out what I'm doing wrong, I'd appreciate it.


Rich (BB code):
#========

def insidebar = close < high[1] and close > low[1];
def trendbar = !insidebar;

def prevbarinside = close[1] < high[2] and close[1] > low[2];

def rangebegincount;
def rangehighcount;
def rangelowcount;
def rangeLow;
def rangeHigh;
def curbarRANGE;

##logic stepthrough
# was the prev bar a trend bar and is the current bar inside?
# yes
    # does the rangecounter have a value in it?
    # yes
        # is the current bar's close higher than the range high or lower than the range low?
            # yes
                # reset rangehigh, rangelow, end counts
                # flag bar as Trend

            # no
                # flag bar as Range
                # increment rangebegincount by 1
                # is the current bar's high greater than the rangehigh?
                    # yes
                       # set new Rangehigh to current bar high
                       # set rangehigh counter to 1

                    # no
                       # keep rangehigh set as prev value
                       # increment rangehigh counter by 1
                # is the current bar's low lesser than the rangelow?
                    # yes
                    # set new Rangelow to current bar low
                    # set rangelow counter to 1

                    # no
                    # keep rangelow set as prev value
                    # increment rangehigh counter by 1
    # no
        # set rangehigh, set rangelow
        # begin counts
        # flag bar as Range



# no
   
    # but the rangecounter has a value in it

    # is the current bar's close higher than the range high or lower than the range low?
        # yes
        # reset rangehigh, rangelow, end counts
        # flag bar as Trend

        # no
        # flag bar as Range
        # increment rangebegincount by 1
        # is the current bar's high greater than the rangehigh?
            # yes
            # set new Rangehigh to current bar high
            # set rangehigh counter to 1

            # no
            # keep rangehigh set as prev value
            # increment rangehigh counter by 1
        # is the current bar's low greater than the rangelow?
            # yes
            # set new Rangelow to current bar low
            # set rangelow counter to 1

            # no
            # keep rangelow set as prev value
            # increment rangehigh counter by 1

    # and the rangecounter has no value in it
    #flag bar as trend
    #reset rangehigh, rangelow, end counts
   

#########
# was the prev bar a trend bar and is the current bar inside?
if (insidebar == 1 and prevbarinside == 0 )
{  
    #yes
    # does the rangecounter have a value in it?
    if (IsNaN(rangebegincount))
    {
        # no
        # set rangehigh, set rangelow
        if (high[1] > high)
        {
            rangeHigh = high[1];
            rangehighcount = 2; #begin counts
        }
        else
        {
            rangeHigh = high;
            rangehighcount = 1; #begin counts
        }

        if (low[1] < low)
        {
            rangeLow = low[1];
            rangelowcount = 2; #begin counts
        }
        else
        {
            rangeLow = low;
            rangelowcount = 1; #begin counts
        }
        # begin counts
        rangebegincount = 2;
        # flag bar as Range
        curbarRANGE = 1;
    }
    else
    {
     

        # yes
        # is the current bar's close higher than the range high or lower than the range low?
        if ( close > rangeHigh or close < rangeLow )
        {
            # yes, trend
            rangeHigh = Double.NaN;
            rangeLow = Double.NaN;
            rangebegincount = Double.NaN;
            rangelowcount = Double.NaN;
            rangehighcount = Double.NaN;
            # reset rangehigh, rangelow, end counts

            curbarRANGE = 0;
            # flag bar as Trend
        }
        else
        {
            # no
            curbarRANGE = 1;
            # flag bar as Range
            rangebegincount = rangebegincount[1] + 1;

            # is the current bar's high greater than the rangehigh?
            if ( high > rangeHigh )
            {
                # yes
                # set new Rangehigh to current bar high
                rangeHigh = high;  
                # set rangehigh counter to 1
                rangehighcount = 1;
            }
            else
            {
                # no
                # keep rangehigh set as prev value
                rangeHigh = rangeHigh[1];
                # increment rangehigh counter by 1
                rangehighcount = rangehighcount[1] + 1;
            }

            # is the current bar's low lesser than the rangelow?
            if ( low < rangeLow )
            {
                # yes
                # set new rangeLow to current bar low
                rangeLow = low;  
                # set rangelow counter to 1
                rangelowcount = 1;
            }
            else
            {
                # no
                # keep rangelow set as prev value
                rangeLow = rangeLow[1];
                # increment rangelow counter by 1
                rangelowcount = rangelowcount[1] + 1;
            }
        }
    }
}
# was the prev bar a trend bar and is the current bar inside?
# no
else
{
    #and the rangecounter has no value in it
    if ( IsNaN(rangebegincount))
    {
        #flag bar as trend
        curbarRANGE = 0;
       
        #reset rangehigh, rangelow, end counts
        rangeHigh = Double.NaN;
        rangeLow = Double.NaN;
        rangebegincount = Double.NaN;
        rangehighcount = Double.NaN;
        rangelowcount = Double.NaN;
    }
    #but there is a value in the rangecounter
    else
    {
        # is the current bar's close higher than the range high or lower than the range low?
        if ( close > rangeHigh or close < rangeLow )
        {
            # yes
            rangeHigh = Double.NaN;
            rangeLow = Double.NaN;
            rangebegincount = Double.NaN;
            rangelowcount = Double.NaN;
            rangehighcount = Double.NaN;
            # reset rangehigh, rangelow, end counts
            curbarRANGE = 0;
            # flag bar as Trend
        }
        else
        {
            # no
            curbarRANGE = 1;
            # flag bar as Range
            # increment rangebegincount by 1
            rangebegincount = rangebegincount[1] + 1;

            # is the current bar's high greater than the rangehigh?
            if ( high > rangeHigh )
            {
                # yes
                # set new Rangehigh to current bar high
                rangeHigh = high;  
                # set rangehigh counter to 1
                rangehighcount = 1;
            }
            else
            {
                # no
                # keep rangehigh set as prev value
                rangeHigh = rangeHigh[1];
                # increment rangehigh counter by 1
                rangehighcount = rangehighcount[1] + 1;
            }

            # is the current bar's low lesser than the rangelow?
            if ( low < rangeLow )
            {
                # yes
                # set new rangeLow to current bar low
                rangeLow = low;  
                # set rangelow counter to 1
                rangelowcount = 1;
            }
            else
            {
                # no
                # keep rangelow set as prev value
                rangeLow = rangeLow[1];
                # increment rangelow counter by 1
                rangelowcount = rangelowcount[1] + 1;
            }
        }
    }

}



plot High_of_Range = rangeHigh;
plot Low_of_Range = rangeLow;
plot Trend_Candle = close;
plot Range_Candle = close;

Range_Candle.DefineColor("Range", GetColor(7));
Trend_Candle.DefineColor("Trend", GetColor(9));
High_of_Range.DefineColor("Resistance", GetColor(5));
Low_of_Range.DefineColor("Support", GetColor(6));

AssignPriceColor(if curbarRANGE == 1 then Range_Candle.Color("Range") else Trend_Candle.Color("Trend"));

High_of_Range.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Low_of_Range.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
High_of_Range.SetDefaultColor(High_of_Range.Color("Resistance"));
Low_of_Range.SetDefaultColor(Low_of_Range.Color("Support"));
 
Last edited:
Realized I never had rangeHigh call to get the value of the high using the counter.
Realized counting range was redundant, did nothing.
made edits, study continues to plot nothing.

Rich (BB code):
#========


def insidebar = close < high[1] and close > low[1];
def trendbar = !insidebar;


def prevbarinside = close[1] < high[2] and close[1] > low[2];


#debug


def isarange;
def rangehighcount;
def rangelowcount;
def rangeLow;
def rangeHigh;
def curbarRANGE;




#########
# was the prev bar a trend bar and is the current bar inside?
if (insidebar == 1 and prevbarinside == 0 )
{   
    #yes
    # does the rangecounter have a value in it or is it set to 0?
    if (IsNaN(isarange) or isarange == 0)
    {
        # no value or set to 0
        # set rangehigh, set rangelow
        if (high[1] > high)
        {
            rangeHigh = high[1];
            rangehighcount = 2; #begin counts
        }
        else
        {
            rangeHigh = high;
            rangehighcount = 1; #begin counts
        }


        if (low[1] < low)
        {
            rangeLow = low[1];
            rangelowcount = 2; #begin counts
        }
        else
        {
            rangeLow = low;
            rangelowcount = 1; #begin counts
        }
        # we're in a range, set to 1
        isarange = 1;
        # flag bar as Range
        curbarRANGE = 1;
    }
    else
    {
        #has value which is not 0
        #fetch the rangeHigh and rangeLow
        rangeHigh = GetValue(high, rangehighcount);
        rangeLow = GetValue(low, rangelowcount);


        # yes
        # is the current bar's close higher than the range high or lower than the range low?
        if ( close > rangeHigh or close < rangeLow )
        {
            # yes, trend
            isarange = 0;
            rangelowcount = 0;
            rangehighcount = 0;
            # reset rangehigh, rangelow, end counts


            curbarRANGE = 0;
            # flag bar as Trend
        }
        else
        {
            # no
            curbarRANGE = 1;
            # flag bar as Range
            isarange = 1;


            # is the current bar's high greater than the rangehigh?
            if ( high > rangeHigh )
            {
                # yes 
                # set rangehigh counter to 1
                rangehighcount = 1;
            }
            else
            {
                # no
                # increment rangehigh counter by 1
                rangehighcount = rangehighcount[1] + 1;
            }


            # is the current bar's low lesser than the rangelow?
            if ( low < rangeLow )
            {
                # yes   
                # set rangelow counter to 1
                rangelowcount = 1;
            }
            else
            {
                # no
                # increment rangelow counter by 1
                rangelowcount = rangelowcount[1] + 1;
            }
        }
    }
}
# was the prev bar a trend bar and is the current bar inside?
# no
else
{
    #and the rangecounter has no value in it or is it set to 0
    if ( IsNaN(isarange) or isarange == 0)
    {
        #flag bar as trend
        isarange = 0;
        curbarRANGE = 0;
        
        #reset rangehigh, rangelow, end counts
        rangeHigh = 0;
        rangeLow = 0;
        rangehighcount = 0;
        rangelowcount = 0;
    }
    #but there is a value in the rangecounter
    else
    {
        #has value which is not 0
        #fetch the rangeHigh and rangeLow




        rangeHigh = GetValue(high, rangehighcount);
        rangeLow = GetValue(low, rangelowcount);
        
        # yes
        # is the current bar's close higher than the range high or lower than the range low?
        if ( close > rangeHigh or close < rangeLow )
        {
            # yes, trend
            isarange = 0;
            rangelowcount = 0;
            rangehighcount = 0;
            # reset rangehigh, rangelow, end counts


            curbarRANGE = 0;
            # flag bar as Trend
        }
        else
        {
            # no
            curbarRANGE = 1;
            # flag bar as Range
            isarange = 1;


            # is the current bar's high greater than the rangehigh?
            if ( high > rangeHigh )
            {
                # yes 
                # set rangehigh counter to 1
                rangehighcount = 1;
            }
            else
            {
                # no
                # increment rangehigh counter by 1
                rangehighcount = rangehighcount[1] + 1;
            }


            # is the current bar's low lesser than the rangelow?
            if ( low < rangeLow )
            {
                # yes   
                # set rangelow counter to 1
                rangelowcount = 1;
            }
            else
            {
                # no
                # increment rangelow counter by 1
                rangelowcount = rangelowcount[1] + 1;
            }
        }
    }
}






plot High_of_Range = rangeHigh;
plot Low_of_Range = rangeLow;


AssignPriceColor(if curbarRANGE == 1 then Color.GRAY else Color.BLUE);


High_of_Range.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Low_of_Range.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
High_of_Range.SetDefaultColor(color.WHITE);
Low_of_Range.SetDefaultColor(color.WHITE);
 
Sorry use of such language is not necessary. I would think the moderator would remove it. Any chance of me helping was lost.
Fortunately, there is an edit button I can use to remove late night ill advised frustrations from posts, and it has been resolved.

I wish you would have asked me to take the time to remove the word rather than refusing to help.
But thanks anyway.
 
Thank you RangeBound1 for the edit. The use of such language is unnecessary and it is sad foul language has become acceptable in today's society. There used to be bounds of acceptable language and behavior in public and in the media. Those bounds have been eroded and society has become crude, less polite, and less able to express ideas with civility. I fear the education system no longer gives people language skills.

Follow the Ten Commandments, follow the Royal Law, and think before you speak.
 
to those that come after me looking for ways to dynamically look back for the highest value, while conditions are true/false, just use pinescript - spent 4 days trying to get the right answer in thinkscript, solved it in pinescript in 3 hours.
may the trading gods watch you in favor
 

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
317 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