Flexible Channel Indicator

ARL

Member
Plus
Greetings! I'm requesting help with the creation of a channel indicator to be used on a 5 minute chart. The script would plot horizontal lines based on a high/low price from the previous day BUT with the flexibility to input the number of bars back from the previous day's close from which to start. For example, the "look-back" from the previous day's close might be 40 bars or 20 bars. Hopefully the screen shot helps clarify how the indicator would look.

FYI, I had received a solution from SleepyZ for a 30 minute channel for the start of the current day, which works great for the first 2 hours of trading. That script is shown below, but try as I might I cannot get this modified to meet the current request.

Thanks in advance for the assistance. Sure appreciate this community . . . =A=

#30 minute high/low channel
#Modified to work on charts whether extended hours are displayed or not
#Credit to SleepyZ at useThinkScript

input bars = 7;
input ORBegin = 0930;

#Thisday snippet
def ymd = GetYYYYMMDD();
def candles = !IsNaN(close);
def capture = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
#Identifies each day with visible bars starting with #1
def thisDay = (HighestAll(dayCount) - dayCount) + 1 ;

def endbn = if SecondsFromTime(ORBegin) == 0 then BarNumber() + bars - 1 else endbn[1];

def endh = if Between(BarNumber(), endbn - bars , endbn) then Highest(high, bars ) else endh[1];

def endl = if Between(BarNumber(), endbn - bars , endbn ) then Lowest(low, bars ) else endl[1];

#Plot of highs/lows (endh/endl) based upon the first x bars input, projected after those first x bars
plot hafter_endbn = if thisDay == 1 and SecondsFromTime(ORBegin) >= 0 and BarNumber() > endbn then endh else Double.NaN;
plot lafter_endbn = if thisDay == 1 and SecondsFromTime(ORBegin) >= 0 and BarNumber() > endbn then endl else Double.NaN;
hafter_endbn.SetDefaultColor(Color.CYAN);
lafter_endbn.SetDefaultColor(Color.CYAN);

#Offset ([-bars]) of endh/endl to backfill those plots over the first x bars
plot h_after_offset = if thisDay == 1 and SecondsFromTime(ORBegin) >= 0 and BarNumber() <= endbn then endh[-bars] else Double.NaN;
plot l_after_offset = if thisDay == 1 and SecondsFromTime(ORBegin) >= 0 and BarNumber() <= endbn then endl[-bars] else Double.NaN;
h_after_offset.SetPaintingStrategy(PaintingStrategy.DASHES);
h_after_offset.SetDefaultColor(Color.WHITE);
l_after_offset.SetPaintingStrategy(PaintingStrategy.DASHES);
l_after_offset.SetDefaultColor(Color.WHITE);

input test = yes;
AddChartBubble(test and BarNumber() <= endbn, endl[-bars], thisDay + " \n" + endh + " \n" + endl, if low == endl[-bars] then Color.WHITE else if high == endh[-bars] then Color.YELLOW else if BarNumber() == endbn[-bars] then Color.MAGENTA else Color.GRAY, no);

input cloud = yes;
AddCloud(h_after_offset, l_after_offset, Color.WHITE);
 
Greetings! I'm requesting help with the creation of a channel indicator to be used on a 5 minute chart. The script would plot horizontal lines based on a high/low price from the previous day BUT with the flexibility to input the number of bars back from the previous day's close from which to start. For example, the "look-back" from the previous day's close might be 40 bars or 20 bars. Hopefully the screen shot helps clarify how the indicator would look.

FYI, I had received a solution from SleepyZ for a 30 minute channel for the start of the current day, which works great for the first 2 hours of trading. That script is shown below, but try as I might I cannot get this modified to meet the current request.

Thanks in advance for the assistance. Sure appreciate this community . . . =A=

#30 minute high/low channel
#Modified to work on charts whether extended hours are displayed or not
#Credit to SleepyZ at useThinkScript

input bars = 7;
input ORBegin = 0930;

#Thisday snippet
def ymd = GetYYYYMMDD();
def candles = !IsNaN(close);
def capture = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
#Identifies each day with visible bars starting with #1
def thisDay = (HighestAll(dayCount) - dayCount) + 1 ;

def endbn = if SecondsFromTime(ORBegin) == 0 then BarNumber() + bars - 1 else endbn[1];

def endh = if Between(BarNumber(), endbn - bars , endbn) then Highest(high, bars ) else endh[1];

def endl = if Between(BarNumber(), endbn - bars , endbn ) then Lowest(low, bars ) else endl[1];

#Plot of highs/lows (endh/endl) based upon the first x bars input, projected after those first x bars
plot hafter_endbn = if thisDay == 1 and SecondsFromTime(ORBegin) >= 0 and BarNumber() > endbn then endh else Double.NaN;
plot lafter_endbn = if thisDay == 1 and SecondsFromTime(ORBegin) >= 0 and BarNumber() > endbn then endl else Double.NaN;
hafter_endbn.SetDefaultColor(Color.CYAN);
lafter_endbn.SetDefaultColor(Color.CYAN);

