Fetch a Segment of Klines Data
Source:R/impl_spottrading_market_data_get_klines.R
fetch_klines_segment.Rd
Retrieves a segment of candlestick (klines) data for a specified trading pair from the KuCoin API asynchronously, handling up to 1500 candles per request.
Usage
fetch_klines_segment(
base_url = get_base_url(),
symbol,
freq = "15min",
from = lubridate::now() - 1 * 3600,
to = lubridate::now(),
retries = 3,
delay_ms = 0,
.__coro_env_parent__ = <environment>
)
Arguments
- base_url
Character string; base URL for the KuCoin API. Defaults to
get_base_url()
.- symbol
Character string; trading pair in KuCoin format (e.g.,
"BTC-USDT"
).- freq
Character string; candlestick interval (e.g.,
"15min"
). Allowed values:"1min"
,"3min"
,"5min"
,"15min"
,"30min"
,"1hour"
,"2hour"
,"4hour"
,"6hour"
,"8hour"
,"12hour"
,"1day"
,"1week"
,"1month"
. Defaults to"15min"
.- from
POSIXct object; start time of the segment. Defaults to one hour before the current time.
- to
POSIXct object; end time of the segment. Defaults to the current time.
- retries
Integer; number of retry attempts for the HTTP request (default 3).
- delay_ms
Numeric; delay in milliseconds before sending the request (default 0).
Value
Promise resolving to a data.table
containing:
datetime
(POSIXct): Converted timestamp.timestamp
(numeric): Raw timestamp in seconds.open
(numeric): Opening price.close
(numeric): Closing price.high
(numeric): Highest price in the interval.low
(numeric): Lowest price in the interval.volume
(numeric): Trading volume.turnover
(numeric): Trading turnover.
Details
Workflow Overview
Delay Application: Pauses for
delay_ms
milliseconds if specified, to throttle requests.Query Construction: Builds the query string with
symbol
,type
(frequency),startAt
, andendAt
usingbuild_query()
.URL Assembly: Combines
base_url
with the endpoint/api/v1/market/candles
and query string.API Request: Sends a GET request with retries using
httr::RETRY()
and a 10-second timeout.Response Processing: Processes the response with
process_kucoin_response()
, converts data to adata.table
, standardises column names, coerces numerics, adds adatetime
column, and orders bydatetime
.
Usage
Utilised as a helper to fetch individual segments of klines data, typically within a broader segmented retrieval strategy.
Examples
if (FALSE) { # \dontrun{
main_async <- coro::async(function() {
dt_segment <- await(fetch_klines_segment(
symbol = "BTC-USDT",
freq = "15min",
from = lubridate::now() - 3600,
to = lubridate::now(),
retries = 3,
delay_ms = 100
))
print(dt_segment)
})
main_async()
while (!later::loop_empty()) later::run_now()
} # }