[#4] Added Poloniex order book query.

This commit is contained in:
Gavin.Chan
2017-11-26 16:26:10 +00:00
parent 57b3262edc
commit 8aee2c440a
3 changed files with 180 additions and 29 deletions
+109 -27
View File
@@ -6,6 +6,7 @@ from pandas.util.testing import assert_frame_equal
import pytest
from libcryptomarket.historical import get_historical_prices
from libcryptomarket.price import get_order_book
def test_get_historical_prices_poloniex(monkeypatch):
@@ -24,7 +25,7 @@ def test_get_historical_prices_poloniex(monkeypatch):
"close": 0.00435873,
"volume": 44.34555992,
"quoteVolume": 10311.88079097,
"weightedAverage":0.00430043
"weightedAverage": 0.00430043
},
{
"date": 1405713600,
@@ -52,27 +53,27 @@ def test_get_historical_prices_poloniex(monkeypatch):
from_time=datetime(2014, 7, 18, 16, 0, 0))
expected_result = pd.DataFrame([
{
"r_date": datetime(2014, 7, 18, 16, 0, 0),
"r_high": 0.0045388,
"r_low": 0.00403001,
"r_open": 0.00404545,
"r_close": 0.00435873,
"r_volume": 44.34555992,
"r_quotevolume": 10311.88079097,
"r_weightedaverage":0.00430043
},
{
"r_date": datetime(2014, 7, 18, 20, 0, 0),
"r_high": 0.00435,
"r_low": 0.00412,
"r_open": 0.00428012,
"r_close": 0.00412,
"r_volume": 19.12271662,
"r_quotevolume": 4531.85801066,
"r_weightedaverage": 0.00421961
}
]).set_index(['r_date'])
{
"r_date": datetime(2014, 7, 18, 16, 0, 0),
"r_high": 0.0045388,
"r_low": 0.00403001,
"r_open": 0.00404545,
"r_close": 0.00435873,
"r_volume": 44.34555992,
"r_quotevolume": 10311.88079097,
"r_weightedaverage": 0.00430043
},
{
"r_date": datetime(2014, 7, 18, 20, 0, 0),
"r_high": 0.00435,
"r_low": 0.00412,
"r_open": 0.00428012,
"r_close": 0.00412,
"r_volume": 19.12271662,
"r_quotevolume": 4531.85801066,
"r_weightedaverage": 0.00421961
}
]).set_index(['r_date'])
expected_result.index.name = 'datetime'
assert_frame_equal(result, expected_result)
@@ -80,12 +81,12 @@ def test_get_historical_prices_poloniex(monkeypatch):
def test_get_historical_prices_poloniex_invalid_period(monkeypatch):
with pytest.raises(ValueError):
result = get_historical_prices(
get_historical_prices(
source='Poloniex',
period='29m',
symbol='BTC/XMR',
from_time=datetime(2014, 7, 18, 16, 0, 0))
def test_get_historical_prices_poloniex_invalid_instrument(monkeypatch):
def mockreturn(url, *args, **kwargs):
@@ -94,7 +95,7 @@ def test_get_historical_prices_poloniex_invalid_instrument(monkeypatch):
@classmethod
def json(cls):
# Query all symbols
return {"error":"Invalid currency pair."}
return {"error": "Invalid currency pair."}
@classmethod
def raise_for_status(cls):
@@ -106,9 +107,90 @@ def test_get_historical_prices_poloniex_invalid_instrument(monkeypatch):
# Test to get historical prices
with pytest.raises(ValueError):
result = get_historical_prices(
get_historical_prices(
source='Poloniex',
period='4h',
symbol='BTC/XXX',
from_time=datetime(2014, 7, 18, 16, 0, 0))
def test_get_order_book_poloniex_all_symbols(monkeypatch):
def mockreturn(url, *args, **kwargs):
# The result is from request.get(...).json()
class MockReturnClass:
@classmethod
def json(cls):
# Query all symbols
import json
return json.loads(
"""
{"BTC_AMP": {"asks": [["0.00002755", 16.03393139],
["0.00002756", 20.24556409]],
"bids": [["0.00002739", 20.23199043], ["0.00002723", 20.30358806]],
"isFrozen": "0",
"seq": 44422443},
"BTC_ARDR": {"asks": [["0.00003331", 62.49465571], ["0.00003332", 6567.629]],
"bids": [["0.00003285", 25], ["0.00003277", 44.83974499]],
"isFrozen": "0",
"seq": 27008837}}
"""
)
@classmethod
def raise_for_status(cls):
pass
return MockReturnClass()
monkeypatch.setattr(requests, 'get', mockreturn)
expected_df = pd.DataFrame([
[0.00002739, 20.231990, 0.00002755, 16.033931,
0.00003285, 25.000000, 0.00003331, 62.494656],
[0.00002723, 20.303588, 0.00002756, 20.245564,
0.00003277, 44.839745, 0.00003332, 6567.629000]],
columns=pd.MultiIndex.from_product(
[['BTC_AMP', 'BTC_ARDR'],
['bids', 'asks'],
['price', 'quantity']]),
index=[1, 2])
df = get_order_book(source="Poloniex", symbol="all", depth=2)
assert_frame_equal(expected_df.sort_index(axis=1),
df.sort_index(axis=1))
def test_get_order_book_poloniex_one_symbol(monkeypatch):
def mockreturn(url, *args, **kwargs):
# The result is from request.get(...).json()
class MockReturnClass:
@classmethod
def json(cls):
# Query all symbols
import json
return json.loads(
"""
{"asks": [["0.00002755", 16.03393139],
["0.00002756", 20.24556409]],
"bids": [["0.00002739", 20.23199043], ["0.00002723", 20.30358806]]}
"""
)
@classmethod
def raise_for_status(cls):
pass
return MockReturnClass()
monkeypatch.setattr(requests, 'get', mockreturn)
expected_df = pd.DataFrame([
[0.00002739, 20.231990, 0.00002755, 16.033931],
[0.00002723, 20.303588, 0.00002756, 20.245564]],
columns=pd.MultiIndex.from_product([['bids', 'asks'],
['price', 'quantity']]),
index=[1, 2])
df = get_order_book(source="Poloniex", symbol="BTC_AMP", depth=2)
assert_frame_equal(expected_df,
df)