Loading ...

各大交易所资金费率汇总

Author: 小草, Date: 2021-12-03 15:31:21
Tags:


var funding_rates = {'Binance':{},'FTX':{},'Huobi':{},'OKEX':{},'Gate':{},'Bybit':{}};
var all_symbols = new Set();
var avg = 0;
var update_avg_time = 0;

function CanParse(json_string) {
	try {
		JSON.parse(json_string);
	} catch (e) {
		return false;
	}
	return true;
}

function GetFundingRate(){
    var finding_rate = HttpQuery("https://fapi.binance.com/fapi/v1/premiumIndex");
    var symbol = null;
    if(finding_rate && CanParse(finding_rate)){
        finding_rate = JSON.parse(finding_rate);
        for (var i = 0; i < finding_rate.length; i++) {
            symbol = finding_rate[i].symbol.slice(0, finding_rate[i].symbol.length - 4);
            if(!finding_rate[i].symbol.endsWith("USDT")){
                continue;
            }
            if(symbol.startsWith(1000)){
                symbol = symbol.slice(4,symbol.length);
            }
            all_symbols.add(symbol);
            funding_rates.Binance[symbol] = _N(parseFloat(finding_rate[i].lastFundingRate)*100,5);
        }
    }
    
    finding_rate = HttpQuery("https://ftx.com/api/funding_rates");
    if(finding_rate && CanParse(finding_rate)){
        finding_rate = JSON.parse(finding_rate).result;
        for (var i = 0; i < finding_rate.length; i++) {
            symbol = finding_rate[i].future.split('-')[0];
            all_symbols.add(symbol);
            funding_rates.FTX[symbol] = _N(parseFloat(finding_rate[i].rate)*800,5);
        }
    }
    
    finding_rate = HttpQuery("https://api.hbdm.com/swap-api/v1/swap_batch_funding_rate");
    if(finding_rate && CanParse(finding_rate)){
        finding_rate = JSON.parse(finding_rate).data;
        for (var i = 0; i < finding_rate.length; i++) {
            symbol = finding_rate[i].contract_code.split('-')[0];
            all_symbols.add(symbol);
            funding_rates.Huobi[symbol] = _N(parseFloat(finding_rate[i].funding_rate)*100,5);
        }
    }
    
    finding_rate = HttpQuery("https://www.okex.com/priapi/v5/public/funding-rate-all?currencyType=1");
    
    if(finding_rate && CanParse(finding_rate)){
        finding_rate = JSON.parse(finding_rate).data[0].fundingList;
        for (var i = 0; i < finding_rate.length; i++) {
            symbol = finding_rate[i].instId.split('-')[0];
            all_symbols.add(symbol);
            funding_rates.OKEX[symbol] = _N(parseFloat(finding_rate[i].fundingRate)*100,5);
        }
    }
    
    finding_rate = HttpQuery("https://api.gateio.ws/api/v4/futures/usdt/tickers");
    if(finding_rate && CanParse(finding_rate)){
        finding_rate = JSON.parse(finding_rate);
        for (var i = 0; i < finding_rate.length; i++) {
            symbol = finding_rate[i].contract.split('_')[0];
            all_symbols.add(symbol);
            funding_rates.Gate[symbol] = _N(parseFloat(finding_rate[i].funding_rate)*100,5);
        }
    }
    
    var info = HttpQuery("https://api.bybit.com/v2/public/symbols");
    if(info && CanParse(info)){
        info = JSON.parse(info).result;
        for (var i = 0; i < info.length; i++) {
            if(info[i].quote_currency != 'USDT'){
                continue
            }
            symbol = info[i].base_currency;
            finding_rate = HttpQuery("https://api-testnet.bybit.com/public/linear/funding/prev-funding-rate?symbol="+info[i].name);
            Sleep(10);
            if(finding_rate && CanParse(finding_rate)){
                finding_rate = JSON.parse(finding_rate).result;
                if(symbol.endsWith(1000)){
                    symbol = symbol.slice(0,symbol.length-4);
                }
                all_symbols.add(symbol);
                funding_rates.Bybit[symbol] = _N(parseFloat(finding_rate.funding_rate)*100,5);
            }
        }
    }

    
}

function GetAvg(symbol){
    var exs = ['Binance', 'FTX', 'Huobi', 'OKEX', 'Gate', 'Bybit'];
    var count = 0;
    var total = 0;
    for(var i = 0; i < exs.length; i++){
        if(symbol in funding_rates[exs[i]]){
            count += 1;
            total += funding_rates[exs[i]][symbol];
        }
    }
    return _N(total/count, 5);
}

function UpdateStatus(){
    //{'Binance':{},'FTX':{},'Huobi':{},'OKEX':{},'Gate':{},'Bybit':{}};
    var table = {type: 'table', title: 'Funding Rate%', 
                  cols: ['symbol', 'Binance', 'FTX', 'Huobi', 'OKEX', 'Gate', 'Bybit', 'Avg'],
                  rows: []
                 }
    var total = 0;
    for (var symbol of all_symbols){
        var symbol_avg_rate = GetAvg(symbol);
        total += symbol_avg_rate;
        table.rows.push([symbol,
                         symbol in funding_rates.Binance ? funding_rates.Binance[symbol] : '',
                         symbol in funding_rates.FTX ? funding_rates.FTX[symbol] : '',
                         symbol in funding_rates.Huobi ? funding_rates.Huobi[symbol] : '',
                         symbol in funding_rates.OKEX ? funding_rates.OKEX[symbol] : '',
                         symbol in funding_rates.Gate ? funding_rates.Gate[symbol] : '',
                         symbol in funding_rates.Bybit ? funding_rates.Bybit[symbol] : '',
                         symbol_avg_rate
                         ])
    }
    avg = _N(total/all_symbols.size, 6);
    if(Date.now() - update_avg_time > 10*60*1000){
        LogProfit(avg);
        update_avg_time = Date.now();
    }
    var logString = _D()+'  策略代码最后更新时间12月01日 11:30\n' + '共监控USDT永续币种:' + all_symbols.size + '个。' + 
        '其中 Binace: ' + Object.keys(funding_rates.Binance).length + 
        ', FTX: ' + Object.keys(funding_rates.FTX).length + 
        ', Huobi: ' + Object.keys(funding_rates.Huobi).length + 
        ', OKEX: ' + Object.keys(funding_rates.OKEX).length + 
        ', Gate: ' + Object.keys(funding_rates.Gate).length + 
        ', Bybit: ' + Object.keys(funding_rates.Bybit).length + '\n' +
        '说明:FTX交易所换算为8h收取一次,不包含币本位合约,特殊的币种如1000SHIB进行了合并,可排序观察费率大小。打印的收益为总平均费率(百分数)。' + '\n' 
        ;
    LogStatus(logString + '`' + JSON.stringify(table) + '`');
}

function main() {
    while(true){
        try{
            GetFundingRate();
            UpdateStatus();
            Sleep(10*1000);
        }catch(e){
            Log(e);
        }
    }
}

More