sDotToLineDist - script for MetaTrader 5

sDotToLineDist - script for MetaTrader 5

Function parameters:

  • double LX1 - the X coordinate of the first point of the line;
  • double LY1 - the Y coordinate of the first point of the line;
  • double LX2 - the X coordinate of the second point of the line;
  • double LY2 - the Y coordinate of the second point of the line;
  • double DX - the X coordinate of the point;
  • double DY - the Y coordinate of the point.

Calculation principle

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:

double K=(LY2-LY1)/(LX2-LX1);

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:

double LA=LY1-K*LX1;

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:

double DA=DX+K*DY;

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:

double CX=(DA-K*LA)/(1.0+K*K);

Then substitute the resulting x in the formula for the first line, and obtain the y coordinate of the point of intersection of lines:

double CY=LA+K*CX;

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 You can also discuss the idea here, in comments.

Download sDotToLineDist - script for MetaTrader 5

Related News

Latest News
Two Expected 2020 IPOs Bring Hope to Egypt’s Subdued Market
10.12.2019 05:15
(Bloomberg) -- Egypt’s ailing stock market could find a much-needed jolt of liquidity from two state-owned enterprises set to go public early next year. The long-awaited initial public offering of one of the biggest banks in the country, Banque du Caire, in particular could draw funds from outside the region, according to Hasnain Malik, head of
Read more
Gold’s Been on a Tear This Year and 2020 May See More Reward
10.12.2019 05:12
(Bloomberg) -- Gold’s impressive advance in 2019 -- aided by trade war frictions, easier monetary policy across the world’s leading economies and sustained central-bank buying -- may be set to spill into the new decade. As 2020 looms, BlackRock Inc (NYSE:BLK)., the world’s largest money manager, remains constructive on bullion as a hedge, while
Read more
Macron Braces for Second Day of Protests Against Pension Reform
10.12.2019 05:09
(Bloomberg) -- Emmanuel Macron is bracing for round-two of a clash with labor unions over his plan to upend France’s pension system, in what could become the defining moment of his presidency. Labor unions will stage a repeat of last week’s mass protests on Tuesday to load pressure the day before Macron’s government announces the final version of
Read more
Japan’s 10-Year Yield Climbs to Zero for First Time Since March
10.12.2019 05:06
(Bloomberg) -- Japan’s 10-year bond yield rose to zero for the first time in nine months amid waning global demand for debt. The nation’s benchmark yields are rising for a fourth month after the Bank of Japan slashed debt purchases and refrained from adding any more easing measures. Bonds have also fallen as optimism that the U.S. and China may
Read more
Australia cartel case against Citi, Deutsche delayed by late night witness statement
10.12.2019 05:03
SYDNEY (Reuters) - An Australian criminal cartel lawsuit against Citigroup Inc (N:C) and Deutsche Bank AG (DE:DBKGn) was postponed on Tuesday after defense lawyers complained the prosecution had given them a redacted statement for a key witness late on the previous day. Though for just one day, the delay raises the chance of pre-trial hearings
Read more