
이 전략은 기계 학습 알고리즘 K 근접 ((KNN) 에 기반한 자기 적응형 변수화 트렌드 추적 시스템이다. 이 전략은 KNN 알고리즘을 통해 동적으로 트렌드 추적 파라미터를 조정하고, 이동 평균과 결합하여 거래 신호를 생성한다. 이 시스템은 시장 환경의 변화에 따라 전략 파라미터를 자동으로 조정하여 전략의 적응성과 안정성을 향상시킬 수 있다. 이 전략은 기계 학습 방법을 사용하여 전통적인 트렌드 추적 전략을 최적화하고, 양적 투자 분야의 기술과 새로운 창조를 결합한다.
전략의 핵심 원칙은 KNN 알고리즘을 사용하여 역사적 가격 데이터를 분석하여 현재 시장 상태와 역사적 데이터의 유사성을 계산하여 가격 움직임을 예측하는 것입니다. 구체적인 구현 단계는 다음과 같습니다:
이 전략은 트렌드 추적 거래에 KNN 알고리즘을 혁신적으로 적용하고, 기계 학습 방법을 통해 전통적인 기술 분석 전략을 최적화한다. 전략은 강력한 적응력과 유연성을 가지고 있으며, 시장 환경의 동성에 따라 매개 변수를 조정할 수 있다. 계산 복잡성과 매개 변수 감수성이 높은 위험이 있음에도 불구하고, 합리적인 최적화 및 위험 제어 조치를 통해 전략은 여전히 좋은 응용 가치를 가지고 있다. 투자자는 실제 응용에서 시장 특성에 따라 매개 변수를 조정하고, 다른 분석 방법과 결합하여 거래 결정을 할 것을 권장한다.
/*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)