Chiến lược chéo trung bình chuyển động nhiều khung thời gian

Tác giả:ChaoZhang, Ngày: 2024-02-19 15:41:29
Tags:

img

Tổng quan

Chiến lược chéo trung bình chuyển động nhiều khung thời gian là một chiến lược giao dịch thuật toán sử dụng các tín hiệu chéo trung bình chuyển động giữa các khoảng thời gian khác nhau để xác định hướng xu hướng.

Chiến lược logic

Chiến lược này tính toán chỉ số CCI trong các khoảng thời gian khác nhau để xác định hướng xu hướng thị trường, kết hợp với các tín hiệu MACD để xác định vị trí thập tự vàng và thập tự chết, và cuối cùng sử dụng chỉ số ATR để thiết lập mức dừng lỗ / lấy lợi nhuận, để mua thấp và bán cao.

Cụ thể, nó đầu tiên tính toán CCI 20 giai đoạn để đánh giá xu hướng tăng hoặc giảm. Sau đó nó kiểm tra xem các đường MACD có vượt qua để xác định các tín hiệu giao dịch hay không. Tiếp theo, ATR được sử dụng để tạo ra các điểm dừng để khóa lợi nhuận. Cuối cùng, tất cả các tín hiệu được hợp nhất để tạo ra các tín hiệu nhập và xuất.

Ưu điểm

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

    Sự kết hợp của CCI, MACD và ATR làm tăng độ tin cậy của tín hiệu giao dịch bằng cách đánh giá chung xu hướng, động lực và biến động.

  2. Phân tích nhiều khung thời gian nắm bắt nhịp thị trường

    CCI thời gian dài hơn nắm bắt xu hướng tổng thể, trong khi MACD tần suất cao hơn xác định các bước ngoặt địa phương, cho phép chiến lược tận dụng những biến động lớn của thị trường.

  3. Điều khiển ATR dừng phía sau rủi ro hiệu quả

    Stop loss dựa trên ATR có thể thích nghi với sự biến động của thị trường, trong khi tính năng kéo theo của nó tiếp tục khóa lợi nhuận khi thị trường di chuyển thuận lợi.

Rủi ro

  1. Không gian tối ưu hóa hạn chế

    Hầu hết các tham số có không gian điều chỉnh hẹp, dễ dàng đạt đến nút thắt hiệu suất.

  2. Tăng tải tính toán

    Nhiều chỉ số chạy cùng nhau có thể làm tăng tải trọng tính toán, gây ra sự chậm trễ trong giao dịch tần số cao.

  3. Các tín hiệu thường xuyên, kiểm soát rủi ro hạn chế

    Các tín hiệu có thể thường xuyên, trong khi kiểm soát rủi ro chủ yếu dựa trên ATR trailing stop, có giới hạn chống lại các động thái cực đoan.

Những cải tiến

  1. Áp dụng máy học để điều chỉnh tham số hiệu quả hơn

    Tối ưu hóa Bayesian, thuật toán di truyền vv có thể cho phép điều chỉnh tham số thông minh và hiệu quả hơn.

  2. Thêm các chỉ số chức năng để cải thiện khả năng thích nghi

    Kết hợp các chỉ số khác như biến động, khối lượng, tâm lý có thể làm cho chiến lược mạnh mẽ hơn và linh hoạt hơn.

  3. Tăng cường quản lý rủi ro để ổn định hơn

    Các quy tắc dừng lỗ khoa học hơn có thể được thiết kế, và các mô-đun khác như kích thước vị trí có thể giúp bảo vệ chống lại các sự kiện cực đoan.

Kết luận

Chiến lược Crossover trung bình chuyển động đa khung thời gian sử dụng sức mạnh của CCI, MACD và ATR để đạt được khả năng nắm bắt xu hướng đáng tin cậy và kiểm soát rủi ro hiệu quả. Nó tính toán xu hướng, động lực và biến động để tạo ra các tín hiệu chính xác, nắm bắt nhịp điệu thị trường và quản lý rủi ro. Mặc dù một số khía cạnh như điều chỉnh tham số, tải trọng tính toán và kiểm soát rủi ro có thể được cải thiện hơn nữa, nhưng nó vẫn là một hệ thống giao dịch thuật toán vững chắc. Với một số cải tiến bằng cách sử dụng máy học, nhiều chỉ số và quản lý rủi ro tốt hơn, hiệu suất của nó có thể đạt được mức mới.


