如何用R通过Binance API获取EMA、RSI等高级振荡指标?
Hey there, great question! I’ve been working with Binance API in R too, and I totally get why you don’t want to roll your own indicator calculations—consistency with Binance’s charts is key.
First off, the straight answer: Binance’s official REST and WebSocket APIs don’t expose pre-calculated technical indicators like EMA or RSI. They stick to providing raw market data (kline/candlestick, ticker, order book, etc.) and leave the indicator math up to developers.
But don’t worry, there are solid ways to get results that match Binance’s charts perfectly without reinventing the wheel:
1. Use R’s dedicated quant packages to calculate indicators (most reliable)
The TTR package in R is built for exactly this, and its calculations align almost perfectly with Binance’s charting tools. Here’s a quick, actionable workflow using Binance API data:
First, fetch your candlestick data (let’s use 1h BTC/USDT as an example):
library(httr) library(jsonlite) library(TTR) # Fetch 1h kline data for BTCUSDT (last 100 candles) response <- GET("https://api.binance.com/api/v3/klines", query = list(symbol = "BTCUSDT", interval = "1h", limit = 100)) klines <- fromJSON(content(response, "text")) # Convert to a usable data frame and extract closing prices kline_df <- data.frame( timestamp = as.POSIXct(klines[,1]/1000, origin="1970-01-01"), close = as.numeric(klines[,5]) ) # Calculate 20-period EMA (matches Binance's default EMA setting) kline_df$ema20 <- EMA(kline_df$close, n = 20) # Calculate 14-period RSI (Binance's standard RSI period) kline_df$rsi14 <- RSI(kline_df$close, n = 14)
The critical step here is matching the period parameters exactly to what you see on Binance’s charts (e.g., 20 for EMA20, 14 for RSI14). TTR uses the same calculation logic as Binance, so your results will be identical to what’s shown in their advanced charts.
2. Third-party APIs that pre-calculate indicators
If you really want to avoid any local calculation, there are third-party data providers that offer pre-computed technical indicators alongside Binance data. These services scrape or compute indicators based on Binance’s raw data and expose them via API. Just make sure to pick one that explicitly states alignment with Binance’s charting standards—double-check a few data points first to confirm consistency.
3. Real-time calculation with WebSockets
If you need live, up-to-the-second indicators (not just historical), you can use Binance’s WebSocket API to stream real-time kline data, then compute indicators on the fly in R. Packages like websocket can handle the stream, and you can feed new candle data into TTR’s functions as it comes in to keep your indicators updated in real-time.
A quick sanity check: Always verify the first few calculated values against Binance’s chart to ensure you’re using the right data column (Binance uses closing prices for most indicators) and matching period settings.
Hope this helps you get the exact indicator values you need without the hassle!
内容的提问来源于stack exchange,提问作者Bart




