Skip to content

Commit 8c5a60d

Browse files
committed
fix trader order book level=3, add test
1 parent 3d06787 commit 8c5a60d

File tree

4 files changed

+89
-26
lines changed

4 files changed

+89
-26
lines changed

gdax/trader.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
import copy
8-
from decimal import Decimal
8+
from decimal import Decimal, InvalidOperation
99
import json
1010
import logging
1111
import time
@@ -71,7 +71,10 @@ def _convert_return_fields(self, fields, decimal_fields, convert_all):
7171
return new_fields
7272
else:
7373
if convert_all and not isinstance(fields, int):
74-
return Decimal(fields)
74+
try:
75+
return Decimal(fields)
76+
except InvalidOperation:
77+
return fields
7578
else:
7679
return fields
7780

tests/helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import uuid
12
from asynctest import PropertyMock, MagicMock
23

34

@@ -18,3 +19,7 @@ async def __aexit__(self, *args):
1819

1920
# two pages
2021
headers = PropertyMock(side_effect=[{'cb-after': 123}, {}])
22+
23+
24+
def generate_id():
25+
return str(uuid.uuid4())

tests/test_orderbook.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import uuid
32
import base64
43
from decimal import Decimal
54

@@ -10,32 +9,28 @@
109
import gdax
1110
import gdax.orderbook
1211

13-
from tests.helpers import AsyncContextManagerMock
14-
15-
16-
def generate_id():
17-
return str(uuid.uuid4())
12+
from tests.helpers import AsyncContextManagerMock, generate_id
1813

1914

2015
id1 = generate_id()
2116
id2 = generate_id()
2217

2318

2419
bids1 = [
25-
["2525.00", "1.5", generate_id()],
26-
["2595.52", "100", id2],
27-
["2595.52", "2", id1],
28-
["2595.62", "1.41152763", id2],
29-
["2595.70", "1.5", generate_id()],
20+
[Decimal("2525.00"), Decimal("1.5"), generate_id()],
21+
[Decimal("2595.52"), Decimal("100"), id2],
22+
[Decimal("2595.52"), Decimal("2"), id1],
23+
[Decimal("2595.62"), Decimal("1.41152763"), id2],
24+
[Decimal("2595.70"), Decimal("1.5"), generate_id()],
3025
]
3126
asks1 = [
32-
["2596.74", "0.2", generate_id()],
33-
["2596.77", "0.07670504", generate_id()],
34-
["2615.1", "0.011", generate_id()],
35-
["2620.05", "0.02", id1],
36-
["2620.1", "100", generate_id()],
37-
["2620.18", "0.01", id1],
38-
["2620.18", "0.02", id2],
27+
[Decimal("2596.74"), Decimal("0.2"), generate_id()],
28+
[Decimal("2596.77"), Decimal("0.07670504"), generate_id()],
29+
[Decimal("2615.1"), Decimal("0.011"), generate_id()],
30+
[Decimal("2620.05"), Decimal("0.02"), id1],
31+
[Decimal("2620.1"), Decimal("100"), generate_id()],
32+
[Decimal("2620.18"), Decimal("0.01"), id1],
33+
[Decimal("2620.18"), Decimal("0.02"), id2],
3934
]
4035
sequence = 3419033239
4136
test_book = {
@@ -154,8 +149,8 @@ async def test_basic_init(self, mock_book, mock_connect):
154149

155150
assert orderbook.get_current_book(product_id) == {
156151
"sequence": 3419033239,
157-
"bids": [[Decimal(r[0]), Decimal(r[1]), r[2]] for r in bids1],
158-
"asks": [[Decimal(r[0]), Decimal(r[1]), r[2]] for r in asks1],
152+
"bids": bids1,
153+
"asks": asks1,
159154
}
160155

161156
assert orderbook.get_ask(product_id) == Decimal('2596.74')
@@ -300,6 +295,7 @@ async def test_logfile(self, mock_book, mock_connect):
300295
async def test_orderbook_advanced(self, mock_book, mock_connect):
301296
# TODO: split test by message type
302297
product_id = 'BTC-USD'
298+
mock_book.return_value = test_book
303299
mock_connect.return_value.aenter.receive_str = CoroutineMock()
304300
mock_connect.return_value.aenter.send_json = CoroutineMock()
305301
messages_expected = [
@@ -333,7 +329,7 @@ async def test_orderbook_advanced(self, mock_book, mock_connect):
333329
{
334330
"type": "done",
335331
"side": "sell",
336-
"order_id": asks1[1][0],
332+
"order_id": asks1[1][2],
337333
"reason": "canceled",
338334
"product_id": product_id,
339335
# no price specified
@@ -380,7 +376,6 @@ async def test_orderbook_advanced(self, mock_book, mock_connect):
380376
json.dumps(message_expected)
381377
for message_expected in messages_expected
382378
]
383-
mock_book.return_value = test_book
384379
async with gdax.orderbook.OrderBook(product_id) as orderbook:
385380
# ignore because of sequence number
386381
current_book = orderbook.get_current_book(product_id)

tests/test_trader.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from decimal import Decimal
44

55
from asynctest import patch, CoroutineMock
6-
from tests.helpers import AsyncContextManagerMock, \
7-
AsyncContextManagerMockPagination
86
import pytest
97

108
import gdax
9+
from tests.helpers import AsyncContextManagerMock, \
10+
AsyncContextManagerMockPagination, generate_id
1111

1212

1313
@pytest.yield_fixture
@@ -367,6 +367,66 @@ async def test_get_product_order_book(self, mock_get):
367367
r = await self.client.get_product_order_book('BTC-USD', level=2)
368368
assert r == expected_orderbook
369369

370+
id1, id2, id3, id4 = (generate_id() for _ in range(4))
371+
orderbook = {
372+
"sequence": 3424562473,
373+
"bids": [
374+
[
375+
"2483.99",
376+
"0.01",
377+
id1
378+
],
379+
[
380+
"2483.98",
381+
"0.9798",
382+
id2
383+
]
384+
],
385+
"asks": [
386+
[
387+
"2486.48",
388+
"1.65567931",
389+
id3
390+
],
391+
[
392+
"2487.72",
393+
"0.03",
394+
id4
395+
]
396+
]
397+
}
398+
expected_orderbook = {
399+
"sequence": 3424562473,
400+
"bids": [
401+
[
402+
Decimal("2483.99"),
403+
Decimal("0.01"),
404+
id1
405+
],
406+
[
407+
Decimal("2483.98"),
408+
Decimal("0.9798"),
409+
id2
410+
]
411+
],
412+
"asks": [
413+
[
414+
Decimal("2486.48"),
415+
Decimal("1.65567931"),
416+
id3
417+
],
418+
[
419+
Decimal("2487.72"),
420+
Decimal("0.03"),
421+
id4
422+
]
423+
]
424+
}
425+
mock_get.return_value.aenter.json = CoroutineMock(
426+
return_value=orderbook)
427+
r = await self.client.get_product_order_book('BTC-USD', level=3)
428+
assert r == expected_orderbook
429+
370430
async def test_get_product_historic_rates(self, mock_get):
371431
rates = [
372432
[

0 commit comments

Comments
 (0)