wallet_manager: Wallet Manager

Description Usage Arguments Value Wallet archive Exchange rates NA measures Schedule wallet tracking Troubleshooting References See Also Examples

View source: R/wallet_manager.R

Description

Downloads wallet balance from multiple sources and calculate value in chosen currency based on actual exchange rates. Function is limited to dictionary api.dict plus fiat-fiat exchange rates.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
wallet_manager(market.sources = NULL, blockchain.sources = NULL,
  manual.sources = NULL, min_amount = 0.0001, value_calc = TRUE,
  value_currency = "USD", value_currency_type = NULL, rate_priority,
  transfer_currency_pair = c(crypto = "BTC", fiat = "USD"),
  api.dict = getOption("Rbitcoin.api.dict",
  stop("no api.dict in options and not provided to wallet_manager")),
  archive_write = getOption("Rbitcoin.wallet_manager.archive_write", FALSE),
  archive_read = getOption("Rbitcoin.wallet_manager.archive_read", FALSE),
  archive_path = getOption("Rbitcoin.wallet_manager.archive_path",
  "wallet_archive.rds"), verbose = getOption("Rbitcoin.verbose", 0))

Arguments

market.sources

list of market sources definition, see examples. Mandatory fields: market, key, secret (for bitstamp also client_id).

blockchain.sources

list of blockchain sources definition, see examples. Mandatory field: address.

manual.sources

list of manually provided amounts, see examples. Mandatory fields: currency, amount, optional field: location, location_type.

min_amount

numeric used to filter out near zero amounts of source currency, default 0.0001.

value_calc

logical calculate value, by default TRUE, can be turned off by setting to FALSE. Process will be slightly faster due to no API calls for exchange rates.

value_currency

character default "USD", target currency in which measure the current value.

value_currency_type

character, optional for most currencies, if value_currency is an exotic currency you need to define its currency type ('crypto' or 'fiat') in this param or update getOption("Rbitcoin.ct.dict") param.

rate_priority

character vector of market and priorioties for sourcing exchange rates, this param needs to be maintained by user, read Exchange rates note below. Example param value rate_priority = c('bitstamp','kraken','hitbtc','bitmarket','btce').

transfer_currency_pair

vector length of 2 of named character, default c(crypto = "BTC", fiat = "USD"), read Exchange rates note below.

api.dict

data.table required when using custom API dictionary, read market.api.process for details.

archive_write

logical, default FALSE, recommended TRUE. If TRUE wallet manager result will be archived to "wallet_archive.rds" file in the working directory, read Wallet archive note below.

archive_read

logical, default FALSE, recommended FALSE. If TRUE it return full archive of wallets data over time grouped by wallet_id. To be used when passing results to rbtc.plot function or performing other analysis over time, read notes below.

archive_path

character, default "wallet_archive.rds", a wallet archive location.

verbose

integer Rbitcoin processing messages, print to console if verbose > 0, each subfunction reduce verbose by 1. If missing then getOption("Rbitcoin.verbose",0) is used.

Value

data.table object with wallet information in denormilized structure. When launch with wallet_read=TRUE then all historical archived wallet statuses will be returned. Field wallet_id is a processing batch id and also the timestamp of single wallet manager processing as integer in Unix time format. Since 0.9.3 new column has been added auth which corresponds to the *.sources args lists elements names, see examples.

Wallet archive

To be able to track wallet assets value over time you need to use archive_write=TRUE. It will archive wallet manager result data.table to wallet_archive.rds file (or filepath as archive_path arg) in not encrypted format (not a plain text also), sensitive data like amount and value will be available from R by readRDS("wallet_archive.rds"). This can be used to correct/manipulate archived data or union the results of the wallet manager performed on different machines by readRDS(); rbindlist(); saveRDS(), see examples. Setting archive_write==FALSE & archive_read==TRUE will skip processing and just load the archive, same as readRDS(). You should be aware the archive file will be growing over time, unless you have tons of sources defined or you scheduled wallet_manager every hour or less you should not experience any issues because of that. In case of the big size of archived rds file you can move data to database, use archive_write=FALSE and wrap function into database archiver function. For later analysis simply query full archive from database, be sure to match original data types.

Exchange rates

Exchange rates will be downloaded from different sources. Fiat-fiat rates will be sourced from yahoo finance, if yahoo would not be available then also fiat-fiat rate cannot be calculated. Rates for cryptocurrencies will be downloaded from markets tickers according to rate_priority and currency pairs available in api.dict. Currency type (crypto or fiat) is already defined in getOption("Rbitcoin.ct.dict"), can be edited for support other/new currency.
Markets used for crypto rates are defined by rate_priority as vector of market names in order of its priority from highest to lowest. User need to chose own trusted exchange rates providers and keep in mind to update rate_priority parameter when necessary. As we recently seen the mtgox after death was still spreading the public API data and any system which sources data from them would be affected, so the control over the source for exchange rates needs to be maintained by user. In case of calculation crypto rate for a currency pair which is not available in api.dict then transfer_currency_pair will be used to get indirect exchange rate. Example: exchange rate for NMC-GBP will be computed as NMC-BTC-USD-GBP using the default transfer_currency_pair and current api.dict. The process was not exhaustively tested, you can track all the exchange rates used in the processing by setting options(Rbitcoin.archive_exchange_rate=TRUE). This option will append the data to exchange_rate_archive.rds file in working directory.

