In [1]:
from monitor import utc_to_tz
from dateutil import tz
from datetime import datetime
import krakenex
import pymongo
import networkx as nx
import pandas
import numpy as np
import tqdm
import seaborn
from matplotlib import pyplot as plt
import collections

In [2]:
def get_prices(asset_pairs, ticker):
    prices = {}
    for asset_key, asset in asset_pairs.iteritems():
        if asset_key.endswith('.d') or 'DASH' in asset_key:
            continue
        asset_name = asset_key
        base = str(asset['base'])
        quote = str(asset['quote'])
        assert asset_name == base + quote, '%s != %s + %s' % (asset_name, base, quote)
        assert asset_name in ticker, '`%s` not in ticker.' % asset_name
        last_price, last_volume = map(float, ticker[asset_name]['c'])
        assert last_price > 0
        assert (base, quote) not in ticker
        assert (quote, base) not in ticker
        prices[base, quote] = last_price
        prices[quote, base] = 1./last_price
    return prices

In [3]:
k = krakenex.API()
k.load_key('lost.key')
asset_pairs = k.query_public("AssetPairs")['result']

In [4]:
exchange_graph = nx.DiGraph()
fees = {}
for asset_key, asset in asset_pairs.iteritems():
    if asset_key.endswith('.d') or 'DASH' in asset_key:
        continue
    asset_name = asset_key
    base = str(asset['base'])
    quote = str(asset['quote'])
    # assert asset_name == base + quote, '(%s) %s != %s + %s' % (asset_key, asset_name, base, quote)
    assert asset['fees'][0][1] in [0.26, 0.2], '(%s) %.4e != %.4e' % (asset_name, asset['fees'][0][1], 0.26)
    fee = 1-asset['fees'][0][1]/100. 
    fees[base, quote] = fee
    fees[quote, base] = fee
    exchange_graph.add_edge(base, quote)
    exchange_graph.add_edge(quote, base)

In [5]:
cycles = map(tuple, nx.simple_cycles(exchange_graph))
edges = {cycle: tuple(zip(cycle[:-1], cycle[1:]) + [(cycle[-1], cycle[0])]) for cycle in cycles}
currencies_to_cycle = {tuple(sorted(c)): c for c in cycles}
cycles = currencies_to_cycle.values()

In [6]:
# ssh -L 27018:127.0.0.1:27017 klc
client = pymongo.MongoClient(host='localhost', port=27018)
db = client['crypto_market_data']
collection = db['tickers']
# collection.create_index('timestamp')
count = collection.count(with_limit_and_skip=True)
cursor = collection.find().sort([('timestamp', pymongo.ASCENDING)]).limit(count)

In [7]:
docs = [doc for doc in tqdm.tqdm_notebook(cursor, total=count)]


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-7-1572a5810785> in <module>()
----> 1 docs = [doc for doc in tqdm.tqdm_notebook(cursor, total=count)]

/home/afrechette2/miniconda2/lib/python2.7/site-packages/tqdm/_tqdm_notebook.pyc in __iter__(self, *args, **kwargs)
    185     def __iter__(self, *args, **kwargs):
    186         try:
--> 187             for obj in super(tqdm_notebook, self).__iter__(*args, **kwargs):
    188                 # return super(tqdm...) will not catch exception
    189                 yield obj

/home/afrechette2/miniconda2/lib/python2.7/site-packages/tqdm/_tqdm.pyc in __iter__(self)
    831 """, fp_write=getattr(self.fp, 'write', sys.stderr.write))
    832 
--> 833             for obj in iterable:
    834                 yield obj
    835                 # Update and print the progressbar.

/home/afrechette2/miniconda2/lib/python2.7/site-packages/pymongo/cursor.pyc in next(self)
   1088             raise StopIteration
   1089         _db = self.__collection.database
-> 1090         if len(self.__data) or self._refresh():
   1091             if self.__manipulate:
   1092                 return _db._fix_outgoing(self.__data.popleft(),

/home/afrechette2/miniconda2/lib/python2.7/site-packages/pymongo/cursor.pyc in _refresh(self)
   1030                                              self.__id,
   1031                                              self.__codec_options,
-> 1032                                              self.__max_await_time_ms))
   1033 
   1034         else:  # Cursor id is zero nothing else to return

/home/afrechette2/miniconda2/lib/python2.7/site-packages/pymongo/cursor.pyc in __send_message(self, operation)
    848             try:
    849                 response = client._send_message_with_response(operation,
--> 850                                                               **kwargs)
    851                 self.__address = response.address
    852                 if self.__exhaust:

/home/afrechette2/miniconda2/lib/python2.7/site-packages/pymongo/mongo_client.pyc in _send_message_with_response(self, operation, read_preference, exhaust, address)
    842             self.__all_credentials,
    843             self._event_listeners,
--> 844             exhaust)
    845 
    846     def _reset_on_error(self, server, func, *args, **kwargs):

/home/afrechette2/miniconda2/lib/python2.7/site-packages/pymongo/mongo_client.pyc in _reset_on_error(self, server, func, *args, **kwargs)
    853         """
    854         try:
