
Chiến lược này đưa ra quyết định giao dịch dựa trên độ lệch của đường trung bình di chuyển (MA) và vị trí tương đối của giá với MA. Chiến lược mua khi độ lệch MA lớn hơn mức giảm giá lệch tối thiểu và giá cao hơn MA. Đồng thời, chiến lược sử dụng Trailing Stop Loss để quản lý rủi ro và tái nhập cảnh trong điều kiện cụ thể (Re-Entry).
Chiến lược này đánh giá xu hướng thông qua độ lệch của đường trung bình di chuyển và vị trí tương đối của giá với đường trung bình di chuyển, và sử dụng cơ chế theo dõi dừng và tái nhập có điều kiện để quản lý giao dịch. Ưu điểm của chiến lược là khả năng theo dõi xu hướng, bảo vệ dừng động và nắm bắt cơ hội tái nhập. Tuy nhiên, chiến lược cũng có các vấn đề tiềm ẩn như nhạy cảm tham số, lỗi nhận dạng xu hướng, tần suất dừng và rủi ro tái nhập.
/*backtest
start: 2024-05-01 00:00:00
end: 2024-05-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("MA Incline Strategy with Trailing Stop-Loss and Conditional Re-Entry", overlay=true, calc_on_every_tick=true)
// Input parameters
windowSize = input.int(10, title="Window Size")
maLength = input.int(150, title="Moving Average Length")
minSlope = input.float(0.001, title="Minimum Slope")
trailingStopPercentage = input.float(2.8, title="Trailing Stop Percentage (%)") / 100
reEntryPercentage = input.float(4.2, title="Re-Entry Percentage Above MA (%)") / 100
// Calculate the moving average
ma = ta.sma(close, maLength)
// Calculate the slope of the moving average over the window size
previousMa = ta.sma(close[windowSize], maLength)
slopeMa = (ma - previousMa) / windowSize
// Check conditions
isAboveMinSlope = slopeMa > minSlope
isAboveMa = close > ma
// Variables to track stop loss and re-entry condition
var bool stopLossOccurred = false
var float trailStopPrice = na
// Buy condition
buyCondition = isAboveMinSlope and isAboveMa and ((not stopLossOccurred) or (stopLossOccurred and low < ma * (1 + reEntryPercentage)))
// Execute strategy
if (buyCondition and strategy.opentrades == 0)
if (stopLossOccurred and close < ma * (1 + reEntryPercentage))
strategy.entry("Long", strategy.long)
stopLossOccurred := false
else if (not stopLossOccurred)
strategy.entry("Long", strategy.long)
// Trailing stop-loss
if (strategy.opentrades == 1)
// Calculate the trailing stop price
trailStopPrice := close * (1 - trailingStopPercentage)
// Use the built-in strategy.exit function with the trailing stop
strategy.exit("Trail Stop", "Long", stop=close * (1 - trailingStopPercentage))
// Exit condition
sellCondition = ta.crossunder(close, ma)
if (sellCondition and strategy.opentrades == 1)
strategy.close("Long")
// Check if stop loss occurred
if (strategy.closedtrades > 0)
lastExitPrice = strategy.closedtrades.exit_price(strategy.closedtrades - 1)
if (not na(trailStopPrice) and lastExitPrice <= trailStopPrice)
stopLossOccurred := true
// Reset stop loss flag if the price crosses below the MA
if (ta.crossunder(close, ma))
stopLossOccurred := false