You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

关于Binance API 24小时priceChangePercent计算及K线适配的问询

Why Your Kline-Based Calculation Doesn’t Match Binance’s 24hr Ticker priceChangePercent

Hey there, let’s break down why you’re seeing this discrepancy and how to correctly replicate Binance’s 24-hour price change calculation using Kline data.

1. The Core Issue: Binance’s 24-Hour Window Is Rolling, Not Kline-Aligned

Binance’s 24hr ticker doesn’t use a time range locked to your Kline interval (15m in your case). Instead, it uses a rolling 24-hour window from the exact moment you call the API.

For example:

  • If you hit the API at 2024-05-20 14:27:30, the window runs from 2024-05-19 14:27:30 to 2024-05-20 14:27:30.
  • This almost never lines up perfectly with the start/end of 15m Klines. So using the first Kline’s open price and last Kline’s close price will always be an approximation, not the exact value Binance calculates.

2. Correct Calculation Using Kline Data

To match Binance’s priceChangePercent, follow these steps:

Step 1: Define the Exact 24-Hour Timestamps

First, calculate the precise start and end times for your window:

const currentTimestamp = Date.now();
const twentyFourHoursAgo = currentTimestamp - 86400 * 1000; // 24 hours in milliseconds

Step 2: Fetch High-Precision Kline Data

Call the Kline/Candlestick API with a smaller interval (like 1m) for better accuracy:

  • symbol=BTCUSDT
  • interval=1m
  • startTime=twentyFourHoursAgo
  • endTime=currentTimestamp

Step 3: Estimate the Price Exactly 24 Hours Ago

Find the Kline that contains the twentyFourHoursAgo timestamp, then use linear interpolation to get the exact price at that moment:

  • Let klineOpenTime = the start time of the containing Kline
  • Let klineCloseTime = the end time of the containing Kline
  • Let klineOpenPrice = the Kline’s open price (parse as a number)
  • Let klineClosePrice = the Kline’s close price (parse as a number)
  • Calculate the elapsed time within the Kline: elapsed = twentyFourHoursAgo - klineOpenTime
  • Calculate the Kline’s total duration: duration = klineCloseTime - klineOpenTime
  • Estimated price at twentyFourHoursAgo:
    startPrice = klineOpenPrice + (klineClosePrice - klineOpenPrice) * (elapsed / duration)
    

Step 4: Get the Current Live Price

Don’t use the last Kline’s close price—use the latest trade price. You can grab this from:

  • The lastPrice field in the 24hr ticker API response
  • Or the /api/v3/ticker/price endpoint for real-time price data

Step 5: Compute the Percentage Change

Calculate the value the same way Binance does:

priceChangePercent = ((currentPrice - startPrice) / startPrice) * 100

3. Shortcut: Use the Ticker’s Built-In Fields

If you don’t strictly need to use Kline data, you can skip the calculations entirely by using the pre-computed fields from the 24hr ticker response:

  • openPrice: The exact price 24 hours ago
  • lastPrice: The latest current price
  • priceChangePercent: Binance’s official calculation, which uses ((lastPrice - openPrice)/openPrice)*100

内容的提问来源于stack exchange,提问作者Fazan Cheng

火山引擎 最新活动