This strategy uses Bollinger Band breakouts to generate trading signals and implement pair trading between two positively correlated assets MCL and YG. It goes long MCL and short YG when MCL price touches the upper band, and goes short MCL and long YG when MCL price touches the lower band, to trade along the price trend.
Firstly, the strategy calculates the SMA line and StdDev based on closing prices over a certain period. Then it adds an offset above and below the SMA to form the upper and lower bands of the Bollinger Bands. A buy signal is generated when price touches the upper band, and a sell signal when price touches the lower band.
The strategy utilizes the breakout trading logic of Bollinger Bands - go long when price breaks above the upper band and go short when price breaks below the lower band. Bollinger Bands dynamically adjust the width of the bands based on market volatility, which helps filter out market noise during ranging periods. Unlike fixed channel bands, Bollinger Bands widen during high volatility and narrow during low volatility. This allows it to filter some noise when volatility is high, and capture smaller breakouts when volatility is low.
It implements pair trading between two positively correlated assets MCL and YG. When MCL breaks above the upper band, it shows MCL is in an uptrend. The strategy goes long MCL and short YG - buying the stronger asset and selling the weaker one, to benefit from the divergence in their prices.
Risks can be reduced by optimizing parameters, selecting assets with stronger correlation and liquidity, setting proper stop loss etc.
Overall the strategy is simple and straightforward, capturing trends with Bollinger Bands and gaining alpha from pair trading. But there is room for improvement in parameters tuning, stop loss, and pair selection. Further testing of parameters, trading vehicles, trend filters etc. can improve the strategy performance.
/*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)