Getting price at the intersection of two drawn lines.

ADM1992

New member
uYbuOWV.png


As shown in the image above, I'm looking to pull the exact price where these two lines intersect. Is this possible to do?

As of right now, I'm stuck using the next day's midpoint value and I need the exact intersection for more accurate analysis.
 
Solution
ADM1992,

AS far as ToS is concerned, the lines don't actually have any value between the points. That is, it draws values for us to see, but the values only exist (for doing things with in ToS) at one point per candle.

Now, you could do an approximation of the value by taking the average value of either line for the bar immediately before and immediately after the cross. It might prove close enough to do what analysis you need. This only really works if the values between points are able to be interpolated in a linear fashion. Since ToS really does linear interpolation between points, this should work.

Your [pseudo-]code could look something like this:
Code:
plot MA1 = moving average 1;
plot MA2 = moving average 2;

def CX = if MA1...
ADM1992,

AS far as ToS is concerned, the lines don't actually have any value between the points. That is, it draws values for us to see, but the values only exist (for doing things with in ToS) at one point per candle.

Now, you could do an approximation of the value by taking the average value of either line for the bar immediately before and immediately after the cross. It might prove close enough to do what analysis you need. This only really works if the values between points are able to be interpolated in a linear fashion. Since ToS really does linear interpolation between points, this should work.

Your [pseudo-]code could look something like this:
Code:
plot MA1 = moving average 1;
plot MA2 = moving average 2;

def CX = if MA1 crosses MA2 then 1 else 0;
def value_at_cx = if CX == 1 then (MA1 + MA1[1]) / 2 else value_at_cx[1];
AddLabel(yes, "   value at last cx: " + value_at_cx + "   ", color.blue);

You might get even closer, given that the slopes would not be identical between your moving averages, by averaging the mid-point values:
Code:
plot MA1 = moving average 1;
plot MA2 = moving average 2;

def CX = if MA1 crosses MA2 then 1 else 0;
def value_at_cx = if CX == 1 then (((MA1 + MA1[1]) / 2) + ((MA2 + MA2[1]) / 2)) / 2 else value_at_cx[1];
AddLabel(yes, "   value at last cx: " + value_at_cx + "   ", color.blue);

But how far down that rabbit hole you want to dive is entirely up to you and depends on how exactly the values need to be approximated to suit your analysis needs.

Hope that helps,
-mashume
 
Solution
uYbuOWV.png


As shown in the image above, I'm looking to pull the exact price where these two lines intersect. Is this possible to do?

As of right now, I'm stuck using the next day's midpoint value and I need the exact intersection for more accurate analysis.

If the lines are part of a thinkscript indicator, post your script and we should be able to assist. If either of the lines are drawn manually using the drawing tool then you would have to manually draw the price line.
 
As shown in the image above, I'm looking to pull the exact price where these two lines intersect. Is this possible to do?

this will find the price where 2 lines cross.
it uses the slopes of 2 lines, between 2 adjacent bars.

it draws several things. all can be turned off,
. a white line at the crossing level.
. displays a bubble with the price.
. a label with the price of the recent crossing.
. a cyan triangle below the bar, after a crossing.
. shows 4 horizontal lines, for the start and stop price levels, of the 2 slope lines.

there are comments in the code describing how i came up with the formula.


this example uses 2 averages for signals.
you can copy this to the end of your study.
then look for the below code, and replace ma1 and ma2 , with your variables.
# ===============================
# assign signals to generic names
def sig1 = ma1;
def sig2 = ma2;
# ===============================


Code:
# crossing_price_01

# find price where 2 lines cross
#  use the slopes of 2 lines, between 2 bars
# crossing is signaled after it happens, on the next bar

def na = double.nan;
def bn = barnumber();
#def lastbar = !isnan(close[0]) and isnan(close[-1]);

# test data -------------------------------------
def price = close;

input MA1_len = 7;
input MA1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);

input MA2_len = 15;
input MA2_type =  AverageType.EXPONENTIAL;
def ma2 = MovingAverage(ma2_type, price, ma2_len);

input plot_signal_lines = yes;
plot z1 = ma1;
plot z2 = ma2;
z1.SetDefaultColor(Color.cyan);
z2.SetDefaultColor(Color.yellow);
z1.SetHiding(!plot_signal_lines);
z2.SetHiding(!plot_signal_lines);


