Huobi Digital Currency API latency is very high, causing K-line lag issues. Solutions:

1. Upgrade to high-config server

2. Switch to Binance API

3. Use Huobi API proxy/mirror

# K-line Data API

https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1d&limit=200

# Advantages:

– Fast response speed

– Completely free

– Stable and reliable

– Similar data format

# K-line Data API

https://www.okx.com/api/v5/market/candles?instId=BTC-USDT&bar=1D

# Advantages:

– Fast access from China

– Free to use

– Real-time data

# Market Data API

https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usdt&days=200&interval=daily

# Advantages:

– No registration required

– Large free quota

– Comprehensive data

# Historical K-line Data

https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USDT&limit=200

# Advantages:

– Fast response

– Basic features are free

– Accurate data

***************

public function getkdata() {

$pid = $this->app->request->param(‘pid’);

if(!$pid) $this->error(‘Product ID cannot be empty’);

$num = $this->app->request->param(‘num’, 30);

if(!$num) $this->error(‘Time cannot be empty’);

$pro = Db::name(‘LcProduct’)->where([‘id’=>$pid])->find();

if(!$pro) $this->error(‘Product information abnormal’);

$interval = input(‘interval’,’1′);

$klength = ($interval == ‘d’) ? 24*60*60*$num : $interval*60*$num;

$k_map[‘pid’] = $pid;

$k_map[‘ktime’] = array(‘between’, array(time() – $klength, time()));

$pro[‘procode’] = $pro[‘code’];

if(strpos($pro[‘procode’], ‘btc’)!==false || strpos($pro[‘procode’], ‘usdt’)!==false) {

// Convert to Binance time interval format

switch ($interval) {

case ‘1’:

$binance_interval = ‘1m’;

break;

case ‘5’:

$binance_interval = ‘5m’;

break;

case ’15’:

$binance_interval = ’15m’;

break;

case ’30’:

$binance_interval = ’30m’;

break;

case ’60’:

$binance_interval = ‘1h’;

break;

case ‘d’:

$binance_interval = ‘1d’;

break;

default:

$this->error(‘Invalid interval’);

break;

}

try {

// Build trading pair

$testacode = explode(‘_’, $pro[‘procode’]);

$symbol = strtoupper($testacode[0].$testacode[1]);

// Set CURL options

$ch = curl_init();

$timeout = 5;

$url = “https://api.binance.com/api/v3/klines?symbol={$symbol}&interval={$binance_interval}&limit={$num}”;

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$data = curl_exec($ch);

if(curl_errno($ch)) {

// If Binance API fails, try backup API (OKX)

$backup_url = “https://www.okx.com/api/v5/market/candles?instId={$testacode[0]}-{$testacode[1]}&bar={$binance_interval}”;

curl_setopt($ch, CURLOPT_URL, $backup_url);

$data = curl_exec($ch);

if(curl_errno($ch)) {

$this->error(‘API request failed’);

}

}

curl_close($ch);

$_data_arr = json_decode($data, true);

// Binance data processing

if(isset($_data_arr) && is_array($_data_arr)) {

$res_arr = [];

foreach($_data_arr as $v) {

// Binance K-line data format:

// [0] Open time [1] Open price [2] High price [3] Low price [4] Close price

$res_arr[] = [

intval($v[0]/1000), // Convert to second-level timestamp

floatval($v[1]), // Open price

floatval($v[4]), // Close price

floatval($v[3]), // Low price

floatval($v[2]) // High price

];

}

// Cache result (optional)

cache(‘kline_’.$symbol.’_’.$interval, $res_arr, 60); // Cache for 1 minute

return $res_arr;

}

} catch(\Exception $e) {

$this->error(‘Data fetch error: ‘ . $e->getMessage());

}

}

$this->error(‘Unsupported trading pair’);

}

*********************

Mirror Proxy

Huobi API

header(‘Access-Control-Allow-Origin: *’); // Allow cross-origin access

header(‘Content-Type: application/json; charset=utf-8’);

class KlineProxy {

private $cache_dir = ‘./cache/’; // Cache directory

private $cache_time = 5; // Cache time (seconds)

private $api_timeout = 5; // API timeout (seconds)

public function __construct() {

if (!is_dir($this->cache_dir)) {

mkdir($this->cache_dir, 0777, true);

}

}

// Main processing method

public function handle() {

try {

// Get parameters

$period = $this->getParam(‘period’, ‘1min’);

$size = $this->getParam(‘size’, ’50’);

$symbol = $this->getParam(‘symbol’, ‘btcusdt’);

// Validate parameters

$this->validateParams($period, $size, $symbol);

// Check cache

$cache_key = “{$symbol}_{$period}_{$size}”;

$cache_data = $this->getCache($cache_key);

if ($cache_data !== false) {

return $this->success($cache_data);