策略源码
/*backtest
start: 2025-01-01 00:00:00
end: 2025-09-09 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
args: [["ContractType","j888",360008]]
*/
//@version=5
strategy("配对交易策略", overlay=true)
// 输入参数
pair_a = input("jm888", title="交易对A", group="交易设置")
pair_b = input("j888", title="交易对B", group="交易设置")
trade_number = input.float(1, title="交易数量", minval=0.01, group="交易设置")
diff_level = input.float(0.005, title="差价阈值", minval=0.001, step=0.001, group="交易设置")
stop_profit_level = input.float(0.04, title="止盈比例", minval=0.001, step=0.001, group="风险管理")
stop_loss_level = input.float(0.02, title="止损比例", minval=0.001, step=0.001, group="风险管理")
// 获取两个交易对的数据
price_a = request.security(pair_a, timeframe.period, close)
open_a = request.security(pair_a, timeframe.period, open)
price_b = close
open_b = open
// 计算价格变化比例差异
change_a = (price_a - open_a) / open_a
change_b = (price_b - open_b) / open_b
ratio = change_a - change_b
// 策略状态变量
var float entry_price = na
var bool in_position = false
var int position_direction = 0 // 1为多头,-1为空头
var float take_profit_price = na
var float stop_loss_price = na
// 交易逻辑
long_condition = not in_position and ratio > diff_level
short_condition = not in_position and ratio < -diff_level
// 开仓逻辑
if long_condition
strategy.entry("Long", strategy.long, qty=trade_number)
entry_price := price_b
in_position := true
position_direction := 1
take_profit_price := entry_price * (1 + stop_profit_level)
stop_loss_price := entry_price * (1 - stop_loss_level)
if short_condition
strategy.entry("Short", strategy.short, qty=trade_number)
entry_price := price_b
in_position := true
position_direction := -1
take_profit_price := entry_price * (1 - stop_profit_level)
stop_loss_price := entry_price * (1 + stop_loss_level)
// 平仓逻辑
if in_position and position_direction == 1
// 多头止盈止损
if price_b >= take_profit_price or price_b <= stop_loss_price
strategy.close("Long")
in_position := false
position_direction := 0
entry_price := na
take_profit_price := na
stop_loss_price := na
if in_position and position_direction == -1
// 空头止盈止损
if price_b <= take_profit_price or price_b >= stop_loss_price
strategy.close("Short")
in_position := false
position_direction := 0
entry_price := na
take_profit_price := na
stop_loss_price := na
// 图表显示
plot(ratio, title="比例差异", color=color.blue, linewidth=2, overlay = false)
hline(diff_level, title="上阈值", color=color.red, linestyle=hline.style_dashed, overlay = false)
hline(-diff_level, title="下阈值", color=color.blue, linestyle=hline.style_dashed, overlay = false)
hline(0, title="零线", color=color.gray, linestyle=hline.style_dotted, overlay = false)
// 标记开仓点
plotshape(long_condition, title="买入信号", location=location.belowbar, style=shape.triangleup, size=size.small, color=color.green)
plotshape(short_condition, title="卖出信号", location=location.abovebar, style=shape.triangledown, size=size.small, color=color.red)
// 警报条件
alertcondition(long_condition, title="买入信号", message="配对交易策略:买入信号触发")
alertcondition(short_condition, title="卖出信号", message="配对交易策略:卖出信号触发")