
Chiến lược này là một chiến lược theo dõi xu hướng dừng lỗ dựa trên biến động giá. Nó sử dụng mức biến động thực trung bình (ATR) để thiết lập đường dừng lỗ cho biến động giá. ATR phản ánh sự biến động và mức độ rủi ro của giá.
Chiến lược này đầu tiên tính toán ATR của độ dao động thực tế trung bình trong một chu kỳ nhất định. Sau đó, tùy theo hướng xu hướng giá hiện tại, nếu xu hướng tăng, thiết lập đường dừng là giá cao nhất hiện tại trừ ATR n lần; nếu xu hướng giảm, đường dừng là giá thấp nhất hiện tại cộng với ATR n lần.
Khi giá phá vỡ đường dừng của xu hướng tăng hoặc phá vỡ đường dừng của xu hướng giảm, xác định xu hướng chuyển hướng. Tại thời điểm này, chiến lược xóa lỗ và thiết lập đường dừng mới theo hướng xu hướng mới.
Nhìn chung, chiến lược này có khả năng đánh giá chính xác xu hướng biến đổi bằng cách sử dụng biến động giá để thiết lập đường dừng lỗ. Khi xu hướng biến đổi, dừng lỗ kịp thời sẽ giúp chiến lược nắm bắt hướng xu hướng mới.
Chiến lược này nói chung là một thực hiện thuật toán tốt hơn để thiết lập đường dừng dựa trên biến động giá. Nó đánh giá chính xác hơn về xu hướng giá, có thể nắm bắt các điểm biến động quan trọng của xu hướng. Đồng thời cũng cung cấp một số thông số để điều chỉnh, có khả năng thích ứng. Là một chiến lược dừng lỗ, nó có thể tránh được rủi ro đảo ngược hiệu quả, đáng để sử dụng trong thị trường thực.
/*backtest
start: 2022-11-30 00:00:00
end: 2023-11-30 00:00:00
period: 1d
basePeriod: 1h
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/
// © laptevmaxim92
//@version=4
strategy("Volatility stop strategy", overlay=true)
length = input(20)
mult = input(2, step = 0.1)
utp = input(false, "Use take profit?")
pr = input(100, "Take profit pips")
usl = input(false, "Use stop loss?")
sl = input(100, "Stop loss pips")
fromday = input(01, defval=01, minval=01, maxval=31, title="From Day")
frommonth = input(01, defval=01, minval= 01, maxval=12, title="From Month")
fromyear = input(2000, minval=1900, maxval=2100, title="From Year")
today = input(31, defval=01, minval=01, maxval=31, title="To Day")
tomonth = input(12, defval=12, minval=01, maxval=12, title="To Month")
toyear = input(2039, minval=1900, maxval=2100, title="To Year")
use_date = (time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 00, 00))
atr_ = atr(length)
max_ = 0.0
min_ = 0.0
max1 = 0.0
max1 := max(nz(max_[1]), close)
min1 = 0.0
min1 := min(nz(min_[1]), close)
vstop = 0.0
is_uptrend = false
is_uptrend_prev = false
is_uptrend_prev := nz(is_uptrend[1], true)
stop = is_uptrend_prev ? max1 - mult * atr_ : min1 + mult * atr_
vstop_prev = nz(vstop[1])
vstop1 = is_uptrend_prev ? max(vstop_prev, stop) : min(vstop_prev, stop)
is_uptrend := close - vstop1 >= 0
is_trend_changed = is_uptrend != is_uptrend_prev
max_ := is_trend_changed ? close : max1
min_ := is_trend_changed ? close : min1
vstop := is_trend_changed ? is_uptrend ? max_ - mult * atr_ : min_ + mult * atr_ : vstop1
plot(vstop, color = is_uptrend ? color.green : color.red, linewidth=2)
longCondition = is_uptrend
if (longCondition and use_date)
strategy.entry("BUY", strategy.long)
shortCondition = not is_uptrend
if (shortCondition and use_date)
strategy.entry("SELL", strategy.short)
if (utp and not usl)
strategy.exit("TP", "BUY", profit = pr)
strategy.exit("TP", "SELL", profit = pr)
if (usl and not utp)
strategy.exit("SL", "BUY", loss = sl)
strategy.exit("SL", "SELL", loss = sl)
if (usl and utp)
strategy.exit("TP/SL", "BUY", loss = sl, profit = pr)
strategy.exit("TP/SL", "SELL", loss = sl, profit = pr)