Type/to search

kNN-based Trend Following Strategy

Cryptocurrency
Created: 2023-12-08 11:33:31
Last modified: 3 years ago
1
Follow
1779
Followers

img

Overview

This strategy utilizes the kNN (k Nearest Neighbors) machine learning algorithm to predict market trends and generate long and short signals accordingly. It comprehensively considers multiple factors like historical data, technical indicators, and so on, dynamically capturing market patterns through training kNN models to realize automated trend following trading.

Strategy Principle

  1. Collect training data: collect historical data like closing prices, trading volumes, as well as technical indicators like RSI, CCI over time.

  2. Data preprocessing: normalize indicator values into range 0-100.

  3. Train kNN model: take current two features in the kNN model, calculate Euclidean distances between these feature vectors and historical ones, choose k nearest neighbor samples based on distances, and count label (bullish or bearish) distribution of these k samples.

  4. Obtain predictions: make predictions on current market trend based on labels of k nearest neighbors. If prediction is bullish, generate long signal. If prediction is bearish, generate short signal.

  5. Trade using stop loss, position sizing, moving average filters.

Advantages

  1. Automatically recognize technical patterns using machine learning without manual intervention.

  2. Flexibility to select different technical indicators as model features for real-time optimization.

  3. Integrates strict risk control mechanisms like stop loss, position sizing.

  4. Visualized stop loss lines for clarity and intuition.

Risks and Solutions

  1. Prediction errors may exist in machine learning. Optimization methods include adjusting k value, feature vectors, sample time range properly.

  2. Potential risks in one-direction trading. Add permission for two-way trading in code to eliminate bugs.

  3. Improper parameter settings may lead to overtrading. Adjust position sizing, trading frequency accordingly.

Optimization Directions

  1. Test different types of technical indicators as kNN input features.

  2. Try other distance metrics like Manhattan distance.

  3. Use sample distances or classification quality to adjust position sizes.

  4. Add model train/test split for rolling optimization.

Conclusion

This strategy realizes market trend prediction using classical kNN algorithm and executes trend following trading based on prediction signals. It features adjustable parameters and controllable risks, providing effective automated trading solutions for users. Users can continuously improve strategy performance by optimizing technical indicator combinations, model hyperparameters and more.

Source
Pine
/*backtest
start: 2023-11-07 00:00:00
end: 2023-12-07 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sosacur01

//@version=5
Strategy parameters
Strategy parameters
Backtest Time Period
Filter Date Range of Backtest
Start Date
End Date
Long & Short Position
On/Off Long Postion
On/Off Short Postion
kNN-based Inputs
Start Date
Stop Date
Indicator
Short Period [1..n]
Long Period [2..n]
Base No. of Neighbours (K) [5..n]
Volatility Filter
Bar Threshold [2..5000]
MA Inputs
Use MA as Filter
MA Type
MA Length
MA Source
Risk Management Inputs
ATR Length
Long Position - Stop Loss - ATR Multiplier
Short Position - Stop Loss - ATR Multiplier
% of Equity at Risk
Comment
All comments (0)
No data
No data
  • 1
iPhone Download
Forums
PINE Language
© 2015 - ∞ INVENTOR PTE LTD (SG)