Package 'wickraexchange'

Title: Streaming-Native Crypto Exchange Connectivity
Description: R bindings for wickra-exchange over its C ABI hub: one synchronous, pull-based API over the ten largest crypto exchanges, plus offline paper and replay simulators that share the exact same API. The same strategy runs paper, replay and live by swapping the constructor.
Authors: kingchenc [aut, cre]
Maintainer: kingchenc <[email protected]>
License: MIT + file LICENSE | Apache License 2.0
Version: 0.2.0
Built: 2026-10-07 08:57:59 UTC
Source: https://github.com/wickra-lib/wickra-exchange

Help Index


Connect a live advanced-orders client (amend, batch cancel).

Description

Fails for a venue without an advanced-order surface (coinbase, upbit).

Usage

wkex_advanced(
  name,
  api_key,
  api_secret,
  passphrase = NULL,
  private_key = NULL,
  testnet = FALSE,
  futures = FALSE
)

Arguments

name, api_key, api_secret

Venue and API credentials.

passphrase, private_key

Optional extra credentials (NULL if unused).

testnet

Use the venue testnet.

futures

Select the USD-M futures market.

Value

A 'wickra_advanced' object.


Amend a resting order's price and/or quantity in place.

Description

Pass 'NA' for 'new_price' or 'new_quantity' to leave that field unchanged.

Usage

wkex_amend_order(
  adv,
  market,
  order_id,
  new_price = NA_real_,
  new_quantity = NA_real_
)

Arguments

adv

A 'wickra_advanced' object.

market

Market symbol.

order_id

Venue order id.

new_price, new_quantity

New values, or NA to leave unchanged.

Value

The refreshed order list.


The free balance of an asset.

Description

The free balance of an asset.

Usage

wkex_balance(ex, asset)

Arguments

ex

A 'wickra_exchange' object.

asset

Asset symbol, e.g. "BTC".

Value

The free balance as a number.


Cancel an open order by venue id.

Description

Cancel an open order by venue id.

Usage

wkex_cancel(ex, market, order_id)

Arguments

ex

A 'wickra_exchange' object.

market

Market string.

order_id

The venue order id.

Value

Invisibly, 'ex'.


Cancel several orders on a market in one request.

Description

Cancel several orders on a market in one request.

Usage

wkex_cancel_batch(adv, market, order_ids)

Arguments

adv

A 'wickra_advanced' object.

market

Market symbol.

order_ids

Character vector of venue order ids.


Flatten the open position in a market with a reduce-only market order.

Description

Flatten the open position in a market with a reduce-only market order.

Usage

wkex_close_position(deriv, market)

Arguments

deriv

A 'wickra_derivatives' object.

market

Market symbol.

Value

The resulting order list.


Connect a live client for a venue.

Description

The derivatives, advanced-orders, user-data and ws-execution constructors each connect internally, so this was the one missing door: without it R could reach those handles against a real venue while having no way to build a plain exchange for market data and order execution.

Usage

wkex_connect(
  name,
  api_key,
  api_secret,
  passphrase = NULL,
  private_key = NULL,
  testnet = FALSE,
  market = "spot",
  margin_mode = "cross",
  position_mode = "one_way"
)

Arguments

name

Venue identifier, e.g. "binance".

api_key, api_secret

API credentials.

passphrase, private_key

Optional extra credentials (NULL if unused).

testnet

Use the venue testnet.

market

Which market of the venue: "spot" (default) or "usdm_futures". A venue is several APIs behind one name, and this is the choice between them. Coin-margined and margin are not offered: no client routes them consistently, and Binance treats coin-margined as spot.

margin_mode

"cross" (default) or "isolated". Two venues carry it on every order, so it belongs here rather than in a later call.

position_mode

"one_way" (default) or "hedge". On a hedged account every order names the side of the account it acts on.

Value

A 'wickra_exchange' object.


Connect a live derivatives (USD-M futures) client.

Description

Positions, leverage, margin mode and reduce-only close. Fails for a spot-only venue (coinbase, upbit).

Usage

