Summary of Funding Rates Across Major Exchanges
JavaScript strategy by 小草.
JavaScriptArbitrage
The original spec has no written description. Read the source below.
Source JavaScript
var funding_rates = {'Binance':{},'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://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', '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(){
let table = {
type: 'table',
title: 'Funding Rate%',
cols: ['symbol'], // Initialize columns, include symbol
rows: []
};
let fundingRates = {};
// Directly from the existing fundings Build fee rate information in the data
exchanges.forEach((e) => {
let eName = e.GetName();
let fundings = getFundings(eName);
if (fundings) {
for (let symbol in fundings) {
let rate = fundings[symbol].Rate; // Assume there is one in each returned object Rate Attribute
if (!fundingRates[symbol]) {
fundingRates[symbol] = { total: 0, count: 0, exchanges: {} };
}
fundingRates[symbol].total += rate;
fundingRates[symbol].count++;
fundingRates[symbol].exchanges[eName] = rate; // Record rate
}
}
});
// Construct the rows and average rates for each trading pair
for (let symbol in fundingRates) {
let data = fundingRates[symbol];
let averageRate = data.total / data.count; // Calculate average rate
// Add column headers to the table
if (table.cols.length === 1) { // Only add trading pairs for the first time
for (let e of exchanges) {
table.cols.push(e.GetName());
}
table.cols.push('Average Rate'); // Add average rate column
}
// Build row data
let row = [symbol];
for (let e of exchanges) {
row.push(data.exchanges[e.GetName()] || 0); // Fill in the rates of each exchange
}
row.push(averageRate); // Fill average rate
table.rows.push(row);
}
// Record status
LogStatus(logString + '`' + JSON.stringify(table) + '`');
}
function main() {
while(true){
try{
GetFundingRate();
UpdateStatus();
Sleep(10*1000);
}catch(e){
Log(e);
}
}
}Published by 小草 on the FMZ strategy square · last modified 2024-11-01. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a spread arbitrage engine with WebSocket order routing and leg-risk controls from strategies/Summary-of-Funding-Rates-Across-Major-Exchanges.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- OKEx Futures HedgingJavaScript · Arbitrage
- RSI Statistical Arbitrage StrategyJavaScript · Arbitrage
- Triangular Arbitrage to Earn Small Coin Bid Ask SpreadJavaScript · Arbitrage
- Hedge_BTC ETH DemoJavaScript · Arbitrage
- Multi Platform Hedging Stable ArbitrageJavaScript · Arbitrage
- Cross Platform Arbitrage Beginner TestJavaScript · Arbitrage · 1h