
Chiến lược này là một chiến lược rút lui sử dụng điểm dừng bậc thang kết hợp với điểm dừng lỗ. Nó sẽ di chuyển dừng lỗ đến điểm cân bằng lỗ khi đạt đến điểm dừng đầu tiên, và sẽ di chuyển dừng lỗ đến điểm dừng đầu tiên khi đạt đến điểm dừng thứ hai, do đó thực hiện một cơ chế dừng lỗ bậc thang. Điều này có thể khóa một phần lợi nhuận trong khi vẫn giữ được không gian lợi nhuận lớn hơn.
Chiến lược này được thực hiện thông qua các phần sau:
Cụ thể, nó đặt khoảng cách dừng 100 điểm và 3 khoảng cách dừng 100/200/300 điểm. Sau đó, định nghĩa hàm tính toán lợi nhuận dựa trên giá hiện tại và giá mở vị trícurProfitInPts, và hàm tính giá dừng lỗ dựa trên khoảng cách điểmcalcStopLossPrice。
Lý luận quan trọng là:getCurrentStageChức năng, nó đánh giá liệu có vị trí hiện tại hay không, và liệu số tiền được hưởng đã vượt quá một điểm dừng nào đó hay không, nếu vượt quá, nó sẽ đi đến giai đoạn tiếp theo. Ví dụ: đi đến giai đoạn thứ hai sau khi đạt đến 100 điểm dừng và đi đến giai đoạn thứ ba sau khi đạt đến 200 điểm dừng.
Cuối cùng, tùy theo giai đoạn, thay đổi giá dừng để thực hiện điểm dừng trượt. Ở giai đoạn đầu tiên, điểm dừng giữ được thiết lập ban đầu, ở giai đoạn thứ hai chuyển sang cân bằng lỗ hổng, và ở giai đoạn thứ ba chuyển sang điểm dừng đầu tiên.
Chiến lược điểm dừng trượt bậc thang này có một số lợi thế:
Chiến lược này cũng có một số rủi ro:
Chiến lược này có thể được tối ưu hóa theo các hướng sau:
/*backtest
start: 2023-11-20 00:00:00
end: 2023-11-27 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/
// © adolgov
// @description
// when tp1 is reached, sl is moved to break-even
// when tp2 is reached, sl is moved to tp1
// when tp3 is reached - exit
//@version=4
strategy("Stepped trailing strategy example", overlay=true)
// random entry condition
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
// sl & tp in points
sl = input(100)
tp1 = input(100)
tp2 = input(200)
tp3 = input(300)
curProfitInPts() =>
if strategy.position_size > 0
(high - strategy.position_avg_price) / syminfo.mintick
else if strategy.position_size < 0
(strategy.position_avg_price - low) / syminfo.mintick
else
0
calcStopLossPrice(OffsetPts) =>
if strategy.position_size > 0
strategy.position_avg_price - OffsetPts * syminfo.mintick
else if strategy.position_size < 0
strategy.position_avg_price + OffsetPts * syminfo.mintick
else
0
calcProfitTrgtPrice(OffsetPts) =>
calcStopLossPrice(-OffsetPts)
getCurrentStage() =>
var stage = 0
if strategy.position_size == 0
stage := 0
if stage == 0 and strategy.position_size != 0
stage := 1
else if stage == 1 and curProfitInPts() >= tp1
stage := 2
else if stage == 2 and curProfitInPts() >= tp2
stage := 3
stage
stopLevel = -1.
profitLevel = calcProfitTrgtPrice(tp3)
// based on current stage set up exit
// note: we use same exit ids ("x") consciously, for MODIFY the exit's parameters
curStage = getCurrentStage()
if curStage == 1
stopLevel := calcStopLossPrice(sl)
strategy.exit("x", loss = sl, profit = tp3, comment = "sl or tp3")
else if curStage == 2
stopLevel := calcStopLossPrice(0)
strategy.exit("x", stop = stopLevel, profit = tp3, comment = "breakeven or tp3")
else if curStage == 3
stopLevel := calcStopLossPrice(-tp1)
strategy.exit("x", stop = stopLevel, profit = tp3, comment = "tp1 or tp3")
else
strategy.cancel("x")
// this is debug plots for visulalize TP & SL levels
plot(stopLevel > 0 ? stopLevel : na, style = plot.style_linebr)
plot(profitLevel > 0 ? profitLevel : na, style = plot.style_linebr)