-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathtest_historical_bento.py
More file actions
1784 lines (1487 loc) · 53.2 KB
/
test_historical_bento.py
File metadata and controls
1784 lines (1487 loc) · 53.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import collections
import datetime as dt
import decimal
from io import BytesIO
from pathlib import Path
from typing import Any
from typing import Callable
from typing import Literal
from unittest.mock import MagicMock
import databento
import databento.common.dbnstore
import numpy as np
import pandas as pd
import pytest
import pytz
import zstandard
from databento.common.constants import SCHEMA_STRUCT_MAP
from databento.common.dbnstore import DBNStore
from databento.common.error import BentoError
from databento.common.error import BentoWarning
from databento.common.publishers import Dataset
from databento.common.types import DBNRecord
from databento_dbn import Compression
from databento_dbn import MBOMsg
from databento_dbn import Schema
from databento_dbn import SType
def test_from_file_when_not_exists_raises_expected_exception() -> None:
# Arrange, Act, Assert
with pytest.raises(FileNotFoundError):
DBNStore.from_file("my_data.dbn")
def test_from_file_when_file_empty_raises_expected_exception(
tmp_path: Path,
) -> None:
"""
Test that creating a DBNStore from an empty file raises a ValueError.
"""
# Arrange
path = tmp_path / "test.dbn"
path.touch()
# Act, Assert
with pytest.raises(ValueError):
DBNStore.from_file(path)
def test_from_file_when_buffer_corrupted_raises_expected_exception(
tmp_path: Path,
) -> None:
"""
Test that creating a DBNStore from an invalid DBN file raises a BentoError.
"""
# Arrange
path = tmp_path / "corrupted.dbn"
path.write_text("this is a test")
# Act, Assert
with pytest.raises(BentoError):
DBNStore.from_file(path)
def test_from_bytes_when_buffer_empty_raises_expected_exception() -> None:
"""
Test that creating a DBNStore from an empty buffer raises a ValueError.
"""
# Arrange, Act, Assert
with pytest.raises(ValueError):
DBNStore.from_bytes(BytesIO())
def test_from_bytes_when_buffer_corrupted_raises_expected_exception() -> None:
"""
Test that creating a DBNStore from an invalid DBN stream raises a
BentoError.
"""
# Arrange, Act, Assert
with pytest.raises(ValueError):
DBNStore.from_bytes(BytesIO())
def test_sources_metadata_returns_expected_json_as_dict(
test_data: Callable[[Dataset, Schema], bytes],
) -> None:
# Arrange, Act
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
dbnstore = DBNStore.from_bytes(data=stub_data)
# Assert
assert dbnstore.metadata.version == 3
assert dbnstore.metadata.dataset == "GLBX.MDP3"
assert dbnstore.metadata.schema == Schema.MBO
assert dbnstore.metadata.stype_in == SType.RAW_SYMBOL
assert dbnstore.metadata.stype_out == SType.INSTRUMENT_ID
assert dbnstore.metadata.start == 1609113600000000000
assert dbnstore.metadata.end == 1609200000000000000
assert dbnstore.metadata.limit == 4
assert dbnstore.metadata.symbols == ["ESH1"]
assert dbnstore.metadata.ts_out is False
assert dbnstore.metadata.partial == []
assert dbnstore.metadata.not_found == []
assert dbnstore.metadata.mappings == {
"ESH1": [
{
"start_date": dt.date(2020, 12, 28),
"end_date": dt.date(2020, 12, 29),
"symbol": "5482",
},
],
}
def test_dbnstore_given_initial_nbytes_returns_expected_metadata(
test_data: Callable[[Dataset, Schema], bytes],
) -> None:
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
# Act
dbnstore = DBNStore.from_bytes(data=stub_data)
# Assert
assert dbnstore.nbytes == 191
assert dbnstore.dataset == "GLBX.MDP3"
assert dbnstore.schema == Schema.MBO
assert dbnstore.symbols == ["ESH1"]
assert dbnstore.stype_in == SType.RAW_SYMBOL
assert dbnstore.stype_out == SType.INSTRUMENT_ID
assert dbnstore.start == pd.Timestamp("2020-12-28 00:00:00+0000", tz="UTC")
assert dbnstore.end == pd.Timestamp("2020-12-29 00:00:00+0000", tz="UTC")
assert dbnstore.limit == 4
assert len(dbnstore.to_ndarray()) == 4
assert dbnstore.mappings == {
"ESH1": [
{
"symbol": "5482",
"start_date": dt.date(2020, 12, 28),
"end_date": dt.date(2020, 12, 29),
},
],
}
assert dbnstore.symbology == {
"symbols": ["ESH1"],
"stype_in": "raw_symbol",
"stype_out": "instrument_id",
"start_date": "2020-12-28",
"end_date": "2020-12-29",
"not_found": [],
"partial": [],
"mappings": {
"ESH1": [
{
"symbol": "5482",
"start_date": dt.date(2020, 12, 28),
"end_date": dt.date(2020, 12, 29),
},
],
},
}
def test_file_dbnstore_given_valid_path_initialized_expected_data(
test_data_path: Callable[[Dataset, Schema], Path],
) -> None:
# Arrange, Act
path = test_data_path(Dataset.GLBX_MDP3, Schema.MBO)
dbnstore = DBNStore.from_file(path=path)
# Assert
assert dbnstore.dataset == "GLBX.MDP3"
assert dbnstore.nbytes == 191
@pytest.mark.parametrize(
"schema,expected_size",
[
(Schema.MBO, 191),
(Schema.DEFINITION, 288),
],
)
def test_to_file_persists_to_disk(
test_data: Callable[[Dataset, Schema], bytes],
tmp_path: Path,
schema: Schema,
expected_size: int,
) -> None:
"""
Test the DBNStore.to_file writes files to disk.
"""
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, schema)
dbnstore = DBNStore.from_bytes(data=stub_data)
# Act
dbn_path = tmp_path / "my_test.dbn"
dbnstore.to_file(path=dbn_path)
# Assert
assert dbn_path.exists()
assert dbn_path.stat().st_size == expected_size
def test_to_file_overwrite(
test_data: Callable[[Dataset, Schema], bytes],
tmp_path: Path,
) -> None:
"""
Test that the default write mode allows files to be overwritten.
"""
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
dbnstore = DBNStore.from_bytes(data=stub_data)
dbn_path = tmp_path / "my_test.dbn"
dbnstore.to_file(path=dbn_path)
assert dbn_path.stat().st_size == 191
# Act
dbnstore.to_file(path=dbn_path)
# Assert
assert dbn_path.exists()
assert dbn_path.stat().st_size == 191
def test_to_file_exclusive(
test_data: Callable[[Dataset, Schema], bytes],
tmp_path: Path,
) -> None:
"""
Test that the exclusive write mode correctly rejects an existing file path.
"""
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
dbnstore = DBNStore.from_bytes(data=stub_data)
dbn_path = tmp_path / "my_test.dbn"
dbnstore.to_file(path=dbn_path)
# Act, Assert
with pytest.raises(FileExistsError):
dbnstore.to_file(path=dbn_path, mode="x")
@pytest.mark.parametrize(
"compression",
[
Compression.NONE,
Compression.ZSTD,
],
)
def test_to_file_compression(
test_data: Callable[[Dataset, Schema], bytes],
tmp_path: Path,
compression: Compression,
) -> None:
"""
Test that specifying a compression for DBNStore.to_file writes the desired
compression mode.
"""
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
dbnstore = DBNStore.from_bytes(data=stub_data)
dbn_path = tmp_path / "my_test.dbn"
dbnstore.to_file(
path=dbn_path,
compression=compression,
)
# Act, Assert
new_store = databento.read_dbn(dbn_path)
assert new_store.compression == compression
def test_to_csv_overwrite(
test_data: Callable[[Dataset, Schema], bytes],
tmp_path: Path,
) -> None:
"""
Test that the default write mode allows files to be overwritten.
"""
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
dbnstore = DBNStore.from_bytes(data=stub_data)
csv_path = tmp_path / "my_test.csv"
dbnstore.to_csv(path=csv_path)
assert csv_path.stat().st_size == 623
# Act
dbnstore.to_csv(path=csv_path)
# Assert
assert csv_path.exists()
assert csv_path.stat().st_size == 623
def test_to_csv_exclusive(
test_data: Callable[[Dataset, Schema], bytes],
tmp_path: Path,
) -> None:
"""
Test that the exclusive write mode correctly rejects an existing file path.
"""
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
dbnstore = DBNStore.from_bytes(data=stub_data)
csv_path = tmp_path / "my_test.csv"
dbnstore.to_csv(path=csv_path)
# Act, Assert
with pytest.raises(FileExistsError):
dbnstore.to_csv(path=csv_path, mode="x")
def test_to_json_overwrite(
test_data: Callable[[Dataset, Schema], bytes],
tmp_path: Path,
) -> None:
"""
Test that the default write mode allows files to be overwritten.
"""
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
dbnstore = DBNStore.from_bytes(data=stub_data)
json_path = tmp_path / "my_test.json"
dbnstore.to_json(path=json_path)
assert json_path.stat().st_size == 1216
# Act
dbnstore.to_json(path=json_path)
# Assert
assert json_path.exists()
assert json_path.stat().st_size == 1216
def test_to_json_exclusive(
test_data: Callable[[Dataset, Schema], bytes],
tmp_path: Path,
) -> None:
"""
Test that the exclusive write mode correctly rejects an existing file path.
"""
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
dbnstore = DBNStore.from_bytes(data=stub_data)
json_path = tmp_path / "my_test.json"
dbnstore.to_json(path=json_path)
# Act, Assert
with pytest.raises(FileExistsError):
dbnstore.to_json(path=json_path, mode="x")
def test_to_parquet_overwrite(
test_data: Callable[[Dataset, Schema], bytes],
tmp_path: Path,
) -> None:
"""
Test that the default write mode allows files to be overwritten.
"""
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
dbnstore = DBNStore.from_bytes(data=stub_data)
parquet_path = tmp_path / "my_test.parquet"
dbnstore.to_parquet(path=parquet_path)
parquet_size = parquet_path.stat().st_size
# Act
dbnstore.to_parquet(path=parquet_path)
# Assert
assert parquet_size > 0 # Should be about ~9000 bytes
assert parquet_path.exists()
assert parquet_path.stat().st_size == parquet_size
def test_to_parquet_exclusive(
test_data: Callable[[Dataset, Schema], bytes],
tmp_path: Path,
) -> None:
"""
Test that the exclusive write mode correctly rejects an existing file path.
"""
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
dbnstore = DBNStore.from_bytes(data=stub_data)
parquet_path = tmp_path / "my_test.parquet"
dbnstore.to_parquet(path=parquet_path)
# Act, Assert
with pytest.raises(FileExistsError):
dbnstore.to_parquet(path=parquet_path, mode="x")
def test_to_ndarray_with_stub_data_returns_expected_array(
test_data: Callable[[Dataset, Schema], bytes],
) -> None:
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
data = DBNStore.from_bytes(data=stub_data)
# Act
array = data.to_ndarray()
# Assert
assert isinstance(array, np.ndarray)
assert str(array) == (
"[(14, 160, 1, 5482, 1609099225061045683, 647784248135, 3675750000000, 2, 40, 0, b'A', b'B', 1609113600000000000, 0, 1180)\n"
" (14, 160, 1, 5482, 1609099225061045683, 647782686353, 3675500000000, 1, 40, 0, b'A', b'B', 1609113600000000000, 0, 1160)\n"
" (14, 160, 1, 5482, 1609099225061045683, 647782884482, 3675250000000, 1, 40, 0, b'A', b'B', 1609113600000000000, 0, 1166)\n"
" (14, 160, 1, 5482, 1609099225061045683, 647782912367, 3675000000000, 1, 40, 0, b'A', b'B', 1609113600000000000, 0, 1166)]"
)
def test_iterator_produces_expected_data(
test_data: Callable[[Dataset, Schema], bytes],
) -> None:
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
data = DBNStore.from_bytes(data=stub_data)
# Act (consume iterator)
handler = collections.deque(data)
# Assert
assert len(handler) == 4
def test_replay_with_stub_data_record_passes_to_callback(
test_data: Callable[[Dataset, Schema], bytes],
) -> None:
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
data = DBNStore.from_bytes(data=stub_data)
handler: list[DBNRecord] = []
# Act
data.replay(callback=handler.append)
record: DBNRecord = handler[0]
# Assert
assert isinstance(record, MBOMsg)
assert record.hd.length == 14
assert record.hd.rtype == 160
assert record.hd.rtype == 160
assert record.hd.publisher_id == 1
assert record.hd.instrument_id == 5482
assert record.hd.ts_event == 1609099225061045683
assert record.order_id == 647784248135
assert record.price == 3675750000000
assert record.size == 2
assert record.flags == 40
assert record.channel_id == 0
assert record.action == "A"
assert record.side == "B"
assert record.ts_recv == 1609113600000000000
assert record.ts_in_delta == 0
assert record.sequence == 1180
@pytest.mark.parametrize(
"schema",
[
s
for s in Schema.variants()
if s
not in (
Schema.OHLCV_1H,
Schema.OHLCV_1D,
Schema.DEFINITION,
Schema.STATISTICS,
)
],
)
def test_to_df_across_schemas_returns_identical_dimension_dfs(
test_data: Callable[[Dataset, Schema], bytes],
schema: Schema,
) -> None:
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, schema)
data = DBNStore.from_bytes(data=stub_data)
# Act
df = data.to_df()
# Assert
assert list(df.columns) == list(df.columns)
assert len(df) == 4
def test_to_df_with_mbo_data_returns_expected_record(
test_data: Callable[[Dataset, Schema], bytes],
) -> None:
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
data = DBNStore.from_bytes(data=stub_data)
# Act
df = data.to_df(
pretty_ts=False,
price_type="fixed",
map_symbols=False,
)
# Assert
assert len(df) == 4
assert df.index.name == "ts_recv"
assert df.index.values[0] == 1609113600000000000
assert df.iloc[0]["ts_event"] == 1609099225061045683
assert df.iloc[0]["rtype"] == 160
assert df.iloc[0]["publisher_id"] == 1
assert df.iloc[0]["instrument_id"] == 5482
assert df.iloc[0]["action"] == "A"
assert df.iloc[0]["side"] == "B"
assert df.iloc[0]["price"] == 3675750000000
assert df.iloc[0]["size"] == 2
assert df.iloc[0]["order_id"] == 647784248135
assert df.iloc[0]["flags"] == 40
assert df.iloc[0]["ts_in_delta"] == 0
assert df.iloc[0]["sequence"] == 1180
def test_to_df_with_stub_ohlcv_data_returns_expected_record(
test_data: Callable[[Dataset, Schema], bytes],
) -> None:
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.OHLCV_1M)
data = DBNStore.from_bytes(data=stub_data)
# Act
df = data.to_df(
pretty_ts=False,
price_type="fixed",
map_symbols=False,
)
# Assert
assert len(df) == 4
assert df.index.name == "ts_event"
assert df.index.values[0] == 1609113600000000000
assert df.iloc[0]["instrument_id"] == 5482
assert df.iloc[0]["open"] == 3_702_750_000_000
assert df.iloc[0]["high"] == 3_704_750_000_000
assert df.iloc[0]["low"] == 3_702_500_000_000
assert df.iloc[0]["close"] == 3_704_750_000_000
assert df.iloc[0]["volume"] == 306
def test_to_df_with_pretty_ts_converts_timestamps_as_expected(
test_data: Callable[[Dataset, Schema], bytes],
) -> None:
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
data = DBNStore.from_bytes(data=stub_data)
# Act
df = data.to_df(pretty_ts=True)
# Assert
index0 = df.index[0]
event0 = df["ts_event"][0]
assert isinstance(index0, pd.Timestamp)
assert isinstance(event0, pd.Timestamp)
assert index0 == pd.Timestamp("2020-12-28 00:00:00.000000000+0000", tz="UTC")
assert event0 == pd.Timestamp("2020-12-27 20:00:25.061045683+0000", tz="UTC")
assert len(df) == 4
@pytest.mark.parametrize(
"schema,columns",
[
[Schema.MBO, ["price"]],
[Schema.TBBO, ["price", "bid_px_00", "ask_px_00"]],
[Schema.TRADES, ["price"]],
[Schema.MBP_1, ["price", "bid_px_00", "ask_px_00"]],
[
Schema.MBP_10,
[
"price",
"bid_px_00",
"bid_px_01",
"bid_px_02",
"bid_px_03",
"bid_px_04",
"bid_px_05",
"bid_px_06",
"bid_px_07",
"bid_px_08",
"bid_px_09",
"ask_px_00",
"ask_px_01",
"ask_px_02",
"ask_px_03",
"ask_px_04",
"ask_px_05",
"ask_px_06",
"ask_px_07",
"ask_px_08",
"ask_px_09",
],
],
],
)
@pytest.mark.parametrize(
"price_type, expected_type",
[
("fixed", np.integer),
("decimal", decimal.Decimal),
("float", np.floating),
],
)
def test_to_df_with_price_type_with_various_schemas_converts_prices_as_expected(
test_data: Callable[[Dataset, Schema], bytes],
schema: Schema,
columns: list[str],
price_type: Literal["float", "decimal"],
expected_type: type,
) -> None:
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, schema)
data = DBNStore.from_bytes(data=stub_data)
# Act
df = data.to_df(price_type=price_type)
# Assert
assert len(df) == 4
for column in columns:
assert isinstance(df[column].iloc(0)[1], expected_type)
def test_to_df_with_price_type_handles_null(
test_data: Callable[[Dataset, Schema], bytes],
) -> None:
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.DEFINITION)
data = DBNStore.from_bytes(data=stub_data)
# Act
df_plain = data.to_df(price_type="fixed")
df_pretty = data.to_df(price_type="float")
# Assert
assert all(df_plain["strike_price"] == 9223372036854775807)
assert all(np.isnan(df_pretty["strike_price"]))
def test_to_df_with_price_type_invalid(
test_data: Callable[[Dataset, Schema], bytes],
) -> None:
# Arrange
stub_data = test_data(Dataset.GLBX_MDP3, Schema.DEFINITION)
data = DBNStore.from_bytes(data=stub_data)
# Act, Assert
with pytest.raises(ValueError):
data.to_df(price_type="US/Eastern")
@pytest.mark.parametrize(
"dataset",
[
Dataset.GLBX_MDP3,
Dataset.XNAS_ITCH,
Dataset.OPRA_PILLAR,
Dataset.EQUS_MINI,
Dataset.IFEU_IMPACT,
Dataset.NDEX_IMPACT,
],
)
@pytest.mark.parametrize(
"schema",
[pytest.param(schema, id=str(schema)) for schema in Schema.variants()],
)
@pytest.mark.parametrize(
"price_type",
[
"fixed",
"float",
],
)
@pytest.mark.parametrize(
"pretty_ts",
[
True,
False,
],
)
@pytest.mark.parametrize(
"map_symbols",
[
True,
False,
],
)
def test_to_parquet(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
test_data: Callable[[Dataset, Schema], bytes],
dataset: Dataset,
schema: Schema,
price_type: Literal["fixed", "float"],
pretty_ts: bool,
map_symbols: bool,
) -> None:
# Arrange
monkeypatch.setattr(databento.common.dbnstore, "PARQUET_CHUNK_SIZE", 1)
stub_data = test_data(dataset, schema)
data = DBNStore.from_bytes(data=stub_data)
parquet_file = tmp_path / "test.parquet"
# Act
expected = data.to_df(
price_type=price_type,
pretty_ts=pretty_ts,
map_symbols=map_symbols,
)
data.to_parquet(
parquet_file,
price_type=price_type,
pretty_ts=pretty_ts,
map_symbols=map_symbols,
)
actual = pd.read_parquet(parquet_file)
# Replace None values with np.nan
actual.fillna(value=np.nan)
# Assert
pd.testing.assert_frame_equal(actual, expected)
def test_to_parquet_kwargs(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
test_data: Callable[[Dataset, Schema], bytes],
) -> None:
# Arrange
monkeypatch.setattr(databento.common.dbnstore, "PARQUET_CHUNK_SIZE", 1)
stub_data = test_data(Dataset.GLBX_MDP3, Schema.MBO)
data = DBNStore.from_bytes(data=stub_data)
parquet_file = tmp_path / "test.parquet"
# Act
expected = data.to_df()
data.to_parquet(
parquet_file,
compression="zstd",
write_statistics="false",
)
actual = pd.read_parquet(parquet_file)
# Replace None values with np.nan
actual.fillna(value=np.nan)
# Assert
pd.testing.assert_frame_equal(actual, expected)
@pytest.mark.parametrize(
"expected_schema",
[pytest.param(schema, id=str(schema)) for schema in Schema.variants()],
)
def test_from_file_given_various_paths_returns_expected_metadata(
test_data_path: Callable[[Dataset, Schema], Path],
expected_schema: Schema,
) -> None:
# Arrange
path = test_data_path(Dataset.GLBX_MDP3, expected_schema)
# Act
data = DBNStore.from_file(path=path)
# Assert
assert data.schema == expected_schema
def test_read_dbn_alias(
test_data_path: Callable[[Dataset, Schema], Path],
) -> None:
# Arrange
path = test_data_path(Dataset.GLBX_MDP3, Schema.MBO)
# Act
data = databento.read_dbn(path=path)
# Assert
assert data.schema == Schema.MBO
assert len(data.to_ndarray()) == 4
def test_mbo_to_csv_writes_expected_file_to_disk(
test_data_path: Callable[[Dataset, Schema], Path],
tmp_path: Path,
) -> None:
# Arrange
data = DBNStore.from_file(path=test_data_path(Dataset.GLBX_MDP3, Schema.MBO))
path = tmp_path / "test.my_mbo.csv"
# Act
data.to_csv(
path,
pretty_ts=False,
pretty_px=False,
map_symbols=False,
)
# Assert
written = path.read_text()
expected = (
"ts_recv,ts_event,rtype,publisher_id,instrument_id,action,side,price,size,channel_id,order_id,flags,ts_in_delta,sequence\n"
"1609113600000000000,1609099225061045683,160,1,5482,A,B,3675750000000,2,0,647784248135,40,0,1180\n"
"1609113600000000000,1609099225061045683,160,1,5482,A,B,3675500000000,1,0,647782686353,40,0,1160\n"
"1609113600000000000,1609099225061045683,160,1,5482,A,B,3675250000000,1,0,647782884482,40,0,1166\n"
"1609113600000000000,1609099225061045683,160,1,5482,A,B,3675000000000,1,0,647782912367,40,0,1166\n"
)
assert written == expected
def test_mbp_1_to_csv_with_no_options_writes_expected_file_to_disk(
test_data_path: Callable[[Dataset, Schema], Path],
tmp_path: Path,
) -> None:
# Arrange
data = DBNStore.from_file(path=test_data_path(Dataset.GLBX_MDP3, Schema.MBP_1))
path = tmp_path / "test.my_mbo.csv"
# Act
data.to_csv(
path,
pretty_ts=False,
pretty_px=False,
map_symbols=False,
)
# Assert
written = path.read_text()
expected = (
"ts_recv,ts_event,rtype,publisher_id,instrument_id,action,side,depth,price,size,flags,ts_in_delta,sequence,bid_px_00,ask_px_00,bid_sz_00,ask_sz_00,bid_ct_00,ask_ct_00\n"
"1609113600006150193,1609113600005871213,1,1,5482,A,B,0,3702250000000,1,130,26128,145805,3702250000000,3702750000000,19,13,11,13\n"
"1609113600062687776,1609113600062570311,1,1,5482,A,B,0,3702250000000,1,130,17256,145827,3702250000000,3702750000000,20,13,12,13\n"
"1609113600076130343,1609113600076022275,1,1,5482,A,A,0,3702750000000,1,130,17470,145852,3702250000000,3702750000000,20,14,12,14\n"
"1609113600076436915,1609113600076339855,1,1,5482,A,B,0,3702250000000,1,130,17409,145853,3702250000000,3702750000000,21,14,13,14\n"
)
assert written == expected
def test_mbp_1_to_csv_with_all_options_writes_expected_file_to_disk(
test_data_path: Callable[[Dataset, Schema], Path],
tmp_path: Path,
) -> None:
# Arrange
data = DBNStore.from_file(path=test_data_path(Dataset.GLBX_MDP3, Schema.MBP_1))
path = tmp_path / "test.my_mbo.csv"
# Act
data.to_csv(
path,
pretty_ts=True,
pretty_px=True,
map_symbols=True,
)
# Assert
written = path.read_text()
expected = (
"ts_recv,ts_event,rtype,publisher_id,instrument_id,action,side,depth,price,size,flags,ts_in_delta,sequence,bid_px_00,ask_px_00,bid_sz_00,ask_sz_00,bid_ct_00,ask_ct_00,symbol\n"
"2020-12-28T00:00:00.006150193Z,2020-12-28T00:00:00.005871213Z,1,1,5482,A,B,0,3702.250000000,1,130,26128,145805,3702.250000000,3702.750000000,19,13,11,13,ESH1\n"
"2020-12-28T00:00:00.062687776Z,2020-12-28T00:00:00.062570311Z,1,1,5482,A,B,0,3702.250000000,1,130,17256,145827,3702.250000000,3702.750000000,20,13,12,13,ESH1\n"
"2020-12-28T00:00:00.076130343Z,2020-12-28T00:00:00.076022275Z,1,1,5482,A,A,0,3702.750000000,1,130,17470,145852,3702.250000000,3702.750000000,20,14,12,14,ESH1\n"
"2020-12-28T00:00:00.076436915Z,2020-12-28T00:00:00.076339855Z,1,1,5482,A,B,0,3702.250000000,1,130,17409,145853,3702.250000000,3702.750000000,21,14,13,14,ESH1\n"
)
assert written == expected
def test_mbo_to_json_with_no_options_writes_expected_file_to_disk(
test_data_path: Callable[[Dataset, Schema], Path],
tmp_path: Path,
) -> None:
# Arrange
data = DBNStore.from_file(path=test_data_path(Dataset.GLBX_MDP3, Schema.MBO))
path = tmp_path / "test.my_mbo.json"
# Act
data.to_json(
path,
pretty_ts=False,
pretty_px=False,
map_symbols=False,
)
# Assert
written = path.read_text()
expected = (
'{"ts_recv":"1609113600000000000","hd":{"ts_event":"1609099225061045683","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","price":"3675750000000","size":2,"channel_id":0,"order_id":"647784248135","flags":40,"ts_in_delta":0,"sequence":1180}\n'
'{"ts_recv":"1609113600000000000","hd":{"ts_event":"1609099225061045683","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","price":"3675500000000","size":1,"channel_id":0,"order_id":"647782686353","flags":40,"ts_in_delta":0,"sequence":1160}\n'
'{"ts_recv":"1609113600000000000","hd":{"ts_event":"1609099225061045683","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","price":"3675250000000","size":1,"channel_id":0,"order_id":"647782884482","flags":40,"ts_in_delta":0,"sequence":1166}\n'
'{"ts_recv":"1609113600000000000","hd":{"ts_event":"1609099225061045683","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","price":"3675000000000","size":1,"channel_id":0,"order_id":"647782912367","flags":40,"ts_in_delta":0,"sequence":1166}\n'
)
assert written == expected
def test_mbo_to_json_with_all_options_writes_expected_file_to_disk(
test_data_path: Callable[[Dataset, Schema], Path],
tmp_path: Path,
) -> None:
# Arrange
data = DBNStore.from_file(path=test_data_path(Dataset.GLBX_MDP3, Schema.MBO))
path = tmp_path / "test.my_mbo.json"
# Act
data.to_json(
path,
pretty_ts=True,
pretty_px=True,
map_symbols=True,
)
# Assert
written = path.read_text()
expected = (
'{"ts_recv":"2020-12-28T00:00:00.000000000Z","hd":{"ts_event":"2020-12-27T20:00:25.061045683Z","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","price":"3675.750000000","size":2,"channel_id":0,"order_id":"647784248135","flags":40,"ts_in_delta":0,"sequence":1180,"symbol":"ESH1"}\n'
'{"ts_recv":"2020-12-28T00:00:00.000000000Z","hd":{"ts_event":"2020-12-27T20:00:25.061045683Z","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","price":"3675.500000000","size":1,"channel_id":0,"order_id":"647782686353","flags":40,"ts_in_delta":0,"sequence":1160,"symbol":"ESH1"}\n'
'{"ts_recv":"2020-12-28T00:00:00.000000000Z","hd":{"ts_event":"2020-12-27T20:00:25.061045683Z","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","price":"3675.250000000","size":1,"channel_id":0,"order_id":"647782884482","flags":40,"ts_in_delta":0,"sequence":1166,"symbol":"ESH1"}\n'
'{"ts_recv":"2020-12-28T00:00:00.000000000Z","hd":{"ts_event":"2020-12-27T20:00:25.061045683Z","rtype":160,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","price":"3675.000000000","size":1,"channel_id":0,"order_id":"647782912367","flags":40,"ts_in_delta":0,"sequence":1166,"symbol":"ESH1"}\n'
)
assert written == expected
def test_mbp_1_to_json_with_no_options_writes_expected_file_to_disk(
test_data_path: Callable[[Dataset, Schema], Path],
tmp_path: Path,
) -> None:
# Arrange
data = DBNStore.from_file(path=test_data_path(Dataset.GLBX_MDP3, Schema.MBP_1))
path = tmp_path / "test.my_mbo.json"
# Act
data.to_json(
path,
pretty_ts=False,
pretty_px=False,
map_symbols=False,
)
# Assert
written = path.read_text()
expected = (
'{"ts_recv":"1609113600006150193","hd":{"ts_event":"1609113600005871213","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","depth":0,"price":"3702250000000","size":1,"flags":130,"ts_in_delta":26128,"sequence":145805,"levels":[{"bid_px":"3702250000000","ask_px":"3702750000000","bid_sz":19,"ask_sz":13,"bid_ct":11,"ask_ct":13}]}\n'
'{"ts_recv":"1609113600062687776","hd":{"ts_event":"1609113600062570311","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","depth":0,"price":"3702250000000","size":1,"flags":130,"ts_in_delta":17256,"sequence":145827,"levels":[{"bid_px":"3702250000000","ask_px":"3702750000000","bid_sz":20,"ask_sz":13,"bid_ct":12,"ask_ct":13}]}\n'
'{"ts_recv":"1609113600076130343","hd":{"ts_event":"1609113600076022275","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"A","depth":0,"price":"3702750000000","size":1,"flags":130,"ts_in_delta":17470,"sequence":145852,"levels":[{"bid_px":"3702250000000","ask_px":"3702750000000","bid_sz":20,"ask_sz":14,"bid_ct":12,"ask_ct":14}]}\n'
'{"ts_recv":"1609113600076436915","hd":{"ts_event":"1609113600076339855","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","depth":0,"price":"3702250000000","size":1,"flags":130,"ts_in_delta":17409,"sequence":145853,"levels":[{"bid_px":"3702250000000","ask_px":"3702750000000","bid_sz":21,"ask_sz":14,"bid_ct":13,"ask_ct":14}]}\n'
)
assert written == expected
def test_mbp_1_to_json_with_all_options_writes_expected_file_to_disk(
test_data_path: Callable[[Dataset, Schema], Path],
tmp_path: Path,
) -> None:
# Arrange
data = DBNStore.from_file(path=test_data_path(Dataset.GLBX_MDP3, Schema.MBP_1))
path = tmp_path / "test.my_mbo.json"
# Act
data.to_json(
path,
pretty_ts=True,
pretty_px=True,
map_symbols=True,
)
# Assert
written = path.read_text()
expected = (
'{"ts_recv":"2020-12-28T00:00:00.006150193Z","hd":{"ts_event":"2020-12-28T00:00:00.005871213Z","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","depth":0,"price":"3702.250000000","size":1,"flags":130,"ts_in_delta":26128,"sequence":145805,"levels":[{"bid_px":"3702.250000000","ask_px":"3702.750000000","bid_sz":19,"ask_sz":13,"bid_ct":11,"ask_ct":13}],"symbol":"ESH1"}\n'
'{"ts_recv":"2020-12-28T00:00:00.062687776Z","hd":{"ts_event":"2020-12-28T00:00:00.062570311Z","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","depth":0,"price":"3702.250000000","size":1,"flags":130,"ts_in_delta":17256,"sequence":145827,"levels":[{"bid_px":"3702.250000000","ask_px":"3702.750000000","bid_sz":20,"ask_sz":13,"bid_ct":12,"ask_ct":13}],"symbol":"ESH1"}\n'
'{"ts_recv":"2020-12-28T00:00:00.076130343Z","hd":{"ts_event":"2020-12-28T00:00:00.076022275Z","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"A","depth":0,"price":"3702.750000000","size":1,"flags":130,"ts_in_delta":17470,"sequence":145852,"levels":[{"bid_px":"3702.250000000","ask_px":"3702.750000000","bid_sz":20,"ask_sz":14,"bid_ct":12,"ask_ct":14}],"symbol":"ESH1"}\n'
'{"ts_recv":"2020-12-28T00:00:00.076436915Z","hd":{"ts_event":"2020-12-28T00:00:00.076339855Z","rtype":1,"publisher_id":1,"instrument_id":5482},"action":"A","side":"B","depth":0,"price":"3702.250000000","size":1,"flags":130,"ts_in_delta":17409,"sequence":145853,"levels":[{"bid_px":"3702.250000000","ask_px":"3702.750000000","bid_sz":21,"ask_sz":14,"bid_ct":13,"ask_ct":14}],"symbol":"ESH1"}\n'
)
assert written == expected
@pytest.mark.parametrize(