Skip to content

Commit

Permalink
Merge pull request #5 from EVEprosper/btc
Browse files Browse the repository at this point in the history
Add support for crypto currencies
  • Loading branch information
lockefox authored Sep 10, 2017
2 parents 9e4738c + 3c5953e commit 81b1556
Show file tree
Hide file tree
Showing 24 changed files with 870 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ python:
- "3.5"
- "3.6"
- "3.6-dev" # 3.6 development branch
- "3.7-dev" # 3.7 development branch
# - "3.7-dev" # 3.7 development branch
install:
- "pip install semantic_version"
- "pip install ."
Expand All @@ -14,6 +14,7 @@ after_success:
- "pip install python-coveralls"
- "coveralls"
deploy:
skip_cleanup: true
provider: pypi
user: lockefox
password:
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include prosper/datareader/version.txt
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ Supported Feeds
- Market News Feed: Google
- Price Quote: Robinhood

**Coins**: helper libraries for fetching info on crypto currencies (via `hitBTC`_)

- Ticker Info: get info about coin<->currency conversion metadata
- Price Quote: get latest OHLC data for given coin
- Order Book: view current orders

.. _pandas-datareader: https://pandas-datareader.readthedocs.io/en/latest/index.html
.. _vader_sentiment: http://www.nltk.org/api/nltk.sentiment.html#module-nltk.sentiment.vader
.. _hitBTC: https://hitbtc.com

.. |Show Logo| image:: http://dl.eveprosper.com/podcast/logo-colour-17_sm2.png
:target: http://eveprosper.com
Expand Down
72 changes: 72 additions & 0 deletions docs/coins_help.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
========================
prosper.datareader.coins
========================

Meant as an extension of `pandas-datareader`_, ``prosper.datareader.coins`` provides the ability to fetch and parse data pertaining to crypto currencies.

``prosper.datareader.coins`` relies on services from `hitBTC`_.

Info
====

General metadata and feed testing tools.

**NOTE**: will implement caching layer for info, since this data should only refresh daily

get_symbol()
------------

``symbol_name = coins.info.get_symbol('COIN_TIKER', 'CONVERT_TICKER')``

Price of a crypto currency is measured in relation to other currencies a la FOREX. `hitBTC`_ requires a smash-cut version of coin + currency.

Examples:

+------+----------+--------+
| Coin | Currency | Ticker |
+======+==========+========+
| BTC | USD | BTCUSD |
+------+----------+--------+
| ETH | EUR | ETHEUR |
+------+----------+--------+
| ETH | BTC | ETHBTC |
+------+----------+--------+

Expected supported currencies:

- ``USD``
- ``EUR``
- ``ETH``
- ``BTC``

For more info, try ``info.supported_currencies()`` for a current list

get_ticker_info()
-----------------

``ticker_info = coins.info.get_ticker_info('TICKER')``

If working backwards from a ticker, this function returns the original `hitBTC symbols`_ data.

Prices
======

get_quote_hitbtc()
------------------

``quote_df = coins.prices.get_quote_hitbtc(['BTC', 'ETH'])``

Get a peek at the current price and trend of your favorite crypto currency. This feed helps get OHLC data as well as mimic `pandas-datareader`_ quote behavior with keys like ``pct_change``.

get_orderbook_hitbtc()
----------------------

``orderbook = coins.prices.get_orderbook_hitbtc('BTC', 'asks')``

When you absolutely, positively, need all the data... go to the orderbook. This supports ``asks`` and ``bids`` for lookup.

## TODO: add ``both`` behavior

.. _pandas-datareader: https://pandas-datareader.readthedocs.io/en/latest/index.html
.. _hitBTC: https://hitbtc.com/
.. _hitBTC symbols: https://hitbtc.com/api#symbols
8 changes: 5 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Supported Feeds

* `Utils`_: General utilities for additional insights
* `Stocks`_: Parse IRL stock quote data

* `Coins`_: Data utilities for cryptocoin price quotes

Index
=====
Expand All @@ -33,9 +33,10 @@ Index
:caption: Contents:

getting_started.rst
utils_help.rst
stocks_help.rst

coins_help.rst
utils_help.rst

Indices and tables
==================

Expand All @@ -47,6 +48,7 @@ Indices and tables
.. _pandas-datareader: https://pandas-datareader.readthedocs.io/en/latest/index.html
.. _Stocks: stocks_help.html
.. _Utils: utils_help.html
.. _Coins: coins_help.html