/*backtest
start: 2024-01-01 00:00:00
end: 2024-01-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy('smplondonclinic Strategy', shorttitle='SMPLC Strategy', overlay=true, pyramiding = 0, process_orders_on_close = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)

direction   = input.string(title='Entry Direction', defval='Long', options=['Long', 'Short', 'Both'],group = "Strategy Entry Direction")

TPPerc = input.float(title='Take Profit (%)', minval=0.0, step=0.1, defval=0.5, group='Strategy TP & SL')
SLPerc = input.float(title='Stop Loss (%)', minval=0.0, step=0.1, defval=0.5, group='Strategy TP & SL')

period = input(20, 'CCI period',group = "TREND MAGIC")
coeff = input(1, 'ATR Multiplier',group = "TREND MAGIC")
AP = input(5, 'ATR Period',group = "TREND MAGIC")
ATR = ta.sma(ta.tr, AP)
srctm = close
upT = low - ATR * coeff
downT = high + ATR * coeff
MagicTrend = 0.0
MagicTrend := ta.cci(srctm, period) >= 0 ? upT < nz(MagicTrend[1]) ? nz(MagicTrend[1]) : upT : downT > nz(MagicTrend[1]) ? nz(MagicTrend[1]) : downT
color1 = ta.cci(srctm, period) >= 0 ? #0022FC : #FC0400
plot(MagicTrend, color=color1, linewidth=3)
tmb = ta.cci(srctm, period) >= 0 and close>MagicTrend
tms = ta.cci(srctm, period) <= 0 and close<MagicTrend

//MACD

res           = input.timeframe("",  "Indicator TimeFrame", group = "MACD")
fast_length   = input.int(title="Fast Length", defval=12, group = "MACD")
slow_length   = input.int(title="Slow Length", defval=26, group = "MACD")
src           = input.source(title="Source", defval=close, group = "MACD")
signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 999, defval = 9, group = "MACD")
sma_source    = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA", "EMA"], group = "MACD")
sma_signal    = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"], group = "MACD")

fast_ma = request.security(syminfo.tickerid, res, sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length))
slow_ma = request.security(syminfo.tickerid, res, sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length))
macd = fast_ma - slow_ma
signal = request.security(syminfo.tickerid, res, sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length))
hist = macd - signal

trend_up   = macd > signal
trend_dn   = macd < signal
cross_UP   = signal[1] >= macd[1] and signal < macd
cross_DN   = signal[1] <= macd[1] and signal > macd
cross_UP_A = (signal[1] >= macd[1] and signal < macd) and macd > 0
cross_DN_B = (signal[1] <= macd[1] and signal > macd) and macd < 0


//UT Bot

srcut = close
showut = input.bool(false, 'Show UT Bot Labels', group = "UT BOT")
keyvalue = input.float(2, title='Key Vaule. \'This changes the sensitivity\'', step=.5, group = "UT BOT")
atrperiod = input(7, title='ATR Period', group = "UT BOT")
xATR = ta.atr(atrperiod)
nLoss = keyvalue * xATR

xATRTrailingStop = 0.0
iff_1 = srcut > nz(xATRTrailingStop[1], 0) ? srcut - nLoss : srcut + nLoss
iff_2 = srcut < nz(xATRTrailingStop[1], 0) and srcut[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), srcut + nLoss) : iff_1
xATRTrailingStop := srcut > nz(xATRTrailingStop[1], 0) and srcut[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), srcut - nLoss) : iff_2

pos = 0
iff_3 = srcut[1] > nz(xATRTrailingStop[1], 0) and srcut < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := srcut[1] < nz(xATRTrailingStop[1], 0) and srcut > nz(xATRTrailingStop[1], 0) ? 1 : iff_3

xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue

//plot(xATR, color=xcolor, title='Trailing Stop')
buy = ta.crossover(srcut, xATRTrailingStop)
sell = ta.crossunder(srcut, xATRTrailingStop)
barcolor = srcut > xATRTrailingStop

plotshape(showut ? buy:na, title='Buy', text='Buy', style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), size=size.tiny)
plotshape(showut ? sell:na, title='Sell', text='Sell', style=shape.labeldown, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), size=size.tiny)

//barcolor(barcolor ? color.green : color.red)

goLong = buy and tmb and cross_UP
goShort = sell and tms and cross_DN

plotshape(goLong, location=location.bottom, style=shape.triangleup, color=color.lime, size=size.small)
plotshape(goShort, location=location.top, style=shape.triangledown, color=color.red, size=size.small)

percentAsPoints(pcnt) =>
    strategy.position_size != 0 ? math.round(pcnt / 100.0 * strategy.position_avg_price / syminfo.mintick) : float(na)

percentAsPrice(pcnt) =>
    strategy.position_size != 0 ? (pcnt / 100.0 + 1.0) * strategy.position_avg_price : float(na)

current_position_size = math.abs(strategy.position_size)
initial_position_size = math.abs(ta.valuewhen(strategy.position_size[1] == 0.0, strategy.position_size, 0))

TP = strategy.position_avg_price + percentAsPoints(TPPerc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
SL = strategy.position_avg_price - percentAsPoints(SLPerc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)

var long = false
var short = false

if direction == 'Long' 
    long := goLong
    short := false

if direction == 'Short'
    short := goShort
    long := false

if direction == 'Both' 
    long := goLong
    short := goShort

if long and strategy.opentrades == 0
    strategy.entry(id='Long', direction=strategy.long)

if short and strategy.opentrades == 0
    strategy.entry(id='Short', direction=strategy.short)

if strategy.position_size > 0

    strategy.exit('TPSL', from_entry='Long', qty=initial_position_size, limit=TP, stop=SL)

if strategy.position_size < 0

    strategy.exit('TPSL2', from_entry='Short', qty=initial_position_size, limit=TP, stop=SL)



Thêm nữa