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

 
tolga gogebakan:

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

It is benign - continue using it as is since it does not influence the work of the indicator
 
Hi Vladimir, thanks a lot for your kindness of sharing your beautiful, awesome, amazing, and incredible futuristic and advanced thought and ideas. I'm your fan of coding this either mql4 or 5. Let's keep that way. As well appreciated much for McLaden help though short explanation but very useful. God bless you all.
 

To the author, kudos!

Everything is written intelligently and compactly. Thank you.

There are wishes for improvement, if you are still doing it.

1. On the history it is necessary to draw not a neighbour, but the last point of the forecast, shifted back by its length (Nfut). I.e. calculate and draw the forecast for some interval in the past. Then the quality of the prediction will be obvious. However, the indicator will take a long time to think at startup.....

2. One neighbour is very little! The result is unstable.

Theoretically, it is necessary to take the number of neighbours = 3 lengths of the starting vector (i.e. pattern), and then average all forecasts (and better, with weights proportional to correlation coefficients).

 

Hey Vladimir,

I wanted to talk to you about this indicator for a customisation job.

Contact me if you can: najnudel@gmail.com.

tks

 
I think the correct rate is somewhat 50:50, but the predicted results are useful for manual trading.
 

I have an improved nearest neighbour algorithm that I use for my purposes. It overcomes the mentioned drawbacks. The idea is simple. When searching for the nearest neighbour in historical patterns, store all past patterns, their known future patterns and the correlation coefficients between these past patterns and the present pattern. Let us call these correlation coefficients r, which is the vector of all past correlations. Instead of selecting the past pattern with the highest abs (r) as the nearest neighbour, use ALL past patterns as "nearest neighbours" but with their contributions weighted exp(sw*abs(r)), where sw is a user-selected exponent that controls the selectivity of predicting past patterns with the highest correlation coefficients. A very large sw value will cause the prediction to be influenced only by the neighbour with the strongest correlation (the true "nearest neighbour"). A very small sw will make all past patterns equally important. Those who understand the idea can rewrite the above code and publish a new indicator. Below is the implementation in Matlab. If you need to include only past patterns with positive correlation, remove the abs () function in r = abs (...). Then past patterns with negative correlation will automatically have the lowest weight.

% Inputs
% sp is a time series to be predicted as a column vector
np=100;             	% past pattern length
nf=np;              	% future pattern length
sw=100;             	% weighting exponent in kNN

% Compute correlations to past patterns in sp
m  =numel(sp);		% number of bars in sp
x  =sp(1:m-np-nf);      % past patterns
y  =sp(m-np-nf+1:m-nf); % current past pattern
f  =sp(np+1:m-nf);      % future patterns
yf =sp(m-nf:m);         % current future pattern
Sx =movsum(x,np,'Endpoints','discard');
Sxx=movsum(x.^2,np,'Endpoints','discard');
Sy =sum(y);
Syy=sum(y.^2);
Sxy=conv(x,flip(y),'valid');
num=Sxy*np-Sx*Sy;
den=Sxx*np-Sx.*Sx;
r  =abs(real(num./sqrt(den)/sqrt(Syy*np-Sy*Sy)));

% Find nearest neighbor and extrapolate
w=exp(sw*r);
wsum=sum(w);
w=w/wsum;		% weighting coefficients of past patterns
a1=num./den;
a0=(Sxx.*Sy-Sx.*Sxy)./den;
a1=w.*a1;
a0=w.*a0;
a0_sum=sum(a0);
xp=conv(x,flip(a1),'valid')+a0_sum; % composite matched past pattern
xf=conv(f,flip(a1),'valid')+a0_sum; % predicted future pattern
xf=[xp(end);xf];

% Plot
figure
plot(1:np,y,'-k'); hold on;
plot(1:np,xp,'-b'); hold on;
plot(np:np+nf,yf,':k'); hold on;
plot(np:np+nf,xf,'-r');
 
Vladimir user-selected exponent that controls the selectivity of predicting past patterns with the highest correlation coefficients. A very large sw value will cause the prediction to be influenced only by the neighbour with the strongest correlation (the true "nearest neighbour"). A very small sw will make all past patterns equally important. Those who understand the idea can rewrite the above code and publish a new indicator. Below is the implementation in Matlab. If you need to include only past patterns with positive correlation, remove the abs () function in r = abs (...). Then past patterns with negative correlation will automatically have the lowest weight.

Vladimir, I understand the idea.
But it will still be a random prediction.

Experimented a lot with finding patterns about 10 years ago. With mirroring, inversion, summation of paterns. Conclusion - complete randomness in continuing a paternus into the future. All coincidences are random.

 
Nikolai Semko #:

Vladimir, I get the idea.
But it will still be a random prediction.

Experimented a lot with paternos search about 10 years ago. With mirroring, inversion, summation of paterns. Conclusion - complete randomness in continuing a paternus into the future. All coincidences are random.

I agree

 
Nikolai Semko #:

Vladimir, I get the idea.
But it will still be a random prediction.

Experimented a lot with paternos search about 10 years ago. With mirroring, inversion, summation of paterns. Conclusion - complete randomness in continuing a paternus into the future. All coincidences are random.

if you consider real time, not at all. Similar movements at similar moments. You take the time factor out of the system and you get randomness.

 
okayjustask #:
I think the correct rate is somewhat 50:50, but the predicted results are useful for manual trading.

this code needs slight improvements but  okayjustask your idea does make sense