Calculation of the slope angle of the trend line. - page 15

 
Daniil Kurmyshev:

Ah, well, then I see)) Then I return the code, maybe someone other thanRenat Akhtyamov will need it, by the way I do not sit on forums much)))

//-----------------------------------------------------------------------------------

// xAngle ///////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------------------

double xAngle(double x1,double y1,double x2,double y2) // x1,x2 Time[] - y1,y2 Price

{

if(x1==0 || x2==0 || y1==y2){return(0);}

if(y1==y2) {return(0);}

//---

double PriceMAX=ChartGetDouble(0,CHART_PRICE_MAX,0);

double PriceMIN=ChartGetDouble(0,CHART_PRICE_MIN,0);

double Bar1proc=(double)(ChartGetInteger(0,CHART_WIDTH_IN_BARS,0)*_Period*60)/100;

double Price1proc=((PriceMAX-PriceMIN)/_Point)/100;

x1=iBarShift(NULL,0,(datetime)x1);

x2=iBarShift(NULL,0,(datetime)x2);

double x1x2=MathAbs(x1-x2)*_Period*60;

double y1y2=MathAbs(y1-y2)/_Point;

double x1x2_proc=x1x2/Bar1proc;

double y1y2_proc=y1y2/Price1proc;

//---

double theta=MathArctan(y1y2_proc/x1x2_proc)*(180/3.1415926535);

if(y2<y1){theta=NormalizeDouble(theta*(-1),2);}

return(theta);

}

//+------------------------------------------------------------------+

Pure function, for calculating the angle, in automatic mode, with correction by chart scale, I use it myself, it works perfectly) As for the trend strategy, by the angle you can estimate the strength, and the approximate duration of the trend.

Here:

double Bar1proc=(double)(ChartGetInteger(0,CHART_WIDTH_IN_BARS,0)*_Period*60)/100;

double Price1proc=((PriceMAX-PriceMIN)/_Point)/100;

The division by 100 is redundant. In the final formula, they (100) are placed in the numerator and denominator when calculating the theta. And they are abbreviated.


Here:

double Price1proc=((PriceMAX-PriceMIN)/_Point)/100;

double y1y2=MathAbs(y1-y2)/_Point;

division by _Point is redundant. The same is true for division by 100.

In the final formula double y1y2_proc=y1y2/Price1proc;

_Points in the numerator and denominator are reduced.

These simplifications first, reduce the calculations (CPU load), and second, possibly increase the accuracy of the final result.


===============================

Or maybe you should try to use ChartTimePriceToXY function to determine pixel values at anchor points of the graphical tool.

It is easier to calculate trigonometric functions using the number of pixels horizontally and vertically. Without the conversion of bars and points.

It should be much simpler.


//-----------------------------------------------------------------------------------

// xAngle ///////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------------------

double xAngle(datetime x1,double y1,datetime x2,double y2) // x1,x2 Time[] - y1,y2 Price

{

if(x1==0 || x2==0 || y1==y2){return(0);}

int _x1, _y1, _x2, _y2;

ChartTimePriceToXY(0,0,x1,y1,_x1, _y1);

ChartTimePriceToXY(0,0,x2,y2,_x2, _y2);

double theta=MathArctan((double)MathAbs(_y1-_y2)/(double)MathAbs(_x1-_x2))*(180/3.1415926535);

if(y2<y1){theta=NormalizeDouble(theta*(-1),2);}

return(theta);

}

Here if(y2<y1){theta=NormalizeDouble(theta*(-1),2);} adjust the sign by the result
 
Eugeni Neumoin:

Here:

.........

Should be much simpler.

Yes all of this is completely useless as it depends on the scale set by the user, which may be different on different charts.

The only true solution is to measure the trend in price per unit of time.

A trend of "0.0001 Eurodollars per hour" will be drawn in exactly the same way by anyone, regardless of any scale, or even platforms.

 
George Merts:

This is all completely useless as it depends on the scale set by the user, which may be different on different charts.

The only correct solution is to measure the trend in price per unit of time.

A trend of "0.0001 Eurodollars per hour" will be drawn in exactly the same way by anyone, regardless of any scale, or even platforms.

