Indicators: Price prediction by Nearest Neighbor found by a weighted correlation coefficient

 

Price prediction by Nearest Neighbor found by a weighted correlation coefficient:

The main disadvantage of the classical Nearest Neighbor algorithm (see an examplary implementation in https://www.mql5.com/en/code/133) is that all prices within a pattern are treated equally. In other words, it assumes that older prices have the same effect on future as newer ones.

To overcome this drawback, this version of the nearest neighbor indicator gives larger weights to most recent prices while searching for the closest pattern in the past. It uses a weighted correlation coefficient, whose weight decays linearly from newer to older prices within a price pattern.

The indicator plots two curves: the blue curve indicates the past prices of the nearest neighbor and the red curve indicates the future prices of the same pattern. The nearest neighbor 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 Neighbor found by a weighted correlation coefficient

 
I know  some of your codes in MQL4  as well and  you  seem to have a firm  grasp of some  advanced subjects , very good work Vladimir.
 
geordie:
I know  some of your codes in MQL4  as well and  you  seem to have a firm  grasp of some  advanced subjects , very good work Vladimir.
Thanks for the kind words and encouragement.
 

What a blessing... I was going to write such "trouble" myself... now it will be possible to just redo it....

Question to the author... what is a pattern? It's not clear to me... because in my version candlestick sequences will be encoded... it is the code from several candles that will be considered a pattern...

What's yours?

because I look in the "book" and see a pattern )))

 
BaTTLeBLooM:

What a blessing... was going to write such a "trouble" myself... now you can just redo it....

Question for the author... what is the pattern? it's not clear to me... because in my version candlestick sequences will be encoded... it is the code from several candles that will be considered as a pattern.....

and what's yours?

Because when I look in the "book" I see a pattern.)

The pattern consists of two parts

  1. Past prices, i.e. prices to the left of some current price (Open in my case) including the current price itself - they are Npast.
  2. Future prices, i.e. prices to the right of the current price - they are Nfut.

For the most recent bar on the chart there are no future prices, only the current price and past prices. That is, the pattern for the current price has only present and past prices (Npast in total) and the indicator predicts Nfut future prices. This incomplete pattern I call current or present (current pattern). The rest of the patterns I call past patterns or simply neighbours. These neighbours have both past and "future" prices. The nearest neighbour is the one that is most correlated with the current pattern in terms of past prices. We can change the distance measure and use Euclidean distance instead of correlation coefficient, which is generally accepted. But here we need to remove the average and normalise prices by high-low.

 

Quite interesting this topic with the search for closest matches. But using it as an indicator makes it difficult to explore. Here, for example, is one of the ways of such a search using a script that is simply thrown on the chart (for dark background). But it is still on mql4. But the idea itself may be useful to you. It also uses the HP filter and the ability to search for continuous data and hourly from time of day.

Files:
 

I wonder how much history array is needed for more or less successful prediction? why 300 bars by default?

and what are the statistics of showing at least directions?

Is it positive, negative or like everything else 50/50?

 

Thank You

 
Greetings ! gpwr, respect, very interesting stuff. The only thing is that you have some bug there. Sometimes I don't understand why, on the screen appears full of vertical red and blue stripes, and in the log there is a record that the correlation coefficient of the found pattern is 50 and more thousand. I haven't understood why it appears yet, I am studying the code. It happens quite rarely, but once in this mode, it does not return to normal, the indicator has to be removed and put again. The only thing is that I have a terminal on my mac running under wineskin. Maybe some bugs ? But it is unlikely. Anyway, thanks a lot. Very interesting, I want to do some digging in this direction myself.
 
oltab:

I wonder what kind of history array is needed for more or less successful prediction? Why 300 bars by default?

and what are the statistics of showing at least directions?

Is it positive, negative or, like everything else, 50/50?

For better prediction I think the idea itself is unsuitable. More precisely, the idea is very, very good, but it requires serious tweaking with a file. That's what I'm trying to do now.
 

warning

There is a warning. Could you please help me? Thank you in advance.