draw a diagonal line on a chart, between 2 points, and after point2

halcyonguy

Moderator - Expert
VIP
Lifetime
draw a diagonal line on a chart, between 2 points, and after point2.

when close crosses the line,
...draw arrows
...trigger an alert

default symbol is zzz
if a specific symbol is entered, then the line appears only for the chart for that symbol.
if zzz is used for the symbol, the line will appear for all symbols. (may have to zoom out to see it)

both points are defined by a date, time, and a price.
can enter year, month, day, hour, minute

default prices are set to 0.
if a price is set to 0, then a bar price parameter can be used. default is close.


can draw vertical and horizontal lines through the 2 points.


Code:
# line_2points_00

# https://usethinkscript.com/threads/close-crosses-user-drawn-level-or-trendline.15475/

def na = Double.NaN;
def bn = barnumber();

def lastbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbn) then 1 else 0;
#def lastbar = !isnan(close[0]) and isnan(close[-1]);
def barz = lastbn - bn + 1;

#---------------------------
# from tos chat - mobius
def datax = getYYYYMMDD();
def yearx = Round(datax/10000, 0);
def monthx = Round((datax % 10000) / 100, 0);
def dayx = (datax % 100);
# addLabel(1, "date: " + monthx + "/" + dayx + "/" + AsPrice(yearx), color.white);

#---------------------------
# hour / minutes of day
def start = 0;
def tsecx = SecondsFromTime(start);
def tminx = tsecx/60;
def hrx = floor(tminx/60);
def minx = tminx % 60;
#addlabel(yes, "hrs " + hrx + " min " +  minx , color.magenta);

#---------------------------
input symbol1 = "zzz";
#hint symbol1: Enter a specific symbol to draw a line for.\n Enter zzz to use the date-time/prices on any chart symbol.

addlabel(1, symbol1, color.white);

#---------------------------
# point 1 date-time / price

input year1 = 2023;
input month1 = 5;
input day1 = 4;
input hour1_EST = 10;
input min1 = 30;
input price1 = 0.0;
#hint price1: If set to 0, then the price1_param value will be used
input price1_param = close;

#---------------------------
# point 2 date-time / price

input year2 = 2023;
input month2 = 5;
input day2 = 10;
input hour2_EST = 14;
input min2 = 45;
input price2 = 0.0;
#hint price2: If set to 0, then the price2_param value will be used
input price2_param = close;

#---------------------------
# compare yr/mo/day  hr/min

def s1 = if (symbol1 == "zzz" or symbol1 == "ZZZ" ) or (GetSymbol() == symbol1) then 1 else 0;

def d1 = (yearx == year1 and monthx == month1 and dayx == day1);
def b1 = (hrx == hour1_EST and minx == min1);

def d2 = (yearx == year2 and monthx == month2 and dayx == day2);
def b2 = (hrx == hour2_EST and minx == min2);

def pt1 = s1 and d1 and b1;
def pt2 = s1 and d2 and b2;

#---------------------------
# set price levels
def pr1 = if price1 == 0 then highestall(if pt1 then price1_param else 0) else price1;
def pr2 = if price2 == 0 then highestall(if pt2 then price2_param else 0) else price2;

#---------------------------

input show_datetime_labels = yes;
input show_price_labels = yes;
addlabel(show_datetime_labels or show_price_labels , " ", color.black);
addlabel(show_datetime_labels, "Pt1", color.white);
addlabel(show_datetime_labels, (month1 + "/" + day1 + "/" + asprice(year1) + "  " + hour1_EST + ":" + min1), color.yellow);
addlabel(show_price_labels, "$ " + pr1, color.green);
addlabel(show_datetime_labels or show_price_labels , " ", color.black);
addlabel(show_datetime_labels, "Pt2", color.white);
addlabel(show_datetime_labels, (month2 + "/" + day2 + "/" + asprice(year2) + "  " + hour2_EST + ":" + min2), color.yellow);
addlabel(show_price_labels, "$ " + pr2, color.green);

#---------------------------

# draw dots on price points

plot zpt1 = if pt1 then pr1 else na;
zpt1.SetPaintingStrategy(PaintingStrategy.POINTS);
zpt1.SetDefaultColor(Color.yellow);
zpt1.setlineweight(4);
zpt1.hidebubble();

plot zpt2 = if pt2 then pr2 else na;
zpt2.SetPaintingStrategy(PaintingStrategy.POINTS);
zpt2.SetDefaultColor(Color.yellow);
zpt2.setlineweight(4);
zpt2.hidebubble();

#---------------------------
# count bars, from pt1 to pt2
def u = 1000;
def off;
def pt1bn;
def pt2bn;
if bn == 1 then {
 off = 0;
 pt1bn = 0;
 pt2bn = 0;
} else if pt1 then {
 off = fold i = 1 to u
  with p
  while getvalue(!pt2, -i)
  do p + 1;
 pt1bn = bn;
 pt2bn = bn+off;
} else {
 off = off[1];
 pt1bn = pt1bn[1];
 pt2bn = pt2bn[1];
}


def prdiff = pr2 - pr1;
def slope = prdiff/off;

def diag1;
if bn == 1 or isnan(close) then {
 diag1 = na;
} else if pt1 then {
 diag1 = pr1;
} else {
 diag1 = diag1[1] + slope;
}

#---------------------------

plot z1 = diag1;
#z1.SetDefaultColor(Color.cyan);
z1.AssignValueColor(if (bn >= pt1bn and bn <= pt2bn ) then color.cyan else color.yellow);
#z1.setlineweight(1);
z1.hidebubble();

#---------------------------
# price crosses diag line

def xup1 = if close crosses above diag1 then 1 else 0;
plot zxup1 = if xup1 then low*0.999 else na;
zxup1.SetPaintingStrategy(PaintingStrategy.arrow_up);
zxup1.SetDefaultColor(Color.cyan);
zxup1.setlineweight(3);
zxup1.hidebubble();

def xdwn1 = if close crosses below diag1 then 1 else 0;
plot zdwn1 = if xdwn1 then high*1.001 else na;
zdwn1.SetPaintingStrategy(PaintingStrategy.arrow_down);
zdwn1.SetDefaultColor(Color.cyan);
zdwn1.setlineweight(3);
zdwn1.hidebubble();

#---------------------------

# alert(condition, text, alert type, sound);
# Sound.Ding,  higher pitch, up sound
# Sound.Bell,  lower pitch, down sound
alert(xup1, "crossed up" ,alert.BAR, sound.DING);
alert(xdwn1, "crossed down" ,alert.BAR, sound.bell);

#---------------------------

input test_vert_lines_on_points = no;
addverticalline(test_vert_lines_on_points and pt1, "-", color.green);
addverticalline(test_vert_lines_on_points and pt2, "-", color.red);


input test_horizontal_lines_on_points = no;
plot zpr1 = if test_horizontal_lines_on_points then pr1 else na;
zpr1.SetStyle(Curve.MEDIUM_DASH);
zpr1.SetDefaultColor(Color.green);
zpr1.setlineweight(1);
zpr1.hidebubble();

plot zpr2 = if test_horizontal_lines_on_points then pr2 else na;
zpr2.SetStyle(Curve.MEDIUM_DASH);
zpr2.SetDefaultColor(Color.red);
zpr2.setlineweight(1);
zpr2.hidebubble();


addchartbubble(0, low*0.999,
#bn + "\n" +
off + "\n"
#(bn + off)
, color.yellow, no);

#

GLD
2 points, a few days apart
4phNMOZ.jpg


use zzz as the symbol, to use the date price levels on any symbol chart
JoedqUI.jpg

hal_diag
 
Last edited:

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