
Chiến lược này là một hệ thống giao dịch đột phá dựa trên đường xu hướng quay ngược tuyến tính. Nó tính toán đường xu hướng quay ngược tuyến tính của giá, giao dịch khi giá vượt qua đường xu hướng một mức độ nhất định, và thiết lập các cơ chế giao dịch dừng lỗ và phản đối. Ý tưởng cốt lõi của chiến lược là nắm bắt hành vi liên tục sau khi giá vượt qua đường xu hướng, đồng thời phản ứng với tín hiệu sai.
Chiến lược sử dụng hàm ta.linreg để tính toán đường xu hướng quay trở lại tuyến tính cho chu kỳ được chỉ định làm cơ sở phán đoán xu hướng chính. Khi giá vượt qua đường xu hướng lên và vượt quá ngưỡng thiết lập, hệ thống tạo ra nhiều tín hiệu; Khi giá vượt qua đường xu hướng xuống và vượt quá ngưỡng thiết lập, hệ thống tạo ra tín hiệu trống.
Chiến lược này xây dựng một hệ thống giao dịch hoàn chỉnh thông qua đường xu hướng quay trở lại tuyến tính và tư tưởng giao dịch đột phá. Nó quản lý rủi ro bằng cách ngăn chặn lỗ và cơ chế giao dịch chống tay, có khả năng theo dõi xu hướng tốt. Tuy nhiên, chiến lược cần thận trọng trong việc đặt tham số và lựa chọn môi trường thị trường, khuyến cáo tối ưu hóa tham số đầy đủ và kiểm tra lại trước khi giao dịch thực.
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-25 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy("BTC Trendline Strategy - 1min - One Direction", overlay=true)
// 输入设置
stop_loss_pct = input.float(10, title="止损百分比", minval=0.1, step=0.1) / 100
take_profit_pct = input.float(10, title="止盈百分比", minval=0.1, step=0.1) / 100
multiplier = input.int(2, title="止损触发时翻倍倍数", minval=1)
length = input.int(20, title="趋势线计算周期", minval=1)
breakout_threshold = input.float(1, title="突破幅度百分比", minval=0.1) / 100 // 设置突破的幅度条件
max_qty = 1000000000000.0 // 设置最大允许的交易量
// 计算线性回归趋势线
regression = ta.linreg(close, length, 0) // 使用线性回归计算价格的趋势线
// 绘制趋势线
plot(regression, color=color.blue, linewidth=2, title="线性回归趋势线")
// 判断突破条件:增加一个价格偏差条件
long_condition = close > (regression * (1 + breakout_threshold)) // 当前价格高于趋势线且突破幅度超过设定百分比时做多
short_condition = close < (regression * (1 - breakout_threshold)) // 当前价格低于趋势线且突破幅度超过设定百分比时做空
// 确保每次只能有一个方向持仓:避免多空同时持仓
if (strategy.position_size == 0) // 当前没有持仓时
if (long_condition)
strategy.entry("Long", strategy.long)
if (short_condition)
strategy.entry("Short", strategy.short)
// 止损和止盈设置
long_stop_loss = strategy.position_avg_price * (1 - stop_loss_pct)
long_take_profit = strategy.position_avg_price * (1 + take_profit_pct)
short_stop_loss = strategy.position_avg_price * (1 + stop_loss_pct)
short_take_profit = strategy.position_avg_price * (1 - take_profit_pct)
// 绘制止损和止盈线,便于调试
plot(long_stop_loss, color=color.red, linewidth=1, title="Long Stop Loss")
plot(long_take_profit, color=color.green, linewidth=1, title="Long Take Profit")
plot(short_stop_loss, color=color.red, linewidth=1, title="Short Stop Loss")
plot(short_take_profit, color=color.green, linewidth=1, title="Short Take Profit")
// 止损和止盈退出策略
strategy.exit("LongExit", from_entry="Long", stop=long_stop_loss, limit=long_take_profit)
strategy.exit("ShortExit", from_entry="Short", stop=short_stop_loss, limit=short_take_profit)
// 反手交易逻辑
reverse_qty = math.min(math.abs(strategy.position_size) * multiplier, max_qty) // 限制最大交易量
if (strategy.position_size < 0 and close > short_stop_loss) // 空单止损时,反手做多并翻倍仓位
strategy.entry("Long Reverse", strategy.long, qty=reverse_qty)
if (strategy.position_size > 0 and close < long_stop_loss) // 多单止损时,反手做空并翻倍仓位
strategy.entry("Short Reverse", strategy.short, qty=reverse_qty)
// 打印日志帮助调试止损
if (strategy.position_size > 0)
label.new(bar_index, close, text="Long SL: " + str.tostring(long_stop_loss), color=color.green, style=label.style_label_up)
if (strategy.position_size < 0)
label.new(bar_index, close, text="Short SL: " + str.tostring(short_stop_loss), color=color.red, style=label.style_label_down)