Đường trung bình động chồng lên độ trễ bằng không kết hợp với chiến lược thoát lệnh giao dịch đường cantilever


Ngày tạo: 2024-01-22 10:03:05 sửa đổi lần cuối: 2024-01-22 10:03:05
sao chép: 0 Số nhấp chuột: 1897
1
tập trung vào
1617
Người theo dõi

Đường trung bình động chồng lên độ trễ bằng không kết hợp với chiến lược thoát lệnh giao dịch đường cantilever

Tổng quan

Ý tưởng chính của chiến lược này là kết hợp các chỉ số di chuyển trung bình ((ZLSMA) để xác định hướng xu hướng, và các chỉ số thoát đường sườn ((CE) để tìm thời gian vào và thoát chính xác hơn. ZLSMA là một chỉ số xu hướng, có thể xác định sớm hơn sự thay đổi xu hướng.

Nguyên tắc chiến lược

  1. Phần ZLSMA:

    • Các đường LMA có chiều dài 130 chu kỳ được tính riêng biệt bằng phương pháp hồi quy tuyến tính.
    • Sau đó xếp hai đường LMA lên nhau để có được giá trị khác nhau của eq.
    • Cuối cùng, tính theo đường LMA ban đầu và cộng thêm chênh lệch eq để tạo ra đường trung bình di chuyển ZLSMA với độ trễ bằng không.
  2. Phần CE:

    • Tính toán chỉ số ATR và nhân hệ số ((tiền định 2) để xác định khoảng cách động từ điểm cao hoặc thấp gần nhất.
    • Khi giá đóng cửa vượt quá đường dừng nhiều đầu gần nhất hoặc đường dừng đầu không, hãy điều chỉnh đường dừng cho phù hợp.
    • Doanh số giao dịch được tính theo vị trí của giá đóng cửa so với đường dừng lỗ.
  3. Thời gian nhập cảnh:

    • ZLSMA đánh giá xu hướng, CE tham gia khi có tín hiệu.
  4. Hết lỗ:

    • Các dây dài có trạm dừng và trạm dừng cố định.
    • Hạn chế cố định thay thế cho xuất động của CE.

Phân tích lợi thế

  1. ZLSMA có thể đánh giá xu hướng sớm hơn để tránh phá vỡ giả.
  2. CE có thể điều chỉnh điểm xuất khẩu một cách linh hoạt theo mức độ biến động của thị trường.
  3. Chiến lược rủi ro lợi nhuận có thể được tùy chỉnh.
  4. Các đường dây dài và ngắn sử dụng các phương pháp dừng lỗ khác nhau để kiểm soát rủi ro.

Phân tích rủi ro

  1. Thiết lập tham số không đúng có thể làm tăng tỷ lệ đầu vào hoặc mở rộng phạm vi dừng.
  2. Nếu tình hình thay đổi nhanh chóng, sẽ có nguy cơ phá vỡ lệnh ngưng.

Hướng tối ưu hóa

  1. Có thể thử nghiệm các tham số tối ưu hóa cho các thị trường và chu kỳ thời gian khác nhau.
  2. Bạn có thể xem xét điều chỉnh các tham số dừng lỗ theo tỷ lệ dao động hoặc theo chu kỳ cụ thể.
  3. Có thể thử kết hợp với các chỉ số hoặc mô hình khác để tăng tỷ lệ lợi nhuận.

Tóm tắt

Chiến lược này chủ yếu sử dụng phương tiện di chuyển xếp chồng lên nhau để đánh giá xu hướng, kết hợp với các chỉ số xuất khẩu đường treo để tìm thời gian ra vào chính xác hơn. Ưu điểm của chiến lược là có thể tùy chỉnh tỷ lệ dừng lỗ và điều chỉnh động lực của xuất khẩu đường treo để kiểm soát rủi ro theo tình hình thị trường. Bước tiếp theo là thử tối ưu hóa tham số và kết hợp chiến lược để tăng cường sự ổn định và lợi nhuận hơn nữa.

Mã nguồn chiến lược
/*backtest
start: 2024-01-14 00:00:00
end: 2024-01-21 00:00:00
period: 3m
basePeriod: 1m
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/
// © GGkurg

//@version=5

strategy(title = "ZLSMA + Chandelier Exit", shorttitle="ZLSMA + CE", overlay=true)


var GRP1 = "take profit / stop loss"
TP = input(title='long TP%', defval=2.0,   inline = "1", group = GRP1) 
SL = input(title='long SL%', defval=2.0,    inline = "1", group = GRP1) 
TP2 = input(title='short TP', defval=2.0,    inline = "2", group = GRP1) 
SL2 = input(title='short SL', defval=2.0,    inline = "2", group = GRP1) 
//-------------------------------------------------calculations
takeProfitPrice = strategy.position_avg_price * (1+(TP/100))
stopLossPrice = strategy.position_avg_price * (1-(SL/100))
takeProfitPrice2 = strategy.position_avg_price * (1-(TP2/100))
stopLossPrice2 = strategy.position_avg_price * (1+(SL2/100))


//---------------------------------------ZLSMA - Zero Lag LSMA
var GRP2 = "ZLSMA settings"
length1 = input(title='Length', defval=130, inline = "1", group = GRP2) 
offset1 = input(title='Offset', defval=0, inline = "2", group = GRP2) 
src = input(close, title='Source', inline = "3", group = GRP2) 
lsma = ta.linreg(src, length1, offset1)
lsma2 = ta.linreg(lsma, length1, offset1)
eq = lsma - lsma2
zlsma = lsma + eq

plot(zlsma, color=color.new(color.yellow, 0), linewidth=3)


//---------------------------------------ZLSMA conditisions 
//---------long
longc1 = close > zlsma
longclose1 = close < zlsma
//---------short
shortc1 = close < zlsma
shortclose1 = close > zlsma


//---------------------------------------Chandelier Exit
var string calcGroup = 'Chandelier exit settings'
length = input.int(title='ATR Period', defval=1, group=calcGroup)
mult = input.float(title='ATR Multiplier', step=0.1, defval=2.0, group=calcGroup)
useClose = input.bool(title='Use Close Price for Extremums', defval=true, group=calcGroup)

var string visualGroup = 'Visuals'
showLabels = input.bool(title='Show Buy/Sell Labels', defval=true, group=visualGroup)
highlightState = input.bool(title='Highlight State', defval=true, group=visualGroup)

var string alertGroup = 'Alerts'
awaitBarConfirmation = input.bool(title="Await Bar Confirmation", defval=true, group=alertGroup)

atr = mult * ta.atr(length)

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

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

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

var color longColor = color.green
var color shortColor = color.red
var color longFillColor = color.new(color.green, 90)
var color shortFillColor = color.new(color.red, 90)
var color textColor = color.new(color.white, 0)

longStopPlot = plot(dir == 1 ? longStop : na, title='Long Stop', style=plot.style_linebr, linewidth=2, color=color.new(longColor, 0))
buySignal = dir == 1 and dir[1] == -1
plotshape(buySignal ? longStop : na, title='Long Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(longColor, 0))
plotshape(buySignal and showLabels ? longStop : na, title='Buy Label', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(longColor, 0), textcolor=textColor)

shortStopPlot = plot(dir == 1 ? na : shortStop, title='Short Stop', style=plot.style_linebr, linewidth=2, color=color.new(shortColor, 0))
sellSignal = dir == -1 and dir[1] == 1
plotshape(sellSignal ? shortStop : na, title='Short Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(shortColor, 0))
plotshape(sellSignal and showLabels ? shortStop : na, title='Sell Label', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(shortColor, 0), textcolor=textColor)

midPricePlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0, display=display.none, editable=false)

longStateFillColor = highlightState ? dir == 1 ? longFillColor : na : na
shortStateFillColor = highlightState ? dir == -1 ? shortFillColor : na : na
fill(midPricePlot, longStopPlot, title='Long State Filling', color=longStateFillColor)
fill(midPricePlot, shortStopPlot, title='Short State Filling', color=shortStateFillColor)

await = awaitBarConfirmation ? barstate.isconfirmed : true
alertcondition(dir != dir[1] and await, title='Alert: CE Direction Change', message='Chandelier Exit has changed direction!')
alertcondition(buySignal and await, title='Alert: CE Buy', message='Chandelier Exit Buy!')
alertcondition(sellSignal and await, title='Alert: CE Sell', message='Chandelier Exit Sell!')




//---------------------------------------Chandelier Exit conditisions 
//---------long
longc2 = buySignal
longclose2 = sellSignal
//---------short
shortc2 = sellSignal
shortclose2 = buySignal



//---------------------------------------Long entry and exit
if longc1 and longc2 
    strategy.entry("long", strategy.long)

if strategy.position_avg_price > 0
    strategy.exit("close long", "long", limit = takeProfitPrice, stop = stopLossPrice, alert_message = "close all orders")

if longclose1 and longclose2 and strategy.opentrades == 1
    strategy.close("long","ema long cross", alert_message = "close all orders")


//---------------------------------------Short entry and exit
if shortc1 and shortc2 
    strategy.entry("short", strategy.short)

if strategy.position_avg_price > 0
    strategy.exit("close short", "short", limit = takeProfitPrice2, stop = stopLossPrice2, alert_message = "close all orders")

if shortclose1 and shortclose2 and strategy.opentrades == 1
    strategy.close("close short","short", alert_message = "close all orders")