--> 855             return func(*args, **kwargs)
    856         except NetworkTimeout:
    857             # The socket has been closed. Don't reset the server.

/home/afrechette2/miniconda2/lib/python2.7/site-packages/pymongo/server.pyc in send_message_with_response(self, operation, set_slave_okay, all_credentials, listeners, exhaust)
    129             try:
    130                 sock_info.send_message(data, max_doc_size)
--> 131                 response_data = sock_info.receive_message(1, request_id)
    132             except Exception as exc:
    133                 if publish:

/home/afrechette2/miniconda2/lib/python2.7/site-packages/pymongo/pool.pyc in receive_message(self, operation, request_id)
    270                 self.sock, operation, request_id, self.max_message_size)
    271         except BaseException as error:
--> 272             self._raise_connection_failure(error)
    273 
    274     def legacy_write(self, request_id, msg, max_doc_size, with_last_error):

/home/afrechette2/miniconda2/lib/python2.7/site-packages/pymongo/pool.pyc in _raise_connection_failure(self, error)
    370             _raise_connection_failure(self.address, error)
    371         else:
--> 372             raise error
    373 
    374     def __eq__(self, other):

KeyboardInterrupt: 

In [40]:
rows = []
tradable_pairs = exchange_graph.to_undirected().edges()
for doc in tqdm.tqdm_notebook(docs):
    try:
        timestamp = utc_to_tz(doc['timestamp'], tz.gettz('PST'))
        prices = get_prices(asset_pairs, doc['ticker'])
        for cur1, cur2 in tradable_pairs:
            price = prices[cur1,cur2]
            rows.append(dict(timestamp=timestamp, cur1=cur1, cur2=cur2, price=price))
    except AssertionError as e:
        print 'Error on id `%s`.' % doc['_id']
        print str(e)
cur_prices_df = pandas.DataFrame(rows)


