Description: This strategy uses a classic machine learning algorithm - k Nearest Neighbours (kNN) - to let you find a prediction for the next (tomorrow's, next month's, etc.) market move. Being an unsupervised machine learning algorithm, kNN is one of the most simple learning algorithms.
To do a prediction of the next market move, the kNN algorithm uses the historic data, collected in 3 arrays - feature1, feature2 and directions, - and finds the k-nearest neighbours of the current indicator(s) values.
The two dimensional kNN algorithm just has a look on what has happened in the past when the two indicators had a similar level. It then looks at the k nearest neighbours, sees their state and thus classifies the current point.
The kNN algorithm offers a framework to test all kinds of indicators easily to see if they have got any *predictive value*. One can easily add cog, wpr and others. Note: TradingViews's playback feature helps to see this strategy in action. Warning: Signals ARE repainting.
Style tags: Trend Following, Trend Analysis Asset class: Equities, Futures, ETFs, Currencies and Commodities Dataset: FX Minutes/Hours+++/Days
Release Notes
⋅
Added alerts.
Release Notes
⋅
An upgrade to Pinescript v.5.
Release Notes
⋅
Major Fix. Changedthe signal filter application point and corrected the class label calculation.
Release Notes
⋅
Major fix. Added bars count for clear condition variable. Replaced endLongTrade and endShortTrade variables with clear_condition
Nice work!! just a quick question, is there any reason why you used as a k floor(sqrt(252))? wouldn't be better using a high number?
capissimo
⋅
@jekojordi88, the k value was chosen arbitrarily. On the one hand, small k leads to unstable results. On the other, I wanted to minimize computing cost and tie it up to trading (ex. 252 trading days; might as well use fibo numbers.). Generally, k value is based on the training accuracy.
jekojordi88
⋅
@capissimo, Thanks for the reply. I tried to implement your code in a basic strategy but the outcome is negative. Have you tried as well?
capissimo
⋅
@jekojordi88, yes I have. My results were relatively better in higher TFs. It looks this indicator should be used together with other indicators. Besides, I did not explore the fast/slow parameter values. Ple regard it as a proof of concept.)
Four17
⋅
How should the yellow cross be interpreted? Is there a video explanation on the indicator?