Thuật toán K-nearest neighbor đa chiều và chiến lược giao dịch phân tích giá khối lượng mẫu nến

SMA KNN RSI VOL MA SD
Ngày tạo: 2025-01-17 16:10:07 sửa đổi lần cuối: 2025-01-17 16:10:07
sao chép: 0 Số nhấp chuột: 436
1
tập trung vào
1617
Người theo dõi

Thuật toán K-nearest neighbor đa chiều và chiến lược giao dịch phân tích giá khối lượng mẫu nến

Tổng quan

Chiến lược này là một hệ thống giao dịch toàn diện kết hợp thuật toán máy học K-nearest neighbor (KNN), nhận dạng mẫu hình nến và phân tích khối lượng. Chiến lược này hình thành khuôn khổ phân tích ba chiều cho thị trường thông qua các phương pháp phân tích đa chiều, bao gồm kênh trung bình động, xác minh ngưỡng khối lượng và thống kê xác suất, từ đó nắm bắt các cơ hội giao dịch tiềm năng.

Nguyên tắc chiến lược

Logic cốt lõi của chiến lược này dựa trên các yếu tố chính sau:

  1. Sử dụng đường trung bình động (SMA) và độ lệch chuẩn để xây dựng các kênh giá nhằm xác định các vùng quá mua và quá bán
  2. Xác định chín mô hình nến cổ điển thông qua các điều kiện được xác định theo chương trình, bao gồm búa, sao băng, nhấn chìm, v.v.
  3. Giới thiệu thuật toán KNN để tìm hiểu xu hướng giá lịch sử và dự đoán xu hướng giá có thể xảy ra trong tương lai
  4. Việc sử dụng khối lượng giao dịch làm chỉ báo xác nhận tín hiệu yêu cầu khối lượng giao dịch phải cao hơn ngưỡng đã đặt khi tín hiệu được kích hoạt
  5. Tính toán phân phối xác suất của sự tăng và giảm và sử dụng nó như một trong các điều kiện lọc tín hiệu

Lợi thế chiến lược

  1. Cơ chế xác nhận tín hiệu đa cấp cải thiện đáng kể độ tin cậy của giao dịch
  2. Việc giới thiệu thuật toán KNN cung cấp góc nhìn máy học cho phân tích kỹ thuật truyền thống
  3. Cơ chế xác minh khối lượng có hiệu quả tránh đột phá sai
  4. Bản vẽ động của các đường hỗ trợ và kháng cự giúp xác định các mức giá quan trọng
  5. Hệ thống cảnh báo toàn diện đảm bảo bạn sẽ không bỏ lỡ các cơ hội giao dịch quan trọng
  6. Các thông số chiến lược có thể điều chỉnh cao và có thể thích ứng với các môi trường thị trường khác nhau

Rủi ro chiến lược

  1. Thuật toán KNN có thể chậm trễ trong thị trường biến động
  2. Điều kiện lọc tín hiệu quá mức có thể dẫn đến việc bỏ lỡ một số cơ hội giao dịch
  3. Ngưỡng khối lượng cố định có thể cần được điều chỉnh động trong các giai đoạn khác nhau
  4. Quá nhiều tín hiệu sai có thể được tạo ra trong các pha đi ngang Khuyến khích:
  • Điều chỉnh các tham số thuật toán một cách động
  • Giới thiệu cơ chế xác định môi trường thị trường
  • Đặt giới hạn tổn thất tối đa
  • Thiết lập hệ thống quản lý kho

Hướng tối ưu hóa chiến lược

  1. Giới thiệu cơ chế điều chỉnh tham số thích ứng để cho phép chiến lược tự động điều chỉnh các tham số theo điều kiện thị trường
  2. Tích hợp các thuật toán học sâu để cải thiện độ chính xác của dự đoán
  3. Thêm nhiều chỉ số vi mô thị trường
  4. Tối ưu hóa phương pháp tính toán động ngưỡng khối lượng giao dịch
  5. Thiết lập hệ thống kiểm soát rủi ro hoàn thiện hơn

Tóm tắt

Chiến lược này xây dựng một hệ thống giao dịch mạnh mẽ bằng cách kết hợp phân tích kỹ thuật truyền thống với các phương pháp học máy hiện đại. Khung phân tích đa chiều và cơ chế xác nhận tín hiệu nghiêm ngặt của chiến lược cung cấp cơ sở đáng tin cậy cho các quyết định giao dịch. Thông qua việc tối ưu hóa liên tục và kiểm soát rủi ro, chiến lược này được kỳ vọng sẽ duy trì hiệu suất ổn định trong nhiều môi trường thị trường khác nhau.

Mã nguồn chiến lược
/*backtest
start: 2024-01-17 00:00:00
end: 2025-01-16 00:00:00
period: 2d
basePeriod: 2d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":49999}]
*/

//@version=6
strategy("Candle Pattern Analyzer with Volume", overlay=true)

// Input parameters
length = input.int(20, "Channel Length", minval=1)
mult = input.float(2.0, "Volatility Multiplier", minval=0.1)
candleLength = input.int(5, "Candle Length", minval=1)
k = input.int(5, "KNN Neighbors", minval=1)
volumeThreshold = input.int(100000, "Volume Threshold", minval=1)

// Calculate channel
basis = ta.sma(close, length)
dev = mult * ta.stdev(close, length)
upper = basis + dev
lower = basis - dev

// Plot channel
plot(basis, color=color.blue)
plot(upper, color=color.green)
plot(lower, color=color.red)

