Intersection Value Functions

Winning entry for the first Pinefest contest. The challenge required providing three functions returning the intersection value between two series source1 and source2 in the event of a cross, crossunder, and crossover.

Feel free to use the code however you like.



//@function Finds intersection value of 2 lines/values if any cross occurs - First function of challenge -> crossValue(source1, source2)
//@param    source1 (float) source value 1 
//@param    source2 (float) source value 2
//@returns  Intersection value


value = crossValue(close, close[25])


//@function Finds intersection value of 2 lines/values if crossover occurs - Second function of challenge -> crossoverValue(source1, source2) 
//@param    source1 (float) source value 1 
//@param    source2 (float) source value 2
//@returns  Intersection value


value = crossoverValue(close, close[25])


//@function Finds intersect of 2 lines/values if crossunder occurs - Third function of challenge -> crossunderValue(source1, source2) 
//@param    source1 (float) source value 1 
//@param    source2 (float) source value 2
//@returns  Intersection value


value = crossunderValue(close, close[25])


A series of values can be displayed as a series of points, where the point location highlights its value, however, it is more common to connect each point with a line to have a continuous aspect.

A line is a geometrical object connecting two points, each having y and x coordinates. A line has a slope controlling its steepness and an intercept indicating where the line crosses an axis. With these elements, we can describe a line as follows:

slope × x + intercept

A cross between two series of values occurs when one series is greater or lower than the other while its previous value isn't.

We are interested in finding the "intersection value", that is the value where two crossing lines are equal. This problem can be approached via linear interpolation.

A simple and direct approach to finding our intersection value is to find the common scaling factor of the slopes of the lines, that is the multiplicative factor that multiplies both lines slopes such that the resulting points are equal.


A = Point A1 + m1 × scaling_factor
B = Point B1 + m2 × scaling_factor

where scaling_factor is the common scaling factor, and m1 and m2 the slopes:

m1 = Point A2 - Point A1
m2 = Point B2 - Point B1

In our cases, since the horizontal distance between two points is simply 1, our lines slopes are equal to their vertical distance (rise).

Under the event of a cross, there exists a scaling_factor satisfying A = B, which allows us to directly compute our intersection value. The solution is given by:

scaling_factor = (B1 - A1)/(m1 - m2)

As such our intersection value can be given by the following equivalent calculations:

(1) A1 + m1 × (B1 - A1)/(m1 - m2)
(2) B1 + m2 × (B1 - A1)/(m1 - m2)
(3) A2 - m2 × (A2 - B2)/(m1 - m2)
(4) B2 - m2 × (A2 - B2)/(m1 - m2)

The proposed functions use the third calculation.

This approach is equivalent to expressions using the classical line equation, with:

slope1 × x + intercept1 = slope2 × x + intercept2

By solving for x, the intersection point is obtained by evaluating any of the line equations for the obtained x solution.


The intersection point of two crossing lines might lead to interesting applications and creations, in this section various information/tools derived from the proposed calculations are presented.

This supplementary material is available within the script.

🔹Intersections As Support/Resistances

The script allows extending the lines of the intersection value when a cross is detected, these extended lines could have applications as support/resistance lines.

🔹Using The Scaling Factor

The core of the proposed calculation method is the common scaling factor, which can be used to return useful information, such as the position of the cross relative to the x coordinates of a line.

The above image highlights two moving averages (in green and red), the cross-interval areas are highlighted in blue, and the intersection point is highlighted as a blue line.

The pane below shows a bar plot displaying:

1 - scaling factor = 1 - [(source1 - source2) / (m1 - m2)]

Values closer to 1 indicate that the cross location is closer to x2 (the right coordinate of the lines), while values closer to 0 indicate that the cross location is closer to x1.

🔹Intersection Matrix

The main proposed functions of this challenge focus on the crossings between two series of values, however, we might be interested in applying this over a collection of series.

We can see in the image above how the lines connecting two points intersect with each other, we can construct a matrix populated with the intersection value of two corresponding lines. If (X, Y) represents the intersection value between lines X and Y we have the following matrix:

       | Line A | Line B | Line C | Line D |
Line A |        | (A, B) | (A, C) | (A, D) |
Line B | (B, A) |        | (B, C) | (B, D) |
Line C | (C, A) | (C, B) |        | (C, D) |
Line D | (D, A) | (D, B) | (D, C) |        |

We can see that the upper triangular part of this matrix is redundant, which is why the script does not compute it. This function is provided in the script as intersectionMatrix:

//@function Return the N * N intersection matrix from an array of values
//@param    array_series (array<float>) array of values, requires an array supporting historical referencing
//@returns  (matrix<float>) Intersection matrix showing intersection values between all array entries

In the script, we create an intersection matrix from an array containing the outputs of simple moving averages with a period in a specific user set range and can highlight if a simple moving average of a certain period crosses with another moving average with a different period, as well as the intersection value.

🔹Magnification Glass

Crosses on a chart can be quite small and might require zooming in significantly to see a detailed picture of them. Using the obtained scaling factor allows reconstructing crossing events with an higher resolution.

A simple supplementary zoomIn function is provided to this effect:

//@function Display an higher resolution representation of intersecting lines
//@param    source1      (float) source value 1
//@param    source2      (float) source value 2
//@param    css1         (color) color of source 1 line
//@param    css2         (color) color of source 2 line
//@param    intersec_css (color) color of intersection line
//@param    area_css     (color) color of box area

Users can obtain a higher resolution by modifying the provided "Resolution" setting.

The function returns a higher resolution representation of the most recent crosses between two input series, the intersection value is also provided.

Check out the indicators we are making at luxalgo:

ด้วยจิตวิญญาณของ TradingView อย่างแท้จริง ผู้เขียนสคริปต์นี้ได้เผยแพร่เป็นโอเพนซอร์ส เพื่อให้ผู้ค้าสามารถเข้าใจและตรวจสอบได้ ไชโยให้กับผู้เขียน! คุณสามารถใช้ได้ฟรี แต่การใช้รหัสนี้ซ้ำในสิ่งพิมพ์อยู่ภายใต้กฎระเบียบการใช้งาน คุณสามารถตั้งเป็นรายการโปรดเพื่อใช้บนชาร์ตได้


ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมที่ เงื่อนไขการใช้บริการ