Chiến lược theo dõi dao động băng tần cách ly

Tác giả:ChaoZhang, Ngày: 2024-02-04 10:28:24
Tags:

img

Tổng quan

Ý tưởng chính của chiến lược này là tính toán các đường stop-loss dài và ngắn dựa trên chỉ số ATR. Nó tạo ra các tín hiệu giao dịch khi giá vượt qua các đường stop-loss này. Nó có cả khả năng theo dõi xu hướng và chụp dao động.

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

Chiến lược này sử dụng ATR giai đoạn N nhân một hệ số để tính các đường dừng lỗ dài và ngắn.

Long Stop = Highest Price - ATR * Coefficient 
Short Stop = Lowest Price + ATR * Coefficient

Nó đi dài khi giá tăng và phá vỡ đường dừng lỗ dài, và đi ngắn khi giá giảm và phá vỡ đường dừng lỗ ngắn. Sau khi đi dài hoặc ngắn, nó sẽ theo dõi biến động giá trong thời gian thực để di chuyển các đường dừng lỗ.

Bằng cách sử dụng băng tần ATR làm mức dừng lỗ, phương pháp này có thể nắm bắt hoàn toàn xu hướng giá trong khi đảm bảo rủi ro dừng lỗ. Nó tạo ra tín hiệu khi có sự đột phá đáng kể trong giá, có thể lọc hiệu quả các sự đột phá sai.

Phân tích lợi thế

Lợi thế lớn nhất của chiến lược này là nó có thể tự động điều chỉnh mức dừng lỗ để nắm bắt xu hướng giá trong khi kiểm soát rủi ro.

  1. Chỉ số dừng lỗ thay đổi dựa trên chỉ số ATR có thể điều chỉnh phạm vi dừng lỗ theo sự biến động của thị trường để kiểm soát hiệu quả lỗ đơn.

  2. Việc áp dụng một phương pháp đột phá để tạo ra tín hiệu có thể lọc ra một số tiếng ồn và tránh theo đuổi đỉnh và đáy.

  3. Điều chỉnh thời gian thực của các đường dừng lỗ để theo dõi biến động giá ngăn chặn việc dừng lỗ quá lỏng lẻo và khóa lợi nhuận nhiều hơn.

Phân tích rủi ro

Chiến lược cũng có một số rủi ro, chủ yếu tập trung vào việc thiết lập mức dừng lỗ và tạo tín hiệu.

  1. Chu kỳ ATR và hệ số không đúng có thể dẫn đến stop-loss quá rộng hoặc hẹp.

  2. Phương pháp tín hiệu đột phá có thể bỏ lỡ các cơ hội xu hướng ban đầu.

  3. Có thể có một số sự chậm trễ trong việc theo dõi dừng lỗ trong thời gian kết thúc xu hướng, không thể thoát hoàn hảo.

Các biện pháp đối phó chủ yếu là điều chỉnh các tham số để làm cho giá dừng lỗ hợp lý hơn hoặc hỗ trợ các chỉ số khác để xác định xu hướng và tín hiệu.

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

Chiến lược có thể được tối ưu hóa thêm trong các khía cạnh sau:

  1. Thiết lập một stop-loss lớp thứ hai để kiểm soát rủi ro hơn nữa.

  2. Kết hợp các chỉ số khác để xác định xu hướng và cải thiện chất lượng tín hiệu.

  3. Thêm các chiến lược dừng lợi nhuận để tăng lợi nhuận khi xu hướng tiếp tục.

  4. Tối ưu hóa chu kỳ ATR và các thông số hệ số để làm cho stop-loss gần hơn với biến động giá thực tế.

Tóm lại

Nhìn chung, chiến lược này rất thực tế. Nó có thể kiểm soát hiệu quả rủi ro bằng cách tự động điều chỉnh mức dừng lỗ, trong khi có được lợi nhuận tốt thông qua theo dõi xu hướng. Chúng ta có thể tối ưu hóa và cải thiện thêm chiến lược bằng cách kết hợp các phương pháp phân tích khác trên cơ sở hiện có để làm cho nó ổn định và thông minh hơn.


