Chiến lược chéo giữa hai mức trung bình động

Tác giả:ChaoZhang, Ngày: 2024-01-12 14:59:18
Tags:

img

Tổng quan

Chiến lược giao thoa trung bình động kép là một chiến lược theo xu hướng điển hình. Nó sử dụng hai đường EMA với các giai đoạn khác nhau và đi dài khi EMA ngắn hơn vượt qua EMA dài hơn và đi ngắn khi giao thoa ngược lại xảy ra để nắm bắt sự đảo ngược xu hướng.

Nguyên tắc

Các chỉ số cốt lõi của chiến lược này là hai đường EMA, một là 30 giai đoạn và một là 60 giai đoạn.

emaLen1 = emaFuncOne(close, lenMA1)
emaLen2 = emaFuncTwo(close, lenMA2)  

Các tín hiệu giao dịch được tạo ra từ việc vượt qua hai đường EMA:

currentState = if emaLen2 > emaLen1
    0
else 
    1

previousState = if emaLastLen2 > emaLastLen1 
    0
else
    1

convergence = if currentState != previousState
    1  
else
    0

Khi EMA ngắn hơn vượt qua EMA dài hơn, currentState không bằng previousState, một tín hiệu chéo được kích hoạt, đi dài. Khi thời gian ngắn hơn EMA vượt qua dưới thời gian dài hơn EMA, currentState không bằng previousState, một tín hiệu chéo được kích hoạt, đi ngắn.

Phân tích lợi thế

Những lợi thế của chiến lược này là:

  1. Logic là đơn giản và trực quan, dễ hiểu và thực hiện
  2. Làm dịu biến động giá với EMA và lọc ra tiếng ồn thị trường
  3. Tự động theo dõi xu hướng, tránh bỏ lỡ giao dịch

Phân tích rủi ro

Ngoài ra còn có một số rủi ro với chiến lược này:

  1. Các tín hiệu chéo có thể bị chậm trễ và không thể bắt được sự đảo ngược kịp thời
  2. Các tín hiệu Whipsaw có thể xảy ra thường xuyên trong các thị trường dao động
  3. Chế độ điều chỉnh tham số kém có thể gây quá nhạy hoặc chậm trễ

Tối ưu hóa có thể được thực hiện bằng cách điều chỉnh các khoảng thời gian EMA hoặc thêm các bộ lọc.

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

Chiến lược này có thể được tối ưu hóa từ các khía cạnh sau:

  1. Kiểm tra các kết hợp thời gian EMA khác nhau
  2. Thêm bộ lọc âm lượng hoặc biến động để giảm tín hiệu sai
  3. Kết hợp các chỉ số khác như MACD để xác nhận xu hướng
  4. Tối ưu hóa quản lý tiền với dừng lỗ và lấy lợi nhuận

Kết luận

Chiến lược chéo trung bình di chuyển kép là một chiến lược đơn giản và thực tế theo xu hướng hệ thống nói chung. Nó đơn giản, dễ thực hiện và có thể tự động theo dõi xu hướng. Nhưng một số rủi ro như chậm trễ và tín hiệu sai tồn tại. Với điều chỉnh tham số và thêm bộ lọc, nó có thể được cải thiện hơn nữa để trở thành một trong những chiến lược giao dịch thuật toán cơ bản.


/*backtest
start: 2024-01-10 00:00:00
end: 2024-01-11 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=2
strategy("ParkerMAStrat", overlay=true)

lenMA1=input(title="Length 1", defval=30)
lenMA2=input(title="Length 2",  defval=60)

x = 0

checkLines(current, last) =>

    if current > last
        x = 1
    else
        x = 0
    x
    

//plot ema based on len1
emaFuncOne(src, time_period) =>

    alpha = 2 / (time_period + 1)
    // we have defined the alpha function above
    ema = 0.0
    // this is the initial declaration of ema, since we dont know the first ema we will declare it to 0.0 [as a decimal]
    ema := alpha * src + (1 - alpha) * nz(ema[1])
    // this returns the computed ema at the current time
    // notice the use of : (colon) symbol before =, it symbolises, that we are changing the value of ema,
    // since the ema was previously declared to 0
    // this is called mutable variale declaration in pine script
    ema
    // return ema from the function

emaLen1 = emaFuncOne(close, lenMA1)

    
plot(emaLen1, color=green, transp=0, linewidth=2)
// now we plot the _10_period_ema

//plot ema based on len2
emaFuncTwo(src, time_period) =>

    alpha = 2 / (time_period + 1)
    // we have defined the alpha function above
    ema = 0.0
    // this is the initial declaration of ema, since we dont know the first ema we will declare it to 0.0 [as a decimal]
    ema := alpha * src + (1 - alpha) * nz(ema[1])
    // this returns the computed ema at the current time
    // notice the use of : (colon) symbol before =, it symbolises, that we are changing the value of ema,
    // since the ema was previously declared to 0
    // this is called mutable variale declaration in pine script
    ema
    // return ema from the function

//plot ema based on len2
emaFuncOneLast(src, time_period) =>

    alpha = 2 / (time_period + 1)
    // we have defined the alpha function above
    ema = 0.0
    // this is the initial declaration of ema, since we dont know the first ema we will declare it to 0.0 [as a decimal]
    ema := alpha * src + (1 - alpha) * nz(ema[0])
    // this returns the computed ema at the current time
    // notice the use of : (colon) symbol before =, it symbolises, that we are changing the value of ema,
    // since the ema was previously declared to 0
    // this is called mutable variale declaration in pine script
    ema
    // return ema from the function

//plot ema based on len2
emaFuncTwoLast(src, time_period) =>

    alpha = 2 / (time_period + 1)
    // we have defined the alpha function above
    ema = 0.0
    // this is the initial declaration of ema, since we dont know the first ema we will declare it to 0.0 [as a decimal]
    ema := alpha * src + (1 - alpha) * nz(ema[0])
    // this returns the computed ema at the current time
    // notice the use of : (colon) symbol before =, it symbolises, that we are changing the value of ema,
    // since the ema was previously declared to 0
    // this is called mutable variale declaration in pine script
    ema
    // return ema from the function



emaLastLen1 = emaFuncOneLast(close, lenMA1)
emaLastLen2 = emaFuncTwoLast(close, lenMA2)
emaLen2 = emaFuncTwo(close, lenMA2)

    
plot(emaLen2, color=red, transp=30, linewidth=2)
// now we plot the _10_period_ema

//now we compare the two and when green crosses red we buy/sell (line1 vs line2)

previousState = if emaLastLen2 > emaLastLen1
    0
else
    1

currentState = if emaLen2 > emaLen1
    0
else
    1

convergence = if currentState != previousState
    1
else
    0

    
lineCheck = if convergence == 1 
    checkLines(currentState, previousState)
    
if lineCheck == 1
    strategy.entry("Long", strategy.long)
else
    strategy.entry("Short", strategy.short)


Thêm nữa