Trung bình di chuyển đơn biểu thức mịn mịn với xu hướng dừng lỗ sau chiến lược

Tác giả:ChaoZhang, Ngày: 2024-01-08 11:37:44
Tags:

img

Tổng quan

Chiến lược này kết hợp Trung bình Di chuyển Lượt Giai đơn (SESMA) và cơ chế dừng lỗ cuối cùng với Chandelier Exit để tạo thành một xu hướng rất ổn định và hiệu quả sau chiến lược. SESMA phục vụ như là đường chính để xác định hướng xu hướng của giá. Cơ chế dừng lỗ cuối cùng có thể làm giảm hiệu quả rủi ro của chiến lược trong khi bảo vệ lợi nhuận.

Chiến lược logic

Chiến lược bao gồm hai chỉ số cốt lõi:

  1. Trung bình Di chuyển Lượt đơn (SESMA): SESMA dựa trên ý tưởng của EMA và cải thiện các thông số để làm cho đường cong mượt mà hơn và giảm chậm trễ. Nó sử dụng hướng và mức độ của SESMA để đánh giá xu hướng giá.

  2. Cơ chế dừng lỗ sau: Kết hợp với giá cao nhất, giá thấp nhất và chỉ số ATR để tính toán các đường dừng lỗ dài và ngắn trong thời gian thực. Đây là một cơ chế dừng lỗ có thể điều chỉnh năng động có thể điều chỉnh phạm vi dừng lỗ dựa trên biến động và xu hướng thị trường. Mối quan hệ giữa đường dừng lỗ và mức giá được sử dụng để xác định thời gian ra lệnh.

Tín hiệu vào của chiến lược này được kích hoạt khi giá vượt qua SESMA. Tín hiệu ra được tạo ra bởi các đường dừng lỗ. Tùy chọn để hiển thị các đánh dấu vào / ra.

Ưu điểm của Chiến lược

  1. Phương pháp tính toán của SESMA được cải tiến và có thể giảm hiệu quả sự chậm trễ và cải thiện khả năng theo dõi xu hướng.
  2. Cơ chế dừng lỗ sau có thể điều chỉnh phạm vi dừng lỗ theo biến động thời gian thực để tránh dừng lỗ quá rộng hoặc quá chật.
  3. Đi kèm với hỗ trợ trực quan để đánh dấu tín hiệu vào và ra.
  4. Các thông số tùy chỉnh phù hợp với các sản phẩm khác nhau và tối ưu hóa thông số.

Rủi ro và hướng tối ưu hóa

  1. Stop loss có thể được kích hoạt sớm trong quá trình đảo ngược xu hướng. Có thể nới lỏng phạm vi stop loss một cách thích hợp.
  2. Các thông số SESMA có thể được tối ưu hóa để tìm ra chiều dài tốt nhất.
  3. Các thông số thời gian của ATR cũng có thể được thử nghiệm.
  4. Kiểm tra hiệu ứng của việc hiển thị các dấu hiệu.

Kết luận

Chiến lược này tích hợp các chỉ số đánh giá xu hướng và kiểm soát rủi ro để tạo thành một xu hướng tương đối mạnh mẽ sau chiến lược. So với các chiến lược trung bình động đơn giản, chiến lược này có thể nắm bắt xu hướng linh hoạt hơn trong khi giảm giảm. Thông qua tối ưu hóa tham số, chiến lược có thể đạt được kết quả tốt hơn trên các thị trường khác nhau.