# ===============================
# assign signals to generic names
def sig1 = ma1;
def sig2 = ma2;
# ===============================

def x = sig1 crosses sig2;

input draw_triangle_on_crossings = yes;
plot zx = if x and draw_triangle_on_crossings then low*0.998 else na;
zx.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
zx.SetDefaultColor(Color.cyan);
zx.setlineweight(3);
zx.hidebubble();


# price levels
input draw_crossing_before_after_levels = yes;
plot sig1_before = if x[-1] then sig1[0] else if x then sig1[1] else na;
plot sig1_after = if x[-1] then sig1[-1] else if x then sig1[0] else na;
plot sig2_before = if x[-1] then sig2[0] else if x then sig2[1] else na;
plot sig2_after = if x[-1] then sig2[-1] else if x then sig2[0] else na;
sig1_before.SetDefaultColor(Color.cyan);
sig1_after.SetDefaultColor(Color.cyan);
sig2_before.SetDefaultColor(Color.yellow);
sig2_after.SetDefaultColor(Color.yellow);
sig1_before.SetHiding(!draw_crossing_before_after_levels);
sig1_after.SetHiding(!draw_crossing_before_after_levels);
sig2_before.SetHiding(!draw_crossing_before_after_levels);
sig2_after.SetHiding(!draw_crossing_before_after_levels);

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

# find x , some % of a slope, between 2 bars,
# for which the same x, produces the same $$ in both signals

# sig1 start + (slope1 * x) = sig2 start + ( slope2 * x)
# (sig1-sig2) = (slope2 * x) - (slope1 * x)
# (sig1-sig2) =  (slope2 - slope1) * x
# (sig1 start - sig2 start) / (slope2 - slope1) = x = slope_factor

def slope1 = if x[0] then sig1[0] - sig1[1] else na;
def slope2 = if x[0] then sig2[0] - sig2[1] else na;

def slope_factor = (sig1[1] - sig2[1]) / (slope2 - slope1);

def fac_pr = (slope1 * slope_factor);
def xprice = sig1[1] + fac_pr;

def recent_cross_lvl = if x then xprice else recent_cross_lvl[1];
addlabel(1, " ", color.black);
addlabel(1, "recent crossing price " + round(recent_cross_lvl,2) , color.yellow);


# plot line at cross level
input show_white_line_at_crossing = yes;
plot xxx = if x[-1] then xprice[-1] else if x[0] then xprice[0] else na;
xxx.SetDefaultColor(Color.white);
xxx.SetHiding(!show_white_line_at_crossing);

input crossing_price_bubbles = yes;
addchartbubble(crossing_price_bubbles and x, xprice,
round(xprice, 2)
, color.yellow, no);


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

input test1 = no;
addchartbubble(test1 and x[0], max(sig1,sig2)*0.999,
ma1[1] + " 1\n" +
slope1 + " s1\n" +
slope_factor + " fac\n" +
fac_pr + " fac$\n" +
xprice + "\n" +
ma2[1] + " 2\n" +
slope2 + " s2\n"
, color.yellow, no);


input test2 = no;
addchartbubble(test2 and x[0], max(sig1,sig2)*0.999,
sig1_before + "\n" +
sig1_after + "\n" +
"--" + "\n" +
sig2_before + "\n" +
sig2_after
, color.yellow, no);
#


KHC day chart 8/16
7SR8UiG.jpg

hal_cross
 
Last edited:
this will find the price where 2 lines cross.
it uses the slopes of 2 lines, between 2 adjacent bars.

it draws several things. all can be turned off,
. a white line at the crossing level.
. displays a bubble with the price.
. a label with the price of the recent crossing.
. a cyan triangle below the bar, after a crossing.
. shows 4 horizontal lines, for the start and stop price levels, of the 2 slope lines.

there are comments in the code describing how i came up with the formula.


this example uses 2 averages for signals.
you can copy this to the end of your study.
then look for the below code, and replace ma1 and ma2 , with your variables.
# ===============================
# assign signals to generic names
def sig1 = ma1;
def sig2 = ma2;
# ===============================


Code:
# crossing_price_01