.. |Build Status| image:: https://travis-ci.org/EVEprosper/ProsperDatareader.svg?branch=master
:target: https://travis-ci.org/EVEprosper/ProsperDatareader
Expand Down
30 changes: 30 additions & 0 deletions docs/source/datareader.coins.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
datareader\.coins package
=========================

Submodules
----------

datareader\.coins\.info module
------------------------------

.. automodule:: datareader.coins.info
:members:
:undoc-members:
:show-inheritance:

datareader\.coins\.prices module
--------------------------------

.. automodule:: datareader.coins.prices
:members:
:undoc-members:
:show-inheritance:


Module contents
---------------

.. automodule:: datareader.coins
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions docs/source/datareader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Subpackages

.. toctree::

datareader.coins
datareader.stocks

Submodules
Expand Down
14 changes: 9 additions & 5 deletions prosper/datareader/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ def get_version():
"""
if not INSTALLED:
warnings.warn(
'Unable to resolve package version until installed',
UserWarning
)
return '0.0.0' #can't parse version without stuff installed
try:
with open('version.txt', 'r') as v_fh:
return v_fh.read()
except Exception:
warnings.warn(
'Unable to resolve package version until installed',
UserWarning
)
return '0.0.0' #can't parse version without stuff installed

return p_version.get_version(HERE)

Expand Down
Empty file.
117 changes: 117 additions & 0 deletions prosper/datareader/coins/info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""datareader.coins.info.py: tools for fetching cryptocoin metadata"""
from datetime import datetime
import itertools
from os import path

import requests
import pandas as pd

from prosper.datareader.config import LOGGER as G_LOGGER
import prosper.datareader.exceptions as exceptions

LOGGER = G_LOGGER
HERE = path.abspath(path.dirname(__file__))

__all__ = (
'get_symbol', 'get_ticker_info', 'supported_symbol_info'
)

SYMBOLS_URI = 'http://api.hitbtc.com/api/1/public/symbols'
def get_supported_symbols_hitbtc(
uri=SYMBOLS_URI,
data_key='symbols'
):
"""fetch supported symbols from API
Note:
Supported by hitbtc
https://hitbtc.com/api#symbols
Args:
uri (str, optional): address for API
data_key (str, optional): data key name in JSON data
Returns:
(:obj:`list`): list of supported feeds
"""
req = requests.get(uri)
req.raise_for_status()
data = req.json()

return data[data_key]

################################################################################

def supported_symbol_info(
key_name
):
"""find unique values for key_name in symbol feed
Args:
key_name (str): name of key to search
Returns:
(:obj:`list`): list of unique values
"""
symbols_df = pd.DataFrame(get_supported_symbols_hitbtc())

unique_list = list(symbols_df[key_name].unique())

return unique_list

def get_symbol(
commodity_ticker,
currency_ticker,
logger=LOGGER
):
"""get valid ticker to look up
Args:
commodity_ticker (str): short-name for crypto coin
currency_ticker (str): short-name for currency
logger (:obj:`logging.logger`, optional): logging handle
Returns:
(str): valid ticker for HITBTC
"""
logger.info('--Fetching symbol list from API')
symbols_df = pd.DataFrame(get_supported_symbols_hitbtc())

symbol = symbols_df.query(
'commodity==\'{commodity}\' & currency==\'{currency}\''.format(
commodity=commodity_ticker.upper(),
currency=currency_ticker.upper()
))

if symbol.empty:
raise exceptions.SymbolNotSupported()

return symbol['symbol'].iloc[0]

def get_ticker_info(
ticker,
logger=LOGGER
):
"""reverse lookup, get more info about a requested ticker
Args:
ticker (str): info ticker for coin (ex: BTCUSD)
force_refresh (bool, optional): ignore local cacne and fetch directly from API
logger (:obj:`logging.logger`, optional): logging handle
Returns:
(:obj:`dict`): hitBTC info about requested ticker
"""
logger.info('--Fetching symbol list from API')
data = get_supported_symbols_hitbtc()

## Skip pandas, vanilla list search ok here
for ticker_info in data:
if ticker_info['symbol'] == ticker.upper():
return ticker_info

raise exceptions.TickerNotFound()
Loading

0 comments on commit 81b1556

Please sign in to comment.