
该策略采用布林带突破来检测交易信号,实现了对MCL和YG两个正相关资产的配对交易。当MCL价格触碰布林带上轨时,做多MCL和做空YG;当MCL价格触碰布林带下轨时,做空MCL和做多YG,实现对价格趋势的顺势交易。
首先,该策略基于一定周期内的收盘价计算出SMA均线和标准差StdDev。然后在SMA均线上下各添加一个偏移量,形成布林带的上轨和下轨。当价格触碰上轨时产生买入信号,触碰下轨时产生卖出信号。
该策略采用了布林带的突破交易思路,即价格突破上轨时看多,突破下轨时看空。布林带通过动态调整通道宽度来适应市场变化,能够有效过滤震荡市场的噪音。与固定通道不同,布林带的通道宽度会随着市场波动性的变化而扩大或缩小。在波动性较大时,布林带上下轨间隙较大,可以过滤掉部分噪音。而在波动性较小时,布林带上下轨间隙较小,可以捕捉较小的突破信号。
对两个正相关资产MCL和YG进行配对交易。当MCL突破上轨时,表明MCL价格处于上升趋势,此时做多MCL,同时做空YG,即买入较强的资产,卖出较弱的资产,以获利于两个资产价格差异的扩大。
可以通过优化参数,选择相关性更强、流动性较好的交易对象,设定合理的止损位置等方法来降低风险。
该策略整体来看较为简单直接,通过布林带捕捉趋势,配对交易获得alpha收益。但存在一些参数优化、止损以及配对选择等可优化空间。通过测试不同参数、交易对象,并适当引入趋势过滤等方法,可以获得更好的策略效果。
/*backtest
start: 2022-11-07 00:00:00
end: 2023-11-13 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © shark792
//@version=5
// 1. Define strategy settings
strategy(title="MCL-YG Pair Trading Strategy", overlay=true,
pyramiding=0, initial_capital=10000,
commission_type=strategy.commission.cash_per_order,
commission_value=4, slippage=2)
smaLength = input.int(title="SMA Length", defval=20)
stdLength = input.int(title="StdDev Length", defval=20)
ubOffset = input.float(title="Upper Band Offset", defval=1, step=0.5)
lbOffset = input.float(title="Lower Band Offset", defval=1, step=0.5)
usePosSize = input.bool(title="Use Position Sizing?", defval=true)
riskPerc = input.float(title="Risk %", defval=0.5, step=0.25)
// 2. Calculate strategy values
smaValue = ta.sma(close, smaLength)
stdDev = ta.stdev(close, stdLength)
upperBand = smaValue + (stdDev * ubOffset)
lowerBand = smaValue - (stdDev * lbOffset)
riskEquity = (riskPerc / 100) * strategy.equity
atrCurrency = (ta.atr(20) * syminfo.pointvalue)
posSize = usePosSize ? math.floor(riskEquity / atrCurrency) : 1
// 3. Output strategy data
plot(series=smaValue, title="SMA", color=color.teal)
plot(series=upperBand, title="UB", color=color.green,
linewidth=2)
plot(series=lowerBand, title="LB", color=color.red,
linewidth=2)
// 4. Determine long trading conditions
enterLong = ta.crossover(close, upperBand)
exitLong = ta.crossunder(close, smaValue)
// 5. Code short trading conditions
enterShort = ta.crossunder(close, lowerBand)
exitShort = ta.crossover(close, smaValue)
// 6. Submit entry orders
if enterLong
strategy.entry(id="EL", direction=strategy.long, qty=posSize)
if enterShort
strategy.entry(id="ES", direction=strategy.short, qty=posSize)
// 7. Submit exit orders
strategy.close(id="EL", when=exitLong)
strategy.close(id="ES", when=exitShort)