wkex_derivatives(
  name,
  api_key,
  api_secret,
  passphrase = NULL,
  private_key = NULL,
  testnet = FALSE
)

Arguments

name, api_key, api_secret

Venue and API credentials.

passphrase, private_key

Optional extra credentials (NULL if unused).

testnet

Use the venue testnet.

Value

A 'wickra_derivatives' object.


Keep the private user-data stream alive.

Description

Refreshes the venue session / sends a heartbeat so the stream is not dropped for inactivity; call it periodically. A dropped stream is also recovered automatically on the next [wkex_user_data_poll()]. A no-op before [wkex_subscribe_user_data()].

Usage

wkex_keepalive_user_data(ud)

Arguments

ud

A 'wickra_user_data' object.

Value

Invisibly, 'ud'.


Historical candles for a market.

Description

Historical candles for a market.

Usage

wkex_klines(ex, market, interval, limit)

Arguments

ex

A 'wickra_exchange' object.

market

Market symbol, e.g. "BTC/USDT".

interval

Candle interval, e.g. "1m".

limit

Maximum candles to return.

Value

A list of OHLCV lists ('open'/'high'/'low'/'close'/'volume'/'timestamp').


The venue identifier of an exchange.

Description

The venue identifier of an exchange.

Usage

wkex_name(ex)

Arguments

ex

A 'wickra_exchange' object.

Value

The venue name ("paper", "replay", "binance", ...).


Open orders, optionally filtered to one market.

Description

Open orders, optionally filtered to one market.

Usage

wkex_open_orders(ex, market = NULL)

Arguments

ex

A 'wickra_exchange' object.

market

Market symbol, or 'NULL' for all markets.

Value

A list of order lists.


Order-book depth snapshot for a market.

Description

Order-book depth snapshot for a market.

Usage

wkex_order_book(ex, market, depth)

Arguments

ex

A 'wickra_exchange' object.

market

Market symbol, e.g. "BTC/USDT".

depth

Maximum levels per side.

Value

A list with 'symbol' and 'bids'/'asks' lists of 'price, quantity'.


Open an offline paper account.

Description

Open an offline paper account.

Usage

wkex_paper(balances, maker_bps = 0, taker_bps = 0, slippage_bps = 0)

Arguments

balances

Named numeric vector of starting balances (asset -> amount).

maker_bps, taker_bps, slippage_bps

Costs in basis points.

Value

A 'wickra_exchange' object.


Place several orders in one request.

Description

The orders are described by parallel vectors. 'prices' uses 'NA' for a market order and a finite value for a limit order.

Usage

wkex_place_batch(adv, markets, sides, quantities, prices)

Arguments

adv

A 'wickra_advanced' object.

markets

Character vector of market symbols.

sides

Character or integer vector of sides ("buy"/"sell").

quantities

Numeric vector of quantities.

prices

Numeric vector of prices (NA for a market order).

Value

A list of results, each 'list(order = , error = )': 'order' on success or 'error' (an integer status code) on a per-order rejection.


Place several full orders in one request.

Description

[wkex_place_batch()] can say only market, side, quantity and price, so a batched order from R could never be a stop-loss, an immediate-or-cancel or a post-only, however carefully the venue clients carried them. This takes the whole request, as parallel vectors: one element of each per order.

Usage

wkex_place_batch_full(
  adv,
  markets,
  sides,
  quantities,
  prices = NA_real_,
  stop_prices = NA_real_,
  times_in_force = "gtc",
  client_order_ids = NULL,
  reduce_onlys = FALSE,
  post_onlys = FALSE,
  stps = "none",
  quantity_texts = NA_character_,
  price_texts = NA_character_,
  stop_price_texts = NA_character_
)

Arguments

adv

A 'wickra_advanced' object.

markets

Character vector of market symbols.

sides

Character or integer vector of sides ("buy"/"sell").

quantities

Numeric vector of quantities.

prices

Numeric vector of limit prices ('NA' for a market order).

stop_prices

Numeric vector of trigger prices ('NA' for none).

times_in_force

