In [2]:
# import scipy
import numpy as np
import matplotlib.pyplot as plt
import math
import pandas as pd
%matplotlib inline

In [60]:
TRADE_COLS = ['timestamp', 'symbol', 'side', 'price', 'size', 'tickDirection']
a = pd.read_csv('/Users/felipe/Downloads/20180701-trades.csv.gz')[TRADE_COLS].iloc[:5000]
a = a[a.price > 1]
a = a.query('symbol == "XBTUSD"')
a.timestamp = a.timestamp.apply(lambda s: pd.Timestamp(s.replace('D', 'T')))
a = a.set_index('timestamp')

In [65]:
a.to_csv('/Users/felipe/bitme/data/20180701-6h.csv.gz', compression='gzip')

In [64]:
a.index[-1] - a.index[0]


Out[64]:
Timedelta('0 days 00:06:03.634538')

In [66]:
'FELIPE'.lower()


Out[66]:
'felipe'

In [68]:
'felipe'[-10:]


Out[68]:
'felipe'

In [107]:
b = a.head(10).copy()

In [ ]:


In [ ]:


In [108]:
b


Out[108]:
symbol side price size tickDirection
timestamp
2018-07-01 00:00:02.490104 XBTUSD Sell 6383.5 3000 ZeroMinusTick
2018-07-01 00:00:03.233707 XBTUSD Sell 6383.5 8173 ZeroMinusTick
2018-07-01 00:00:03.233707 XBTUSD Sell 6383.5 383 ZeroMinusTick
2018-07-01 00:00:03.233707 XBTUSD Sell 6383.5 6444 ZeroMinusTick
2018-07-01 00:00:03.358225 XBTUSD Sell 6383.5 37 ZeroMinusTick
2018-07-01 00:00:03.474112 XBTUSD Sell 6383.5 200 ZeroMinusTick
2018-07-01 00:00:03.963948 XBTUSD Buy 6384.0 1 PlusTick
2018-07-01 00:00:04.418778 XBTUSD Sell 6383.5 35 MinusTick
2018-07-01 00:00:04.418778 XBTUSD Sell 6383.5 2 ZeroMinusTick
2018-07-01 00:00:05.038344 XBTUSD Buy 6384.0 484 PlusTick

In [109]:
def combine(x):
    y = x.iloc[0].copy()
    y['size'] = x['size'].sum()
    return y
c = b.groupby(['timestamp','price']).apply(combine).reset_index('price', drop=True).drop(columns=['timestamp'])


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-109-48a0815d66ab> in <module>()
      3     y['size'] = x['size'].sum()
      4     return y
----> 5 c = b.groupby(['timestamp','price']).apply(combine).reset_index('price', drop=True).drop(columns=['timestamp'])

~/anaconda3/envs/bitme-dev/lib/python3.6/site-packages/pandas/core/generic.py in drop(self, labels, axis, index, columns, level, inplace, errors)
   2528         for axis, labels in axes.items():
   2529             if labels is not None:
-> 2530                 obj = obj._drop_axis(labels, axis, level=level, errors=errors)
   2531 
   2532         if inplace:

~/anaconda3/envs/bitme-dev/lib/python3.6/site-packages/pandas/core/generic.py in _drop_axis(self, labels, axis, level, errors)
   2560                 new_axis = axis.drop(labels, level=level, errors=errors)
   2561             else:
-> 2562                 new_axis = axis.drop(labels, errors=errors)
   2563             dropped = self.reindex(**{axis_name: new_axis})
   2564             try:

~/anaconda3/envs/bitme-dev/lib/python3.6/site-packages/pandas/core/indexes/base.py in drop(self, labels, errors)
   3742             if errors != 'ignore':
   3743                 raise ValueError('labels %s not contained in axis' %
-> 3744                                  labels[mask])
   3745             indexer = indexer[~mask]
   3746         return self.delete(indexer)

ValueError: labels ['timestamp'] not contained in axis

In [102]:
c


Out[102]:
symbol side price size tickDirection
timestamp
2018-07-01 00:00:02.490104 XBTUSD Sell 6383.5 3000 ZeroMinusTick
2018-07-01 00:00:03.233707 XBTUSD Sell 6383.5 15000 ZeroMinusTick
2018-07-01 00:00:03.358225 XBTUSD Sell 6383.5 37 ZeroMinusTick
2018-07-01 00:00:03.474112 XBTUSD Sell 6383.5 200 ZeroMinusTick
2018-07-01 00:00:03.963948 XBTUSD Buy 6384.0 1 PlusTick
2018-07-01 00:00:04.418778 XBTUSD Sell 6383.5 37 MinusTick
2018-07-01 00:00:05.038344 XBTUSD Buy 6384.0 484 PlusTick

