Skip to content

Instantly share code, notes, and snippets.

@Terryhung
Created March 25, 2025 11:00
Show Gist options
  • Save Terryhung/d2356d47c8502bc5fa9502ab9b73f625 to your computer and use it in GitHub Desktop.
Save Terryhung/d2356d47c8502bc5fa9502ab9b73f625 to your computer and use it in GitHub Desktop.

加密貨幣期貨現貨套利策略簡報

策略簡介

  • 策略名稱: 加密貨幣期貨現貨套利(Funding Rate 套利)
  • 策略目標:
    透過對沖現貨與期貨倉位,在永續期貨資金費率機制下捕捉收益。
  • 基本概念:
    • 當 funding rate 為正:多頭支付費用,空頭獲得收益 → 策略:做空期貨、持有現貨
    • 當 funding rate 為負:反之則操作 → 策略:做多期貨、空現貨

Funding Rate 機制解析

  • Funding Rate 說明:
    • 永續期貨合約會依照現貨價格進行調整,透過定時收取或支付資金費用來維持價格貼近現貨。
    • 資金費率正負代表不同的市場情緒與套利機會。
  • 套利思路:
    • 若 funding rate 為正:做空期貨、做多現貨以獲取正費率收益
    • 若 funding rate 為負:做多期貨、空現貨以捕捉反向收益

策略流程架構

  1. 數據擷取: 取得現貨、期貨行情及 funding rate
  2. 套利判斷: 檢查 funding rate 是否達到進場門檻,並同時參考現貨與期貨價格差
  3. 交易決策: 根據 funding rate 正負決定對沖方向
  4. 交易執行: 建立對沖倉位
  5. 風險管理: 持續監控,必要時平倉止損

市場數據擷取模組 (Python)

def fetch_market_data(asset):
    """
    從交易所 API 取得現貨與期貨行情以及 funding rate
    """
    spot_data = api.get_spot_data(asset)           # 例如:{'price': 50000, ...}
    futures_data = api.get_futures_data(asset)       # 例如:{'price': 50200, ...}
    funding_rate = api.get_funding_rate(asset)       # 例如:0.0012 表示 0.12%
    return spot_data, futures_data, funding_rate

套利機會判斷模組 (Python)

def check_arbitrage_opportunity(spot_data, futures_data, funding_rate, threshold):
    """
    判斷是否存在套利機會:
    若 abs(funding_rate) 超過設定門檻,則認定存在套利機會
    """
    price_diff = futures_data['price'] - spot_data['price']
    if abs(funding_rate) > threshold:
        return True, price_diff, funding_rate
    else:
        return False, price_diff, funding_rate

交易策略決策模組 (Python)

def strategy_decision(funding_rate, parameters):
    """
    根據 funding_rate 與設定參數決定交易信號
    parameters 範例: {'positive_threshold': 0.0005, 'negative_threshold': 0.0005}
    """
    if funding_rate > parameters['positive_threshold']:
        # funding rate 正:做空期貨,持有現貨
        signal = "short futures, long spot"
    elif funding_rate < -parameters['negative_threshold']:
        # funding rate 負:做多期貨,空現貨
        signal = "long futures, short spot"
    else:
        signal = "hold"
    return signal

交易執行模組 (Python)

def execute_trade(signal, asset, quantity):
    """
    根據交易信號在現貨與期貨市場下單
    """
    if signal == "short futures, long spot":
        api.place_order("buy", "spot", asset, quantity)
        api.place_order("sell", "futures", asset, quantity)
    elif signal == "long futures, short spot":
        api.place_order("sell", "spot", asset, quantity)
        api.place_order("buy", "futures", asset, quantity)
    else:
        print("No trade executed, holding position.")

風險管理模組 (Python)

def risk_management(position, current_price, stop_loss, take_profit):
    """
    持續監控倉位,當價格觸及停損或止盈點時平倉
    """
    if current_price <= stop_loss or current_price >= take_profit:
        api.close_position(position)
        print("Position closed due to risk limits.")

策略整合

def main():
    asset = "BTC"
    quantity = 0.1  # 交易數量
    threshold = 0.0005  # funding rate 閥值
    parameters = {'positive_threshold': 0.0005, 'negative_threshold': 0.0005}
    
    while api.market_is_open():
        spot_data, futures_data, funding_rate = fetch_market_data(asset)
        opportunity, price_diff, current_funding = check_arbitrage_opportunity(
            spot_data, futures_data, funding_rate, threshold)
        
        if opportunity:
            signal = strategy_decision(current_funding, parameters)
            execute_trade(signal, asset, quantity)
        else:
            print("No arbitrage opportunity detected.")
        
        # 取得當前倉位與市場價格作風險管理判斷
        current_position = api.get_current_position(asset)
        current_price = api.get_current_market_price(asset)
        stop_loss = current_price * 0.95   # 假設停損 5%
        take_profit = current_price * 1.05 # 假設止盈 5%
        risk_management(current_position, current_price, stop_loss, take_profit)

策略優化與注意事項

  • 回測與優化:
    • 利用歷史數據模擬測試策略表現,調整 funding rate 閥值與交易參數
    • 考慮手續費、滑點與借貸成本的影響
  • 風險提示:
    • 注意現貨與期貨價格可能產生偏離(基差風險)
    • 持續監控市場波動並及時調整策略參數
  • 技術重點:
    • 數據準確性與即時性
    • 交易執行的速度與同步性
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment