Based on the coordinates of two points of the line, we get the line formula: y=a+k*x. The k coefficient determines the slope of the line, i.e. a change in the y coordinate per a unit of change in the x coordinate. The k coefficient is calculated as follows:
The a constant determines the value of the line, if x=0, i.e. corresponds to the level, where the line crosses the y axis. Knowing the slope coefficient and the X coordinate of one of the line points, we calculate a:
The distance from a point to the line is determined based on the perpendicular drawn to the line. If you rotate the drawing 90 degrees counterclockwise, the d line will have the same slope as the main line, but with the opposite sign. Knowing the slope and the coordinates of one point, we can get the equation of the line, but in this case in the form x =a-ky. So, the a constant:
Having the formulas of two lines, we find the point of their intersection. For this purpose, we substitute y expressed by the formula of the first line to the formula of the second line, make some transformations and obtain the x coordinate of the intersection point:
Then substitute the resulting x in the formula for the first line, and obtain the y coordinate of the point of intersection of lines:
Knowing the coordinates of two points, we can calculate the distance between them along the x and y axes (the projections on the axis). Further, using the Pythagorean theorem, calculate the distance:
A discussion in Russian is available at https://www.mql5.com/ru/forum/237198. You can also discuss the idea here, in comments.
Translated from Russian by MetaQuotes Software Corp.
Original code: https://www.mql5.com/ru/code/20481
The Expert Advisor trades using levels. It can have a maximum of two positions of each type (Buy and Sell) in the market.WCCI
A weighted CCI.