
Chiến lược này là một hệ thống theo dõi xu hướng theo tham số tự thích ứng dựa trên thuật toán học máy K gần kề (KNN). Chiến lược này điều chỉnh động các tham số theo dõi xu hướng thông qua thuật toán KNN, kết hợp với việc tạo tín hiệu giao dịch trên đường trung bình di chuyển. Hệ thống có thể tự động điều chỉnh tham số chiến lược theo sự thay đổi của môi trường thị trường, nâng cao tính thích ứng và ổn định của chiến lược.
Nguyên tắc cốt lõi của chiến lược là sử dụng thuật toán KNN để phân tích dữ liệu giá lịch sử và dự đoán xu hướng giá bằng cách tính toán tình trạng thị trường hiện tại tương tự như dữ liệu lịch sử. Các bước thực hiện cụ thể như sau:
Chiến lược này áp dụng thuật toán KNN một cách sáng tạo cho giao dịch theo dõi xu hướng, tối ưu hóa chiến lược phân tích kỹ thuật truyền thống bằng phương pháp học máy. Chiến lược có khả năng thích ứng và linh hoạt cao, có thể điều chỉnh tham số theo động lực của môi trường thị trường. Mặc dù có rủi ro như tính toán phức tạp và nhạy cảm với tham số, chiến lược vẫn có giá trị ứng dụng tốt hơn thông qua các biện pháp tối ưu hóa và kiểm soát rủi ro hợp lý.
/*backtest
start: 2024-10-01 00:00:00
end: 2024-10-31 23:59:59
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy("Trend Following Strategy with KNN", overlay=true,commission_value=0.03,currency='USD', commission_type=strategy.commission.percent,default_qty_type=strategy.cash)
// Input parameters
k = input.int(5, title="K (Number of Neighbors)", minval=1) // Number of neighbors for KNN algorithm
window_size = input.int(20, title="Window Size", minval=1) // Window size for feature vector calculation
ma_length = input.int(50, title="MA Length", minval=1) // Length of the moving average
// Calculate moving average
ma = ta.sma(close, ma_length)
// Initialize variables
var float[] features = na
var float[] distances = na
var int[] nearest_neighbors = na
if bar_index >= window_size - 1 // Ensure there is enough historical data
features := array.new_float(0) // Keep only the current window data
for i = 0 to window_size - 1
array.push(features, close[i])
// Calculate distances
distances := array.new_float(0) // Clear the array for each calculation
for i = 0 to window_size - 1 // Calculate the distance between the current price and all prices in the window
var float distance = 0.0
for j = 0 to window_size - 1
distance += math.pow(close[j] - array.get(features, j), 2)
distance := math.sqrt(distance)
array.push(distances, distance)
// Find the nearest neighbors
if array.size(distances) > 0 and array.size(distances) >= k
nearest_neighbors := array.new_int(0)
for i = 0 to k - 1
var int min_index = -1
var float min_distance = na
for j = 0 to array.size(distances) - 1
if na(min_distance) or array.get(distances, j) < min_distance
min_index := j
min_distance := array.get(distances, j)
if min_index != -1
array.push(nearest_neighbors, min_index)
array.remove(distances, min_index) // Remove the processed neighbor
// Calculate the average price change of the neighbors
var float average_change = 0.0
if array.size(nearest_neighbors) > 0
for i = 0 to array.size(nearest_neighbors) - 1
var int index = array.get(nearest_neighbors, i)
// Ensure index + 1 is within range
if index + 1 < bar_index
average_change += (close[index] - close[index + 1])
average_change := average_change / array.size(nearest_neighbors)
// Generate trading signals
if average_change > 0 and close > ma
strategy.entry("Long", strategy.long)
else if average_change < 0 and close < ma
strategy.entry("Short", strategy.short)