How to plot horizontal lines at price levels where the price ends with a certain number

ikymerai

New member
Hi All,

New to the forum and trying to learn thinkscript...I'd like to create an indicator that plots horizontal lines at price levels where the price ends with a certain number (user input number), i.e. userPriceEndsWith = "1.50" so we want to plot horizontal lines at every price level on the chart where the price ends with "1.50"

What I'd like to do is plot horizontal lines at 1.50, 11.50, 21.50, 31.50 etc

How can one do this in thinkscript? Are there loops in the language? Any example(s) would be greatly appreciated

Thanks in advance for all your help!

Adam
 
Solution
I want to draw 2 horizontal lines across my charts (for daily and weekly views) where the high horizontal line is defined as the high of the first trading day of any week (obviously this is usually Monday unless a holiday week) and the low horizontal line is the low of the first trading day of the week. So these horizontal lines will change each week as new First Trading Day of the Week Highs and lows change. I would like the lines to persist on both daily and weekly charts.

This seems to work as you want for daily, however Monday is not available on weekly.

Screenshot 2024-08-05 095144.jpg
Code:
input show_last_only = no;
def start_day_week   = if GetWeek() != GetWeek()[1]
                       then if GetDayOfWeek(GetYYYYMMDD()) == 1...
This seems to work as you want for daily, however Monday is not available on weekly.
thats exactly what I was hoping for.....is there any way to prevent those line from drawing up or down after the end of the weeks to make it look cleaner or is the solution to use a different line pattern?
 
thats exactly what I was hoping for.....is there any way to prevent those line from drawing up or down after the end of the weeks to make it look cleaner or is the solution to use a different line pattern?

Yes, this addition plots the horizontal lines as opposed to the default lines in the above.
hh.setpaintingStrategy(paintingStrategy.HORIZONTAL);
ll.setpaintingStrategy(paintingStrategy.HORIZONTAL);[

Full code
Screenshot 2024-08-05 140204.png
Code:
input show_last_only = no;
def start_day_week   = if GetWeek() != GetWeek()[1]
                       then if GetDayOfWeek(GetYYYYMMDD()) == 1
                            then 1
                            else 2
                       else start_day_week[1] ;
def h   = if GetDayOfWeek(GetYYYYMMDD()) == start_day_week then high(period = AggregationPeriod.DAY) else h[1];
def l   = if GetDayOfWeek(GetYYYYMMDD()) == start_day_week then low(period = AggregationPeriod.DAY) else l[1];
plot hh = if show_last_only and GetWeek() != GetLastWeek() then Double.NaN else h;
plot ll = if show_last_only and GetWeek() != GetLastWeek() then Double.NaN else l;
hh.setpaintingStrategy(paintingStrategy.HORIZONTAL);
ll.setpaintingStrategy(paintingStrategy.HORIZONTAL);
 
here is a working study
it can plot up to 5 lines above and below the reference level, at some factor

Code:
# horz_lines_increm_near_close_0

# plot some quantity of horizontal lines,
# above and below a reference price near the close.
# the ref price is a multiple of the spacing_factor, that is closest to the close.

#https://usethinkscript.com/threads/how-to-plot-horizontal-lines-at-price-levels-where-the-price-ends-with-a-certain-number.7692/#post-74052

# Line At Price
# Mobius
# Alternative to using the HighestAll() function
def barsBack = 1000;
def c = if !IsNaN(close) and IsNaN(close[-1])
        then close
        else c[1];
#plot line = if isNaN(close[-barsBack])
def line = if isNaN(close[-barsBack])
            then c[-barsBack]
            else Double.NaN;
#line.SetLineWeight(1);
#line.SetDefaultColor(Color.LIME);
#line.SetStyle(Curve.MEDIUM_DASH);

def na = double.nan;
input quantity_of_lines = 4;
def q = quantity_of_lines;

input spacing_factor = 0.5;
def basefactor = round( line/spacing_factor, 0);
def baseline = basefactor * spacing_factor;

plot b = baseline;
b.SetLineWeight(1);
b.SetDefaultColor(Color.LIME);
b.SetStyle(Curve.MEDIUM_DASH);

addlabel(1, " ", color.black);
addlabel(1, "baseline " + baseline, color.yellow);

plot line5up = if 5 > q then na else baseline + (spacing_factor * 5);
plot line4up = if 4 > q then na else baseline + (spacing_factor * 4);
plot line3up = if 3 > q then na else baseline + (spacing_factor * 3);
plot line2up = if 2 > q then na else baseline + (spacing_factor * 2);
plot line1up = if 1 > q then na else baseline + (spacing_factor * 1);

plot line1dwn = if 1 > q then na else baseline - (spacing_factor * 1);
plot line2dwn = if 2 > q then na else baseline - (spacing_factor * 2);
plot line3dwn = if 3 > q then na else baseline - (spacing_factor * 3);
plot line4dwn = if 4 > q then na else baseline - (spacing_factor * 4);
plot line5dwn = if 5 > q then na else baseline - (spacing_factor * 5);
#


AAL 5min
line spacing = 0.1
quatity of line = 4
View attachment 15738
hal_horz
Hello. Could you please assist me with tweaking this to plot the horizontal lines from a manually entered input price and add a 6th horizontal line? For example, I would like to enter my own price (534.25) then use the line spacing as it is to plot the lines.
 

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