# find price where 2 lines cross
#  use the slopes of 2 lines, between 2 bars
# crossing is signaled after it happens, on the next bar

def na = double.nan;
def bn = barnumber();
#def lastbar = !isnan(close[0]) and isnan(close[-1]);

# test data -------------------------------------
def price = close;

input MA1_len = 7;
input MA1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);

input MA2_len = 15;
input MA2_type =  AverageType.EXPONENTIAL;
def ma2 = MovingAverage(ma2_type, price, ma2_len);

input plot_signal_lines = yes;
plot z1 = ma1;
plot z2 = ma2;
z1.SetDefaultColor(Color.cyan);
z2.SetDefaultColor(Color.yellow);
z1.SetHiding(!plot_signal_lines);
z2.SetHiding(!plot_signal_lines);


# ===============================
# assign signals to generic names
def sig1 = ma1;
def sig2 = ma2;
# ===============================

def x = sig1 crosses sig2;

input draw_triangle_on_crossings = yes;
plot zx = if x and draw_triangle_on_crossings then low*0.998 else na;
zx.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
zx.SetDefaultColor(Color.cyan);
zx.setlineweight(3);
zx.hidebubble();


# price levels
input draw_crossing_before_after_levels = yes;
plot sig1_before = if x[-1] then sig1[0] else if x then sig1[1] else na;
plot sig1_after = if x[-1] then sig1[-1] else if x then sig1[0] else na;
plot sig2_before = if x[-1] then sig2[0] else if x then sig2[1] else na;
plot sig2_after = if x[-1] then sig2[-1] else if x then sig2[0] else na;
sig1_before.SetDefaultColor(Color.cyan);
sig1_after.SetDefaultColor(Color.cyan);
sig2_before.SetDefaultColor(Color.yellow);
sig2_after.SetDefaultColor(Color.yellow);
sig1_before.SetHiding(!draw_crossing_before_after_levels);
sig1_after.SetHiding(!draw_crossing_before_after_levels);
sig2_before.SetHiding(!draw_crossing_before_after_levels);
sig2_after.SetHiding(!draw_crossing_before_after_levels);

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

# find x , some % of a slope, between 2 bars,
# for which the same x, produces the same $$ in both signals

# sig1 start + (slope1 * x) = sig2 start + ( slope2 * x)
# (sig1-sig2) = (slope2 * x) - (slope1 * x)
# (sig1-sig2) =  (slope2 - slope1) * x
# (sig1 start - sig2 start) / (slope2 - slope1) = x = slope_factor

def slope1 = if x[0] then sig1[0] - sig1[1] else na;
def slope2 = if x[0] then sig2[0] - sig2[1] else na;

def slope_factor = (sig1[1] - sig2[1]) / (slope2 - slope1);

def fac_pr = (slope1 * slope_factor);
def xprice = sig1[1] + fac_pr;

def recent_cross_lvl = if x then xprice else recent_cross_lvl[1];
addlabel(1, " ", color.black);
addlabel(1, "recent crossing price " + round(recent_cross_lvl,2) , color.yellow);


# plot line at cross level
input show_white_line_at_crossing = yes;
plot xxx = if x[-1] then xprice[-1] else if x[0] then xprice[0] else na;
xxx.SetDefaultColor(Color.white);
xxx.SetHiding(!show_white_line_at_crossing);

input crossing_price_bubbles = yes;
addchartbubble(crossing_price_bubbles and x, xprice,
round(xprice, 2)
, color.yellow, no);


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

input test1 = no;
addchartbubble(test1 and x[0], max(sig1,sig2)*0.999,
ma1[1] + " 1\n" +
slope1 + " s1\n" +
slope_factor + " fac\n" +
fac_pr + " fac$\n" +
xprice + "\n" +
ma2[1] + " 2\n" +
slope2 + " s2\n"
, color.yellow, no);


input test2 = no;
addchartbubble(test2 and x[0], max(sig1,sig2)*0.999,
sig1_before + "\n" +
sig1_after + "\n" +
"--" + "\n" +
sig2_before + "\n" +
sig2_after
, color.yellow, no);
#


KHC day chart 8/16
7SR8UiG.jpg

hal_cross
Thank you so much for this! It's exactly what I was looking for. And very easy to toggle on/off by the way. Nicely done.
 

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