
Chiến lược này dựa trên đường hồi quy tuyến tính và đường trung bình di chuyển, thiết kế một hệ thống giao dịch theo dõi xu hướng đơn giản. Khi đường hồi quy tuyến tính đi qua đường trung bình di chuyển, hãy làm nhiều và khi đường hồi quy tuyến tính đi qua đường trung bình di chuyển, hãy làm trống.
Trend Following Regression Trading Strategy (Chiến lược giao dịch theo hồi quy xu hướng)
Chiến lược này bao gồm các phần quan trọng sau:
Đường co lại tuyến tính phù hợp với hướng xu hướng trong một khoảng thời gian gần đây. Điều này có thể được sử dụng để xác định hướng của xu hướng tổng thể. Khi giá phá vỡ đường SMA, chúng tôi cần phải đánh giá thêm về hướng của đường co lại tuyến tính có phù hợp với sự phá vỡ này hay không. Chỉ khi hai hướng này phù hợp, tín hiệu giao dịch sẽ được tạo ra.
Ngoài ra, chiến lược này cũng đặt một cơ chế dừng lỗ. Khi giá chạm vào đường dừng lỗ, lệnh dừng lỗ sẽ được thực hiện. Cũng có một đường dừng, khóa một phần lợi nhuận.
Chiến lược này có những ưu điểm sau:
Chiến lược này cũng có một số rủi ro:
Chúng ta có thể tối ưu hóa đối với những rủi ro này bằng cách:
Chiến lược này có thể được tối ưu hóa bằng cách:
Chiến lược này kết hợp các tính năng theo dõi xu hướng của trung bình di chuyển với các tính năng phán đoán xu hướng của sự hồi phục tuyến tính, tạo thành một hệ thống giao dịch theo dõi xu hướng tương đối đơn giản và dễ dàng. Trong thị trường có xu hướng rõ ràng, chiến lược này có thể có hiệu quả tốt hơn.
/*backtest
start: 2023-11-17 00:00:00
end: 2023-12-05 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy(title="Regression Trading Strategy", shorttitle="RTS", overlay=true)
// Input parameters
n = input(14, title="SMA Period")
stop_loss_percentage = input(2, title="Stop Loss Percentage")
take_profit_percentage = input(2, title="Take Profit Percentage")
// Calculate the SMA
sma = sma(close, n)
// Linear regression function
linear_regression(src, length) =>
sumX = 0.0
sumY = 0.0
sumXY = 0.0
sumX2 = 0.0
for i = 0 to length - 1
sumX := sumX + i
sumY := sumY + src[i]
sumXY := sumXY + i * src[i]
sumX2 := sumX2 + i * i
slope = (length * sumXY - sumX * sumY) / (length * sumX2 - sumX * sumX)
intercept = (sumY - slope * sumX) / length
line = slope * length + intercept
line
// Calculate the linear regression
regression_line = linear_regression(close, n)
// Plot the SMA and regression line
plot(sma, title="SMA", color=color.blue)
plot(regression_line, title="Regression Line", color=color.red)
// Trading strategy conditions
long_condition = crossover(close, sma) and close > regression_line
short_condition = crossunder(close, sma) and close < regression_line
// Exit conditions
stop_loss_price = close * (1 - stop_loss_percentage / 100)
take_profit_price = close * (1 + take_profit_percentage / 100)
// Plot entry and exit points on the chart
plotshape(series=long_condition, title="Long Entry", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(series=short_condition, title="Short Entry", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
plotshape(series=crossunder(close, stop_loss_price), title="Stop Loss", location=location.abovebar, color=color.red, style=shape.labeldown, text="SL")
plotshape(series=crossover(close, take_profit_price), title="Take Profit", location=location.belowbar, color=color.green, style=shape.labelup, text="TP")
// Strategy orders
strategy.entry("Long", strategy.long, when = long_condition)
strategy.entry("Short", strategy.short, when = short_condition)
strategy.exit("Exit", from_entry = "Long", when = crossover(close, stop_loss_price) or crossover(close, take_profit_price))
strategy.exit("Exit", from_entry = "Short", when = crossunder(close, stop_loss_price) or crossunder(close, take_profit_price))