The K-nearest neighbors (KNN) algorithm is a simple, easy-to-implement supervised machine learning algorithm that can be used to solve both classification and regression problems.
The KNN algorithm assumes that similar things exist in close proximity. In other words, similar things are near to each other. KNN captures the idea of similarity (sometimes called distance, proximity, or closeness) with some mathematics we might have learned in our childhood— calculating the distance between points on a graph to predict the next move.
Thanks to 'capissimo' for his first coding structure of KNN on Tradingview.
Some pieces of the code are similar to his original idea of implementing the KNN machine learning in pinescript. All gratitude to him.
Functions:
-🔌Source : select the source data for your script (Close, Open,.. you can even use EMA50 and so on for the algorithm)
-💹 Long - 📉 Short : select the trade direction of the strategy
-Skip trading in Flat markets : Skip trading when market is flat. If the price is within % treshold of the bands (1/1.5/2% most common), it's considered as sideway ranging or non-trending market.
-📍Indicators : select indicators to use for KNN machine learning algorithm (8 indicators to choose from or select All - algorithm will be predicting market sentiment from indicators)
- Fast/Medium/Slow period : This three dimensional kNN algorithm has a look on what has happened in the past when the indicators with 3 different periods had a similar level. It then looks at the k nearest neighbours, sees their state and classifies the current point. I don't recommend changing this, 10-15-20 is a nice balanced weight between 3 periods on most of the indicators - focus on changing K-neighbours value.
- K-number of neighbours : To select the K that’s right for your data, we run the KNN algorithm several times with different values of K and choose the K that reduces the number of errors we encounter while maintaining the algorithm’s ability to accurately make predictions when it’s given data it hasn’t seen before.As we decrease the value of K, our predictions become less stable.Inversely, as we increase the value of K, our predictions become more stable due to majority voting / averaging, and thus, more likely to make more accurate predictions.
I suggest using 5000 or 10000K mostly and large time frames. Scripts needs to do a lot of calculation, <1h time-frame charts probably won't load in time on most of the markets.
Buy/Sell prediction : 0 is a neutral signal, when Machine Learning calculation goes over 1 it suggests the price will increase, below -1 decrease. You can just leave this at 0 or +1/-1, this is fine tuning of the prediction power.
Buy only on signal change : default is true, it's buying only when signal changes from neutral or negative to positive, if false it's repeating buys as long as the prediction is positive / negative. You will see that if you use Take Profit or Stop Loss limit orders.
Sell on Clear Condition : it will close an open position if the prediction is neutral- between the Buy/Sell prediction decision.