Chiến lược xu hướng chéo trung bình động kép

Tác giả:ChaoZhang, Ngày: 2023-11-22 17:29:04
Tags:

img

Tổng quan

Chiến lược xu hướng chéo trung bình di chuyển kép là một chiến lược theo xu hướng tạo ra tín hiệu mua và bán khi đường trung bình di chuyển nhanh và chậm giao nhau. Nó kết hợp nhiều chỉ số như MACD và RSI để xác định hướng xu hướng và có khả năng theo dõi xu hướng mạnh mẽ.

Chiến lược logic

Chiến lược chủ yếu sử dụng các chỉ số sau đây để đánh giá:

  1. Các đường trung bình di chuyển nhanh và chậm: chữ thập vàng cho tín hiệu mua, chữ thập chết cho tín hiệu bán.

  2. MACD: Đường MACD trên đường tín hiệu và đường MACD tăng thấp nhất cho tín hiệu tăng.

  3. RSI: RSI trên 50 cho tăng, dưới 50 cho giảm.

  4. Awesome Oscillator (AO): AO vượt trên đường 0 để mua, vượt dưới để bán.

  5. Ba đường trung bình di chuyển hàng ngày: thời gian ngắn hơn MA hàng ngày vượt qua thời gian dài hơn MA hàng ngày như tín hiệu mua.

Chiến lược này kết hợp nhiều khung thời gian và chỉ số để tạo ra logic mua và bán. Nó tạo ra các lệnh mua khi nhiều chỉ số cho thấy tín hiệu tăng cùng một lúc và bán lệnh khi các tín hiệu giảm xuất hiện, để theo dõi xu hướng.

Phân tích lợi thế

Chiến lược có những lợi thế sau:

  1. Sự kết hợp nhiều chỉ số làm giảm tín hiệu sai và cải thiện độ chính xác.

  2. Kết hợp nhiều khung thời gian xác định hướng xu hướng lớn hơn.

  3. Điều chỉnh tham số cung cấp lợi nhuận tốt.

  4. Sử dụng stop loss di chuyển để kiểm soát rủi ro và giới hạn lỗ.

  5. Theo dõi xu hướng tự động mà không cần can thiệp bằng tay, giảm chi phí.

Phân tích rủi ro

Nó cũng có một số rủi ro:

  1. Có thể xảy ra nhiều biến động hơn ở các thị trường giới hạn trong phạm vi.

  2. Các sự kiện thiên nga đen có thể gây ra sự rút lui mạnh.

  3. Logic mua / bán phức tạp dựa trên dữ liệu lịch sử lớn để tìm các thông số tối ưu.

  4. Thiết lập stop loss không phù hợp dẫn đến thoát sớm.

Hướng dẫn tối ưu hóa

Chiến lược có thể được cải thiện từ các khía cạnh sau:

  1. Kiểm tra nhiều kết hợp chỉ số hơn cho các tín hiệu ổn định và chính xác hơn, như chỉ số biến động, OBV vv

  2. Tối ưu hóa các thông số chỉ số bằng máy học và thuật toán di truyền để giảm quá mức giao dịch.

  3. Đưa ra các kỹ thuật tập hợp mô hình để tích hợp các tín hiệu từ nhiều mô hình chiến lược độc lập, cải thiện độ bền.

  4. Tham gia giao dịch trong khung thời gian cao hơn, thoát ra trong khung thời gian thấp hơn.

  5. Xây dựng mô-đun kiểm soát rủi ro định lượng với giới hạn nghiêm ngặt về tỷ lệ dừng lỗ mỗi giao dịch, rút tiền tối đa vv.

Tóm lại

Chiến lược xu hướng chéo trung bình di chuyển kép sử dụng chéo MA nhanh và chậm như các tín hiệu giao dịch, cùng với MACD, RSI để đánh giá hướng xu hướng cho việc theo dõi xu hướng tự động.


