The resource loading... loading...

FMZ funding rate acquisition and monitoring strategies

Author: The grass, Created: 2024-10-31 16:41:13, Updated: 2024-11-04 20:32:25

img

Multi-platform strategy for access and monitoring of futures rates

It is described as:

The strategy is used to obtain and monitor capital rates from multiple futures platforms (e.g. OKCoin, Binance, Bitget, etc.); to query the perpetual contract markets of exchanges in parallel threads, and to obtain capital rate data while optimizing request frequency using delay mechanisms.

This article has made some changes to the strategy, supporting display and fund rate alert push functionality.

This is the address of the open source project:https://www.fmz.com/strategy/470345

Features:

  • Multi-platform supportSynchronize the funds rates of multiple trading platforms and set different request delays for each platform.
  • Obtaining specific symbolsSupport for access to funds for specific trading pairs (such as BTC/USDT, ETH/USDT).
  • Optimized for different platformsA distinction is made between platforms that do not require individual market query (such as Binance) and platforms that need to cover all markets (such as OKCoin).
  • Rate shows: Show the capital rate for multiple trading platforms, because the charging intervals are different, for direct comparison, uniform adjustment to 24h rate.
  • Tariff early warning push: You can set a certain threshold, and when the 24-hour equivalent rate exceeds the customization, it will be pushed to the FMZ mobile app.

How to use:

You can adjust the list of platforms, symbol lists and routing intervals as needed to meet specific trading needs.

The Strategy Code

The script is divided into several main parts:

  1. startFundingWorker• Starting separate threads for each exchange to monitor capital rates, avoiding the limit flow problem caused by single threads requesting too much data.
  2. getFundings: read the data of the exchange's capital rate from the storage.
  3. UpdateStatusProcessing and updating the capital rate tables of all exchanges, presenting the aggregated data as a table, and recording the symbols of high rates in the logs.
  4. mainStart the main process, start the monitoring thread and update the aggregate capital rate status regularly.
// 启动资金费率监控线程,为每个交易所的资金费率数据创建一个单独的线程
function startFundingWorker() {
    exchanges.forEach((_, pos) => {
        __Thread(function (pos) {
            let e = exchanges[pos]
            let eName = e.GetName()
            // 设置不同交易所的请求延迟,以防止频繁请求导致被限流
            let delaySettings = {
                'Futures_OKCoin': 20,
                'Futures_Binance': 500,
                'Futures_MEXC': 100,
            }
            // 需要遍历所有市场的交易所名称列表,这些交易所不支持一次获取所有交易对
            let needInterate = ['Futures_OKCoin', 'Futures_Bitget','Futures_OKX', 'Futures_KuCoin', 'Futures_MEXC'] 
            // 根据交易所名称设定延迟
            let delay = function () {
                let n = delaySettings[eName]
                if (n) {
                    Sleep(n)
                }
            }
            // 设定更新间隔,每两分钟更新一次
            let epoch = 60000 * 2;
            let ts = 0;
            let fundings = {}
            // 无限循环,以固定间隔获取资金费率
            while (true) {
                let now = new Date().getTime()
                if (now - ts < epoch) {
                    // 未达到更新周期则暂停1秒后继续检查
                    Sleep(1000)
                    continue
                }
                let markets = e.GetMarkets()
                if (!markets) {
                    // 如果未能获取到市场信息,则延迟后重试
                    Sleep(1000)
                    continue
                }
                // 如果交易所在需要遍历的列表中,逐个市场请求资金费率
                if (needInterate.includes(eName)) {
                    for (let symbol in markets) {
                        if (symbol.includes('.swap') && symbol.includes('_USDT')) {
                            let ret = e.GetFundings(symbol)
                            if (ret) {
                                for (let r of ret) {
                                    fundings[r.Symbol] = r
                                }
                            }
                            delay();
                        }
                    }
                } else {
                    // 不在遍历列表中的交易所,仅请求 USDT.swap 的资金费率
                    let ret = e.GetFundings('USDT.swap')
                    if (ret) {
                        for (let r of ret) {
                            fundings[r.Symbol] = r
                        }
                    }
                }
                // 更新数据时间戳
                ts = now
                // 存储该交易所的资金费率数据
                __threadSetData(0, eName+"_funding", fundings)
            }
        }, pos)
    })
}

// 获取指定交易所的资金费率数据
function getFundings(eName) {
    let efundings = __threadGetData(0, eName+"_funding")
    if (!efundings) {
        return null
    }
    return efundings
}

// 更新资金费率表并在日志中显示
function UpdateStatus(){
    let table = { 
        type: 'table', 
        title: 'Funding Rate%', 
        cols: ['index', 'symbol'], // 初始化列,包含 symbol 
        rows: [] 
    };
    let fundingRates = {};
    exchanges.forEach((e) => {
        let eName = e.GetName();
        if (fundings[eName]) {
            for (let symbol in fundings[eName]) {
                // 解析简短的 symbol 名称,去除多余前缀
                let short_symbol = symbol.split('_')[0].replace(/^(100|1000|10000|100000|1000000|10000000)|^(100|1000|10000|100000|1000000|10000000)$/g, '');
                let rate = fundings[eName][symbol].Rate;
                let day = 24 / (fundings[eName][symbol].Interval / 3600000)
                // 初始化符号的数据结构
                if (!fundingRates[short_symbol]) {
                    fundingRates[short_symbol] = { total: 0, count: 0,  day_rate: {},  next_time: {}, last_time:0};
                }
                // 对超过阈值的费率进行记录并推送
                if (Math.abs(rate) > 0.01 && Date.now() - fundingRates[short_symbol].last_time > 30*60*1000) {
                    Log(e.GetName(), symbol, rate, '@')
                    fundingRates[short_symbol].last_time = Date.now()
                }
                fundingRates[short_symbol].total += rate;
                fundingRates[short_symbol].count++;
                fundingRates[short_symbol].day_rate[eName] = _N(rate * day , 6); // 记录费率
                fundingRates[short_symbol].next_time[eName] = _N((fundings[eName][symbol].Time - Date.now()) / 3600000 , 1) + 'h'
            }
        }
    });
    // 为每个交易所添加费率列和下次更新的时间列
    for (let e of exchanges) {
        table.cols.push(e.GetName()+' Rate');
        table.cols.push('Next Time');
    }
   
    table.cols.push('Average Rate'); // 添加平均费率列
    let i = 0;
    // 遍历每个符号并填充数据
    for (let symbol in fundingRates) {
        let data = fundingRates[symbol];
        if (data.count == 1) {
            continue // 只包含单个数据点的符号忽略
        }
        let averageRate = data.total / data.count; // 计算平均费率
        let row = [i++, symbol];
        for (let e of exchanges) {
            row.push(data.day_rate[e.GetName()] || null); // 填充各个交易所的费率
            row.push(data.next_time[e.GetName()] || null);
        }
        row.push(_N(averageRate, 6)); // 填充平均费率
        table.rows.push(row);
    }
    LogStatus('`' + JSON.stringify(table) + '`');
}

// 主函数,启动资金费率监控和状态更新
var fundings = {}
function main() {
    startFundingWorker() // 启动每个交易所的监控线程
    while (true) {
        exchanges.forEach((e) => {
            let eName = e.GetName()
            let eFundings = getFundings(eName)
            fundings[eName] = eFundings
        })
        Sleep(15000) // 每15秒更新一次
        UpdateStatus()
    }
}

More