使用CoinGlass API及Google Cloud Platform(GCP)結合 LINE Notify執行資金費率套利回報Part.1

加密貨幣永續合約中的資金費率是一個關鍵的概念,它是用來平衡合約交易價格和標的資產市場價格之間的差異的機制。這個機制的目的是確保合約的價格與標的資產市場價格保持一致,防止合約出現巨大的價格差異。(以上資訊由ChatGPT生成) 

資金費率每8個小時計算一次,資金費率為負,空頭合約持有者支付資金費率給多頭合約持有者,反之資金費率為正,多頭合約持有者支付資金費率給空頭合約持有者,因為交易所的資金費率不同,當差距較大時,我們可以在兩間不同交易所各開一個價格數量相同但是多空不同的倉位,這樣會讓我們多空單的盈虧總合為0,但是在結算資金費率時套利出資金費率。 可以藉由觀察CoinGlass中的資金費率套利頁面,查詢現在有套利空間的交易對,
但是CoinGlass在此頁面中統計了衍伸品流動性低的火幣,且排除了Bitget以及BingX交易所,這導致網頁中的資訊不是內函所有的套利機會。因此我們需要一個排除火幣加入Bitget、BingX的套利清單。

我們可以經由CoinGlass的API中的fundingrate頁面,來獲得所有交易所衍生品的交易對的資金費率,登入帳號獲得APIKey之後,可以照著頁面說明獲得Python程式抓取資料的範例。

這時可以使用以下程式碼來獲得CoinGlass的資料
import requests
import time
import json

url = "https://open-api.coinglass.com/public/v2/funding"
headers = {
    "accept": "application/json",
    "coinglassSecret": "11111111111111111111111111111"
}

response = requests.get(url, headers=headers)
result = response.json()
with open("coinglass_fundingrate_output.json", "w") as file:
    json.dump(result, file, indent=4)
獲得的資料可以存成json格式如下所示:
{
    "code": "0",
    "msg": "success",
    "data": [
        {
            "symbol": "BTC",
            "symbolLogo": "https://cdn.coinglasscdn.com/static/img/coins/bitcoin-BTC.png",
            "status": 0,
            "uMarginList": [
                {
                    "rate": 0.004874,
                    "exchangeLogo": "https://cdn.coinglasscdn.com/static/exchanges/270.png",
                    "exchangeName": "Binance",
                    "status": 1,
                    "nextFundingTime": 1693929600000
                },
                {
                    "rate": 0.00122191216796,
                    "predictedRate": 0.00161720310695,
                    "exchangeLogo": "https://cdn.coinglasscdn.com/static/exchanges/okx2.png",
                    "exchangeName": "OKX",
                    "status": 2,
                    "nextFundingTime": 1693929600000
                },
其中使用的是
  • data欄位
  • data欄位中的各個貨幣對資料中的"symbol"
  • data欄位中的各個貨幣對資料中的"uMarginList"
  • uMarginList欄位中的"rate"以及"exchangeName"
我們這時要做資料清理的動作只留上述資料還有Binance、OKX、Bybit、Bitget、BingX的資料即可,可以用以下程式碼達成功能:
output_list = []
output_dict = {}
for i in range (len(result["data"])):
    data_dict = {}    
    list_temp = []
    count = 0
    exchange = ['Binance', 'OKX', 'Bybit', 'Bitget', 'BingX']
    for j in range (len(result["data"][i]["uMarginList"])):
        dict_temp = {}
        if  result["data"][i]["uMarginList"][j]["exchangeName"] in exchange and 'exchangeName' in result["data"][i]["uMarginList"][j].keys() and 'rate' in result["data"][i]["uMarginList"][j].keys() and result["data"][i]["symbol"] is not None:
            count+=1
            dict_temp['exchangeName'] = str(result["data"][i]["uMarginList"][j]["exchangeName"])
            dict_temp['rate'] = float(result["data"][i]["uMarginList"][j]["rate"])   
            list_temp.append(dict_temp)
            data_dict["symbol"] = result["data"][i]["symbol"]              
            data_dict["pair"] = list_temp
    if count>1:
        output_list.append(data_dict)
output_dict['data'] = output_list
with open("coinglass_fundingrate_output_step1.json", "w") as file:
    json.dump(output_dict, file, indent=4)

print("成功")
參考資料:

留言

這個網誌中的熱門文章

以dlib實現人臉辨識打卡系統

以python讀取yolo的輸出-darknet.py

使用Python達成影像形態學處理(不使用Opencv函式庫)