Error on id `58ec55ca881dba23d1b5f7af`.
`XXBTZCAD` not in ticker.
Error on id `58ec662f881dba31611372f4`.
`XXBTZCAD` not in ticker.
Error on id `58ec6886881dba316113736b`.
`XXBTZCAD` not in ticker.
Error on id `58ec6911881dba3161137385`.
`XXBTZCAD` not in ticker.
Error on id `58ec69a3881dba3161137391`.
`XZECZUSD` not in ticker.
Error on id `58ec69b0881dba3161137394`.
`XXBTZCAD` not in ticker.
Error on id `58ec69b9881dba3161137396`.
`XXBTZCAD` not in ticker.
Error on id `58ec6c61881dba3161137419`.
`XXBTZCAD` not in ticker.
Error on id `58ec6c77881dba316113741e`.
`XXBTZCAD` not in ticker.
Error on id `58ec6f37881dba316113749a`.
`XXBTZCAD` not in ticker.
Error on id `58ec711f881dba31611374fd`.
`XXBTZCAD` not in ticker.
Error on id `58ec71dc881dba3161137518`.
`XXBTZCAD` not in ticker.
Error on id `58ec71fe881dba3161137520`.
`XXBTZCAD` not in ticker.
Error on id `58ec7207881dba3161137522`.
`XXBTZCAD` not in ticker.
Error on id `58ec74b1881dba3161137596`.
`XXBTZCAD` not in ticker.
Error on id `58ec74b6881dba3161137597`.
`XXBTZCAD` not in ticker.
Error on id `58ec74cb881dba316113759c`.
`XXBTZCAD` not in ticker.
Error on id `58ec777a881dba316113760f`.
`XZECZUSD` not in ticker.
Error on id `58ec778b881dba3161137613`.
`XXBTZCAD` not in ticker.
Error on id `58ec7a1b881dba3161137685`.
`XZECZEUR` not in ticker.
Error on id `58ec7a30881dba316113768a`.
`XXBTZCAD` not in ticker.
Error on id `58ec7bba881dba31611376dc`.
`XXBTZCAD` not in ticker.
Error on id `58ec7ca3881dba31611376f9`.
`XXBTZCAD` not in ticker.
Error on id `58ec7cde881dba3161137700`.
`XXBTZCAD` not in ticker.
Error on id `58ec7d00881dba3161137708`.
`XXBTZCAD` not in ticker.
Error on id `58ec7d0c881dba316113770b`.
`XXBTZCAD` not in ticker.
Error on id `58ec7fac881dba316113777e`.
`XXMRZUSD` not in ticker.
Error on id `58ec7fb5881dba3161137780`.
`XXBTZCAD` not in ticker.
Error on id `58ec7fc1881dba3161137783`.
`XXBTZCAD` not in ticker.
Error on id `58ec7fc6881dba3161137784`.
`XXBTZCAD` not in ticker.
Error on id `58ec8181881dba31611377df`.
`XXBTZCAD` not in ticker.
Error on id `58ec82b7881dba316113780a`.
`XXBTZCAD` not in ticker.
Error on id `58ec8345881dba3161137827`.
`XXBTZCAD` not in ticker.
Error on id `58ec8587881dba316113788d`.
`XXMRZUSD` not in ticker.
Error on id `58ec8598881dba3161137891`.
`XXBTZCAD` not in ticker.
Error on id `58ec85a1881dba3161137893`.
`XXBTZCAD` not in ticker.
Error on id `58ec85a5881dba3161137894`.
`XXBTZCAD` not in ticker.
Error on id `58ec85a9881dba3161137895`.
`XXBTZCAD` not in ticker.
Error on id `58ec8825881dba31611378f8`.
`XXBTZCAD` not in ticker.
Error on id `58ec882e881dba31611378fa`.
`XXBTZCAD` not in ticker.
Error on id `58ec89bd881dba316113794b`.
`XXBTZCAD` not in ticker.
Error on id `58ec8b17881dba3161137985`.
`XXBTZCAD` not in ticker.
Error on id `58ec8cb2881dba31611379d7`.
`XXBTZCAD` not in ticker.
Error on id `58ec8e4c881dba3161137a0c`.
`XZECZEUR` not in ticker.
Error on id `58ec8e5e881dba3161137a10`.
`XXBTZCAD` not in ticker.
Error on id `58ec8e6b881dba3161137a13`.
`XXBTZCAD` not in ticker.
Error on id `58ec9152881dba3161137a94`.
`XXBTZCAD` not in ticker.
Error on id `58ec9168881dba3161137a99`.
`XXBTZCAD` not in ticker.
Error on id `58ec91a5881dba3161137aa4`.
`XXBTZCAD` not in ticker.
Error on id `58ecf44d881dba7aeb134390`.
`XXBTZCAD` not in ticker.
Error on id `58ecf5e4881dba7aeb1343da`.
`XXBTZCAD` not in ticker.
Error on id `58ecf879881dba7e06b81e5f`.
`XXBTZCAD` not in ticker.
Error on id `58ecf894881dba7e06b81e64`.
`XXBTZCAD` not in ticker.
Error on id `58ecf89f881dba7e06b81e66`.
`XXBTZCAD` not in ticker.
Error on id `58ecf8a4881dba7e06b81e67`.
`XXBTZCAD` not in ticker.
Error on id `58ecf8ae881dba7e06b81e69`.
`XXBTZCAD` not in ticker.
Error on id `58ecfa3c881dba7e06b81e9f`.
`XXBTZCAD` not in ticker.
Error on id `58ecfa4e881dba7e06b81ea2`.
`XXBTZCAD` not in ticker.
Error on id `58ecfa63881dba7e06b81ea6`.
`XXBTZCAD` not in ticker.
Error on id `58ecfb58881dba7e06b81ece`.
`XXBTZCAD` not in ticker.
Error on id `58ecfc41881dba7e06b81ee7`.
`XXMRZUSD` not in ticker.
Error on id `58ecfc5c881dba7e06b81eec`.
`XXBTZCAD` not in ticker.
Error on id `58ecfc66881dba7e06b81eee`.
`XXBTZCAD` not in ticker.
Error on id `58ecfc7c881dba7e06b81ef1`.
`XXBTZCAD` not in ticker.
Error on id `58ecfe7d881dba7e06b81f3a`.
`XXBTZCAD` not in ticker.
Error on id `58ecfe98881dba7e06b81f3f`.
`XXBTZCAD` not in ticker.
Error on id `58ed1623881dba17d5c59918`.
`XXBTZCAD` not in ticker.
Error on id `58ed1639881dba17d5c5991c`.
`XXBTZCAD` not in ticker.
Error on id `58ed1649881dba17d5c5991f`.
`XXBTZCAD` not in ticker.
Error on id `58ed1658881dba17d5c59921`.
`XXBTZCAD` not in ticker.
Error on id `58ed17de881dba17d5c59959`.
`XXMRZUSD` not in ticker.
Error on id `58ed1804881dba17d5c59960`.
`XXBTZCAD` not in ticker.
Error on id `58ed180a881dba17d5c59961`.
`XXBTZCAD` not in ticker.
Error on id `58ed19a2881dba17d5c59999`.
`XXBTZCAD` not in ticker.
Error on id `58ed19a7881dba17d5c5999a`.
`XXBTZCAD` not in ticker.
Error on id `58ed19b7881dba17d5c5999d`.
`XXBTZCAD` not in ticker.
Error on id `58ed1b1e881dba17d5c599cc`.
`XXBTZCAD` not in ticker.
Error on id `58ed1b45881dba17d5c599ce`.
`XZECZEUR` not in ticker.
Error on id `58ed1b4b881dba17d5c599cf`.
`XXBTZCAD` not in ticker.
Error on id `58ed1b65881dba17d5c599d4`.
`XXBTZCAD` not in ticker.
Error on id `58ed1b70881dba17d5c599d6`.
`XXBTZCAD` not in ticker.
Error on id `58ed1dfa881dba17d5c59a2b`.
`XXBTZCAD` not in ticker.
Error on id `58ed1e83881dba17d5c59a39`.
`XXBTZCAD` not in ticker.
Error on id `58ed1ead881dba17d5c59a3c`.
`XXBTZCAD` not in ticker.
Error on id `58ed1eb8881dba17d5c59a3e`.
`XXBTZCAD` not in ticker.
Error on id `58ed1ece881dba17d5c59a42`.
`XXBTZCAD` not in ticker.
Error on id `58ed1fc1881dba17d5c59a66`.
`XXBTZCAD` not in ticker.
Error on id `58ed205d881dba17d5c59a76`.
`XXBTZCAD` not in ticker.
Error on id `58ed2088881dba17d5c59a7e`.
`XXBTZCAD` not in ticker.
Error on id `58ed20da881dba17d5c59a8c`.
`XXBTZCAD` not in ticker.
Error on id `58ed212c881dba17d5c59a99`.
`XXBTZCAD` not in ticker.
Error on id `58ed21e7881dba17d5c59aaf`.
`XXBTZCAD` not in ticker.
Error on id `58ed21f8881dba17d5c59ab2`.
`XXBTZCAD` not in ticker.
Error on id `58ed220d881dba17d5c59ab6`.
`XXBTZCAD` not in ticker.
Error on id `58ed2225881dba17d5c59ab9`.
`XXBTZCAD` not in ticker.
Error on id `58ed239a881dba17d5c59aec`.
`XXBTZCAD` not in ticker.
Error on id `58ed23fc881dba17d5c59afb`.
`XXBTZCAD` not in ticker.
Error on id `58ed242f881dba17d5c59b03`.
`XXBTZCAD` not in ticker.
Error on id `58ed252d881dba17d5c59b1b`.
`XXBTZCAD` not in ticker.
Error on id `58ed2538881dba17d5c59b1d`.
`XXBTZCAD` not in ticker.
Error on id `58ed2542881dba17d5c59b1f`.
`XXBTZCAD` not in ticker.
Error on id `58ed26f6881dba17d5c59b56`.
`XXBTZCAD` not in ticker.
Error on id `58ed287f881dba17d5c59b8f`.
`XXBTZCAD` not in ticker.
Error on id `58ed289f881dba17d5c59b95`.
`XXBTZCAD` not in ticker.
Error on id `58ed2a21881dba17d5c59bcb`.
`XXBTZCAD` not in ticker.
Error on id `58ed2a26881dba17d5c59bcc`.
`XXBTZCAD` not in ticker.
Error on id `58ed2a51881dba17d5c59bd4`.
`XXBTZCAD` not in ticker.
Error on id `58ed2bc8881dba17d5c59c05`.
`XXBTZCAD` not in ticker.
Error on id `58ed2d4c881dba17d5c59c3b`.
`XXBTZCAD` not in ticker.
Error on id `58ed2d99881dba17d5c59c48`.
`XXBTZCAD` not in ticker.
Error on id `58ed2e6e881dba17d5c59c65`.
`XXBTZCAD` not in ticker.
Error on id `58ed2f1b881dba17d5c59c74`.
`XXBTZCAD` not in ticker.
Error on id `58ed310e881dba17d5c59cbc`.
`XXBTZCAD` not in ticker.
Error on id `58ed3114881dba17d5c59cbd`.
`XXBTZCAD` not in ticker.
Error on id `58ed316d881dba17d5c59ccb`.
`XXBTZCAD` not in ticker.
Error on id `58ed32dd881dba17d5c59cfa`.
`XXBTZCAD` not in ticker.
Error on id `58ed34f7881dba17d5c59d43`.
`XXBTZCAD` not in ticker.
Error on id `58ed3507881dba17d5c59d46`.
`XXBTZCAD` not in ticker.
Error on id `58ed350d881dba17d5c59d47`.
`XXBTZCAD` not in ticker.
Error on id `58ed3700881dba17d5c59d83`.
`XXMRZUSD` not in ticker.
Error on id `58ed372a881dba17d5c59d8b`.
`XXBTZCAD` not in ticker.
Error on id `58ed38d8881dba17d5c59dc8`.
`XXBTZCAD` not in ticker.
Error on id `58ed3aff881dba17d5c59e16`.
`XXBTZCAD` not in ticker.
Error on id `58ed3d02881dba17d5c59e5c`.
`XXBTZCAD` not in ticker.
Error on id `58ed3d28881dba17d5c59e62`.
`XXBTZCAD` not in ticker.
Error on id `58ed3f03881dba17d5c59ea6`.
`XXBTZCAD` not in ticker.
Error on id `58ed3f0d881dba17d5c59ea8`.
`XXBTZCAD` not in ticker.
Error on id `58ed3f13881dba17d5c59ea9`.
`XXBTZCAD` not in ticker.
Error on id `58ed3f28881dba17d5c59ead`.
`XXBTZCAD` not in ticker.
Error on id `58ed40b7881dba17d5c59eec`.
`XXBTZCAD` not in ticker.
Error on id `58ed40c1881dba17d5c59eed`.
`XXBTZCAD` not in ticker.
Error on id `58ed4101881dba17d5c59ef6`.
`XXBTZCAD` not in ticker.
Error on id `58ed42da881dba17d5c59f34`.
`XZECZEUR` not in ticker.
Error on id `58ed444c881dba17d5c59f6d`.
`XXBTZCAD` not in ticker.
Error on id `58ed44c4881dba17d5c59f78`.
`XXBTZCAD` not in ticker.
Error on id `58ed44ce881dba17d5c59f7a`.
`XXBTZCAD` not in ticker.