I wasn't paying attention to the subject of the thread. It was interesting to calculate the angle to make sloping inscriptions parallel to the trend. I wrote a simplified function for calculating the angle above. It does not depend on the scale at all. To avoid distortion of the inscription when changing the scale and other chart properties, you can use CHARTEVENT_CHART_CHANGE

=========

I wanted to display a caption near the lines, like on the picture. With the changed function above, it turned out like this:


 
Eugeni Neumoin:

I wanted to display the labels near the lines, as in the picture. This is how it turned out with the above function:

Beautiful!
 
Eugeni Neumoin:

I agree with you, I don't like it, but when I initially created this function, I checked a few different ways, and then I didn't change it) and about the pixels, I completely agree with you, the most ideal calculation of the angle for me personally is this way, here is my function:

//-----------------------------------------------------------------------------------

// xAnglePixels /////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------------------

double xAnglePixels(datetime x1,double y1,datetime x2,double y2) // x1,x2 Time[] - y1,y2

{

if(x1==0 || x2==0 || y1==y2){return(0);}

//---

int x[2],y[2];

ChartTimePriceToXY(0,0,x1,y1,x[0],y[0]);

ChartTimePriceToXY(0,0,x2,y2,x[1],y[1]);

//---

if(x[1]<x[0]){return(-1000);}

//---

double x1x2=MathAbs(x[0]-x[1]);

double y1y2=MathAbs(y[0]-y[1]);

//---

double theta;

if(x1x2!=0){theta=MathArctan(y1y2/x1x2)*(180/3.1415926535);}

else{theta=90;}

if(y2<y1){theta=NormalizeDouble(theta*(-1),2);}else{theta=NormalizeDouble(theta,2);}

return(theta);

}

//---------------------------------------------------------------------------------

p.s. in the functionif(x[1]<x[0]){return(-1000);} can be removed, as I used it for myself), and more specifically it is needed if the user has swapped two points of the object in places.

//-----------------

By the way, for the inscriptions on the chart, I use it. Earlier in the branch I posted examples of work of this indicator, there you can see it.

//-----------------

Special thanks to you and respect)

 
Eugeni Neumoin:

It was interesting to calculate the angle to make slanted inscriptions parallel to the trend. Above I wrote a simplified function for calculating the angle. It does not depend on the scale at all.

Yes, it is quite a different case. The task here is not to determine the trend but rather determine the slope of the line plotted on the monitor. And in this case, it is the right way. And everything is beautiful on the picture.
 
George Merts:
Yes, this is a different matter. Here the task is not to determine the trend, but to determine the slope of the line displayed on the monitor. And in this case it is the right way. And everything is beautiful on the picture.
Proceeding from your words, we can draw trend lines and they will show the trend direction and the correct angle will be displayed relatively to the drawn line without playing games. I have attached an example in the picture, my robot builds according to all the rules, but the essence is clear.
Files:
 
Daniil Kurmyshev:
If we proceed from your words, the idea is that we can draw trend lines and they will show the trend direction, and the correct angle will be shown in relation to the drawn line without any tambourine. I attached an example in the picture, my robot builds according to the rules, but the essence is clear.
The picture is a visualization of your calculations. The visualisation proportions can be distorted (change the scale of the chart) and the image will change significantly - the lines will change their slope angles in degrees. But the results of your calculations will not change when you change the scale. Unless, of course, you calculate trends in pixels
 
Alexander Puzanov:
The picture is a visualisation of your calculations. You can distort the proportions of the visualisation (change the scale of the chart) and the picture will change significantly - the lines will change their slope angles in degrees. But the results of your calculations will not change when you change the scale. Unless, of course, you were calculating trends in pixels.
I agree, but you do realize that on each TF, the trends are different; the ones that seemed big on a small one become small on a bigger one and the slopes will change; this changes just the trend presentation, not the angle measuring system; if the measurement system changes, nothing good will come out of it.
 
Daniil Kurmyshev:
I agree with you, but you understand that on each TF, the trends are different, what seemed big on a small one, became small on a larger one, and the slopes will change, just the representation of the trend changes, not the angle measurement system, if the measurement system will change, nothing good will come out of it.
Changing the TF has nothing to do with it - when you change the TF, your calculations change. And when you change the scale (with the +/- buttons or the button on the terminal, sometimes just scrolling the chart), nothing in the calculations changes, and the ° is floating in the middle of nowhere.
Reason: