api_dict: API dictionary

Description Usage Details API dictionary actions API dictionary exceptions Note References Examples

View source: R/dictionaries.R

Description

By default provided to getOption("Rbitcoin.api.dict"). This function returns built-in Rbitcoin data set contains API dictionary for market.api.process function to perform pre-process API call arguments, post-process API call results and catch market level errors. Still there is function market.api.query that do not require any dictionary and can operate on any currency pair. Granularity of api dictionary data is c("market", "base", "quote", "action"). This dictionary can be edited/extended by user for new markets, currency pairs and actions.
Currently supported markets, currency pairs, actions use: api_dict()[!is.na(base), .(market, currency_pair = paste0(base,quote))][,unique(.SD)]

Usage

1

Details

Default dictionary post-process function returns list or data.table. The data.table is returned in case of (always) one row response from API (ticker, place_limit_order, etc.). The list is returned in case of (possible) multiple rows response from API (trades, wallet, etc.).

API dictionary actions

API dictionary exceptions

Note

Do not use api.dict from untrusted source or read whole it's code to ensure it is safe! The api dictionary was not fully tested, please follow the examples, if you find any bugs please report.

References

API documentation: https://www.bitstamp.net/api/, https://btc-e.com/api/documentation, https://www.kraken.com/help/api, https://www.bitmarket.pl/docs.php?file=api_private.html, https://github.com/hitbtc-com/hitbtc-api, https://www.btcc.com/apidocs

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
## Not run: 
api_dict()[]

# hitbtc cancel order exception
#req = list(oid = "") # as it is for other markets
req = list(oid = "",
           symbol = "BTCUSD",
           side = "sell") # issue open: https://github.com/hitbtc-com/hitbtc-api/issues/3
cancel_order <- market.api.process(market = 'hitbtc', action = 'cancel_order',
                                   req = req, key = '', secret = '')

# hitbtc payment (main account) balance
r <- market.api.query(market="hitbtc", url="https://api.hitbtc.com/api/1/payment/balance",
                      key="", secret="")

# historical data FAST: bitcoincharts full archive
browseURL("http://api.bitcoincharts.com/v1/csv/")
# download particular market data dump, extract and load using fread
trades <- fread(".hitbtcEUR.csv")
trades[,`:=`(date = as.POSIXct(V1,origin="1970-01-01", tz="UTC"),
             price = V2, amount = V3,
             tid = NA_character_, type = NA_character_)
       ][,c("V1","V2","V3"):=NULL][]

# historical data SLOW: loop using `tid` param - works only on kraken, hitbtc, bitmarket, btcchina!
market="kraken"
currency_pair=c("BTC","LTC")
csv.file = paste(market,paste(currency_pair,collapse=""),"trades.csv",sep="_")
batch_size = 1000 # kraken 1000, hitbtc 1000, bitmarket 500, btcchina 1000
last_tid = 0 # from the beginning
repeat{
  trades_batch = tryCatch(
    market.api.process(market=market,currency_pair=currency_pair, action="trades",
                       req=list(tid = last_tid))[["trades"]],
    error = function(e){
      message(e[["message"]])
      invisible(NULL)
    }
  )
  if(is.null(trades_batch)) next # error, skip
  if(nrow(trades_batch)==0) break # last batch empty
  last_tid <- trades_batch[length(tid),tid]
  write.table(trades_batch[,date:=as.integer(date)], csv.file, sep=",", row.names=FALSE,
              col.names=!file.exists(csv.file),
              append=file.exists(csv.file))
  cat(as.character(Sys.time(),"%Y-%m-%d %H-%M-%S"),": inserted ",nrow(trades_batch),
      " rows to ",csv.file," file, last inserted tid is ",last_tid,"\n",sep="")
  if(nrow(trades_batch) < batch_size) break # last batch
}
trades = fread(csv.file)[,date:=as.POSIXct(date,origin="1970-01-01",tz="UTC")]
print(trades)

# convert trades to xts
trades.xts = trades[,list(date,price,amount)][,xts(.SD[,-1,with=FALSE], order.by=date)]

## End(Not run)

jangorecki/Rbitcoin documentation built on May 18, 2019, 12:24 p.m.