Эта стратегия используется для получения и мониторинга ставок финансирования с нескольких фьючерсных платформ, таких как OKCoin, Binance, Bitget и т. Д. Она просматривает рынки вечных контрактов различных бирж через параллельные потоки и получает данные по ставкам финансирования, используя механизм задержки для оптимизации частоты запросов.
В настоящей статье вносятся некоторые изменения в стратегию для поддержки функций отображения и толкания тревоги по скорости финансирования.
Адрес открытого исходного кода:https://www.fmz.com/strategy/470345
Вы можете настроить список платформ, список символов и интервал опроса по мере необходимости для удовлетворения ваших конкретных потребностей в торговле.
Сценарий разделен на несколько основных частей:
// Start the funding rate monitoring thread and create a separate thread for each exchange's funding rate data
function startFundingWorker() {
exchanges.forEach((_, pos) => {
__Thread(function (pos) {
let e = exchanges[pos]
let eName = e.GetName()
// Set request delays for different exchanges to prevent frequent requests from causing throttling
let delaySettings = {
'Futures_OKCoin': 20,
'Futures_Binance': 500,
'Futures_MEXC': 100,
}
// Need to traverse the list of exchange names for all markets, these exchanges do not support getting all trading pairs at once
let needInterate = ['Futures_OKCoin', 'Futures_Bitget','Futures_OKX', 'Futures_KuCoin', 'Futures_MEXC']
// Set delay based on exchange name
let delay = function () {
let n = delaySettings[eName]
if (n) {
Sleep(n)
}
}
// Set the update interval to update every two minutes
let epoch = 60000 * 2;
let ts = 0;
let fundings = {}
// Infinite loop, get funding rate at fixed intervals
while (true) {
let now = new Date().getTime()
if (now - ts < epoch) {
// If the update cycle is not reached, pause for 1 second and then continue checking
Sleep(1000)
continue
}
let markets = e.GetMarkets()
if (!markets) {
// If market information cannot be obtained, try again after a delay
Sleep(1000)
continue
}
// If the exchange is in the list that needs to be traversed, request the funding rate for each market
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 {
// For exchanges not in the traversal list, only request the funding rate of USDT.swap
let ret = e.GetFundings('USDT.swap')
if (ret) {
for (let r of ret) {
fundings[r.Symbol] = r
}
}
}
// Update data timestamp
ts = now
// Stores the exchange's funding rate data
__threadSetData(0, eName+"_funding", fundings)
}
}, pos)
})
}
// Get the funding rate data of the specified exchange
function getFundings(eName) {
let efundings = __threadGetData(0, eName+"_funding")
if (!efundings) {
return null
}
return efundings
}
// Update the funding rate table and display it in the log
function UpdateStatus(){
let table = {
type: 'table',
title: 'Funding Rate%',
cols: ['index', 'symbol'], // Initialization column, containing symbol
rows: []
};
let fundingRates = {};
exchanges.forEach((e) => {
let eName = e.GetName();
if (fundings[eName]) {
for (let symbol in fundings[eName]) {
// Parse short symbol names and remove unnecessary prefixes
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)
// Initialize symbol data structure
if (!fundingRates[short_symbol]) {
fundingRates[short_symbol] = { total: 0, count: 0, day_rate: {}, next_time: {}, last_time:0};
}
// Record and push rates that exceed the threshold
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); // Record rates
fundingRates[short_symbol].next_time[eName] = _N((fundings[eName][symbol].Time - Date.now()) / 3600000 , 1) + 'h'
}
}
});
// Added rate columns and next update time columns for each exchange
for (let e of exchanges) {
table.cols.push(e.GetName()+' Rate');
table.cols.push('Next Time');
}
table.cols.push('Average Rate'); // Add an average rate column
let i = 0;
// Iterate over each symbol and fill in the data
for (let symbol in fundingRates) {
let data = fundingRates[symbol];
if (data.count == 1) {
continue // Symbols containing only a single data point are ignored
}
let averageRate = data.total / data.count; // Calculate average rate
let row = [i++, symbol];
for (let e of exchanges) {
row.push(data.day_rate[e.GetName()] || null); // Filling the fees of various exchanges
row.push(data.next_time[e.GetName()] || null);
}
row.push(_N(averageRate, 6)); // Filling average rate
table.rows.push(row);
}
LogStatus('`' + JSON.stringify(table) + '`');
}
// Main function, start funding rate monitoring and status update
var fundings = {}
function main() {
startFundingWorker() // Start monitoring threads for each exchange
while (true) {
exchanges.forEach((e) => {
let eName = e.GetName()
let eFundings = getFundings(eName)
fundings[eName] = eFundings
})
Sleep(15000) // Update every 15 seconds
UpdateStatus()
}
}