In [41]:
cur_prices_df


Out[41]:
cur1 cur2 price timestamp
0 XXMR ZEUR 2.062388e+01 2017-04-10 21:02:56.633000-07:00
1 XXMR ZUSD 2.162706e+01 2017-04-10 21:02:56.633000-07:00
2 XXMR XXBT 1.806000e-02 2017-04-10 21:02:56.633000-07:00
3 XMLN XETH 6.893100e-01 2017-04-10 21:02:56.633000-07:00
4 XMLN XXBT 2.480000e-02 2017-04-10 21:02:56.633000-07:00
5 ZGBP XETH 2.813731e-02 2017-04-10 21:02:56.633000-07:00
6 ZGBP XXBT 1.049314e-03 2017-04-10 21:02:56.633000-07:00
7 XXBT ZUSD 1.205100e+03 2017-04-10 21:02:56.633000-07:00
8 XXBT ZJPY 1.345320e+05 2017-04-10 21:02:56.633000-07:00
9 XXBT ZEUR 1.133995e+03 2017-04-10 21:02:56.633000-07:00
10 XXBT ZCAD 1.585001e+03 2017-04-10 21:02:56.633000-07:00
11 XXBT XLTC 1.192862e+02 2017-04-10 21:02:56.633000-07:00
12 XXBT XXRP 3.532321e+04 2017-04-10 21:02:56.633000-07:00
13 XXBT XETC 4.735521e+02 2017-04-10 21:02:56.633000-07:00
14 XXBT XETH 2.717908e+01 2017-04-10 21:02:56.633000-07:00
15 XXBT XXLM 4.310345e+05 2017-04-10 21:02:56.633000-07:00
16 XXBT XXDG 3.225806e+06 2017-04-10 21:02:56.633000-07:00
17 XXBT XICN 2.898551e+03 2017-04-10 21:02:56.633000-07:00
18 XXBT XZEC 1.871187e+01 2017-04-10 21:02:56.633000-07:00
19 XXBT XREP 1.142204e+02 2017-04-10 21:02:56.633000-07:00
20 ZUSD XLTC 1.002004e-01 2017-04-10 21:02:56.633000-07:00
21 ZUSD USDT 1.001803e+00 2017-04-10 21:02:56.633000-07:00
22 ZUSD XZEC 1.555210e-02 2017-04-10 21:02:56.633000-07:00
23 ZUSD XREP 9.365743e-02 2017-04-10 21:02:56.633000-07:00
24 ZUSD XXLM 3.510004e+02 2017-04-10 21:02:56.633000-07:00
25 ZUSD XETC 3.858784e-01 2017-04-10 21:02:56.633000-07:00
26 ZUSD XETH 2.245647e-02 2017-04-10 21:02:56.633000-07:00
27 ZJPY XETH 2.014678e-04 2017-04-10 21:02:56.633000-07:00
28 ZEUR XLTC 1.054207e-01 2017-04-10 21:02:56.633000-07:00
29 ZEUR XZEC 1.663891e-02 2017-04-10 21:02:56.633000-07:00
... ... ... ... ...
161508 XXBT ZJPY 1.357958e+05 2017-04-11 14:11:18.424000-07:00
161509 XXBT ZEUR 1.153730e+03 2017-04-11 14:11:18.424000-07:00
161510 XXBT ZCAD 1.604104e+03 2017-04-11 14:11:18.424000-07:00
161511 XXBT XLTC 1.279591e+02 2017-04-11 14:11:18.424000-07:00
161512 XXBT XXRP 3.680530e+04 2017-04-11 14:11:18.424000-07:00
161513 XXBT XETC 4.784575e+02 2017-04-11 14:11:18.424000-07:00
161514 XXBT XETH 2.823981e+01 2017-04-11 14:11:18.424000-07:00
161515 XXBT XXLM 4.366812e+05 2017-04-11 14:11:18.424000-07:00
161516 XXBT XXDG 3.125000e+06 2017-04-11 14:11:18.424000-07:00
161517 XXBT XICN 3.030303e+03 2017-04-11 14:11:18.424000-07:00
161518 XXBT XZEC 1.957905e+01 2017-04-11 14:11:18.424000-07:00
161519 XXBT XREP 1.196029e+02 2017-04-11 14:11:18.424000-07:00
161520 ZUSD XLTC 1.033049e-01 2017-04-11 14:11:18.424000-07:00
161521 ZUSD USDT 1.002607e+00 2017-04-11 14:11:18.424000-07:00
161522 ZUSD XZEC 1.618368e-02 2017-04-11 14:11:18.424000-07:00
161523 ZUSD XREP 9.790330e-02 2017-04-11 14:11:18.424000-07:00
161524 ZUSD XXLM 3.508772e+02 2017-04-11 14:11:18.424000-07:00
161525 ZUSD XETC 3.879367e-01 2017-04-11 14:11:18.424000-07:00
161526 ZUSD XETH 2.289902e-02 2017-04-11 14:11:18.424000-07:00
161527 ZJPY XETH 2.021356e-04 2017-04-11 14:11:18.424000-07:00
161528 ZEUR XLTC 1.110741e-01 2017-04-11 14:11:18.424000-07:00
161529 ZEUR XZEC 1.666713e-02 2017-04-11 14:11:18.424000-07:00
161530 ZEUR XETH 2.438430e-02 2017-04-11 14:11:18.424000-07:00
161531 ZEUR XXLM 3.824092e+02 2017-04-11 14:11:18.424000-07:00
161532 ZEUR XETC 4.147347e-01 2017-04-11 14:11:18.424000-07:00
161533 ZEUR XREP 1.010961e-01 2017-04-11 14:11:18.424000-07:00
161534 ZCAD XETH 1.739128e-02 2017-04-11 14:11:18.424000-07:00
161535 XETH XREP 4.149722e+00 2017-04-11 14:11:18.424000-07:00
161536 XETH XICN 1.081081e+02 2017-04-11 14:11:18.424000-07:00
161537 XETH XETC 1.709369e+01 2017-04-11 14:11:18.424000-07:00

