Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

Scripts

sDotToLineDist - script for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語

Views:
358
Rating:
votes: 12
Published:
2018.06.06 14:38

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:

MathSqrt(MathPow(DX-CX,2)+MathPow(DY-CY,2))

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

maximus_vX lite maximus_vX lite

The Expert Advisor trades using levels. It can have a maximum of two positions of each type (Buy and Sell) in the market.

WCCI WCCI

A weighted CCI.

Poker_SHOW Poker_SHOW

An Expert Advisor based on a random number generator. Trend is determined using the iMA (Moving Average, MA) indicator.

TrueSort_1100 TrueSort_1100

The Expert Advisor waits when the lines of MA(10), MA(20), MA(50), MA(100) and MA(200) get arranged one above the other (or one below the other).