NA measures

In case of missing exchange path (direct and indirect through transfer_currency_pair based on api.dict) between the currency in the wallet and the value_currency the NA will be provided to value for that currency. Any errors while downloading wallet data or exchange rates data will also result NA measure. Be sure to avoid NA measures: for unavailable sources you can provide amounts as manual source, for not supported alt cryptocurrencies precalculate its value to supported currency and provide as manual source. While plotting wallet_manager data any wallet batches which contain at least one NA measure will be omitted from plot.

Schedule wallet tracking

User may consider to schedule execution of the function with archive_write=TRUE for better wallet assets tracking over time. Schedule can be setup on OS by run prepared R script with wallet_manager function execution. In case of scheduling also plot of wallet manager data use archive_read=TRUE and pass results to rbtc.plot, more on rbtc.plot.wallet_manager.

Troubleshooting

In case of the issues with this function try first to verify if all of the sources are returning correct data, use blockchain.api.process and market.api.process functions. Possible sources for wallet data: market api, blockchain api, manually provided. Possible sources for exchange rate data: market tickers (taken from api.dict according to rate_priority), yahoo (for fiat-fiat, see references). If all sources works and issue still occurs please report. Fiat to fiat conversion using yahoo may not be available for all possible fiat currency pairs.

References

https://code.google.com/p/yahoo-finance-managed/wiki/csvQuotesDownload

See Also

rbtc.plot, blockchain.api.process, market.api.process, antiddos

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
## Not run: 
# example market.sources
market.sources <- list(
  "john smith" = list(market='kraken', key='', secret=''),
  "jane smith" = list(market='kraken', key='', secret=''),
  "john smith" = list(market='btce', key='', secret=''),
  "jane smith" = list(market='btce', key='', secret='')
)
# example blockchain.sources
blockchain.sources <- list(
  "john smith" = list(address='')
)
# example manual.sources
manual.sources <- list(
  "john smith" = list(location='bitfinex', location_type='market',
                      currency=c('BTC','USD'), amount=c(0.4,0)),
  "john smith" = list(location='fidor', location_type='bank',
                      currency=c('EUR','USD'), amount=c(20,0)),
  "jane smith" = list(location='fidor', location_type='bank',
                      currency=c('EUR','GBP'), amount=c(10,105))
)
# execute
wallet_dt <- wallet_manager(
  market.sources = market.sources,
  blockchain.sources = blockchain.sources,
  manual.sources = manual.sources,
  value_currency = 'USD', # your target currency
  rate_priority = c('bitstamp','kraken','hitbtc','btce','bitmarket'), # rates source priority
  archive_write = TRUE # by default FALSE, read ?wallet_manager
)
print(wallet_dt)

# plot recent wallet balances
rbtc.plot(wallet_dt, type="recent")

# load archive
wallet_dt <- wallet_manager(archive_write=FALSE, archive_read=TRUE)

# aggregate measures by time and currency
wallet_dt[,list(amount = sum(amount), value = sum(value)),
           keyby = c('wallet_id','currency','value_currency')]
# aggregate value by time and location
wallet_dt[,list(value = sum(value)),
           keyby = c('wallet_id','location','value_currency')]

# plot value over time
rbtc.plot(wallet_dt)

# remove processing batch from archive, by id
setkey(wallet_dt,wallet_id)
wallet_id_to_remove <- 1390000000
saveRDS(wallet_dt[!.(wallet_id_to_remove)], "wallet_archive.rds")

# To track exchange rates used set option Rbitcoin.archive_exchange_rate
options(Rbitcoin.archive_exchange_rate=TRUE)
wallet_dt <- wallet_manager(market.sources,
                            blockchain.sources,
                            manual.sources = manual.sources,
                            rate_priority = c('bitstamp','kraken','hitbtc','bitmarket','btce')
                            archive_write = TRUE)
exchange_rates_dt <- readRDS("exchange_rate_archive.rds")

# track hitbtc main account balance using dynamically created manual source element
manual.sources <- list(
  "john smith" = tryCatch(
    expr = {
      r <- market.api.query(market="hitbtc", url="https://api.hitbtc.com/api/1/payment/balance",
                            key="", secret="")
      list(location="hitbtc", location_type="market",
           currency=r[["balance"]][["currency_code"]], amount=r[["balance"]][["balance"]])
    },
    error = function(e) list(location="hitbtc", location_type="market",
                             currency=NA_character_, amount=NA_real_)
  )
)

## End(Not run)

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