Character vector: "gtc", "ioc" or "fok".

client_order_ids

Character vector of ids, or 'NULL' for none.

reduce_onlys

Logical vector: close-only.

post_onlys

Logical vector: maker-only.

stps

Character vector: "none", "expire_maker", "expire_taker" or "expire_both".

quantity_texts

Character vector of exact quantities, or 'NA' per element to use the numeric beside it. R's numeric is a double and holds about fifteen significant digits; a string reaches the venue as written.

price_texts

Character vector of exact limit prices, or 'NA'.

stop_price_texts

Character vector of exact trigger prices, or 'NA'.

Details

Scalars are recycled to the number of markets, so the common case of "same time-in-force for every leg" needs one value rather than a repeated vector.

Value

A list of results, each 'list(order = , error = )': 'order' on success or 'error' (an integer status code) on a per-order rejection.


Place a limit order.

Description

Place a limit order.

Usage

wkex_place_limit(ex, market, side, quantity, price)

Arguments

ex

A 'wickra_exchange' object.

market

Market string.

side

"buy" or "sell".

quantity

Order quantity.

price

Limit price.

Value

The resulting order as a list.


Place a market order.

Description

Place a market order.

Usage

wkex_place_market(ex, market, side, quantity)

Arguments

ex

A 'wickra_exchange' object.

market

Market string.

side

"buy" or "sell".

quantity

Order quantity.

Value

The resulting order as a list.


Place a one-cancels-other bracket.

Description

A take-profit limit leg at 'price' paired with a stop leg triggered at 'stop_price'. A finite 'stop_limit_price' makes the stop leg a stop-limit; 'NA' leaves it a stop-market.

Usage

wkex_place_oco(
  adv,
  market,
  side,
  quantity,
  price,
  stop_price,
  stop_limit_price = NA_real_
)

Arguments

adv

A 'wickra_advanced' object.

market

Market symbol.

side

"buy" or "sell".

quantity

Order quantity.

price

Take-profit limit price.

stop_price

Stop trigger price.

stop_limit_price

Stop-leg limit price, or NA for a stop-market.

Value

A list of the resulting order legs.


Place a full order.

Description

'wkex_place_market()' and 'wkex_place_limit()' take a market, a side, a quantity and a price, which is all an order could ever be from R. This is the call that can also carry the trigger price that makes a stop-loss a stop-loss, the time-in-force that says an order must not rest, post-only, reduce-only, self-trade prevention, and the client order id that makes a retried placement idempotent.

Usage

wkex_place_order(
  ex,
  market,
  side,
  quantity,
  price = NA_real_,
  stop_price = NA_real_,
  time_in_force = "gtc",
  client_order_id = NULL,
  reduce_only = FALSE,
  post_only = FALSE,
  stp = "none",
  quantity_text = NA_character_,
  price_text = NA_character_,
  stop_price_text = NA_character_
)

Arguments

ex

A 'wickra_exchange' object.

market

Market string.

side

"buy" or "sell".

quantity

Order quantity.

price

Limit price, or 'NA' for a market order.

stop_price

Trigger price, or 'NA' for a non-trigger order.

time_in_force

"gtc" (the default), "ioc" or "fok".

client_order_id

An id of your choosing, or 'NULL'.

reduce_only

Close-only: the order may not increase a position.

post_only

Maker-only: cancelled rather than crossing the spread.

stp

"none" (the default), "expire_maker", "expire_taker" or "expire_both".

quantity_text

Exact quantity as a string, or 'NA' to use 'quantity'.

R's numeric is a double and holds about fifteen significant digits, where this library holds every order number in an exact decimal. Written as a number, '12345678.90123456789' becomes '12345678.90123457' – a different order, placed without a word. A string reaches the venue as written.

price_text

Exact limit price as a string, or 'NA' to use 'price'.

stop_price_text

Exact trigger price as a string, or 'NA' to use 'stop_price'.

Details

The order type is derived rather than named: a 'price' makes it a limit order, a 'stop_price' promotes it into its trigger form.

A field the venue cannot express refuses the order rather than weakening it, which surfaces here as an error rather than as a differently-shaped order reaching the exchange.

Value

The resulting order as a list.


Drain buffered events.

Description

Drain buffered events.

Usage

wkex_poll(ex, capacity = 16L)

Arguments

ex

A 'wickra_exchange' object.

capacity

Maximum events to return per call.

Value

A list of event lists.


The open position in a market.

Description

The open position in a market.

Usage

wkex_position(deriv, market)

Arguments

deriv

A 'wickra_derivatives' object.

market

Market symbol, e.g. "BTC/USDT".

Value

A position list (errors if flat).


Every open position (list-all).

Description

Pass a 'market' to scope to one symbol, or 'NULL' for all.

Usage

wkex_positions(deriv, market = NULL)

Arguments

deriv

A 'wickra_derivatives' object.

market

Optional market symbol, or NULL for all positions.

Value

A list of position lists.


Look up a single order by venue id.

Description

Look up a single order by venue id.

Usage

wkex_query_order(ex, market, order_id)

Arguments

ex

A 'wickra_exchange' object.

market

Market symbol, e.g. "BTC/USDT".

order_id

Venue order id.

Value

The order as a list.


Open a replay account driven by a recorded tape of trades.

Description

Open a replay account driven by a recorded tape of trades.

Usage

wkex_replay_trades(
  market,
  tape,
  balances,
  maker_bps = 0,
  taker_bps = 0,
  slippage_bps = 0
)

Arguments

market

Market string, e.g. "BTC/USDT".

tape

Numeric vector of trade prices.

balances

Named numeric vector of starting balances.

maker_bps, taker_bps, slippage_bps

Costs in basis points.

Value

A 'wickra_exchange' object.


Set the leverage for a market.

Description

Set the leverage for a market.

Usage

wkex_set_leverage(deriv, market, leverage)

Arguments

deriv

A 'wickra_derivatives' object.

market

Market symbol.

leverage

Integer leverage.


Set the margin mode ("cross" or "isolated") for a market.

Description

Set the margin mode ("cross" or "isolated") for a market.

Usage

wkex_set_margin_mode(deriv, market, mode)

Arguments

deriv

A 'wickra_derivatives' object.

market

Market symbol.

mode

"cross" or "isolated".


Set the mark price a paper account fills against (paper backend only).

Description

Set the mark price a paper account fills against (paper backend only).

Usage

wkex_set_price(ex, market, price)

Arguments

ex

A 'wickra_exchange' object.

market

Market string.

price

Mark price.

Value

Invisibly, 'ex'.


Subscribe to the order-book stream for a market.

Description

Subscribe to the order-book stream for a market.

Usage

wkex_subscribe_book(ex, market)

Arguments

ex

A 'wickra_exchange' object.

market

Market symbol, e.g. "BTC/USDT".

Value

Invisibly, 'ex'.


Subscribe to the ticker stream for a market.

Description

Subscribe to the ticker stream for a market.

Usage

wkex_subscribe_ticker(ex, market)

Arguments

ex

A 'wickra_exchange' object.

market

Market symbol, e.g. "BTC/USDT".

Value

Invisibly, 'ex'.


Subscribe to the public trade stream for a market.

Description

Subscribe to the public trade stream for a market.

Usage

wkex_subscribe_trades(ex, market)

Arguments

ex

A 'wickra_exchange' object.

market

Market symbol, e.g. "BTC/USDT".

Value

Invisibly, 'ex'.


Open the private user-data stream.

Description

Open the private user-data stream.

Usage

wkex_subscribe_user_data(ud)

Arguments

ud

A 'wickra_user_data' object.

Value

Invisibly, 'ud'.


The current ticker for a market.

Description

The current ticker for a market.

Usage

wkex_ticker(ex, market)

Arguments

ex

A 'wickra_exchange' object.

market

Market symbol, e.g. "BTC/USDT".

Value

A list with 'symbol', 'last', 'bid', 'ask', 'volume'.


Connect a live private user-data client.

Description

After [wkex_subscribe_user_data()], [wkex_user_data_poll()] surfaces the account's own order and balance updates. Fails for a spot-only venue.

Usage

wkex_user_data(
  name,
  api_key,
  api_secret,
  passphrase = NULL,
  private_key = NULL,
  testnet = FALSE,
  futures = FALSE
)

Arguments

name, api_key, api_secret

Venue and API credentials.

passphrase, private_key

Optional extra credentials (NULL if unused).

testnet

Use the venue testnet.

futures

Select the USD-M futures market.

Value

A 'wickra_user_data' object.


Drain buffered user-data events.

Description

Drain buffered user-data events.

Usage

wkex_user_data_poll(ud, capacity = 16L)

Arguments

ud

A 'wickra_user_data' object.

capacity

Maximum events to return per call.

Value

A list of event lists.


The wickra-exchange library version.

Description

The wickra-exchange library version.

Usage

wkex_version()

Value

A version string.


Cancel an order over the WebSocket order API by venue id.

Description

Cancel an order over the WebSocket order API by venue id.

Usage

wkex_ws_cancel_order(wse, market, order_id)

Arguments

wse

A 'wickra_ws_execution' object.

market

Market string.

order_id

The venue order id.

Value

Invisibly, 'wse'.


Connect a live WebSocket order-API client (place/cancel over the ws-api).

Description

Native on binance/bybit/okx/gateio/kraken; on bitget/kucoin/htx the methods error (no WebSocket order-entry API). Fails for a spot-only venue.

Usage

wkex_ws_execution(
  name,
  api_key,
  api_secret,
  passphrase = NULL,
  private_key = NULL,
  testnet = FALSE,
  futures = FALSE
)

Arguments

name, api_key, api_secret

Venue and API credentials.

passphrase, private_key

Optional extra credentials (NULL if unused).

testnet

Use the venue testnet.

futures

Select the USD-M futures market.

Value

A 'wickra_ws_execution' object.


Place an order over the WebSocket order API.

Description

Place an order over the WebSocket order API.

Usage

wkex_ws_place_order(wse, market, side, quantity, price = NA_real_)

Arguments

wse

A 'wickra_ws_execution' object.

market

Market string.

side

"buy" or "sell".

quantity

Order quantity.

price

Limit price, or NA for a market order.

Value

The resulting order as a list.


Place a full order over the WebSocket order API.

Description

The [wkex_place_order()] form of [wkex_ws_place_order()], for the same reason the REST path has one: the narrow call cannot carry a trigger price, a time-in-force, or any of the flags that decide what the order actually is.

Usage

wkex_ws_place_order_full(
  wse,
  market,
  side,
  quantity,
  price = NA_real_,
  stop_price = NA_real_,
  time_in_force = "gtc",
  client_order_id = NULL,
  reduce_only = FALSE,
  post_only = FALSE,
  stp = "none",
  quantity_text = NA_character_,
  price_text = NA_character_,
  stop_price_text = NA_character_
)

Arguments

wse

A 'wickra_ws_execution' object.

market

Market string.

side

"buy" or "sell".

quantity

Order quantity.

price

Limit price, or 'NA' for a market order.

stop_price

Trigger price, or 'NA' for a non-trigger order.

time_in_force

"gtc" (the default), "ioc" or "fok".

client_order_id

An id of your choosing, or 'NULL'.

reduce_only

Close-only: the order may not increase a position.

post_only

Maker-only: cancelled rather than crossing the spread.

stp

"none" (the default), "expire_maker", "expire_taker" or "expire_both".

quantity_text

Exact quantity as a string, or 'NA' to use 'quantity'.

R's numeric is a double and holds about fifteen significant digits, where this library holds every order number in an exact decimal. Written as a number, '12345678.90123456789' becomes '12345678.90123457' – a different order, placed without a word. A string reaches the venue as written.

price_text

Exact limit price as a string, or 'NA' to use 'price'.

stop_price_text

Exact trigger price as a string, or 'NA' to use 'stop_price'.

Value

The resulting order as a list.