Skip to content

Commit 884458f

Browse files
committed
import orderbook tests
1 parent 6b4e6bf commit 884458f

File tree

1 file changed

+84
-17
lines changed

1 file changed

+84
-17
lines changed

tests/test_orderbook.py

Lines changed: 84 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,13 @@ async def test_logfile(self, mock_book, mock_connect):
298298

299299
@patch('gdax.trader.Trader.get_product_order_book')
300300
async def test_orderbook_advanced(self, mock_book, mock_connect):
301+
# TODO: split test by message type
302+
product_id = 'BTC-USD'
301303
mock_connect.return_value.aenter.receive_str = CoroutineMock()
302304
mock_connect.return_value.aenter.send_json = CoroutineMock()
303305
messages_expected = [
304306
{ # ignored
305-
"product_id": "BTC-USD",
307+
"product_id": product_id,
306308
"sequence": sequence - 1,
307309
},
308310
{ # ignored
@@ -312,7 +314,7 @@ async def test_orderbook_advanced(self, mock_book, mock_connect):
312314
"size": "0.10000000",
313315
"price": "2602.22000000",
314316
"side": "sell",
315-
"product_id": "BTC-USD",
317+
"product_id": product_id,
316318
"sequence": sequence + 1,
317319
"time": "2017-06-25T11:23:14.792000Z"
318320
},
@@ -321,52 +323,78 @@ async def test_orderbook_advanced(self, mock_book, mock_connect):
321323
"side": "sell",
322324
"order_id": asks1[0][2],
323325
"reason": "canceled",
324-
"product_id": "BTC-USD",
326+
"product_id": product_id,
325327
"price": "2596.74",
326328
"remaining_size": "0.00000000",
327329
"sequence": sequence + 2,
328330
"time": "2017-06-25T11:23:14.775000Z"
329331
},
332+
{
333+
"type": "done",
334+
"side": "sell",
335+
"order_id": asks1[1][0],
336+
"reason": "canceled",
337+
"product_id": product_id,
338+
# no price specified
339+
"remaining_size": "0.20000000",
340+
"sequence": sequence + 3,
341+
"time": "2017-06-25T11:23:14.937000Z"
342+
},
343+
{
344+
"type": "match",
345+
"trade_id": 17545513,
346+
"maker_order_id": asks1[1][2],
347+
"taker_order_id": "bf07445d-03e3-4293-b5e6-26e34ce643b0",
348+
"side": "sell",
349+
"size": "0.01",
350+
"price": "2596.77",
351+
"product_id": product_id,
352+
"sequence": sequence + 4,
353+
"time": "2017-06-29T01:45:36.865000Z"
354+
},
355+
{
356+
"type": "match",
357+
"trade_id": 17545514,
358+
"maker_order_id": id2,
359+
"taker_order_id": "bf07445d-03e3-4293-b5e6-26e34ce643b0",
360+
"side": "buy",
361+
"size": "0.41152763",
362+
"price": "2595.62",
363+
"product_id": product_id,
364+
"sequence": sequence + 5,
365+
"time": "2017-06-29T01:45:36.865000Z"
366+
},
330367
{
331368
"type": "open",
332369
"side": "sell",
333370
"price": "2602.22000000",
334371
"order_id": "26c22ff5-01b1-4ca3-859c-6349d6eb06b4",
335372
"remaining_size": "0.10000000",
336-
"product_id": "BTC-USD",
337-
"sequence": sequence + 3,
373+
"product_id": product_id,
374+
"sequence": sequence + 6,
338375
"time": "2017-06-25T11:23:14.792000Z"
339376
},
340-
{
341-
"type": "done",
342-
"side": "sell",
343-
"order_id": "94b38e12-cc81-46b4-ad86-cbf435ce03a2",
344-
"reason": "canceled",
345-
"product_id": "BTC-USD",
346-
# no price specified
347-
"remaining_size": "0.20000000",
348-
"sequence": sequence + 4,
349-
"time": "2017-06-25T11:23:14.937000Z"
350-
}
351377
]
352378
mock_connect.return_value.aenter.receive_str.side_effect = [
353379
json.dumps(message_expected)
354380
for message_expected in messages_expected
355381
]
356-
product_id = 'BTC-USD'
357382
book = {'bids': [], 'asks': [], 'sequence': 1}
358383
mock_book.return_value = test_book
359384
async with gdax.orderbook.OrderBook(product_id) as orderbook:
385+
# ignore because of sequence number
360386
current_book = orderbook.get_current_book(product_id)
361387
message = await orderbook.handle_message()
362388
assert message == messages_expected[0]
363389
assert orderbook.get_current_book(product_id) == current_book
364390

391+
# ignore because receive
365392
message = await orderbook.handle_message()
366393
assert message == messages_expected[1]
367394
current_book['sequence'] += 1
368395
assert orderbook.get_current_book(product_id) == current_book
369396

397+
# done
370398
price = Decimal('2596.74')
371399
price2 = Decimal('2596.77')
372400
assert orderbook.get_asks(product_id, price) == \
@@ -380,10 +408,49 @@ async def test_orderbook_advanced(self, mock_book, mock_connect):
380408
current_book['sequence'] += 1
381409
assert orderbook.get_current_book(product_id) != current_book
382410

411+
# done 2
383412
message = await orderbook.handle_message()
384413
assert message == messages_expected[3]
414+
current_book['sequence'] += 1
415+
assert orderbook.get_current_book(product_id) != current_book
385416
# TODO
386417

418+
# match
419+
current_book = orderbook.get_current_book(product_id)
420+
assert orderbook.get_min_ask_depth(product_id) == \
421+
Decimal('0.07670504')
387422
message = await orderbook.handle_message()
388423
assert message == messages_expected[4]
424+
assert orderbook.get_min_ask_depth(product_id) == \
425+
Decimal('0.06670504')
426+
assert orderbook.get_ask(product_id) == price2
427+
current_book['sequence'] += 1
428+
assert orderbook.get_current_book(product_id) != current_book
429+
430+
price3 = Decimal('2595.62')
431+
price4 = Decimal('2595.70')
432+
# match 2
433+
current_book = orderbook.get_current_book(product_id)
434+
assert orderbook.get_bids(product_id, price3) == \
435+
bids1_internal[price3]
436+
assert orderbook.get_bids(product_id, price4) == \
437+
bids1_internal[price4]
438+
assert orderbook.get_bid(product_id) == price4
439+
message = await orderbook.handle_message()
440+
assert message == messages_expected[5]
441+
bids1_internal[price3][0]['size'] = Decimal('1.0')
442+
assert orderbook.get_bids(product_id, price3) == \
443+
bids1_internal[price3]
444+
assert orderbook.get_bids(product_id, price4) == \
445+
bids1_internal[price4]
446+
assert orderbook.get_bid(product_id) == price4
447+
current_book['sequence'] += 1
448+
assert orderbook.get_current_book(product_id) != current_book
449+
450+
# open
451+
current_book = orderbook.get_current_book(product_id)
452+
message = await orderbook.handle_message()
453+
assert message == messages_expected[6]
454+
current_book['sequence'] += 1
455+
assert orderbook.get_current_book(product_id) != current_book
389456
# TODO

0 commit comments

Comments
 (0)