Chiến lược lợi nhuận hàng tháng hai cực

Tác giả:ChaoZhang, Ngày: 2023-11-06 16:06:55
Tags:

img

## Tổng quan Chiến lược này sử dụng các điểm pivot để xác định sự đảo ngược xu hướng và có các vị trí dài / ngắn phù hợp.

Làm thế nào nó hoạt động

  • Sử dụngpivothigh()pivotlow()để tính các điểm pivot, cho thấy sự đảo ngược xu hướng.
  • Đi dài khi giá phá vỡ trên pivot cao. Đi ngắn khi giá phá vỡ dưới pivot thấp.
  • Tính toán lợi nhuận hàng tháng vào đầu mỗi tháng và lưu vào mảng.
  • Tính toán lợi nhuận hàng năm vào đầu mỗi năm và lưu vào mảng.
  • Kéo bảng lợi nhuận để xem trực quan hiệu suất hàng tháng và hàng năm.

## Phân tích lợi thế

  • Các điểm pivot lọc ra một số tín hiệu đảo ngược sai.
  • Khóa lợi nhuận hàng tháng làm giảm tổn thất tháng tác động - lợi nhuận lưỡng cực.
  • Bảng trả lại cho thấy xu hướng hiệu suất rõ ràng.

## Phân tích rủi ro

  • Các pivot có thể thay đổi, gây ra các mục đảo ngược sai. Có thể tối ưu hóa param hoặc thêm bộ lọc.
  • Việc đóng cửa hàng tháng bị buộc bỏ lỡ lợi nhuận hơn nữa.
  • Bảng thiếu các chỉ số tối đa và rủi ro.

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

  • Thêm các bộ lọc gần các pivot để tránh các đảo ngược không chính xác thường xuyên.
  • Đóng một phần thay vì toàn bộ vị trí để giảm cơ hội bỏ lỡ.
  • Thêm các chỉ số rủi ro định lượng như tối đa, tỷ lệ Sharpe.

##Tóm lại Chiến lược này giao dịch đảo ngược tại các điểm trọng tâm và khóa lợi nhuận hàng tháng để kiểm soát rút tiền. Nhưng một số tham số và logic có thể được cải thiện để có tín hiệu chính xác hơn và quản lý rủi ro mạnh mẽ hơn. Bảng lợi nhuận trực quan giúp phân tích. Nhìn chung, chiến lược này có giá trị nhưng đòi hỏi đánh giá thận trọng cho giao dịch trực tiếp.


/*backtest
start: 2022-11-05 00:00:00
end: 2023-03-23 05:20:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("Monthly Returns in PineScript Strategies", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 25, calc_on_every_tick = true, commission_type = strategy.commission.percent, commission_value = 0.1)

// Inputs 
leftBars  = input(2)
rightBars = input(1)
prec      = input(2, title = "Return Precision")

// Pivot Points 
swh = pivothigh(leftBars, rightBars)
swl = pivotlow(leftBars, rightBars)

hprice = 0.0
hprice := not na(swh) ? swh : hprice[1]

lprice = 0.0
lprice := not na(swl) ? swl : lprice[1]

le = false
le := not na(swh) ? true : (le[1] and high > hprice ? false : le[1])

se = false
se := not na(swl) ? true : (se[1] and low < lprice ? false : se[1])

if (le)
	strategy.entry("PivRevLE", strategy.long, comment="PivRevLE", stop=hprice + syminfo.mintick)

if (se)
	strategy.entry("PivRevSE", strategy.short, comment="PivRevSE", stop=lprice - syminfo.mintick)

plot(hprice, color = color.green, linewidth = 2)
plot(lprice, color = color.red,   linewidth = 2)

///////////////////
// MONTHLY TABLE //

new_month = month(time) != month(time[1])
new_year  = year(time)  != year(time[1])

eq = strategy.equity

bar_pnl = eq / eq[1] - 1

cur_month_pnl = 0.0
cur_year_pnl  = 0.0

// Current Monthly P&L
cur_month_pnl := new_month ? 0.0 : 
                 (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1 

// Current Yearly P&L
cur_year_pnl := new_year ? 0.0 : 
                 (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1  

// Arrays to store Yearly and Monthly P&Ls
var month_pnl  = array.new_float(0)
var month_time = array.new_int(0)

var year_pnl  = array.new_float(0)
var year_time = array.new_int(0)

if (not na(cur_month_pnl[1]) and (new_month or barstate.islast))
    array.push(month_pnl , cur_month_pnl[1])
    array.push(month_time, time[1])

if (not na(cur_year_pnl[1]) and (new_year or barstate.islast))
    array.push(year_pnl , cur_year_pnl[1])
    array.push(year_time, time[1])

// Monthly P&L Table    
var monthly_table = table(na)

if (barstate.islast)
    monthly_table := table.new(position.bottom_right, columns = 14, rows = array.size(year_pnl) + 1, border_width = 1)

    table.cell(monthly_table, 0,  0, "",     bgcolor = #cccccc)
    table.cell(monthly_table, 1,  0, "Jan",  bgcolor = #cccccc)
    table.cell(monthly_table, 2,  0, "Feb",  bgcolor = #cccccc)
    table.cell(monthly_table, 3,  0, "Mar",  bgcolor = #cccccc)
    table.cell(monthly_table, 4,  0, "Apr",  bgcolor = #cccccc)
    table.cell(monthly_table, 5,  0, "May",  bgcolor = #cccccc)
    table.cell(monthly_table, 6,  0, "Jun",  bgcolor = #cccccc)
    table.cell(monthly_table, 7,  0, "Jul",  bgcolor = #cccccc)
    table.cell(monthly_table, 8,  0, "Aug",  bgcolor = #cccccc)
    table.cell(monthly_table, 9,  0, "Sep",  bgcolor = #cccccc)
    table.cell(monthly_table, 10, 0, "Oct",  bgcolor = #cccccc)
    table.cell(monthly_table, 11, 0, "Nov",  bgcolor = #cccccc)
    table.cell(monthly_table, 12, 0, "Dec",  bgcolor = #cccccc)
    table.cell(monthly_table, 13, 0, "Year", bgcolor = #999999)


    for yi = 0 to array.size(year_pnl) - 1
        table.cell(monthly_table, 0,  yi + 1, tostring(year(array.get(year_time, yi))), bgcolor = #cccccc)
        
        y_color = array.get(year_pnl, yi) > 0 ? color.new(color.green, transp = 50) : color.new(color.red, transp = 50)
        table.cell(monthly_table, 13, yi + 1, tostring(round(array.get(year_pnl, yi) * 100, prec)), bgcolor = y_color)
        
    for mi = 0 to array.size(month_time) - 1
        m_row   = year(array.get(month_time, mi))  - year(array.get(year_time, 0)) + 1
        m_col   = month(array.get(month_time, mi)) 
        m_color = array.get(month_pnl, mi) > 0 ? color.new(color.green, transp = 70) : color.new(color.red, transp = 70)
        
        table.cell(monthly_table, m_col, m_row, tostring(round(array.get(month_pnl, mi) * 100, prec)), bgcolor = m_color)

Thêm nữa