Skip to content

Python Interface

Chris Priest edited this page Nov 6, 2017 · 2 revisions

Current Price

>>> from moneywagon import get_current_price
>>> get_current_price('btc', 'usd')
391.324
>>> get_current_price('ltc', 'rur', report_services=True)
([<Service: BTCE (1 in cache)>], 226)

A float is always returned. Older versions of moneywagon returned a two item tuple. Starting with moneywagon version 1.9.0, only a float is retuened. If your application needs to know which service was used, set the report_services argument to True. (See example above).

If an external service is down, the net service in the chain is tried, until a result is found.

If the API has changed, or the currency pairs is not implemented, an exception will be raised:

>>> get_current_price('nxt', 'mex')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "moneywagon/__init__.py", line 22, in get_current_price
    raise NoService("Can not find price for %s to %s" % (crypto, fiat))
NoService: Can not find price for nxt to mex

There is also a convert_to argument that can be used to convert the resulting exchange rate to another fiat currency. This feature is useful to be combined with the services argument:

>>> from moneywagon import get_current_price
>>> from moneywagon.services import OKcoin, GDAX
>>> get_current_price('btc', 'cny', convert_to='usd', services=[OKcoin])
827.5890061463529
>>> get_current_price('btc', 'usd', services=[GDAX])
831.86

Historical Cryptocurrency Price

The API is similar to the low-level current price API. There are two differences:

  1. The method is named get_historical instead of get_price.
  2. It takes an extra argument at_time. This can be a either a datetime instance representing when you'd like to get the price, or a string that will get converted to a datetime object by arrow.get
>>> from moneywagon import HistoricalCryptoPrice
>>> service = HistoricalCryptoPrice()
>>> service.action('btc', 'usd', '2013-11-13')
(354.94,
'BITCOIN/BITSTAMPUSD',
datetime.datetime(2013, 11, 13, 0, 0))

The result is the same, except there is a third item in the tuple. This third value is the time of the actual price. There are gaps in Quandl's data, so sometimes the actual price returned is from a day before or a day after.

Unlike the current price API, the historical price API only has an implementation for one service, and that service is Quandl.com. If Quandl is ever down, this feature will not work. If you know of an API service that hosts historical cryptocurrency prices, please let the moneywagon developers know.

Also, the Quandl service does not have every single cryptocurrency to fiat exchange history, so for some pairs, moneywagon has to make two different calls to Quandl.

>>> service.action('vtc', 'rur', '2014-11-13'))
(3.2636992,
'CRYPTOCHART/VTC x BITCOIN/BTCERUR',
datetime.datetime(2014, 11, 13, 0, 0))

In this case, moneywagon first gets the conversion rate from VTC-BTC on 2014-11-13. Then it gets hte conversion rate for BTC->RUR on 2014-11-13. The result that is returned is those two values multiplied together. This is similar to the process described earlier The nature of this calculation can also be seen in the source string (the second item in the response).

Address Balance

>>> from moneywagon import AddressBalance
>>> AddressBalance().action('ppc', 'PVoei4A3TozCSK8W9VvS55fABdTZ1BCwfj')
103.98

Get Blocks

>>> from moneywagon import get_block
>>> get_block('btc', latest=True)
{
    "block_number": 368900,
    "confirmations": 1,
    "hash": "0000000000000000043ab9d01e2e88ff460b6205b43cf3508ddeb8461bddc2fd",
    "merkle_root": "7205cd649ffe5645e1841ef2ba19d7c48166dc9a6f15584aa24d4af61236d96e",
    "mining_difficulty": 52278304845.59181,
    "next_hash": null,
    "previous_hash": "00000000000000000f3a1d9508d69a1310a1ed41e18025f143f28c8ea5f5575e",
    "sent_value": 8762.56646775,
    "size": 219868,
    "time": "2015-08-08T05:55:01+00:00",
    "total_fees": 0.07663532,
    "txids": [
      "da8812c9c7e02d4c159bc2c9978aa50dd295d88fe14f10e07101c729e771510e",
      "ce6c456ecf46be306eb65eb9ac8210466d6aacd0e44b8dfdeaf100d9aaadca47",
      ...
    ]
}
>>> get_block('btc', block_number=242)
{
    "block_number": 242,
    "confirmations": 829724,
    "hash": "3849a1aabc09d147d815652cadee10b55f8eddf63efe4174479dba7e74d76cf1",
    "merkle_root": "30a914ec415904b0dac0cf9bf5eed275b721cbb87a757878bc6d425817c52027",
    "mining_difficulty": 0.00024414,
    "next_hash": "1f427c34e3d98d7d0eb205be0881ea15d49c5e41f3d783e345f30747d2baad3b",
    "previous_hash": "a6af6882076ece122753d12c134815f33b2b3f3d9e8feeeb5529f6ec5ef3b31c",
    "sent_value": 50.0,
    "size": 215,
    "time": "2011-10-13T03:13:40+00:00",
    "total_fees": 0.0,
    "txids": [
      "da8812c9c7e02d4c159bc2c9978aa50dd295d88fe14f10e07101c729e771510e",
      "ce6c456ecf46be306eb65eb9ac8210466d6aacd0e44b8dfdeaf100d9aaadca47",
      ...
    ]
}
>>> get_block('doge', block_hash='a53d288822382a53250b930193562b7e61b218c8a9a449a9d003dafa2534a736')
{
    "block_number": 242,
    "confirmations": 824212,
    "hash": "a53d288822382a53250b930193562b7e61b218c8a9a449a9d003dafa2534a736",
    "merkle_root": "83d53e8dbbfdcf9e24a1ece401801e73a430db9c80da2ca3f74dc3b73c18abbf",
    "mining_difficulty": 0.00024414,
    "next_hash": "1aca39498439acff59afbabb6992bf9fa178415674415283f8a127120211a3dd",
    "previous_hash": "bb623eabcde58af2b3a412eb9866f54f414d5eef1de5f54bd6e396834c8ccc75",
    "sent_value": 790312.0,
    "size": 190,
    "time": "2013-12-08T04:07:20+00:00",
    "total_fees": 0.0,
    "txids": [
      "da8812c9c7e02d4c159bc2c9978aa50dd295d88fe14f10e07101c729e771510e",
      "ce6c456ecf46be306eb65eb9ac8210466d6aacd0e44b8dfdeaf100d9aaadca47",
      ...
    ]
}

Historical Transactions

>>> from moneywagon import HistoricalTransactions
>>> HistoricalTransactions().action('ltc', 'Lb78JDGxMcih1gs3AirMeRW6jaG5V9hwFZ')
[{'amount': 147.58363366,
'confirmations': 9093,
'date': datetime.datetime(2014, 11, 16, 23, 53, 37, tzinfo=tzutc()),
'txid': u'cb317dec84514773f34e4258cd0ff49eed6bfcf1770709b1ed07855d2e1a4aa4'},
{'amount': 19.7,
'confirmations': 100494,
'date': datetime.datetime(2014, 6, 16, 0, 7, 26, tzinfo=tzutc()),
'txid': u'846d316f369906f990262e1758eb0a2a953ebd47a9b1cf13d57aadc9ad2e19a3'},
{'amount': 71.75600005,
'confirmations': 219032,
'date': datetime.datetime(2013, 11, 27, 16, 36, 14, tzinfo=tzutc()),
'txid': u'9152784755564c3c680aa47a3a1cdc28e4896657bfc2e60626a0ee22b200af7c'}]