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에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.

차트에 이 스크립트를 사용하시겠습니까?