#Offset ([-bars]) of endh/endl to backfill those plots over the first x bars
plot h_after_offset = if thisDay == 1 and SecondsFromTime(ORBegin) >= 0 and BarNumber() <= endbn then endh[-bars] else Double.NaN;
plot l_after_offset = if thisDay == 1 and SecondsFromTime(ORBegin) >= 0 and BarNumber() <= endbn then endl[-bars] else Double.NaN;
h_after_offset.SetPaintingStrategy(PaintingStrategy.DASHES);
h_after_offset.SetDefaultColor(Color.WHITE);
l_after_offset.SetPaintingStrategy(PaintingStrategy.DASHES);
l_after_offset.SetDefaultColor(Color.WHITE);

input test = yes;
AddChartBubble(test and BarNumber() <= endbn, endl[-bars], thisDay + " \n" + endh + " \n" + endl, if low == endl[-bars] then Color.WHITE else if high == endh[-bars] then Color.YELLOW else if BarNumber() == endbn[-bars] then Color.MAGENTA else Color.GRAY, no);

input cloud = yes;
AddCloud(h_after_offset, l_after_offset, Color.WHITE);

This will allow you to choose the days and bars lookbacks, along with verticals and bubbles.

Screenshot 2023-11-10 051536.png
Code:
#Flexible Channel Indicator

input lookback_x_bars = 40;

input lookback_x_days = 1;
input verticals = yes;
input bubbles = yes;

#Days
def ymd = GetYYYYMMDD();
def ok  = !IsNaN(close);
def capture  = ok and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture
                                    then dayCount[1] + 1
                                    else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;

#End of Range
def bn  = BarNumber();
AddVerticalLine(verticals and thisDay == lookback_x_days and SecondsFromTime(1600) == 0, "       End ", color.white);
def lastbar  = highestall(if thisDay == lookback_x_days and SecondsFromTime(1600) == 0 then bn else Double.NaN);

#Start of Range
def startbar = (lastbar - lookback_x_bars);
AddVerticalLine(verticals and bn == HighestAll(lastbar - lookback_x_bars), "      Start ", color.white);

#High of Range
def h   = (if bn == startbar then high else if SecondsFromTime(1600) <= 0 then Max(high, h[1]) else double.nan);
plot hh = if bn < startbar then Double.NaN else highestall(h);

#Low of Range
def l   = if bn == startbar then low else if SecondsFromTime(1600) <= 0 then Min(low, l[1]) else double.nan;
plot ll = if bn < startbar then Double.NaN else lowestall(l);

#Bubbles
addchartBubble(bubbles and bn==highestall(bn), hh, "H " + lookback_x_days, hh.takeValueColor());
addchartBubble(bubbles and bn==highestall(bn), ll, "L " + lookback_x_days, ll.takeValueColor());

#
 

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

This is great, thank you! What's the modification of the code, please, in order to get it to display when extended hours are not used on a chart?

This modification should work on both charts with and without extended hourss enabled

Screenshot 2023-11-11 090853.png
Code:
#Flexible Channel Indicator

input lookback_x_bars = 40;

input lookback_x_days = 1;
input verticals = yes;
input bubbles = yes;

#Days
def ymd = GetYYYYMMDD();
def ok  = !IsNaN(close);
def capture  = ok and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture
                                    then dayCount[1] + 1
                                    else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;

def isExEnabled =
    if !isExEnabled[1]
    then !Between(
        GetTime(),
        RegularTradingStart(GetYYYYMMDD()),
        RegularTradingEnd(GetYYYYMMDD()))
    else isExEnabled[1];
#AddLabel(1, isExEnabled); #@Joshua snippet

#End of Range
def bn  = BarNumber();
AddVerticalLine(verticals and thisDay[1] == lookback_x_days and
(if isExEnabled then SecondsFromTime(1600) == 0 else thisDay < lookback_x_days), "       End ", Color.WHITE);
def lastbar  = HighestAll(
if thisDay[1] == lookback_x_days and
   (if isExEnabled then SecondsFromTime(1600) == 0
    else thisDay < lookback_x_days)
then bn
else Double.NaN);

#Start of Range
def startbar = (lastbar - lookback_x_bars);
AddVerticalLine(verticals and bn == HighestAll(lastbar - lookback_x_bars), "      Start ", Color.WHITE);

#High of Range
def h  =  if bn == startbar
          then high
          else if
                 (if isExEnabled
                  then SecondsFromTime(1600) <= 0
                  else thisDay == lookback_x_days)
          then Max(high, h[1]) else Double.NaN;
plot hh = if bn < startbar then Double.NaN else HighestAll(h);

#Low of Range
def l   = if bn == startbar
          then low
          else if
                 (if isExEnabled
                  then SecondsFromTime(1600) <= 0
                  else thisDay == lookback_x_days)
          then Min(low, l[1]) else Double.NaN;
plot ll = if bn < startbar then Double.NaN else LowestAll(l);

#Bubbles
AddChartBubble(bubbles and bn == HighestAll(bn), hh, "H " + lookback_x_days, hh.TakeValueColor());
AddChartBubble(bubbles and bn == HighestAll(bn), ll, "L " + lookback_x_days, ll.TakeValueColor());

#
 
  • Thumbs Up
Reactions: ARL

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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