Machine Learning: kNN-based Strategy

kNN-based Strategy (FX and Crypto)

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
Versionshinweise: Added alerts.
Open-source Skript

Ganz im Sinne von TradingView hat der Autor dieses Skripts es als Open-Source veröffentlicht, damit Trader es verstehen und überprüfen können. Ein Hoch auf den Autor! Sie können es kostenlos verwenden, aber die Wiederverwendung dieses Codes in einer Publikation unterliegt den Hausregeln. Sie können das Skript den Favoriten hinzufügen, um es auf dem Chart zu verwenden.

Möchten Sie dieses Skript auf einem Chart verwenden?


500 Coins
+17 Antworten
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?
+15 Antworten
capissimo jekojordi88
@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.
+8 Antworten
jekojordi88 capissimo
@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
@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.)
+2 Antworten
How will i read this?
What is the meaning of symbols? blue, red arrow, plus
+14 Antworten
This publication is now featured in our Editors' Picks: .
In the name of all TradingView traders, thank you for your valuable contribution to the TradingView community, and congrats!
+5 Antworten
capissimo PineCoders
Hello, I have seen you added alerts , however you say that "Signals ARE repainting" . Does it make sense to use Alerts if the study repaint ? If yes, how do you recommend to use the Alerts "once per bar" or "once per bar close" ? Thank you for great work.
+3 Antworten
Do they repaint?
+1 Antworten