If you're diving into the world of trading, you may have come across the classic Nearest Neighbor algorithm. While it's a solid starting point, it has one major flaw: it treats all historical prices equally. This means that older prices are given the same weight as newer ones when predicting future trends. To tackle this issue, we've got an updated version of the nearest neighbor indicator that focuses on giving more importance to recent prices while identifying past patterns.
This enhanced indicator employs a weighted correlation coefficient, where the weight decreases linearly from the most recent to older prices within a price pattern. This way, you get a more accurate picture of how the latest trends can influence future price movements.
Here’s a quick rundown of the input parameters for the indicator:
- Npast - the number of past bars in a pattern;
- Nfut - the number of future bars in a pattern (this must be less than Npast).
The indicator visualizes its predictions with two distinct curves: a blue curve representing past prices of the nearest neighbor, and a red curve showing the anticipated future prices of the same pattern. The scaling of the nearest neighbor is based on the linear regression slope between the historical pattern and the current one. Plus, it displays key information, such as the starting date of the nearest neighbor and its correlation coefficient with the present pattern. For instance:
2010.07.09 11:37:10 Nearest Neighbor - weighted corr (EURUSD,H1) Nearest neighbor starts on 2003.02.21 13:00:00 and ends on 2003.03.12 00:00:00. Its correlation coefficient with the current pattern is 0.9521726745708775


Comments 0