Skip to content

Commit

Permalink
Migrate CoinMarketCap to API V2 / Introduce CoinMarketCapPro (resolve x…
Browse files Browse the repository at this point in the history
  • Loading branch information
Zapata committed Aug 24, 2018
1 parent 92722bc commit ec21075
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ Bitstamp | OK | Crypto | No | last and volume (in quote currency) from CEX ticke
Bittrex | OK | Crypto | No | last and volume (in quote currency) from summary api (bulk)
Coincap | OK | ALTCAP & ALTCAP.X | No | use provided market cap, no volume
CoinEgg | OK | Crypto |No | last and volume (in quote currency) from CEX ticker api
Coinmarketcap | OK | Crypto | No | volume weighted average of all prices reported at each market, volume in USD, 5 minutes delay (see https://coinmarketcap.com/faq/). TODO: Migrate to v2 before 30 November 2018
Coinmarketcap | Warn | Crypto | No | volume weighted average of all prices reported at each market, volume in USD, 5 minutes delay (see https://coinmarketcap.com/faq/). V1 API will be closed December 4th, 2018.
CoinmarketcapPro | OK | Crypto | Yes | volume weighted average of all prices reported at each market, volume in quote, 1 minutes delay. Use v2 api.
Currencylayer | OK | FIAT, BTC | Yes | ticker from api, only USD as base and hourly updated with free subscription, no volume info. From various source (https://currencylayer.com/faq)
CoinTiger | OK | Crypto | No | last and volume (in quote currency) from summary api (bulk)
Fixer | OK | FIAT | Yes | Very similar to CurrencyLayer, ticker from api, daily from European Central Bank, only EUR with free subscription, no volume info.
Expand Down Expand Up @@ -187,6 +188,7 @@ export CURRENCYLAYER_APIKEY=
export ALPHAVANTAGE_APIKEY=
export WORLDCOININDEX_APIKEY=
export MAGICWALLET_APIKEY=
export COINMARKETCAP_APIKEY=
```

To run all tests use: `pytest`.
Expand Down
21 changes: 21 additions & 0 deletions bitshares_pricefeed/sources/coinmarketcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,24 @@ def _fetch_altcap(self, feed):
if 'ALTCAP.X' in self.quotes:
self.add_rate(feed, 'BTC', 'ALTCAP.X', btc_altcapx_price, 1.0)
return feed

class CoinmarketcapPro(FeedSource):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if not hasattr(self, 'api_key'):
raise Exception("CoinmarketcapPro FeedSource requires 'api_key'.")

def _fetch(self):
feed = {}
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol={}&convert={}'
headers = { **_request_headers, 'X-CMC_PRO_API_KEY': self.api_key }
all_quotes = ','.join(self.quotes)
for base in self.bases:
response = requests.get(url=url.format(all_quotes, base), headers=headers, timeout=self.timeout)
result = response.json()
for quote, ticker in result['data'].items():
price = ticker['quote'][base]['price']
volume = ticker['quote'][base]['volume_24h'] / price
self.add_rate(feed, base, quote, price, volume)

return feed
8 changes: 7 additions & 1 deletion tests/sources/test_coinmarketcap.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from bitshares_pricefeed.sources.coinmarketcap import Coinmarketcap
import os
from bitshares_pricefeed.sources.coinmarketcap import Coinmarketcap, CoinmarketcapPro

def test_coinmarketcap_fetch(checkers):
source = Coinmarketcap(quotes=['BTC', 'BTS'], bases=['BTC', 'USD'])
Expand All @@ -15,3 +16,8 @@ def test_coinmarketcap_full_fetch(checkers):
source = Coinmarketcap(quotes=['ALTCAP', 'BTS'], bases=['BTC', 'USD'])
feed = source.fetch()
checkers.check_feed(feed, ['ALTCAP:BTC', 'BTS:BTC', 'BTS:USD'])

def test_coinmarketcappro_fetch(checkers):
source = CoinmarketcapPro(quotes=['BTC', 'BTS'], bases=['BTC', 'USD'], api_key=os.environ['COINMARKETCAP_APIKEY'])
feed = source.fetch()
checkers.check_feed(feed, ['BTC:USD', 'BTS:USD', 'BTS:BTC'])

0 comments on commit ec21075

Please sign in to comment.