
Strategi ini adalah strategi keluar yang menggunakan stop-loss bertahap yang digabungkan dengan stop-loss slippage. Ini akan memindahkan stop-loss ke titik ekuilibrium kerugian setelah mencapai stop-loss pertama, dan stop-loss ke stop-loss pertama setelah mencapai stop-loss kedua, sehingga memungkinkan mekanisme stop-loss slippage bertahap. Ini dapat mengunci sebagian dari keuntungan sambil mempertahankan ruang keuntungan yang lebih besar.
Strategi ini mencakup beberapa bagian utama untuk mencapai titik berhenti geser bertahap:
Secara khusus, ia pertama-tama menetapkan jarak stop loss 100 dan 3 jarak stop loss 100/200/300. Ia kemudian mendefinisikan fungsi untuk menghitung nilai keuntungan berdasarkan harga saat ini dan harga pembukaan posisi.curProfitInPts, dan fungsi stop loss berdasarkan jarak titikcalcStopLossPrice。
Logika yang penting adalah:getCurrentStageFungsi, yang menilai apakah ada posisi saat ini, dan apakah jumlah keuntungan melebihi titik berhenti tertentu, jika melebihi, masuk ke tahap berikutnya. Misalnya, memasuki tahap kedua setelah mencapai titik berhenti 100 dan memasuki tahap ketiga setelah mencapai titik berhenti 200.
Akhirnya, modifikasi harga stop loss sesuai dengan tahap yang berbeda, sehingga mencapai titik stop slip. Pada tahap pertama, stop loss tetap pada pengaturan awal, pada tahap kedua pindah ke profit dan loss equity, dan pada tahap ketiga pindah ke titik stop pertama.
Strategi stop-slip bertingkat ini memiliki beberapa keuntungan:
Strategi ini juga memiliki beberapa risiko:
Strategi ini dapat dioptimalkan dari beberapa arah:
/*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)