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
Remove from Favorite Scripts Add to Favorite Scripts


+3 Reply
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?
+9 Reply
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.
+5 Reply
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.)
+1 Reply
Do they repaint?
+1 Reply
capissimo thegoodwillhunter
@capissimo, oh then its an issue
capissimo madhu1948menon
@madhu1948menon, right you are. Repainting IS an issue. Use one more indicator that shows an overall trend, and avoid going short in an upward trend and going long in a downward trend.
@capissimo, bro can you make this in multi time frame that is in a resolution greater than current time frame
Home Stock Screener Forex Screener Crypto Screener Economic Calendar How It Works Chart Features Pricing Refer a friend House Rules Help Center Website & Broker Solutions Widgets Charting Solutions Lightweight Charting Library Blog & News Twitter
Profile Profile Settings Account and Billing Referred friends Coins My Support Tickets Help Center Ideas Published Followers Following Private Messages Chat Sign Out