
この戦略は,機械学習アルゴリズム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)