B-Xtrender chiến lược chéo trung bình chuyển động theo cấp số

Tác giả:ChaoZhang, Ngày: 2024-02-20 14:45:17
Tags:

img

Tổng quan

Đây là một chiến lược giao dịch dựa trên nguyên tắc chéo trung bình chuyển động theo cấp số nhân (EMA). Nó cũng kết hợp chỉ số RSI và bộ lọc trung bình chuyển động để tạo thành một hệ thống giao dịch theo xu hướng và đảo ngược tương đối hoàn chỉnh.

Chiến lược logic

  1. Tạo tín hiệu giao dịch thông qua EMA chéo nhanh và chậm.
  2. Đi dài khi đường nhanh vượt qua đường chậm, và đi ngắn khi đường nhanh vượt qua dưới. Sử dụng RSI để xác nhận thứ cấp, chỉ nhận tín hiệu khi RSI cũng vượt qua cùng một hướng.
  3. Thêm bộ lọc trung bình động 200 ngày để tránh các tín hiệu trong thời gian hỗn loạn.

Ưu điểm

  1. Xác nhận RSI làm tăng đáng kể độ tin cậy tín hiệu và giảm tín hiệu sai.
  2. Lựa chọn tham số EMA cân bằng sự nhạy cảm và ổn định.
  3. Bộ lọc MA loại bỏ tiếng ồn để tránh giao dịch không cần thiết.

Rủi ro

  1. EMA có vấn đề chậm về biến động giá mạnh, tăng lỗ hoặc tín hiệu bị thiếu.
  2. Cài đặt RSI kém cũng có thể đưa ra sự chậm trễ tín hiệu.
  3. Bộ lọc MA có thể lọc các tín hiệu xu hướng ban đầu.

Cơ hội gia tăng

  1. Tối ưu hóa các thông số EMA theo chu kỳ.
  2. Thử nghiệm với các chỉ số khác như MACD để kết hợp với RSI.
  3. Parameter bộ lọc MA tinh chỉnh để giảm tiếng ồn tối ưu và nắm bắt cơ hội.

Kết luận

Đây là một chiến lược tổng thể vững chắc trong việc xây dựng một hệ thống giao dịch EMA hoàn chỉnh, với xác nhận RSI bổ sung để tăng chất lượng tín hiệu. Nó đáng để nghiên cứu và tối ưu hóa. Tuy nhiên, rủi ro trễ chỉ số vốn có cũng nên được quản lý thông qua việc dừng lỗ thích hợp.


/*backtest
start: 2023-02-13 00:00:00
end: 2024-02-19 00:00:00
period: 1d
basePeriod: 1h
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/
// © QuantTherapy
//@version=4
strategy("B-Xtrender [Backtest Edition] @QuantTherapy")

i_short_l1  = input(5 , title="[Short] L1")
i_short_l2  = input(20, title="[Short] L2")
i_short_l3  = input(15, title="[Short] L3")

i_long_l1   = input(20, title="[Long] L1")
i_long_l2   = input(15, title="[Long] L2")

i_ma_use    = input(true , title="[MA Filter] Yes/No" )
i_ma_len    = input(200  , title="[MA Filter] length" )
i_ma_type   = input("EMA", title="[MA Filter] type", options = ["SMA", "EMA"])

shortTermXtrender = rsi( ema(close, i_short_l1) - ema(close, i_short_l2), i_short_l3 ) - 50
longTermXtrender  = rsi( ema(close, i_long_l1), i_long_l2 ) - 50

shortXtrenderCol = shortTermXtrender > 0 ? shortTermXtrender > shortTermXtrender[1] ? color.lime : #228B22 : shortTermXtrender > shortTermXtrender[1] ? color.red : #8B0000
plot(shortTermXtrender, color=shortXtrenderCol, style=plot.style_columns, linewidth=1, title="B-Xtrender Osc. - Histogram", transp = 40)

longXtrenderCol   = longTermXtrender> 0 ? longTermXtrender > longTermXtrender[1] ? color.lime : #228B22 : longTermXtrender > longTermXtrender[1] ? color.red : #8B0000
macollongXtrenderCol =  longTermXtrender > longTermXtrender[1] ? color.lime : color.red
plot(longTermXtrender , color=longXtrenderCol, style=plot.style_columns, linewidth=2, title="B-Xtrender Trend - Histogram", transp = 90)

plot(longTermXtrender , color=#000000             , style=plot.style_line, linewidth=5, title="B-Xtrender Trend - Line", transp = 100)
plot(longTermXtrender , color=macollongXtrenderCol, style=plot.style_line, linewidth=3, title="B-Xtrender Trend - Line", transp = 100)

// --- Initialize MA Filter
ma = i_ma_type == "EMA" ? ema(close, i_ma_len) : sma(close, i_ma_len)
maFilterLong = true
maFilterShort = true
if i_ma_use
    maFilterLong  := close > ma ? true : false
    maFilterShort := close < ma ? true : false

long  = shortTermXtrender > 0 and longTermXtrender > 0 and maFilterLong
closeLong = shortTermXtrender < 0 or longTermXtrender < 0 
short = shortTermXtrender < 0 and longTermXtrender < 0 and maFilterShort
closeShort = shortTermXtrender > 0 or longTermXtrender > 0 

plotshape(long[1]==true  and long[2]==false  ? 0 : na , location=location.absolute, style=shape.labelup  , color=color.lime, size=size.small, transp=10)
plotshape(short[1]==true and short[2]==false ? 0 : na, location=location.absolute, style=shape.labeldown, color=color.red , size=size.small, transp=10)
plotshape(closeLong[1]==true and closeLong[2]==false
 or closeShort[1]==true and closeShort[2]==false ? 0 : na, location=location.absolute, style=shape.circle, color=color.orange , size=size.small)

i_perc     = input(defval = 20.0, title = "[TSL-%] Percent"  , minval = 0.1 )
i_src = close // constant for calculation
sl_val = i_src * i_perc / 100

strategy.entry("Long", strategy.long, when = long ) 
strategy.close("Long", when = closeLong)

strategy.entry("Short", strategy.short, when = short) 
strategy.close("Short", when = closeShort)

// Calculate SL
longStopPrice = 0.0, shortStopPrice = 0.0
longStopPrice := if (strategy.position_size > 0)
    stopValue = close - sl_val
    max(stopValue, longStopPrice[1])
else
    0

shortStopPrice := if (strategy.position_size < 0)
    stopValue = close + sl_val
    min(stopValue, shortStopPrice[1])
else
    syminfo.mintick*1000000

// For TSL Visualisation on Chart    
// plot(series=(strategy.position_size > 0) ? longStopPrice : na,
//      color=color.fuchsia, style = plot.style_circles,
//      linewidth=1, title="Long Trail Stop")
     
// plot(series=(strategy.position_size < 0) ? shortStopPrice : na,
//      color=color.fuchsia, style = plot.style_circles,
//      linewidth=1, title="Short Trail Stop")

if (strategy.position_size > 0)
    strategy.exit(id="TSL Long", stop=longStopPrice)

if (strategy.position_size < 0)
    strategy.exit(id="TSL Short", stop=shortStopPrice)    

Thêm nữa