/*backtest
start: 2024-01-04 00:00:00
end: 2024-02-03 00:00:00
period: 1h
basePeriod: 15m
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/
// © melihtuna
//@version=4
strategy("Chandelier Exit - Strategy",shorttitle="CE-STG" , overlay=true, default_qty_type=strategy.cash, default_qty_value=10000, initial_capital=10000, currency=currency.USD, commission_value=0.03, commission_type=strategy.commission.percent)

length = input(title="ATR Period", type=input.integer, defval=22)
mult = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
showLabels = input(title="Show Buy/Sell Labels ?", type=input.bool, defval=false)
useClose = input(title="Use Close Price for Extremums ?", type=input.bool, defval=true)
highlightState = input(title="Highlight State ?", type=input.bool, defval=true)

atr = mult * atr(length)

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

shortStop = (useClose ? lowest(close, length) : lowest(length)) + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := close[1] < shortStopPrev ? 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

longStopPlot = plot(dir == 1 ? longStop : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
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=longColor, transp=0)
plotshape(buySignal and showLabels ? longStop : na, title="Buy Label", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor, textcolor=color.white, transp=0)

shortStopPlot = plot(dir == 1 ? na : shortStop, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)
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=shortColor, transp=0)
plotshape(sellSignal and showLabels ? shortStop : na, title="Sell Label", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor, textcolor=color.white, transp=0)

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

longFillColor = highlightState ? (dir == 1 ? longColor : na) : na
shortFillColor = highlightState ? (dir == -1 ? shortColor : na) : na
fill(midPricePlot, longStopPlot, title="Long State Filling", color=longFillColor)
fill(midPricePlot, shortStopPlot, title="Short State Filling", color=shortFillColor)


long_short = input(true, "Long-Short",type=input.bool, group="Strategy Settings")

start     = input(timestamp("2019-01-01"), "Date", type=input.time, group="Strategy Settings")
finish    = input(timestamp("2025-01-01"), "Date", type=input.time, group="Strategy Settings")   
window()  => true

slRatio=input(5, "Manuel Stop Loss Ratio", type=input.float, minval=0, group="Strategy Settings")
tpRatio=input(20, "Take Profit Ratio", type=input.float, minval=0, group="Strategy Settings")
tsStartRatio=input(10, "Trailing Stop Start Ratio", type=input.float, minval=0, group="Strategy Settings")
tsRatio=input(5, "Trailing Stop Ratio", type=input.float, minval=1, group="Strategy Settings")

lastBuyPrice = strategy.position_avg_price

diffHiPriceRatio = (high-lastBuyPrice)/lastBuyPrice*100
diffLoPriceRatio = (close-lastBuyPrice)/lastBuyPrice*100
posHiRatio=0.0
posHiRatio:= strategy.position_size > 0 ? diffHiPriceRatio > posHiRatio[1] ? diffHiPriceRatio : posHiRatio[1] : 0

s_diffHiPriceRatio = (low-lastBuyPrice)/lastBuyPrice*100
s_diffLoPriceRatio = (close-lastBuyPrice)/lastBuyPrice*100
s_posHiRatio=0.0
s_posHiRatio:= strategy.position_size < 0 ? s_diffLoPriceRatio < s_posHiRatio[1] ? s_diffLoPriceRatio : s_posHiRatio[1] : 0

strategy.entry("LONG", strategy.long, when = window() and buySignal)
strategy.close("LONG", when = window() and sellSignal)
strategy.close("LONG", when = diffLoPriceRatio<(slRatio*(-1)), comment="STOP-LONG")
strategy.close("LONG", when = diffHiPriceRatio>tpRatio, comment="TAKE-PROFIT-LONG")
strategy.close("LONG", when = ((posHiRatio[1]>tsStartRatio) and (posHiRatio[1]-diffHiPriceRatio)>tsRatio), comment="TRAILING-STOP-LONG")

if long_short
    strategy.entry("SHORT", strategy.short, when = window() and sellSignal)
    strategy.close("SHORT", when = window() and buySignal)
    strategy.close("SHORT", when = s_diffLoPriceRatio>(slRatio), comment="STOP-SHORT")
    strategy.close("SHORT", when = s_diffHiPriceRatio<(tpRatio*(-1)), comment="TAKE-PROFIT-SHORT")
    strategy.close("SHORT", when = ((s_posHiRatio[1]*(-1)>tsStartRatio) and ((s_posHiRatio[1]-s_diffLoPriceRatio))*(-1)>tsRatio), comment="TRAILING-STOP-SHORT")

Thêm nữa