A estratégia é um sistema de negociação de ruptura baseado em uma linha de tendência de retorno linear. Ela opera a linha de tendência de retorno linear calculada pelo preço, negocia quando o preço ultrapassa a linha de tendência em uma certa amplitude e configura um stop loss e um mecanismo de negociação de contra-mão.
A estratégia usa a função ta.linreg para calcular a linha de tendência de retorno linear do período especificado como base principal de determinação da tendência. Quando o preço ultrapassa a linha de tendência para cima e a amplitude excede o limiar definido, o sistema gera um sinal de multiplicação; Quando o preço ultrapassa a linha de tendência para baixo e a amplitude excede o limiar definido, o sistema gera um sinal de cancelamento.
A estratégia constrói um sistema de negociação completo através de linhas de tendência de retorno linear e ideias de negociação de ruptura. Gerencia o risco através de um mecanismo de negociação de stop-loss e de contra-mão, com uma melhor capacidade de acompanhamento de tendências. Mas a estratégia precisa ser cautelosa na configuração de parâmetros e na escolha do ambiente de mercado, recomendando a otimização e o retorno de parâmetros suficientes antes da negociação física.
/*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)