Date Range Horizontal Lines

british43

Member
VIP
I'm trying to plot horizontal lines for the high and low for a date range that extends with user input. The code I've messing around with is below.

Ruby:
input startdate = 20210719;
input enddate = 20210820;

def ActivePeriod = if DaysFromDate(startdate) >= 0 and DaysTillDate(enddate) >= 0 then yes else Double.NaN;

def vbegin = GetYYYYMMDD() == startdate;
def vend = GetYYYYMMDD() == enddate;

AddVerticalLine(vbegin, "Range Starts", Color.GREEN, Curve.FIRM);
AddVerticalLine(vend, "Range Ends", Color.GREEN, Curve.FIRM);


plot Test = if vbegin then Highest(high) else Double.NaN;
Test.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Test.SetDefaultColor(Color.YELLOW);

plot Test1 = if vbegin then lowest(low) else Double.NaN;
Test1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Test1.SetDefaultColor(Color.YELLOW);
 
Last edited:
I'm trying to plot horizontal lines for the high and low for a date range that extends with user input. The code I've messing around with is below.

replace the last lines with these

Code:
#plot Test = if vbegin then Highest(high) else Double.NaN;
plot hi2 = if ActivePeriod then Highestall( if ActivePeriod then high else 0) else na;
hi2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hi2.SetDefaultColor(Color.YELLOW);

def big = 999999;
#plot Test1 = if vbegin then lowest(low) else Double.NaN;
plot lo2 = if ActivePeriod then lowestall( if ActivePeriod then low else big) else na;
lo2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lo2.SetDefaultColor(Color.YELLOW);
 

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

@halcyonguy Thanks! What would need to add to extend the lines?

make new variables to get the high and low price levels on the end date.
then keep them and plot them till the last bar.

Code:
# horz_lines_2dates_0

# https://usethinkscript.com/threads/date-range-horizontal-lines.10567/
def na = double.nan;

input startdate = 20210719;
input enddate = 20210820;

addlabel(1,"start " + startdate , color.yellow);
addlabel(1,"end " + enddate , color.yellow);

def ActivePeriod = if DaysFromDate(startdate) >= 0 and DaysTillDate(enddate) >= 0 then yes else Double.NaN;

def vbegin = GetYYYYMMDD() == startdate;
def vend = GetYYYYMMDD() == enddate;

AddVerticalLine(vbegin, "Range Starts", Color.GREEN, Curve.FIRM);
AddVerticalLine(vend, "Range Ends", Color.GREEN, Curve.FIRM);


#plot Test = if vbegin then Highest(high) else Double.NaN;
plot hi2 = if ActivePeriod then Highestall( if ActivePeriod then high else 0) else na;
hi2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hi2.SetDefaultColor(Color.YELLOW);

def big = 999999;
#plot Test1 = if vbegin then lowest(low) else Double.NaN;
plot lo2 = if ActivePeriod then lowestall( if ActivePeriod then low else big) else na;
lo2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lo2.SetDefaultColor(Color.YELLOW);

input extend_lines = yes;

def hi_ext = if !extend_lines or isnan(close) then na
else if vend then hi2
else hi_ext[1];

def lo_ext = if !extend_lines or isnan(close) then na
else if vend then lo2
else lo_ext[1];

plot zhi = hi_ext;
plot zlo = lo_ext;
zhi.SetDefaultColor(Color.gray);
zlo.SetDefaultColor(Color.gray);
#
 
@halcyonguy I'm trying to get the count to work but it is not showing the right amount. What I'm doing wrong?

Ruby:
    # inputs
input startdate = 20210719;
input enddate = 20210820;

def ActivePeriod = if DaysFromDate(startdate) >= 0 and DaysTillDate(enddate) >= 0 then yes else Double.NaN;

def vbegin = GetYYYYMMDD() == startdate;
def vend = GetYYYYMMDD() == enddate;

def count2 = if startDate <= GetYYYYMMDD() then CountTradingDays( startDate, if( GetYYYYMMDD() < startDate, startDate, GetYYYYMMDD() ) ) else Double.NaN;

def v = round((count2/2),0);

addlabel(1, v, color.yellow);

AddVerticalLine(vbegin, "Range Starts", Color.GREEN, Curve.FIRM);
AddVerticalLine(vend, "Range Ends", Color.GREEN, Curve.FIRM);

#plot Test = if vbegin then Highest(high) else Double.NaN;
plot hi2 = if ActivePeriod then Highestall( if ActivePeriod then high else 0) else double.NaN;
hi2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hi2.SetDefaultColor(Color.YELLOW);

def big = 999999;
#plot Test1 = if vbegin then lowest(low) else Double.NaN;
plot lo2 = if ActivePeriod then lowestall( if ActivePeriod then low else big) else double.NaN;
lo2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lo2.SetDefaultColor(Color.YELLOW);

def mean = round((hi2 + lo2) /2,0);

plot m2 = if ActivePeriod then Highestall( if ActivePeriod then mean else 0) else double.NaN;
m2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
m2.SetDefaultColor(Color.YELLOW);

def count = if activeperiod and close crosses above m2
                       then 1
                      else if close > m2
                      then count[1] + 1
                      else count[1];

def count1 = if activeperiod and close crosses below m2
                       then 1
                      else if close < m2
                      then count1[1] + 1
                      else count1[1];
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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