161538 rows × 4 columns


In [50]:
plot_df = cur_prices_df[
    (cur_prices_df['timestamp'] < datetime(2017, 4, 11, 2, tzinfo=tz.gettz('PST')))
]

fg = seaborn.FacetGrid(
    data=plot_df,
    col='cur1',
    col_order=sorted(plot_df['cur1'].unique()),
    row='cur2',
    row_order=sorted(plot_df['cur2'].unique()),
    sharex=True,
    sharey=False,
    margin_titles=True
)
def facet(data, color):
    plt.plot(data['timestamp'], data['price'], color=color)
    
#plt.figure(figsize=(12,12))

fg.map_dataframe(facet)
# for (i, j, k), data in fg.facet_data():
#     if data.empty:
#         ax = fg.facet_axis(i, j)
#         ax.set_axis_off()
    
# max_df = plot_df.groupby('timestamp')['rate'].max().reset_index()
# for (i, j, k), data in fg.facet_data():
#     if k == 0:
#         plt.plot(max_df['timestamp'], max_df['rate'], color='k', label='max', linewidth=1.5)
        
        
fg.set_xticklabels(rotation=30)
fg.add_legend()
# fg.set(yscale='log')
fg.set_xlabels('time')
fg.set_ylabels('rate')
# fg.add_legend()
plt.savefig('cur_prices.png')
plt.show()