/*backtest
start: 2023-10-22 00:00:00
end: 2023-11-21 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy('SteffVans', shorttitle='SteffVans strategy', overlay=true, process_orders_on_close = true)

// Input settings
macd_fast_length = input(12)
macd_slow_length = input(26)
macd_signal_length = input(9)

// Calculate MACD values
[macd_line, signal_line, _] = ta.macd(close, macd_fast_length, macd_slow_length, macd_signal_length)
mg = ta.lowest(signal_line, 30) >= -0

// RSI
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

rsiLengthInput = input.int(14, minval=1)
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
RSI = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))


//  AO
AO = ta.sma((high + low) / 2, 5) - ta.sma((high + low) / 2, 34)
crossaosell = AO < AO[1] and AO[1] < AO[2] and AO[2] > AO[3]  and ta.lowest(low,3)

// Uptrend sma
len1 = input.int(5, minval=1)
len2 = input.int(10, minval=1)
len3 = input.int(20, minval=1)
src = input(close)

out1 = ta.sma(src, len1)
out2 = ta.sma(src, len2)
out3 = ta.sma(src, len3)



// Timeframe 
macdl60 = request.security(syminfo.tickerid, "60", signal_line,lookahead = barmerge.lookahead_on)
ao = request.security(syminfo.tickerid, "60", AO,lookahead = barmerge.lookahead_on)
rsi = request.security(syminfo.tickerid, "60", RSI,lookahead = barmerge.lookahead_on)
good = request.security(syminfo.tickerid, "60", mg,lookahead = barmerge.lookahead_on)
bad = request.security(syminfo.tickerid, "60", crossaosell,lookahead = barmerge.lookahead_on)

ma1 = request.security(syminfo.tickerid, "D", out1,lookahead = barmerge.lookahead_on)
ma2 = request.security(syminfo.tickerid, "D", out2, lookahead = barmerge.lookahead_on)
ma3 = request.security(syminfo.tickerid, "D", out3, lookahead = barmerge.lookahead_on)






// Kriteria BUY and SELL
uptrend1 =  request.security(syminfo.tickerid, "D", close,lookahead = barmerge.lookahead_on) > ma1 and ma1 > ma3 and ma2 > ma3
uptrend2 = ta.lowest(ma1,12) > ta.lowest(ma3,12) and ta.lowest(ma2,12) > ta.lowest(ma3,12) 


 

// Triger BUY and SELL 
cross1 = ao > ao[1] and ao[1] < ao[2] and ao > 0 and good and rsi >= 60 and uptrend1
cross2 = ao > 0 and ao[1] < 0 and good and rsi >=50 and uptrend1
cross3 =  ao > 0 and ao[1] < 0 and not good and uptrend2 and uptrend1
cross4 =  ao > ao[1] and ao[1] > ao[2] and ao[2] < ao[3] and ao[3] < ao[4]  and not good and uptrend2 and uptrend1

s1 = ao < ao[1] and ao[1] < ao[2] and ao[2] < ao[3] and ao > 0 and rsi < 50 and request.security(syminfo.tickerid, "D", close,lookahead = barmerge.lookahead_on) < ma1
s2 =  ao < 0 and ao < ao[2] and rsi < 50 and request.security(syminfo.tickerid, "D", close,lookahead = barmerge.lookahead_on) < ma1 

// Variabel Buy dan Sell
buySignal = false
sellSignal = false

// Syarat masuk Buy
buyCondition =  cross1 or cross2 or cross3 or cross4
if buyCondition
    buySignal := true

// Syarat masuk Sell
sellCondition = s1 or s2
if sellCondition
    sellSignal := true

// Reset sinyal jika ada sinyal berulang
if buySignal and sellSignal
    sellSignal := false
if sellSignal and buySignal
    buySignal := false

// Logika perdagangan
if buySignal
    strategy.entry("Buy", strategy.long, comment = "BUY")
if sellSignal
    strategy.close("Buy")


plotshape(cross1,title = "Stefkuy1", style = shape.labelup, location = location.belowbar, color = color.green,text = "1", textcolor = color.white,size = size.small)
plotshape(cross2,title = "Stefkuy2", style = shape.labelup, location = location.belowbar, color = color.green, text = "2", textcolor= color.white, size = size.small)
plotshape(cross3,title = "StefVan1", style = shape.labelup, location = location.belowbar, color = color.rgb(0, 153, 255), text = "3", textcolor= color.white,size = size.small)
plotshape(cross4,title = "StefVan2", style = shape.labelup, location = location.belowbar, color = color.rgb(0, 153, 255), text = "4", textcolor= color.white,size = size.small)


Thêm nữa