
La estrategia es un sistema de negociación de ruptura basado en la línea de tendencia de retorno lineal. Se realiza mediante el cálculo de la línea de tendencia de retorno lineal del precio, se realiza la negociación cuando el precio rompe la línea de tendencia por un cierto margen, y se establece una parada de pérdida y un mecanismo de negociación de contra-mano. La idea central de la estrategia es capturar el comportamiento persistente después de que el precio rompa la línea de tendencia, mientras que se responde a la señal errónea mediante un mecanismo de negociación de contra-mano.
La estrategia utiliza la función ta.linreg para calcular la línea de tendencia de regresión lineal de un período determinado como la base principal para determinar la tendencia. Cuando el precio rompe la línea de tendencia hacia arriba y supera el umbral establecido, el sistema genera una señal múltiple. Cuando el precio rompe la línea de tendencia hacia abajo y supera el umbral establecido, el sistema genera una señal de vacío.
La estrategia construye un sistema de negociación completo a través de una línea de tendencia de retorno lineal y una idea de negociación de ruptura. Gestiona el riesgo mediante un mecanismo de negociación de stop loss y anti-manos, con una mejor capacidad de seguimiento de tendencias. Sin embargo, la estrategia requiere precaución en la configuración de parámetros y la selección del entorno de mercado, y se recomienda una adecuada optimización de parámetros y verificación de retroalimentación antes de la negociación en vivo.
/*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)