Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1279

 
leonerd:

Poke your nose into a simple code that calculates the angle of price movement. The picture below shows it.


It depends on what x(pixels, bars or time) and y(pixels or price) are measured in.
And what is the task: to measure the angle on the current chart with the current scale (bar width in pixels) and time frame? Or to measure the angle so that it remains the same for any TF and scale?
Different tasks and coordinate systems - different solutions.
 
leonerd:

Poke your nose into a simple code that calculates the angle of price movement. The picture below shows it.


Well, you can calculate it with a tangent. And you don't need the angle itself, you just need to know the tangent.

 
Alexey Viktorov:

And you don't really need an angle per se, you just need a tangent.

Exactly! And our tangent is called: speed.

In this example, the price has passed through 1884 points in 39 one-hour candles. The total speed of this wave is: 1884/(39/24) = 1159 pips per day.

double interval=double(iBarShift(symbol,frame,start_time)-iBarShift(symbol,frame,finish_time)*PeriodSeconds(frame)/86400;
int distance=MathAbs(finish_price-start_price);
int speed=interval>0?(distance/interval):0;
And we don't need an angle, why do we need an angle? When we start stretching or flattening the graph, what will the angle look like then? Angles can be measured when the x- and y-axes have the same units. For instance, if you take a notebook with squares, draw a line along the diagonals of these squares, you will get 45 degrees. And what's up and what's to the right are centimetres. And here, what is one price point equal to? One hour, one day? Those are not comparable values.
 
Aleksei Stepanenko:

Exactly! And our tangent is called: speed.

In this example, the price has passed 1,884 points in 39 one-hour candles. The total speed of this wave is: 1884/(39/24) = 1159 pips per day.

And we don't need an angle, why do we need an angle? When we start stretching or flattening the graph, what will the angle look like then? Angles can be measured when the x- and y-axes have the same units. For instance, if you take a notebook with squares, draw a line along the diagonals of these squares, you will get 45 degrees. And what's up and what's to the right are centimetres. And here, what is one price point equal to? One hour, one day?Those are not comparable values.

Why do we need to compare them? Kilometres and time are also not comparable, but that doesn't stop us from counting speed in km/hour.

If your tangent is speed, it will be that speed per unit time of the chart period. If price has moved 1884 pips in 39 hours, it will be 48 pips per hour. There is no need to convert it to days.

But you can also calculate "acceleration" as a ratio of current speed to the speed on the previous bar. And by collecting several of these values, the average acceleration can be determined.

 
Aleksei Stepanenko:

And here, what does one price point equal? One hour, one day? The values are not comparable.

Well, distance is also measured in light years. So it's not a problem.
Let's introduce a constant speed. For example, 1 _Point for one 1 minute bar and bring the coordinate system to a price on both axes .

Then the angles will be the same on any timeframe.

The main thing is to correctly calculate the number of minute bars betweenx1and x2.

In this system an angle of 45 degrees would mean that price changes by N*tg(45) =N pips in N-minute bars and an angle of 30 degrees would mean that price changes by N*tg(30)=N*0.57735 pips in N-minute bars.
It is better to use minute bars rather than time as there are time holes in the form of weekends, low volatility, etc.
 

I agree 100%, because the candlestick can be stretched both horizontally and vertically. And there should be some standard for determining the angle (candlestick width in pixels and the number of points in a pixel).

Can I ask an off-topic question?

How do I correctly convert datatime to double so that the compiler won't swear (data loss due to incorrect conversion is possible)?

double A = Time[1];

Thanks in advance!

 
Alexey Viktorov:

If you have 1884 pips in 39 hours, the speed is 48 pips per hour. And there is no need to convert that to days at all.

Yes, that's my assumption. Since I made the speed int (i.e. whole pips), I thought I would use a larger period, so that fractions of a pip in speed wouldn't matter too much and could be discarded. So I got points per day. Well, this agrees with the human biological clock. Tired, slept, got up in the morning: "What was the speed yesterday? It looks consistent, so that's the standard I set for myself. And then, of course, it's a matter of personal convenience.


Nikolai Semko:

Well, distance is also measured in light years. So that's not a problem.
Let's introduce a constant velocity. For example, 1 _Point for one 1 minute bar and bring the coordinate system to a price on both axes .

Yes, again the speed is obtained, only in units: points per minute. As for the weekends and holes, I took the following way: what candles are on the chart is the time interval, and what is absent (days/hours) is not there. Because if we take into account weekends, and the price stands still at that time, the speed will noticeably drop without any reason. But then again, this is to each his own:)


Roni Iron:

How to properly convert datatime to double, so that the compiler didn't swear (data loss due to incorrect conversion is possible).

double A = (double)Time[1];

Why would you store datatime in double? If you store different types of variables in the same array, and bring them here and there, consider replacing the usual array with a structure array:


struct My
   {
   datetime time;
   double price;  
   } my[];

int finish=0;
ArrayResize(my,finish+1);
my[finish].time=Time[1];
my[finish].price=Close[1];
 

Thank you very much !

Do the structures only work in mql 5 or already in mql 4 ?

 
Yes they work in four, it's very convenient to write with them, clearly.
 
Roni Iron:

Thank you very much !

Do structures work only in mql 5 or already in mql 4 ?

Not only structures work, but union too.