本策略是一种基于多周期中轴和相对强弱指数(RSI)的动态趋势突破交易策略。通过结合周线级别的价格支撑和压力位与RSI指标,该策略旨在捕捉金融市场中的趋势性机会,同时提供精细的仓位管理和风险控制机制。
策略的核心原理包括以下关键步骤:
该策略通过多周期、多指标的综合分析,构建了一个相对稳健的趋势突破交易方法。其核心优势在于对市场趋势的动态捕捉和精细化的风险管理。未来的优化空间包括算法智能化和风险控制模型的迭代。
/*backtest
start: 2024-03-31 00:00:00
end: 2025-03-29 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © yuxishejiang
//@version=6
//@version=5
strategy(title="BTC中轴策略优化-V2", overlay=true, pyramiding=1, initial_capital=5000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, calc_on_order_fills=false, slippage=0, commission_type=strategy.commission.percent, commission_value=0.075)
// 核心参数
strat_dir_input = input.string(title="Strategy Direction", defval="long", options=["long", "short"])
strat_dir_value = strat_dir_input == "long" ? strategy.direction.long : strat_dir_input == "short" ? strategy.direction.short : strategy.direction.all
strategy.risk.allow_entry_in(strat_dir_value)
// 指标计算
higherTF = input.timeframe("W", "Higher Timeframe")
pc = request.security(syminfo.tickerid, higherTF, close[1], barmerge.gaps_off, barmerge.lookahead_on)
ph = request.security(syminfo.tickerid, higherTF, high[1], barmerge.gaps_off, barmerge.lookahead_on)
pl = request.security(syminfo.tickerid, higherTF, low[1], barmerge.gaps_off, barmerge.lookahead_on)
PP = (ph + pl + pc) / 3
R1 = PP + (PP - pl)
S1 = PP - (ph - PP)
R2 = PP + (ph - pl)
S2 = PP - (ph - pl)
factor = input.int(2, "Factor")
R3 = ph + factor * (PP - pl)
S3 = pl - 2 * (ph - PP)
length = input.int(21, "RSI Length")
p = close
vrsi = ta.rsi(p, length)
pp_ema = ta.ema(vrsi, length)
d = (vrsi - pp_ema) * 5
cc = (vrsi + d + pp_ema) / 2
// 仓位管理变量
var float entry_qty = na
// 交易执行逻辑
longEntry = ta.crossover(cc, 0)
longExit = ta.crossover(high, R3) // 使用实时最高价判断
shortEntry = ta.crossunder(low, S3) // 改为使用S3支撑位
shortExit = ta.crossunder(cc, 0) // 同步修改为下穿
if (longEntry)
strategy.entry("Long", strategy.long)
entry_qty := strategy.position_size
if (strategy.position_size > 0 and longExit)
strategy.close("Long", comment="5M背离离场")
if (shortEntry)
strategy.entry("Short", strategy.short)
entry_qty := strategy.position_size
if (strategy.position_size < 0 and shortExit)
strategy.close("Short", comment="空头离场")
// 止盈止损模块
per(pcnt) =>
strategy.position_size != 0 ? math.round(math.abs(pcnt/100 * strategy.position_avg_price / syminfo.mintick)) : na
stoploss = input.float(15, "Stop Loss (%)", minval=0.01)
tp1 = input.float(3, "Take Profit 1 (%)", minval=0.01)
tp2 = input.float(5, "Take Profit 2 (%)", minval=0.01)
tp3 = input.float(7, "Take Profit 3 (%)", minval=0.01)
tp4 = input.float(10, "Take Profit 4 (%)", minval=0.01)
// 分阶段平仓逻辑
if strategy.position_size != 0
qty_total = math.abs(entry_qty)
qty1 = math.floor(qty_total * 0.25)
qty2 = math.floor(qty_total * 0.25)
qty3 = math.floor(qty_total * 0.25)
qty4 = qty_total - (qty1 + qty2 + qty3)
if strategy.position_size > 0
strategy.exit("x1", qty=qty1, profit=per(tp1), loss=per(stoploss))
strategy.exit("x2", qty=qty2, profit=per(tp2), loss=per(stoploss))
strategy.exit("x3", qty=qty3, profit=per(tp3), loss=per(stoploss))
strategy.exit("x4", qty=qty4, profit=per(tp4), loss=per(stoploss))
else
strategy.exit("x1", qty=qty1, profit=per(tp1), loss=per(stoploss))
strategy.exit("x2", qty=qty2, profit=per(tp2), loss=per(stoploss))
strategy.exit("x3", qty=qty3, profit=per(tp3), loss=per(stoploss))
strategy.exit("x4", qty=qty4, profit=per(tp4), loss=per(stoploss))
// 可视化部分保持不变
// 多头入场可视化
if (longEntry)
label.new(bar_index, low, "多头入场", color=color.green, textcolor=color.white, style=label.style_label_up, size=size.small)
// 多头离场可视化
if (strategy.position_size > 0 and longExit)
label.new(bar_index, high, "多头离场", color=color.red, textcolor=color.white, style=label.style_label_down, size=size.small)
// 空头入场可视化
if (shortEntry)
label.new(bar_index, high, "空头入场", color=color.red, textcolor=color.white, style=label.style_label_down, size=size.small)
// 空头离场可视化
if (strategy.position_size < 0 and shortExit)
label.new(bar_index, low, "空头离场", color=color.green, textcolor=color.white, style=label.style_label_up, size=size.small)