Editors' picksOPEN-SOURCE SCRIPT

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.

🔶**CHALLENGE FUNCTIONS**

🔹**crossValue()**

example:

🔹**crossoverValue()**

example:

🔹**crossunderValue()**

example:

🔶**DETAILS**

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:

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.

Given:

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

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:

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

The proposed functions use the third calculation.

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

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

🔶**APPLICATIONS**

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:

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:

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*:

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:

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.

Feel free to use the code however you like.

🔶

🔹

example:

🔹

example:

🔹

example:

🔶

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

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.

Given:

where

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

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

The proposed functions use the third calculation.

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

By solving for

🔶

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.

🔹

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.

🔹

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

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:

Values closer to 1 indicate that the cross location is closer to

🔹

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

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

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.

🔹

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

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.

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.