// Identify candle patterns
isBullish = close > open
isBearish = close < open

// Pre-calculate SMAs
smaLow = ta.sma(low, candleLength)
smaHigh = ta.sma(high, candleLength)
smaClose = ta.sma(close, candleLength)

// Hammer pattern
isHammer = isBullish and 
           low < smaLow and 
           close > smaClose and 
           (close - low) / (high - low) > 0.6 and
           low < low[1]

// Shooting Star pattern
isShootingStar = isBearish and 
                 high > smaHigh and 
                 close < smaClose and 
                 (high - close) / (high - low) > 0.6 and
                 high > high[1]

// Inverse Hammer pattern
isInverseHammer = isBullish and 
                   high > smaHigh and 
                   close < smaClose and 
                   (high - close) / (high - low) > 0.6 and
                   high > high[1]

// Bullish Engulfing pattern
isBullishEngulfing = isBullish and 
                      close > high[1] and 
                      open < low[1]

// Bearish Engulfing pattern
isBearishEngulfing = isBearish and 
                      close < low[1] and 
                      open > high[1]

// Morning Star pattern
isMorningStar = isBullish and close[2] < open[2] and close[1] < open[1] and  close > open[1]

// Evening Star pattern
isEveningStar = isBearish and  close[2] > open[2] and  close[1] > open[1] and  close < open[1]

// Three Black Crows pattern
isThreeBlackCrows = isBearish and 
                     close < close[1] and 
                     close[1] < close[2] and 
                     close[2] < close[3]

// Three White Soldiers pattern
isThreeWhiteSoldiers = isBullish and close > close[1] and  close[1] > close[2] and  close[2] > close[3]

// Compare previous candles
prevCandleUp = close[1] > open[1]
prevCandleDown = close[1] < open[1]

// Calculate probability
probUp = ta.sma(close > open ? 1 : 0, candleLength) / candleLength
probDown = ta.sma(close < open ? 1 : 0, candleLength) / candleLength

// Generate signals
buySignal = isHammer and prevCandleDown and probUp > probDown and volume > volumeThreshold
sellSignal = isShootingStar and prevCandleUp and probDown > probUp and volume > volumeThreshold

// Highlight patterns
color candleColor = na
if (isHammer)
    candleColor := color.green
    label.new(bar_index, high, "Hammer", color=color.green, style=label.style_label_up)

else if (isShootingStar)
    candleColor := color.red
    label.new(bar_index, low, "Shooting Star", color=color.red, style=label.style_label_down)
else if (isInverseHammer)
    candleColor := color.blue
    label.new(bar_index, high, "Inverse Hammer", color=color.blue, style=label.style_label_up)
else if (isBullishEngulfing)
    candleColor := color.yellow
    label.new(bar_index, high, "Bullish Engulfing", color=color.yellow, style=label.style_label_up)
else if (isBearishEngulfing)
    candleColor := color.purple
    label.new(bar_index, low, "Bearish Engulfing", color=color.purple, style=label.style_label_down)

else if (isMorningStar)
    candleColor := color.orange
    label.new(bar_index, high, "Morning Star", color=color.orange, style=label.style_label_up)

else if (isEveningStar)
    candleColor := color.new(color.red, 80)
    label.new(bar_index, low, "Evening Star", color=color.new(color.red, 80), style=label.style_label_down)

else if (isThreeBlackCrows)
    candleColor := color.black
    label.new(bar_index, low, "Three Black Crows", color=color.black, style=label.style_label_down)

else if (isThreeWhiteSoldiers)
    candleColor := color.white
    label.new(bar_index, high, "Three White Soldiers", color=color.white, style=label.style_label_up)


// Plot candles
barcolor(candleColor)

// KNN algorithm
var float[] knnData = array.new_float(k, na)
var float[] knnLabels = array.new_float(k, na) // Create an array to store KNN labels
array.set(knnLabels, 0, 1.0) // Label for "up" movement

// Shift KNN dataset to make room for new data point
for i = 1 to k-1
    array.set(knnData, i, array.get(knnData, i-1))
    array.set(knnLabels, i, array.get(knnLabels, i-1))

// Predict next movement using KNN algorithm
float prediction = 0.0
for i = 0 to k-1
    float distance = math.abs(close - array.get(knnData, i))
    prediction += array.get(knnLabels, i) / distance

prediction /= k

// Plot prediction
// line.new(bar_index, close, bar_index + 1, prediction, color=color.purple)

// Plot resistance and support lines
float resistance = ta.sma(high, length)
float support = ta.sma(low, length)
// line.new(bar_index, resistance, bar_index + 1, resistance, color=color.green, style=line.style_dashed)
// line.new(bar_index, support, bar_index + 1, support, color=color.red, style=line.style_dashed)

// Plot buy and sell signals with prices
if (buySignal)
    // label.new(bar_index, low, "Buy at " + str.tostring(low), color=color.green, style=label.style_label_up)
    strategy.entry("Buy", strategy.long, comment="Buy at " + str.tostring(low))
if (sellSignal)
    // label.new(bar_index, high, "Sell at " + str.tostring(high), color=color.red, style=label.style_label_down)
    strategy.entry("Sell", strategy.short, comment="Sell at " + str.tostring(high))

// Create alerts
alertcondition(buySignal, title="Buy Signal", message="Buy signal generated!")
alertcondition(sellSignal, title="Sell Signal", message="Sell signal generated!")