Time-Lag Arbitrage Strategy


Created on: 2025-09-04 10:15:01 Modified on: 2025-09-10 11:51:36
Copy: 0 Number of hits: 102
avatar of ianzeng123 ianzeng123
2
Follow
319
Followers

 Time-Lag Arbitrage Strategy  Time-Lag Arbitrage Strategy

双胞胎价差套利策略 | Twin Price Arbitrage Strategy

PAIR TRADING, ARBITRAGE, CORRELATION

🎭 What’s This Amazing Operation? Two Coins Can Be Traded Like This!

Did you know? There’s a trading method as interesting as observing behavioral differences between twins! This strategy specifically watches two highly correlated trading pairs (like TRUMP and MELANIA), and when their price movements become “out of sync,” that’s our money-making opportunity!

Key point! This isn’t about betting on ups and downs, but capturing the reversion after “relationship imbalance.” It’s like twins usually walking in sync, but when one suddenly walks faster, the other will definitely catch up~

📊 Core Strategy Logic: Finding “Imbalance” Means Finding Opportunity

The essence of this strategy lies in calculating the difference between two coins’ price change ratios. When the difference exceeds the set threshold (default 2%): - Difference too large → Go long on the relatively lagging coin - Difference too small → Go short on the relatively leading coin

Pitfall Guide: Don’t use this strategy on completely unrelated coins - it’s like expecting apples and oranges to have the same price movements!

⚙️ Parameter Settings: Simple and Straightforward

Trading Trigger Conditions: - Price difference threshold: 2% (adjustable) - Trade quantity: 100 (adjust based on capital)

Risk Control: - Take profit: 5% - Stop loss: 3%

This setup is like installing “airbags” for your trading - it captures opportunities while keeping losses under control!

🎯 Use Cases: When Is This Move Most Effective?

Best Usage Timing: 1. Strong historical correlation between two coins 2. Moderate market volatility (avoid using in extreme market conditions) 3. Sufficient liquidity support

Friendly Reminder: This strategy works best in ranging markets, like fishing on a calm lake - when it’s too stormy, it’s better to wait it out!

Remember, trading isn’t gambling, but doing the right thing at the right time. What this strategy teaches us is: sometimes, observing “relationships” is more important than predicting “direction”! 🚀

Strategy source code
/*backtest
start: 2025-01-20 17:00:00
end: 2025-01-22 07:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"MELANIA_USDT","balance":500000,"tradesMode":"1"}]
*/

//@version=5
strategy("配对交易策略", overlay=true)

// 输入参数
pair_a = input("TRUMP_USDT.swap", title="交易对A", group="交易设置")
pair_b = input("MELANIA_USDT.swap", title="交易对B", group="交易设置")
trade_number = input.float(100, title="交易数量", minval=0.01, group="交易设置")
diff_level = input.float(0.02, title="差价阈值", minval=0.001, step=0.001, group="交易设置")
stop_profit_level = input.float(0.05, title="止盈比例", minval=0.001, step=0.001, group="风险管理")
stop_loss_level = input.float(0.03, 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="配对交易策略:卖出信号触发")