Chiến lược định lượng theo dõi xu hướng sức mạnh MA

Tác giả:ChaoZhang
Tags:

img

Tổng quan

Chiến lược này đánh giá sức mạnh của xu hướng thị trường bằng cách tính toán sức mạnh của các đường trung bình động (MA) trên các khung thời gian khác nhau và theo dõi xu hướng tương ứng. Khi các đường trung bình ngắn hạn liên tục tăng lên, chúng đạt điểm cao hơn và tạo thành một chỉ số MA Strength. Khi chỉ số này vượt qua đường trung bình dài hạn của chính nó, các tín hiệu mua được tạo ra. Chiến lược cho phép người dùng cấu hình các sự kết hợp MA trên ngắn hạn và dài hạn để theo dõi xu hướng trong các chu kỳ tùy chỉnh.

Chiến lược logic

  1. Tính toán các MA 5 ngày, 10 ngày, 20 ngày v.v. Xác định nếu giá vượt quá mỗi MA, điểm điểm breakout tích lũy thành chỉ số MA Strength.

Phân tích lợi thế

  1. Theo dõi chu kỳ tùy chỉnh. Điều chỉnh các tham số MA ngắn hạn có thể nắm bắt xu hướng trên các cấp khác nhau; điều chỉnh các tham số MA dài hạn kiểm soát tốc độ thoát. Người dùng có thể cấu hình chu kỳ dựa trên điều kiện thị trường.
  2. Long chỉ tránh sai giết và theo dõi xu hướng tăng liên tục.

Phân tích rủi ro

  1. Rủi ro khôi phục tồn tại khi MA ngắn hạn vượt qua dưới MA dài hạn, có thể là tổn thất lớn.
  2. Rủi ro đảo ngược là không thể tránh khỏi trong xu hướng dài hạn, đòi hỏi phải thoát khỏi lỗ dừng kịp thời.

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

  1. Các tín hiệu bộ lọc bổ sung với nhiều chỉ số hơn.
  2. Thêm các phương pháp dừng lỗ. Di chuyển dừng lỗ, dừng lỗ đường cong có thể làm giảm tổn thất khôi phục. Xem xét phương pháp lấy lợi nhuận để khóa lợi nhuận và tránh đảo ngược.
  3. Xem xét các sản phẩm tương lai và ngoại hối. MA breakouts phù hợp hơn với các sản phẩm xu hướng. Đánh giá sự ổn định của các tham số trên các sản phẩm để chọn tối ưu.

Kết luận

Chiến lược này đánh giá xu hướng giá bằng cách tính toán chỉ số sức mạnh MA và sử dụng đường chéo MA làm nguồn tín hiệu để theo dõi xu hướng. Ưu điểm của nó nằm trong việc xác định chính xác sức mạnh xu hướng để có độ tin cậy. Những rủi ro chính đến từ sự đảo ngược xu hướng và điều chỉnh tham số. Bằng cách tối ưu hóa độ chính xác tín hiệu, thêm stop loss, chọn các sản phẩm phù hợp, có thể có được lợi nhuận tốt.


/*backtest
start: 2023-12-19 00:00:00
end: 2024-01-18 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed

//@version=4
strategy("MA Strength Strategy", overlay=false, initial_capital = 20000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01)
MAType = input(title="Moving Average Type", defval="ema", options=["ema", "sma", "hma", "rma", "vwma", "wma"])
LookbackPeriod = input(10, step=10)

IndexMAType = input(title="Moving Average Type", defval="hma", options=["ema", "sma", "hma", "rma", "vwma", "wma"])
IndexMAPeriod = input(200, step=10)

considerTrendDirection = input(true)
considerTrendDirectionForExit = input(true)
offset = input(1, step=1)
tradeDirection = input(title="Trade Direction", defval=strategy.direction.long, options=[strategy.direction.all, strategy.direction.long, strategy.direction.short])
i_startTime = input(defval = timestamp("01 Jan 2010 00:00 +0000"), title = "Start Time", type = input.time)
i_endTime = input(defval = timestamp("01 Jan 2099 00:00 +0000"), title = "End Time", type = input.time)
inDateRange = true

f_getMovingAverage(source, MAType, length)=>
    ma = sma(source, length)
    if(MAType == "ema")
        ma := ema(source,length)
    if(MAType == "hma")
        ma := hma(source,length)
    if(MAType == "rma")
        ma := rma(source,length)
    if(MAType == "vwma")
        ma := vwma(source,length)
    if(MAType == "wma")
        ma := wma(source,length)
    ma
    

f_getMaAlignment(MAType, includePartiallyAligned)=>
    ma5 = f_getMovingAverage(close,MAType,5)
    ma10 = f_getMovingAverage(close,MAType,10)
    ma20 = f_getMovingAverage(close,MAType,20)
    ma30 = f_getMovingAverage(close,MAType,30)
    ma50 = f_getMovingAverage(close,MAType,50)
    ma100 = f_getMovingAverage(close,MAType,100)
    ma200 = f_getMovingAverage(close,MAType,200)

    upwardScore = 0.0
    upwardScore := close > ma5? upwardScore+1.10:upwardScore
    upwardScore := ma5 > ma10? upwardScore+1.10:upwardScore
    upwardScore := ma10 > ma20? upwardScore+1.10:upwardScore
    upwardScore := ma20 > ma30? upwardScore+1.10:upwardScore
    upwardScore := ma30 > ma50? upwardScore+1.15:upwardScore
    upwardScore := ma50 > ma100? upwardScore+1.20:upwardScore
    upwardScore := ma100 > ma200? upwardScore+1.25:upwardScore
    
    upwards = close > ma5 and ma5 > ma10 and ma10 > ma20 and ma20 > ma30 and ma30 > ma50 and ma50 > ma100 and ma100 > ma200
    downwards = close < ma5 and ma5 < ma10 and ma10 < ma20 and ma20 < ma30 and ma30 < ma50 and ma50 < ma100 and ma100 < ma200
    trendStrength = upwards?1:downwards?-1:includePartiallyAligned ? (upwardScore > 6? 0.5: upwardScore < 2?-0.5:upwardScore>4?0.25:-0.25) : 0
    [trendStrength, upwardScore]
    
includePartiallyAligned = true
[trendStrength, upwardScore] = f_getMaAlignment(MAType, includePartiallyAligned)

upwardSum = sum(upwardScore, LookbackPeriod)

indexSma = f_getMovingAverage(upwardSum,IndexMAType,IndexMAPeriod)

plot(upwardSum, title="Moving Average Strength", color=color.green, linewidth=2, style=plot.style_linebr)
plot(indexSma, title="Strength MA", color=color.red, linewidth=1, style=plot.style_linebr)
buyCondition = crossover(upwardSum,indexSma) and (upwardSum > upwardSum[offset] or not considerTrendDirection) 
sellCondition = crossunder(upwardSum,indexSma) and (upwardSum < upwardSum[offset]  or not considerTrendDirection)

exitBuyCondition = crossunder(upwardSum,indexSma)
exitSellCondition = crossover(upwardSum,indexSma) 
strategy.risk.allow_entry_in(tradeDirection)
strategy.entry("Buy", strategy.long, when= inDateRange and buyCondition, oca_name="oca_buy")
strategy.close("Buy", when = considerTrendDirectionForExit? sellCondition : exitBuyCondition)
strategy.entry("Sell", strategy.short, when= inDateRange and sellCondition, oca_name="oca_sell")
strategy.close( "Sell", when = considerTrendDirectionForExit? buyCondition : exitSellCondition)


Thêm nữa