/*backtest
start: 2023-12-31 00:00:00
end: 2024-01-07 00:00:00
period: 10m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/


//@version=5
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © simwai
strategy('Chandelier Exit ZLSMA Strategy', shorttitle='CE_ZLSMA', overlay = true, initial_capital = 1000, default_qty_value = 10, default_qty_type = strategy.percent_of_equity, calc_on_every_tick = false, process_orders_on_close = true, commission_value = 0.075)

// -- Colors --
color maximumYellowRed = color.rgb(255, 203, 98) // yellow
color rajah = color.rgb(242, 166, 84) // orange
color magicMint = color.rgb(171, 237, 198)
color languidLavender = color.rgb(232, 215, 255)
color maximumBluePurple = color.rgb(181, 161, 226)
color skyBlue = color.rgb(144, 226, 244)
color lightGray = color.rgb(214, 214, 214)
color quickSilver = color.rgb(163, 163, 163)
color mediumAquamarine = color.rgb(104, 223, 153)
color carrotOrange = color.rgb(239, 146, 46)

// -- Inputs --
length = input(title='ATR Period', defval=1)
mult = input.float(title='ATR Multiplier', step=0.1, defval=2)
showLabels = input(title='Show Buy/Sell Labels ?', tooltip='Created by Chandelier Exit (CE)', defval=false)
isSignalLabelEnabled = input(title='Show Signal Labels ?', defval=true)
useClose = input(title='Use Close Price for Extrema ?', defval=true)
zcolorchange = input(title='Enable Rising/Decreasing Highlightning', defval=false)
zlsmaLength = input(title='ZLSMA Length', defval=50)
offset = input(title='Offset', defval=0)

// -- CE - Credits to @everget --
float haClose = float(1) / 4 * (open[1] + high[1] + low[1] + close[1])
atr = mult * ta.atr(length)[1]

longStop = (useClose ? ta.highest(haClose, length) : ta.highest(haClose, length)) - atr
longStopPrev = nz(longStop[1], longStop)
longStop := haClose > longStopPrev ? math.max(longStop, longStopPrev) : longStop

shortStop = (useClose ? ta.lowest(haClose, length) : ta.lowest(haClose, length)) + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := haClose < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop

var int dir = 1
dir := haClose > shortStopPrev ? 1 : haClose < longStopPrev ? -1 : dir

buySignal = dir == 1 and dir[1] == -1
plotshape(buySignal and showLabels ? longStop : na, title='Buy Label', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=mediumAquamarine, textcolor=color.white)
sellSignal = dir == -1 and dir[1] == 1
plotshape(sellSignal and showLabels ? shortStop : na, title='Sell Label', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=carrotOrange, textcolor=color.white)

changeCond = dir != dir[1]

// -- ZLSMA - Credits to @netweaver2011 --
lsma = ta.linreg(haClose, zlsmaLength, offset)
lsma2 = ta.linreg(lsma, zlsmaLength, offset)
eq = lsma - lsma2
zlsma = lsma + eq

zColor = zcolorchange ? zlsma > zlsma[1] ? magicMint : rajah : languidLavender
plot(zlsma, title='ZLSMA', linewidth=2, color=zColor)

// -- Signals --
var string isTradeOpen = ''
var string signalCache = ''

bool enterLong = buySignal and ta.crossover(haClose, zlsma) 
bool exitLong = ta.crossunder(haClose, zlsma) 
bool enterShort = sellSignal and ta.crossunder(haClose, zlsma)
bool exitShort = ta.crossover(haClose, zlsma)

if (signalCache == 'long entry')
    signalCache := ''
    enterLong := true
else if (signalCache == 'short entry')
    signalCache := ''
    enterShort := true

if (isTradeOpen == '')
    if (exitShort and (not enterLong))
        exitShort := false
    if (exitLong and (not enterShort))
        exitLong := false   
    if (enterLong and exitShort)
        isTradeOpen := 'long'
        exitShort := false
    else if (enterShort and exitLong)
        isTradeOpen := 'short'
        exitLong := false
    else if (enterLong)
        isTradeOpen := 'long'
    else if (enterShort)
        isTradeOpen := 'short'
else if (isTradeOpen == 'long')
    if (exitShort)
        exitShort := false
    if (enterLong)
        enterLong := false
    if (enterShort and exitLong)
        enterShort := false
        signalCache := 'short entry'
    if (exitLong)
        isTradeOpen := ''
else if (isTradeOpen == 'short')
    if (exitLong)
        exitLong := false
    if (enterShort)
        enterShort := false
    if (enterLong and exitShort)
        enterLong := false
        signalCache := 'long entry'
    if (exitShort)
        isTradeOpen := ''

plotshape((isSignalLabelEnabled and enterLong) ? zlsma : na, title='LONG', text='L', style=shape.labelup, color=mediumAquamarine, textcolor=color.white, size=size.tiny, location=location.absolute)
plotshape((isSignalLabelEnabled and enterShort) ? zlsma : na, title='SHORT', text='S', style=shape.labeldown, color=carrotOrange, textcolor=color.white, size=size.tiny, location=location.absolute)
plotshape((isSignalLabelEnabled and exitLong) ? zlsma : na, title='LONG EXIT', style=shape.circle, color=magicMint, size=size.tiny, location=location.absolute)
plotshape((isSignalLabelEnabled and exitShort) ? zlsma : na, title='SHORT EXIT', style=shape.circle, color=rajah, size=size.tiny, location=location.absolute)

barcolor(color=isTradeOpen == 'long' ? mediumAquamarine : isTradeOpen == 'short' ? carrotOrange : na)

// -- Long Exits --
if (exitLong and strategy.position_size > 0)
    strategy.close('long', comment='EXIT_LONG')

// -- Short Exits --
if (exitShort and strategy.position_size < 0)
    strategy.close('short', comment='EXIT_SHORT')

// -- Long Entries --
if (enterLong)
    strategy.entry('long', strategy.long, comment='ENTER_LONG')

// -- Short Entries --
if (enterShort)
    strategy.entry('short', strategy.short, comment='ENTER_SHORT')

Thêm nữa