diff --git a/exchanges/cex.py b/exchanges/cex.py new file mode 100644 index 0000000..662f3ed --- /dev/null +++ b/exchanges/cex.py @@ -0,0 +1,18 @@ +from exchanges.base import Exchange + + +class Cex(Exchange): + + TICKER_URL = 'https://cex.io/api/ticker/BTC/USD' + + @classmethod + def _current_price_extractor(cls, data): + return data.get('last') + + @classmethod + def _current_bid_extractor(cls, data): + return data.get('bid') + + @classmethod + def _current_ask_extractor(cls, data): + return data.get('ask') diff --git a/exchanges/coinbase.py b/exchanges/coinbase.py new file mode 100644 index 0000000..5184a50 --- /dev/null +++ b/exchanges/coinbase.py @@ -0,0 +1,28 @@ +from decimal import Decimal + +from exchanges.base import Exchange +from exchanges.helpers import get_response + + +class Coinbase(Exchange): + + TICKER_URL = 'https://api.coinbase.com/v2/prices/BTC-{}/{}' + + @classmethod + def _get_current_price(cls, currency='USD', price_type='spot'): + url = cls.TICKER_URL.format(currency, price_type) + data = get_response(url) + price = data.get('data').get('amount') + return Decimal(price) + + @classmethod + def get_current_price(cls, currency='USD'): + return cls._get_current_price(currency=currency, price_type='spot') + + @classmethod + def get_current_bid(cls, currency='USD'): + return cls._get_current_price(currency=currency, price_type='buy') + + @classmethod + def get_current_ask(cls, currency='USD'): + return cls._get_current_price(currency=currency, price_type='sell') diff --git a/exchanges/gemini.py b/exchanges/gemini.py new file mode 100644 index 0000000..86d148d --- /dev/null +++ b/exchanges/gemini.py @@ -0,0 +1,50 @@ +from datetime import datetime, timedelta +from decimal import Decimal +from exchanges.base import Exchange +from helpers import get_response + + +class Gemini(Exchange): + + TICKER_URL = 'https://api.gemini.com/v1/pubticker/btcusd' + + @classmethod + def _current_price_extractor(cls, data): + return data.get('last') + + @classmethod + def _current_bid_extractor(cls, data): + return data.get('bid') + + @classmethod + def _current_ask_extractor(cls, data): + return data.get('ask') + + @classmethod + def get_historical_data_as_dict(cls, start='2013-09-01'): + start_date = datetime.strptime(start, '%Y-%m-%d') + delta = datetime.today() - start_date + days = [start_date + timedelta(x) for x in range(delta.days + 1)] + data = map(cls._get_historical_data, days) + prices = {datetime.strftime(k, '%Y-%m-%d'): Decimal(str(v['price'])) + for (k,v) in zip(days, data)} + return prices + + @classmethod + def get_historical_data_as_list(cls, start='2013-09-01'): + pass + start_date = datetime.strptime(start, '%Y-%m-%d') + delta = datetime.today() - start_date + days = [start_date + timedelta(x) for x in range(delta.days + 1)] + data = map(lambda day: cls._get_historical_data(day), days) + ret = [ + {'date': datetime.strftime(k, '%Y-%m-%d'), + 'price': Decimal(str(v['price']))} + for (k,v) in zip(days, data) + ] + return ret + + @classmethod + def _get_historical_data(cls, day): + url = 'https://api.gemini.com/v1/trades/btcusd?since=%s&limit_trades=1' + return get_response(url % day.strftime('%s'))[0] diff --git a/setup.py b/setup.py index c01788b..f311b9c 100644 --- a/setup.py +++ b/setup.py @@ -1,14 +1,12 @@ # Copyright (C) 2015 Bitquant Research Laboratories (Asia) Limited # Released under the Simplified BSD License -from setuptools import ( - setup, - find_packages, -) +from setuptools import setup + setup( name='bitcoin-price-api', - version = '0.0.4', + version='0.0.5', author='Matthew Madurski', author_email='madurskimr@gmail.com', url='https://github.com/dursk/bitcoin-price-api', @@ -16,6 +14,6 @@ long_description='''Price API's for bitcoin exchanges''', license='MIT', packages=['exchanges'], - install_requires = ['requests', 'python-dateutil'], - use_2to3 = True + install_requires=['python-dateutil==2.4.2', 'requests==2.9.1'], + use_2to3=True )