Indicators: Price prediction by Nearest Neighbor

 

Price prediction by Nearest Neighbor:

This indicator uses the Nearest Neighbor clustering technique, also called k-NN, to search for the most similar pattern in history and use its past prices as predictions of the current pattern future prices.

The present indicator finds only one nearest neighbor. So, in essence, it is a 1-NN algorithm. It uses the Pearson correlation coefficient between the current pattern and all past patterns as the measure of distance between them.

The indicator plots two curves: the blue curve indicates the past prices of the nearest neighbour and the red curve indicates the future prices of the same pattern. The nearest neighbour is scaled according to the linear regression slope between this pattern and the current pattern. The indicator also prints the information about the starting date of the nearest neighbor and its correlation coefficient to the present pattern.

Author: Vladimir

Price prediction by Nearest Neighbour

 

the number of pi is better set like this.

pi = 4*MathArctan(1);
I got the idea once, and now I'm passing it on to you.
 
Prival:

the number of pi is better set like this.

I got the tip once, now I'm passing it on to you.
Thanks, I'll keep that in mind. By the way, pi is not used there. I left that line in by mistake.
 

There is also a section Mathematical const ants in the MQL5 language:

Special constants containing values are reserved for some mathematical expressions. These constants can be used anywhere in the mql5 program instead of calculating their value using mathematical functions.

Constant

Description

Value

M_E

e

2.71828182845904523536

M_LOG2E

log2(e)

1.44269504088896340736

M_LOG10E

log10(e)

0.434294481903251827651

M_LN2

ln(2)

0.693147180559945309417

M_LN10

ln(10)

2.30258509299404568402

M_PI

pi

3.14159265358979323846

 
Rosh:

There is also a section Mathematical constants in MQL5:


What you need.
 
Rosh:

There is also a section Mathematical constants in MQL5:


Are these constants set with the maximum possible accuracy or not? I used this entry because it was necessary to have the maximum precision, otherwise the error accumulated
 
Prival:
Are these constants set with the maximum possible precision or not ? I used this entry because it was necessary to have exactly the maximum precision, otherwise the error accumulated.

You need to take it and check it. Here is the script, it gives five times 100500.

#property copyright "Copyright © 2010 Eugene Logunov (lea)"
#property version "1.00"

int CalcDifference(double approx, double exact) {
   double diff = MathAbs(approx - exact);
   if (diff == 0.0) {
      return 100500;
   }
   else {
      return (int)(-MathLog10(diff));
   }
}

void OnStart() {
   PrintFormat("%d", CalcDifference(MathExp(1.0), M_E));
   PrintFormat("%d", CalcDifference(MathLog10(MathExp(1)), M_LOG10E));
   PrintFormat("%d", CalcDifference(MathLog(2.0), M_LN2));
   PrintFormat("%d", CalcDifference(MathLog(10.0), M_LN10));
   PrintFormat("%d", CalcDifference(MathArctan(1.0) * 4.0, M_PI));
}

p.s. If the algorithm produces a bad result because of an error in 16 decimal places to the right of the decimal point in a constant, it is obviously not the constant. And why do you need such calculation accuracy if the quotes are filtered anyway?

 
lea:

I need to take this and check it out. Here is the script, it gives five times 100500.

p.s. If the algorithm produces a bad result because of an error in 16 decimal places to the right of the decimal point in a constant, it's obviously not the constant. And why do you need such calculation accuracy if the quotes are filtered anyway?

I advise you as a programmer to read about iterative algorithms at least once in your life, what they are and what they are used for. And then rounding http://dic.academic.ru/dic.nsf/enc_mathematics/1999/ИТЕРАЦИОННЫЙ mathematicians puzzle over how to deal with this rounding (to come up with stable algorithms), and here it's easy and not forced, and why ...

Z.y. thanks for the advice. I already once lost 2 weeks to check https://www.mql5.com/en/code/8309 now I will not step on this rake.

З.З.Ы and so already sick of checking, in the five it is already becoming paranoia ...

 
Prival:

I advise you, as a programmer, to read once in your life about iterative algorithms, what they are and what they eat. And then rounding http://dic.academic.ru/dic.nsf/enc_mathematics/1999/ИТЕРАЦИОННЫЙ mathematicians puzzle over how to deal with this rounding (to come up with stable algorithms), and here it is easy and not forced, and why ...

I had to read about it all last year. Besides, I was hinting at instability (by saying that it's not about constants).

If you feel the lack of accuracy just because of types - you can recommend something like http://gmplib.org/ (of course, the speed of calculations will drop).

H.S.Y. I'm already sick of checking, in 5 it becomes paranoid....

You should always check everything you can, even though it sometimes takes a lot of time. Otherwise there can be surprises.

 
lea:

I've been reading about it for the past year. Besides, I was hinting at instability (by saying that it is not about constants).

If you feel the lack of accuracy just because of types, you can recommend something like http://gmplib.org/ (of course, the speed of calculations will drop).

You should always check everything you can, though it is sometimes time-consuming. Otherwise there may be surprises.

You have no idea what I programmed and you give advice. And you teach a person who started programming (judging by your profile) when you went to kindergarten.

You are not a developer, I didn't ask you the question, but you started teaching me how to check the code. Do you want to help me - give me the results of how accurately all the constants are set? I can check it without advice, I just want to save my time.

 
Very Nice