In [29]:
rows = []
for doc in tqdm.tqdm_notebook(docs):
    try:
        timestamp = utc_to_tz(doc['timestamp'], tz.gettz('PST'))
        prices = get_prices(asset_pairs, doc['kraken_ticker'])
        for cycle in cycles:
            cycle_edges = edges[cycle]
            for e in cycle_edges:
                if e not in prices:
                    raise ValueError('Edge %s not in prices.' % str(e))
            prod = lambda x,y : x*y
            cycle_fee = reduce(prod, map(fees.get, cycle_edges))
            cycle_price = reduce(prod, map(prices.get, cycle_edges))
            cycle_price = max(cycle_price, 1./cycle_price)
            rate = cycle_fee*cycle_price
            rows.append(dict(timestamp=timestamp, cycle=cycle, rate=rate))
    except AssertionError as e:
        print 'Error on id `%s`.' % doc['_id']
        print str(e)
        
df = pandas.DataFrame(rows)


Error on id `58edad0f881dba631bd50da1`.
`XXBTZCAD` not in ticker.
Error on id `58edad20881dba631bd50da4`.
`XXBTZCAD` not in ticker.
Error on id `58edb09c881dba631bd50e2c`.
`XXBTZCAD` not in ticker.
Error on id `58edb0a1881dba631bd50e2d`.
`XXBTZCAD` not in ticker.
Error on id `58edb476881dba631bd50ebc`.
`XXBTZCAD` not in ticker.
Error on id `58edb47b881dba631bd50ebd`.
`XXBTZCAD` not in ticker.
Error on id `58edb492881dba631bd50ec1`.
`XXBTZCAD` not in ticker.
Error on id `58edb6ad881dba631bd50f1a`.
`XXBTZCAD` not in ticker.
Error on id `58edb7d6881dba631bd50f40`.
`XXBTZCAD` not in ticker.
Error on id `58edbb20881dba631bd50fc2`.
`XXBTZCAD` not in ticker.
Error on id `58edbe61881dba631bd51047`.
`XXBTZCAD` not in ticker.
Error on id `58edbee9881dba631bd51059`.
`XXBTZCAD` not in ticker.
Error on id `58edbeee881dba631bd5105a`.
`XXBTZCAD` not in ticker.
Error on id `58edbf05881dba631bd5105e`.
`XXBTZCAD` not in ticker.
Error on id `58edc1f7881dba631bd510c1`.
`XXBTZCAD` not in ticker.
Error on id `58edc1fd881dba631bd510c2`.
`XXBTZCAD` not in ticker.
Error on id `58edc22f881dba631bd510cb`.
`XXBTZCAD` not in ticker.
Error on id `58edc28b881dba631bd510d9`.
`XXBTZCAD` not in ticker.
Error on id `58edc65c881dba631bd51165`.
`XXBTZCAD` not in ticker.
Error on id `58edc8a7881dba631bd511b6`.
`XXBTZCAD` not in ticker.
Error on id `58edcaeb881dba631bd5120d`.
`XXBTZCAD` not in ticker.
Error on id `58edcba4881dba631bd5121f`.
`XXBTZCAD` not in ticker.
Error on id `58edcbc0881dba631bd51224`.
`XXBTZCAD` not in ticker.
Error on id `58edcbcb881dba631bd51226`.
`XXBTZCAD` not in ticker.
Error on id `58edcdd1881dba631bd51270`.
`XXBTZCAD` not in ticker.
Error on id `58edce47881dba631bd5127f`.
`XXBTZCAD` not in ticker.
Error on id `58edce4d881dba631bd51280`.
`XXBTZCAD` not in ticker.
Error on id `58edce68881dba631bd51285`.
`XXBTZCAD` not in ticker.
Error on id `58edcfad881dba631bd512b3`.
`XXBTZCAD` not in ticker.
Error on id `58edcfe9881dba631bd512bb`.
`XXBTZCAD` not in ticker.
Error on id `58edd0c2881dba631bd512d2`.
`XXBTZCAD` not in ticker.
Error on id `58edd0d8881dba631bd512d6`.
`XXBTZCAD` not in ticker.
Error on id `58edd0f4881dba631bd512db`.
`XXBTZCAD` not in ticker.
Error on id `58edd359881dba631bd5132f`.
`XXBTZCAD` not in ticker.
Error on id `58edd4c5881dba631bd5136b`.
`XXBTZCAD` not in ticker.
Error on id `58edd7f2881dba631bd513e3`.
`XXBTZCAD` not in ticker.
Error on id `58edd813881dba631bd513e9`.
`XXBTZCAD` not in ticker.
Error on id `58edda2a881dba631bd51430`.
`XXBTZCAD` not in ticker.
Error on id `58edda36881dba631bd51431`.
`XXBTZCAD` not in ticker.
Error on id `58edda42881dba631bd51433`.
`XXBTZCAD` not in ticker.
Error on id `58edda5d881dba631bd51438`.
`XXBTZCAD` not in ticker.
Error on id `58edda6e881dba631bd5143b`.
`XXBTZCAD` not in ticker.
Error on id `58edda73881dba631bd5143c`.
`XXBTZCAD` not in ticker.
Error on id `58eddce0881dba631bd51496`.
`XXBTZCAD` not in ticker.
Error on id `58eddcec881dba631bd51498`.
`XXBTZCAD` not in ticker.
Error on id `58edddcc881dba631bd514b7`.
`XXBTZCAD` not in ticker.
Error on id `58eddefd881dba631bd514d4`.
`XXBTZCAD` not in ticker.
Error on id `58eddf19881dba631bd514d9`.
`XXBTZCAD` not in ticker.
Error on id `58eddf1e881dba631bd514da`.
`XXBTZCAD` not in ticker.
Error on id `58eddf2f881dba631bd514dd`.
`XXBTZCAD` not in ticker.
Error on id `58ede0f5881dba631bd5151a`.
`XXBTZCAD` not in ticker.
Error on id `58ede12a881dba631bd5151d`.
`XXBTZCAD` not in ticker.
Error on id `58ede16d881dba631bd51529`.
`XXBTZCAD` not in ticker.
Error on id `58ede3ad881dba631bd5156f`.
`XXBTZCAD` not in ticker.
Error on id `58ede3ce881dba631bd51575`.
`XXBTZCAD` not in ticker.
Error on id `58ede570881dba631bd515b3`.
`XXBTZCAD` not in ticker.
Error on id `58ede5ce881dba631bd515ba`.
`XXBTZCAD` not in ticker.
Error on id `58ede5ec881dba631bd515bd`.
`XXMRZUSD` not in ticker.
Error on id `58ede60d881dba631bd515c3`.
`XXBTZCAD` not in ticker.
Error on id `58ede618881dba631bd515c5`.
`XXBTZCAD` not in ticker.
Error on id `58ede61e881dba631bd515c6`.
`XXBTZCAD` not in ticker.
Error on id `58ede82b881dba631bd51607`.
`XXBTZCAD` not in ticker.
Error on id `58ede836881dba631bd51609`.
`XXBTZCAD` not in ticker.
Error on id `58ede83b881dba631bd5160a`.
`XXBTZCAD` not in ticker.
Error on id `58ede841881dba631bd5160b`.
`XXBTZCAD` not in ticker.
Error on id `58edea65881dba631bd51659`.
`XXBTZCAD` not in ticker.
Error on id `58edea7b881dba631bd5165d`.
`XXBTZCAD` not in ticker.
Error on id `58edea80881dba631bd5165e`.
`XXBTZCAD` not in ticker.
Error on id `58edeb99881dba631bd51687`.
`XXBTZCAD` not in ticker.
Error on id `58edec6b881dba631bd5169b`.
`XXBTZCAD` not in ticker.
Error on id `58edeeaf881dba631bd516ea`.
`XXBTZCAD` not in ticker.
Error on id `58edeeb5881dba631bd516eb`.
`XXBTZCAD` not in ticker.
Error on id `58edeec0881dba631bd516ed`.
`XXBTZCAD` not in ticker.
Error on id `58edeed7881dba631bd516f1`.
`XXBTZCAD` not in ticker.
Error on id `58edeedc881dba631bd516f2`.
`XXBTZCAD` not in ticker.
Error on id `58edeee7881dba631bd516f4`.
`XXBTZCAD` not in ticker.
Error on id `58edeff3881dba631bd51719`.
`XXBTZCAD` not in ticker.
Error on id `58edf0dc881dba631bd51732`.
`XXBTZCAD` not in ticker.
Error on id `58edf0e2881dba631bd51733`.
`XXBTZCAD` not in ticker.
Error on id `58edf114881dba631bd51739`.
`XXBTZCAD` not in ticker.
Error on id `58edf2e8881dba631bd5177a`.
`XXBTZCAD` not in ticker.
Error on id `58edf4f9881dba631bd517c4`.
`XXMRZUSD` not in ticker.
Error on id `58edf526881dba631bd517cc`.
`XXBTZCAD` not in ticker.
Error on id `58edf6f5881dba631bd51807`.
`XXBTZCAD` not in ticker.
Error on id `58edf700881dba631bd51809`.
`XXBTZCAD` not in ticker.
Error on id `58edf716881dba631bd5180d`.
`XXBTZCAD` not in ticker.
Error on id `58edf727881dba631bd51810`.
`XXBTZCAD` not in ticker.
Error on id `58edf8d3881dba631bd51851`.
`XXBTZCAD` not in ticker.
Error on id `58edfa3e881dba631bd51886`.
`XXBTZCAD` not in ticker.
Error on id `58edfb2f881dba631bd5189c`.
`XXBTZCAD` not in ticker.
Error on id `58edfb45881dba631bd518a0`.
`XXBTZCAD` not in ticker.
Error on id `58edfb4b881dba631bd518a1`.
`XXBTZCAD` not in ticker.
Error on id `58edfd3d881dba631bd518f5`.
`XXBTZCAD` not in ticker.
Error on id `58edfe3e881dba631bd51911`.
`XXBTZCAD` not in ticker.
Error on id `58edfe65881dba631bd51918`.
`XXBTZCAD` not in ticker.
Error on id `58edfe6b881dba631bd51919`.
`XXBTZCAD` not in ticker.
Error on id `58ee0141881dba631bd5197b`.
`XXBTZCAD` not in ticker.
Error on id `58ee01e9881dba631bd5198d`.
`XXBTZCAD` not in ticker.
Error on id `58ee0234881dba631bd51999`.
`XXBTZCAD` not in ticker.
Error on id `58ee03cf881dba631bd519d2`.
`XXBTZCAD` not in ticker.
Error on id `58ee0437881dba631bd519dc`.
`XXMRZUSD` not in ticker.
Error on id `58ee0464881dba631bd519e4`.
`XXBTZCAD` not in ticker.
Error on id `58ee0612881dba631bd51a1d`.
`XXBTZCAD` not in ticker.
Error on id `58ee0757881dba631bd51a47`.
`XXBTZCAD` not in ticker.


In [30]:
df['length'] = df['cycle'].apply(lambda c: len(c))

In [35]:
currency = 'XETH'
plot_cycles = [cycle for cycle in cycles if currency in cycle]
plot_cycles = cycles


plot_df = df[
    (df['length']==3) &
    (df['timestamp'] > datetime(2017, 4, 12, 8, tzinfo=tz.gettz('PST'))) &
    # (df['timestamp'] < datetime(2017, 4, 11, 2, tzinfo=tz.gettz('PST'))) &
    (df['cycle'].isin(plot_cycles))
]

fg = seaborn.FacetGrid(
    data=plot_df,
   
    col='cycle',
    col_wrap=5,
    col_order=sorted(plot_df['cycle'].unique()),
    
    # hue='cycle',
    # hue_order=sorte|d(plot_df['cycle'].unique()),
    # size=8,
    # legend_out=True,
    
    sharex=True,
    sharey=True,
    margin_titles=True
)
def facet(data, color):
    plt.plot(data['timestamp'], data['rate'], color=color)
    
#plt.figure(figsize=(12,12))
fg.map_dataframe(facet)
for (i, j, k), data in fg.facet_data():
    if k == 0:
        ax = fg.facet_axis(i, j)
        ax.axhline(y=1.0, linestyle='--', color='r', linewidth=1.)
    
# max_df = plot_df.groupby('timestamp')['rate'].max().reset_index()
# for (i, j, k), data in fg.facet_data():
#     if k == 0:
#         plt.plot(max_df['timestamp'], max_df['rate'], color='k', label='max', linewidth=1.5)
        
        
fg.set_xticklabels(rotation=30)
fg.set(ylim=(0.95,1.1))
fg.set_xlabels('time')
fg.set_ylabels('rate')
# fg.add_legend()
plt.savefig('3cycles-over-time.png')
plt.show()



In [ ]: