该策略是一个基于线性回归趋势线的突破交易系统。它通过计算价格的线性回归趋势线,在价格突破趋势线一定幅度时进行交易,并设置了止损止盈和反手交易机制。策略的核心思想是捕捉价格突破趋势线后的持续性行情,同时通过反手交易机制来应对错误信号。
策略使用ta.linreg函数计算指定周期的线性回归趋势线作为主要的趋势判断依据。当价格向上突破趋势线且幅度超过设定阈值时,系统产生做多信号;当价格向下突破趋势线且幅度超过设定阈值时,系统产生做空信号。策略采用单向持仓机制,即同一时间只允许持有多头或空头仓位。为了控制风险,设置了止损和止盈条件,同时引入了反手交易机制,在止损触发时自动反向开仓并按设定倍数增加仓位。
该策略通过线性回归趋势线和突破交易思想构建了一个完整的交易系统。通过止损止盈和反手交易机制来管理风险,具有较好的趋势跟踪能力。但策略在参数设置和市场环境选择上需要谨慎,建议在实盘交易前进行充分的参数优化和回测验证。未来可以通过引入更多的技术指标和优化交易规则来提高策略的稳定性和适应性。
/*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)