In [106]:
c.groupby(['price', 'timestamp']).apply(lambda x: x.iloc[0])


Out[106]:
symbol side price size tickDirection
price timestamp
6383.5 2018-07-01 00:00:02.490104 XBTUSD Sell 6383.5 3000 ZeroMinusTick
2018-07-01 00:00:03.233707 XBTUSD Sell 6383.5 15000 ZeroMinusTick
2018-07-01 00:00:03.358225 XBTUSD Sell 6383.5 37 ZeroMinusTick
2018-07-01 00:00:03.474112 XBTUSD Sell 6383.5 200 ZeroMinusTick
2018-07-01 00:00:04.418778 XBTUSD Sell 6383.5 37 MinusTick
6384.0 2018-07-01 00:00:03.963948 XBTUSD Buy 6384.0 1 PlusTick
2018-07-01 00:00:05.038344 XBTUSD Buy 6384.0 484 PlusTick

In [181]:
c = a.resample('1min').agg({'symbol': 'last', 'price': 'ohlc', 'size': 'sum'})

In [184]:
c


Out[184]:
symbol price size
symbol open high low close size
timestamp
2018-07-01 00:00:00 XBTUSD 6383.5 6389.0 6383.5 6388.5 2005959
2018-07-01 00:01:00 XBTUSD 6389.0 6396.0 6388.5 6396.0 2368558
2018-07-01 00:02:00 XBTUSD 6395.5 6420.0 6395.5 6408.0 7427788
2018-07-01 00:03:00 XBTUSD 6407.5 6410.0 6395.5 6401.5 3252048
2018-07-01 00:04:00 XBTUSD 6401.5 6408.0 6401.0 6401.0 1272841
2018-07-01 00:05:00 XBTUSD 6401.0 6410.0 6401.0 6406.5 2657345
2018-07-01 00:06:00 XBTUSD 6406.5 6406.5 6405.0 6405.0 163149

In [215]:
c.columns = c.columns.get_level_values(1)

In [216]:
c


Out[216]:
symbol open high low close size
timestamp
2018-07-01 00:00:00 XBTUSD 6383.5 6389.0 6383.5 6388.5 2005959
2018-07-01 00:01:00 XBTUSD 6389.0 6396.0 6388.5 6396.0 2368558
2018-07-01 00:02:00 XBTUSD 6395.5 6420.0 6395.5 6408.0 7427788
2018-07-01 00:03:00 XBTUSD 6407.5 6410.0 6395.5 6401.5 3252048
2018-07-01 00:04:00 XBTUSD 6401.5 6408.0 6401.0 6401.0 1272841
2018-07-01 00:05:00 XBTUSD 6401.0 6410.0 6401.0 6406.5 2657345
2018-07-01 00:06:00 XBTUSD 6406.5 6406.5 6405.0 6405.0 163149

In [218]:
from collections import deque
import queue

In [221]:
q = queue.Queue()

In [223]:
a = deque([1,2,3])

In [225]:
for i in a:
    print(i)
    a.pop()


1
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-225-cd8271793206> in <module>()
----> 1 for i in a:
      2     print(i)
      3     a.pop()

RuntimeError: deque mutated during iteration

In [19]:
a = pd.read_csv('/Users/felipe/bitme/data/20180701-1h-trades.csv.gz')
b = pd.read_csv('/Users/felipe/bitme/data/20180701-1h-quotes.csv.gz')

In [34]:
a.index = pd.DatetimeIndex([pd.Timestamp(i) for i in a['timestamp']])
b.index = pd.DatetimeIndex([pd.Timestamp(i) for i in b['timestamp']])
b['askPrice'] = 6388

In [35]:
b[['bidPrice', 'askPrice']].plot()
a['price'].plot()


Out[35]:
<matplotlib.axes._subplots.AxesSubplot at 0x124101b70>

In [33]:
b.loc[pd.
      Timestamp('2018-07-01 00:00:57.237578')]


Out[33]:
timestamp    2018-07-01 00:00:57.237578
symbol                           XBTUSD
bidSize                          131261
bidPrice                           6388
askPrice                           6389
askSize                           17543
Name: 2018-07-01 00:00:57.237578, dtype: object

In [28]:
def nearest(items, pivot):
    return min(items, key=lambda x: abs(x - pivot))

In [ ]: