Xu hướng ma trận chênh lệch theo chiến lược

Tác giả:ChaoZhang, Ngày: 2023-12-12 17:05:27
Tags:

img

Tổng quan

Chiến lược Divergence Matrix Trend Following là một chiến lược giao dịch định lượng kết hợp giữa phân tích xu hướng, chênh lệch và trung bình động. Chiến lược này sử dụng các chỉ số RSI kép để đánh giá hướng xu hướng thị trường và các trung bình động ma trận để tạo ra các tín hiệu đầu vào.

Chiến lược logic

Chiến lược Tiếp theo xu hướng ma trận chênh lệch bao gồm các phần chính sau:

  1. RSI kép để đánh giá xu hướng

    Sử dụng chỉ số RSI nhanh và chỉ số RSI chậm để xác định hướng xu hướng thị trường.

  2. Trung bình động ma trận cho tín hiệu giao dịch

    Thiết lập một nhóm các đường trung bình động ma trận dựa trên giá nhập cảnh. Khi giá chạm vào đường trung bình động, điều chỉnh vị trí phù hợp. Điều này cho phép thu được nhiều lợi nhuận hơn trong xu hướng.

  3. Giao dịch hai chiều

    Chọn mặc định là giao dịch hai chiều.

Logic giao dịch cụ thể là:

  1. Sử dụng chỉ số RSI nhanh để phát hiện mức mua/bán quá mức tạm thời trên thị trường.

  2. Sử dụng chỉ số RSI chậm để xác định hướng xu hướng trung bình đến dài hạn của thị trường.

  3. Khi chỉ số RSI nhanh cho thấy cực đoan và chỉ số RSI chậm báo hiệu đảo ngược xu hướng, hãy có các vị trí dựa trên xu hướng dài / ngắn bởi chỉ số RSI chậm.

  4. Sau khi nhập vị trí, thiết lập một nhóm các đường trung bình động ma trận. Các đường ma trận này dựa trên giá nhập, với kích thước khoảng thời gian được xác định trong tham số Matrix Interval %.

  5. Khi giá chạm vào một đường ma trận, điều chỉnh kích thước vị trí phù hợp. ví dụ, tăng dài trên breakouts lên, giảm ngắn trên phá vỡ xuống.

  6. Khi giá thấy sự điều chỉnh lớn, các vị trí sẽ được đặt lại mức ban đầu.

Điều trên mô tả logic giao dịch chính của chiến lược này.

Ưu điểm

Chiến lược Tiếp theo xu hướng ma trận chênh lệch có những lợi thế sau:

  1. Các tín hiệu RSI kép đáng tin cậy hơn. RSI nhanh tránh các đột phá sai và RSI chậm đảm bảo xu hướng chính là chính xác.

  2. Các đường trung bình động ma trận được hưởng lợi từ xu hướng. Điều chỉnh kích thước vị trí dựa trên chênh lệch giá cho phép thu được lợi nhuận bền vững.

  3. Hỗ trợ giao dịch hai chiều. mặc định là giao dịch hai chiều, nhưng cũng có thể chỉ dài. Điều này thích nghi với nhiều môi trường thị trường hơn.

  4. Cơ chế đặt lại vị trí kiểm soát rủi ro. Đặt lại vị trí khi giá thấy sự điều chỉnh lớn cho phép dừng lỗ kịp thời.

  5. Cài đặt tham số linh hoạt. Người dùng có thể chọn kết hợp tham số tối ưu dựa trên dữ liệu lịch sử, các công cụ giao dịch vv.

  6. Cấu trúc mã sạch. Phân tách rõ ràng trách nhiệm làm cho mã dễ hiểu, tối ưu hóa và mở rộng.

Tóm lại, lợi thế lớn nhất của chiến lược này là cải thiện chất lượng tín hiệu thông qua nhiều cơ chế trong khi theo đuổi lợi nhuận cao hơn trong các rủi ro được kiểm soát.

Rủi ro

Chiến lược Tiếp theo xu hướng ma trận chênh lệch cũng có một số rủi ro, chủ yếu là trong các lĩnh vực sau:

  1. Rủi ro thất bại của tín hiệu RSI kép. Khi thị trường bị giới hạn trong phạm vi, RSI thường đưa ra tín hiệu sai. Sự can thiệp thủ công là cần thiết để điều chỉnh các tham số hoặc đình chỉ giao dịch.

  2. Rủi ro trung bình động ma trận không đúng. Nếu các tham số ma trận không được đặt đúng cách, các điều chỉnh vị trí có thể quá hung hăng, do đó làm tăng tổn thất. Kiểm tra tham số bảo thủ là bắt buộc.

  3. Rủi ro các vị trí bị đòn bẩy quá mức. Việc điều chỉnh kích thước vị trí quá mức cũng sẽ làm gia tăng lỗ. Các thông số kích thước vị trí tối đa cần phải được thiết lập một cách thận trọng.

  4. Rủi ro đảo ngược xu hướng. Nếu không đóng các vị trí ngay lập tức khi xu hướng đảo ngược, có thể phải chịu tổn thất lớn. Điều này đòi hỏi phải theo dõi các chỉ số xu hướng dài hạn.

  5. Rủi ro không gian tối ưu hóa hạn chế. Chiến lược này đã khá trưởng thành. Khả năng tối ưu hóa tiếp tục bị hạn chế. Các nâng cấp lớn có thể cần thiết nếu chế độ thị trường thay đổi đáng kể.

Đánh giá và tối ưu hóa chiến lược là chìa khóa để giảm thiểu những rủi ro này - điều chỉnh các tham số, theo dõi các chỉ số dài hạn, v.v., có thể giảm thiểu rủi ro ở một mức độ nào đó.

Cơ hội gia tăng

Có chỗ cho việc tăng cường hơn nữa xu hướng ma trận chênh lệch theo chiến lược sau:

  1. Tối ưu hóa các tham số RSI kép. Kiểm tra nhiều kết hợp tham số hơn và chọn các khoảng thời gian RSI với độ chính xác cao nhất.

  2. Các đường ma trận có thể tùy chỉnh. Cho phép người dùng tham số hóa các cài đặt ma trận dựa trên các dụng cụ khác nhau để phù hợp hơn với các đặc điểm của họ.

  3. Thêm cơ chế dừng lỗ. Ví dụ, thiết lập các đường thoát để dừng các vị trí nếu giá phá vỡ các đường đó.

  4. Thêm các quy tắc kích thước vị trí khoa học hơn. Quản lý điều chỉnh kích thước vị trí một cách dần dần hơn để ngăn ngừa đòn bẩy quá mức.

  5. Thêm các chỉ số khác. giới thiệu các chỉ số bổ sung như MACD, KD vv để cải thiện độ chính xác tín hiệu.

  6. Tối ưu hóa cấu trúc mã. Cải thiện thêm khả năng mở rộng, khả năng duy trì và hiệu quả thực thi mã.

Kết luận

Chiến lược Divergence Matrix Trend Following là một chiến lược giao dịch định lượng tinh vi kết hợp nhiều cơ chế - sử dụng RSI kép cho hướng xu hướng và các đường ma trận để kiếm lợi từ xu hướng. So với các chiến lược chỉ dẫn đơn, nó cung cấp các tín hiệu giao dịch ổn định và hiệu quả hơn. Với các phần mở rộng điều chỉnh và tối ưu hóa tham số, chiến lược này có thể thích nghi với nhiều điều kiện và chế độ thị trường hơn, làm cho nó rất linh hoạt. Nhìn chung, chiến lược này đạt được sự cân bằng tốt giữa rủi ro và lợi nhuận, và xứng đáng được các nhà đầu tư tích cực áp dụng và tiếp tục nâng cao.


/*backtest
start: 2023-01-01 00:00:00
end: 2023-10-14 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3
strategy("The Matrix 7.0 Strategy", overlay=false)

//Matrix Settings 
entry_size = input(title="Entry Size", defval = 1)
max_size = input(title="Max Size", defval = 10000)
matrix = input(title="Matrix Interval %", defval = 2)
matrix_price_overwrite = input(title="Matrix Overwrite $", defval = 0.0)
adjustment = input(title="Adjustment Size", defval = 1000)
trade_short = input(title="Trade Short", type=bool, defval = true)

//RSI Settings
periods = input(title="RSI Periods", defval = 14)
overbought_short = input(title="RSI Overbought", defval = 65)
oversold_short = input(title="RSI Oversold", defval = 30)

//RSI Trend Settings
resolution_long = input(title="Resolution Trend", defval = "D")
periods_long = input(title="RSI Trend Periods", defval = 14)
overbought_long = input(title="RSI Trend Overbought", defval = 64)
oversold_long = input(title="RSI Trend Oversold", defval = 30)

//Round Off to 2 decimals
round2(x) =>
    a = x * 10 * 10
    a := floor(a + 0.5)
    a := a / 10 / 10
    a

//RSI Function
RSI = rsi(close, periods)

//RSI Market Function
rsi_oversold = RSI < oversold_short
rsi_overbought = RSI > overbought_short

market_rsi = 0.0
market_rsi := if (rsi_oversold)
    RSI - oversold_short
else
    if (rsi_overbought)
        RSI - overbought_short
    else
        0

//RSI Trend Function
rsi_long = request.security(syminfo.tickerid,resolution_long,rsi(close,periods_long))
trend_rsi_long = rsi_long < oversold_long
trend_rsi_short = rsi_long > overbought_long
trend_rsi = 0
trend_rsi := if (trend_rsi_short)
    -1
else
    if (trend_rsi_long)
        1
    else
        trend_rsi[1] 

// // Shorter time resolution to make "close" crosses give faster positives.
// short_resolution = security(tickerid, "1", close)
// quick = round2(short_resolution) //ROUND OFF TO 2 DECIMAL PLACES.

//Declare Other Variables
entry_price = 0.0
entry_price := nz(entry_price[1])

position_size = 0.0
position_size := nz(position_size[1])

last_traded_price = 0.0
last_traded_price := nz(last_traded_price[1])


matrix_price = 0.0
if matrix_price_overwrite > 0.0
    matrix_price := matrix_price_overwrite
else
    matrix_price := round2((matrix/100) * entry_price)

level = 0
level := nz(level[1])

level_price = entry_price
if not na(level_price[1])
    level_price := level_price[1]

// Calculate Level
if close > level_price 
    level_change = floor((high - level_price)/matrix_price)
    level := level + level_change
else
    if close < level_price 
        level_change = ceil((low - level_price)/matrix_price)
        level := level + level_change
        
// Calculate Level Price   
level_price := (level * matrix_price) + entry_price

// Calculate Matrix Position
matrix_position = 0.0

if position_size > 0
    matrix_position :=  ((-1 * level) * adjustment) + entry_size
else
    if position_size < 0
        matrix_position :=  ((-1 * level) * adjustment) - entry_size
    
//Trend Entry or Reversal Conditions
trend_reversal_up = trend_rsi == 1 and (trend_rsi[1] == -1 or trend_rsi == 0) and position_size <= 0
trend_reversal_down = trend_rsi == -1 and (trend_rsi[1] == 1 or trend_rsi == 0) and position_size >= 0 and trade_short == true
flatten_position = trend_rsi == -1 and (trend_rsi[1] == 1 or trend_rsi == 0) and position_size >= 0 and trade_short == false

//Reset Conditions
reset_long = (position_size > 0) and (close - entry_price > matrix_price) and (market_rsi < 0) and (position_size != entry_size) 
reset_short = (position_size < 0) and (entry_price - close > matrix_price) and (market_rsi > 0) and (position_size != (-1 * entry_size)) 

//Adjustment Conditions
increase_long = (position_size > 0) and (matrix_position > position_size) and (market_rsi < 0) and (matrix_position <= max_size) 
decrease_long = (position_size > 0) and (matrix_position < position_size) and (market_rsi > 0) 
increase_short = (position_size < 0) and (matrix_position < position_size) and (market_rsi > 0) and (matrix_position >= (-1 * max_size)) 
decrease_short = (position_size < 0) and (matrix_position > position_size) and (market_rsi < 0)  

//Transactions
//Trend Reversals
if trend_reversal_up
    strategy.entry("OL", strategy.long, qty=entry_size)
    position_size := entry_size
    matrix_position := entry_size
    level := 0
else
    if trend_reversal_down 
        strategy.entry("OS", strategy.short, qty=entry_size)
        position_size := -1 * entry_size
        matrix_position := -1 * entry_size   
        level := 0
        
    //Reset Positions    
    else
        if reset_long
            order = entry_size - position_size[1]
            strategy.order("RL", strategy.long, qty=order)
            position_size := entry_size
            matrix_position := entry_size
            level := 0
        else
            if reset_short
                order = position_size[1] - (-1* entry_size)
                strategy.order("RS", strategy.short, qty=order)
                position_size := -1 * entry_size
                matrix_position := -1 * entry_size
                level := 0

    //Position Adjustments
            else    
                if increase_long
                    order = matrix_position - position_size[1]
                    strategy.order("IL", strategy.long, qty=order)
                    position_size := position_size[1] + order
                else
                    if decrease_long
                        order = position_size[1] - matrix_position
                        strategy.order("DL", strategy.short, qty=order)
                        position_size := position_size[1] - order
                    else
                        if increase_short
                            order = position_size[1] - matrix_position
                            strategy.order("IS", strategy.short, qty=order)
                            position_size := position_size[1] - order
                        else
                            if decrease_short
                                order = matrix_position - position_size[1]
                                strategy.order("DS", strategy.long, qty=order)
                                position_size := position_size[1] + order
                            else 
                                if flatten_position
                                    strategy.close_all()
                                    position_size := 0.0
                                    matrix_position := 0.0
                                    level := 0

//Grouped Actions
if trend_reversal_up or trend_reversal_down or reset_short or reset_long
    entry_price := round2(close)
    last_traded_price := round2(close)

if increase_long or decrease_long or increase_short or decrease_short
    last_traded_price := round2(close)

// //RSI Trend & Adjustment Moments. (strategy)
p1 = plot(market_rsi, color = trend_rsi > 0 ? green : red, linewidth = 4, title='Market', transp =0)
p2 = plot(trend_rsi, color = trend_rsi > 0 ? green : red, linewidth = 4, title='Trend', transp = 0)
fill(p1,p2, color=trend_rsi > 0 ? green : red, transp=0)
p3 = plot((rsi_long - 50) *2, color = white, title="Trend Index")
fill(p2,p3, color=white)
hline((overbought_long -50) * 2)
hline((oversold_long -50) * 2)

//Position Plots (strategy)
plot(matrix_position / 100, title='Matrix', color=white, linewidth = 4)
plot(position_size / 100, title='Position', color=blue, linewidth = 4)
plot(strategy.position_size / 100, title='Strategy', color=orange, linewidth = 4)

// //Price Plots (study)
// plot(level_price, title="Matrix Level Price", linewidth=4)
// plot(last_traded_price, title="Last Traded Price", linewidth=2, color=orange)
// plot(entry_price + (4 * matrix_price), title='Adjustment 4', color=white, linewidth = 1)
// plot(entry_price + (3 * matrix_price), title='Adjustment 3', color=white, linewidth = 1)
// plot(entry_price + (2 * matrix_price), title='Adjustment 2', color=white, linewidth = 1)
// plot(entry_price + matrix_price, title='Adjustment 1', color=white, linewidth = 1)
// plot(entry_price, title='Entry Price', color=white, linewidth = 3)
// plot(entry_price - matrix_price, title='Adjustment -1', color=white, linewidth = 1)
// plot(entry_price - (2 * matrix_price), title='Adjustment -2', color=white, linewidth = 1)
// plot(entry_price - (3 * matrix_price), title='Adjustment -3', color=white, linewidth = 1)
// plot(entry_price - (4 * matrix_price), title='Adjustment -4', color=white, linewidth = 1)


// //Alerts (study only)
// alertcondition(trend_reversal_up, title='Trend Reversal Up', message='Market Oversold, Lets Buy')
// alertcondition(trend_reversal_down, title='Trend Reversal Down', message='Market Overbought, Lets Sell')
// alertcondition(reset_long, title='Reset Long', message='Higher Bottom, Lets Buy')
// alertcondition(reset_short, title='Reset Short', message='Lower Top, Lets Sell')
// alertcondition(increase_long, title='Increase Long', message='Price Dropped, Lets Buy')
// alertcondition(decrease_long, title='Decrease Long', message='Price Spiked, Lets Sell')
// alertcondition(increase_short, title='Increase Short', message='Price Spiked, Lets Sell')
// alertcondition(decrease_short, title='Decrease Short', message='Price Dropped, Lets Buy')

// //Grouped Conditions
// condition_buy = trend_reversal_up or increase_long or decrease_short or reset_long
// condition_sell = trend_reversal_down or decrease_long or increase_short or reset_short
// adjustment_matrix = trend_reversal_up or increase_long or decrease_short or trend_reversal_down or decrease_long or increase_short or reset_long or reset_short

// //Grouped Alerts
// alertcondition(condition_buy, title='Condition Buy', message='You Need to Buy')
// alertcondition(condition_sell, title='Condition Sell', message='You Need to Sell!')
// alertcondition(adjustment_matrix, title='Adjustment Matrix', message='You Need to Adjust')



Thêm nữa