Price Cross Time Custom Range Interactive█ OVERVIEW
This indicator was a time-based indicator and intended as educational purpose only based on pine script v5 functions for ta.cross() , ta.crossover() and ta.crossunder() .
I realised that there is some overlap price with the cross functions, hence I integrate them into Custom Range Interactive with value variance and overlap displayed into table.
This was my submission for Pinefest #1 , I decided to share this as public, I may accidentally delete this as long as i keep as private.
█ INSPIRATION
Inspired by design, code and usage of CAGR. Basic usage of custom range / interactive, pretty much explained here . Credits to TradingView.
█ FEATURES
1. Custom Range Interactive
2. Label can be resize and change color.
3. Label show tooltip for price and time.
4. Label can be offset to improve readability.
5. Table can show price variance when any cross is true.
6. Table can show overlap if found crosss is overlap either with crossover and crossunder.
7. Table text color automatically change based on chart background (light / dark mode).
8. Source 2 is drawn as straight line, while Source 1 will draw as label either above line for crossover, below line for crossunder and marked 'X' if crossing with Source 2's line.
9. Cross 'X' label can be offset to improve readability.
10. Both Source 1 and Source 2 can select Open, Close, High and Low, which can be displayed into table.
█ LIMITATIONS
1. Table is limited to intraday timeframe only as time format is not accurate for daily timeframe and above. Example daily timeframe will give result less 1 day from actual date.
2. I did not include other sources such external source or any built in sources such as hl2, hlc3, ohlc4 and hlcc4.
█ CODE EXPLAINATION
I pretty much create custom function with method which returns tuple value.
method crossVariant(float price = na, chart.point ref = na) =>
cross = ta.cross( price, ref.price)
over = ta.crossover( price, ref.price)
under = ta.crossunder(price, ref.price)
Unfortunately, I unable make the labels into array which i plan to return string value by getting the text value from array label, hence i use label.all and add incremental int value as reference.
series label labelCross = na, labelCross.delete()
var int num = 0
if over
num += 1
labelCross := label.new()
if under
num += 1
labelCross := label.new()
if cross
num += 1
labelCross := label.new()
I realised cross value can be overlap with crossover and crossunder, hence I add bool to enable force overlap and add additional bools.
series label labelCross = na, labelCross.delete()
var int num = 0
if forceOverlap
if over
num += 1
labelCross := label.new()
if under
num += 1
labelCross := label.new()
if cross
num += 1
labelCross := label.new()
else
if cross and over
num += 1
labelCross := label.new()
if cross and under
num += 1
labelCross := label.new()
if cross and not over and not under
num += 1
labelCross := label.new()
█ USAGE / EXAMPLES
Pinefest
Intersection Value FunctionsWinning 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()
//@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
example:
value = crossValue(close, close )
🔹 crossoverValue()
//@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
example:
value = crossoverValue(close, close )
🔹 crossunderValue()
//@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
example:
value = crossunderValue(close, close )
🔶 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:
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.
Given:
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.
🔶 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:
1 - scaling factor = 1 -
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) array of values, requires an array supporting historical referencing
//@returns (matrix) 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.
Trend Line XrossTrend Line Xross (TLX) Uses User Input Points to draw trendlines and displays the exact intersection point of those trendlines.
This is the public indicator of the practical application for this intersection method included in my entry for Pinefest #1.
To determine the exact intersection point I am using the y-intercept method as seen below.
The code is notated for more information on the technical workings.
One difference to note between this version and the pinefest version is that I had to change the line drawings to use bar_index values so that I can use line.get_price() to grab the current value of the line to make alerts from.
Additionally, there are alerts built-in to this version for every type of cross on all of the visible lines.
Enjoy!