forked from bitvavo/python-bitvavo-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublic_models.py
More file actions
1087 lines (910 loc) · 36.6 KB
/
public_models.py
File metadata and controls
1087 lines (910 loc) · 36.6 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
"""Pydantic models for validating API responses."""
from __future__ import annotations
from decimal import Decimal
from typing import Any
from pydantic import BaseModel, ConfigDict, Field, RootModel, field_validator
class ServerTime(BaseModel):
"""Example Pydantic model for server time response."""
model_config = ConfigDict(
frozen=True,
extra="forbid",
str_strip_whitespace=True,
)
time: int = Field(..., description="Server timestamp in milliseconds", gt=0)
time_ns: int = Field(..., alias="timeNs", description="Server timestamp in nanoseconds", gt=0)
class Market(BaseModel):
"""Pydantic model for a single market entry from the /markets endpoint."""
model_config = ConfigDict(
frozen=True,
extra="forbid",
populate_by_name=True, # Allows using both field names and aliases
str_strip_whitespace=True,
validate_assignment=True, # Re-validate on assignment (useful for frozen=True)
)
market: str = Field(..., description="Market symbol (e.g., 'BTC-EUR')", min_length=1)
status: str = Field(..., description="Market status (e.g., 'trading', 'halted')")
base: str = Field(..., description="Base asset symbol", min_length=1)
quote: str = Field(..., description="Quote asset symbol", min_length=1)
price_precision: int = Field(..., alias="pricePrecision", description="Price precision", ge=0)
min_order_in_base_asset: str = Field(
...,
alias="minOrderInBaseAsset",
description="Minimum order size in base asset",
)
min_order_in_quote_asset: str = Field(
...,
alias="minOrderInQuoteAsset",
description="Minimum order size in quote asset",
)
max_order_in_base_asset: str = Field(
...,
alias="maxOrderInBaseAsset",
description="Maximum order size in base asset",
)
max_order_in_quote_asset: str = Field(
...,
alias="maxOrderInQuoteAsset",
description="Maximum order size in quote asset",
)
quantity_decimals: int = Field(..., alias="quantityDecimals", description="Quantity decimal places", ge=0)
notional_decimals: int = Field(..., alias="notionalDecimals", description="Notional decimal places", ge=0)
tick_size: str | None = Field(default=None, alias="tickSize", description="Minimum price increment")
max_open_orders: int = Field(..., alias="maxOpenOrders", description="Maximum open orders", ge=0)
fee_category: str = Field(..., alias="feeCategory", description="Fee category")
order_types: list[str] = Field(..., alias="orderTypes", description="Supported order types", min_length=1)
@field_validator("order_types")
@classmethod
def validate_order_types(cls, v: list[str]) -> list[str]:
"""Ensure all order types are non-empty strings."""
if not all(isinstance(order_type, str) and order_type.strip() for order_type in v):
msg = "All order types must be non-empty strings"
raise ValueError(msg)
return v
class Markets(RootModel[list[Market]]):
"""Wrapper model representing a list of Market objects (API /markets response)."""
model_config = ConfigDict(
frozen=True,
populate_by_name=True,
validate_assignment=True,
)
def __len__(self) -> int:
"""Return the number of markets."""
return len(self.root)
def __getitem__(self, index: int) -> Market:
"""Allow indexing into the markets list."""
return self.root[index]
@property
def markets(self) -> list[Market]:
"""Get the underlying list of markets."""
return self.root
def get_market(self, symbol: str) -> Market | None:
"""Get a market by symbol.
Args:
symbol: Market symbol (e.g., 'BTC-EUR')
Returns:
Market model if found, None otherwise
"""
for market in self.root:
if market.market == symbol:
return market
return None
def filter_by_status(self, status: str) -> list[Market]:
"""Filter markets by status.
Args:
status: Status to filter by (e.g., 'trading')
Returns:
List of markets with the specified status
"""
return [market for market in self.root if market.status == status]
def get_base_assets(self) -> set[str]:
"""Get all unique base assets.
Returns:
Set of base asset symbols
"""
return {market.base for market in self.root}
def get_quote_assets(self) -> set[str]:
"""Get all unique quote assets.
Returns:
Set of quote asset symbols
"""
return {market.quote for market in self.root}
class Asset(BaseModel):
"""Pydantic model for a single asset/currency entry (from the /assets endpoint)."""
model_config = ConfigDict(
frozen=True,
extra="forbid",
str_strip_whitespace=True,
validate_assignment=True,
)
symbol: str = Field(
...,
description="Asset symbol (e.g. 'BTC')",
min_length=1,
)
name: str = Field(
...,
description="Human readable name",
)
decimals: int = Field(
...,
description="Decimal places supported",
ge=0,
)
deposit_fee: str = Field(
...,
alias="depositFee",
description="Deposit fee as string (e.g. '0')",
)
deposit_confirmations: int = Field(
...,
alias="depositConfirmations",
description="Required confirmations for deposit",
ge=0,
)
deposit_status: str = Field(
...,
alias="depositStatus",
description="Deposit status (e.g. 'OK', 'MAINTENANCE', 'DELISTED')",
)
withdrawal_fee: str = Field(..., alias="withdrawalFee", description="Withdrawal fee as string")
withdrawal_min_amount: str = Field(
...,
alias="withdrawalMinAmount",
description="Minimum withdrawal amount as string",
)
withdrawal_status: str = Field(
...,
alias="withdrawalStatus",
description="Withdrawal status (e.g. 'OK', 'MAINTENANCE', 'DELISTED')",
)
networks: list[str] = Field(
...,
description="List of supported networks (e.g. ['Mainnet', 'ETH'])",
)
message: str = Field(
...,
description="Optional message from the API",
min_length=0,
)
@field_validator("symbol", "name")
@classmethod
def non_empty_str(cls, v: str) -> str:
if not v or not v.strip():
msg = "must be a non-empty string"
raise ValueError(msg)
return v
@field_validator("networks")
@classmethod
def validate_networks(cls, v: list[str]) -> list[str]:
"""Ensure all networks are non-empty strings."""
if not all(isinstance(network, str) and network.strip() for network in v):
msg = "All networks must be non-empty strings"
raise ValueError(msg)
return v
@field_validator("deposit_fee", "withdrawal_fee", "withdrawal_min_amount", "deposit_confirmations", mode="before")
@classmethod
def normalize_fee_strings(cls, v: Any) -> Any:
# keep fees/min-amounts as strings as the API returns them,
# but ensure they are not None. Leave validation of numeric format
# to higher-level code/tests if needed.
if v is None:
msg = "fee/min-amount fields must be provided"
raise ValueError(msg)
return v
class Assets(RootModel[list[Asset]]):
"""Wrapper model representing a list of Asset objects (API /assets response)."""
model_config = ConfigDict(
frozen=True,
validate_assignment=True,
)
def __len__(self) -> int:
return len(self.root)
def __getitem__(self, index: int) -> Asset:
return self.root[index]
@property
def assets(self) -> list[Asset]:
return self.root
def get_asset(self, symbol: str) -> Asset | None:
"""Return the Asset with matching symbol or None."""
for asset in self.root:
if asset.symbol == symbol:
return asset
return None
def filter_by_deposit_status(self, status: str) -> list[Asset]:
"""Return assets filtered by depositStatus."""
return [a for a in self.root if a.deposit_status == status]
def filter_by_withdrawal_status(self, status: str) -> list[Asset]:
"""Return assets filtered by withdrawalStatus."""
return [a for a in self.root if a.withdrawal_status == status]
class PriceLevel(RootModel[list[str]]):
"""A single price level represented as a list (e.g. [price, amount, ...]).
The Bitvavo API returns bids/asks as nested lists. We normalize each inner
list to a list of stripped strings and require at least price and amount.
"""
model_config = ConfigDict(frozen=True, str_strip_whitespace=True)
@field_validator("root", mode="before")
@classmethod
def _ensure_list_of_str(cls, v: Any) -> list[str]:
# Accept tuples/lists and also already-parsed PriceLevel instances.
if isinstance(v, PriceLevel):
return v.root
try:
items = list(v)
except TypeError as exc:
msg = "price level must be an iterable"
raise ValueError(msg) from exc
if len(items) < 2: # noqa: PLR2004
msg = "price level must contain at least price and amount"
raise ValueError(msg)
# Convert all items to strings and strip whitespace
return [("" if it is None else str(it)).strip() for it in items]
def price(self) -> str:
return self.root[0]
def amount(self) -> str:
return self.root[1]
def extras(self) -> list[str]:
return self.root[2:]
class OrderBook(BaseModel):
"""Model for the order book snapshot returned by the API.
Example input:
{'market': 'BTC-EUR', 'nonce': 91722611, 'bids': [[...], ...], 'asks': [[...], ...], 'timestamp': 1756...}
"""
model_config = ConfigDict(
frozen=True,
extra="forbid",
str_strip_whitespace=True,
populate_by_name=True,
validate_assignment=True,
)
market: str = Field(..., description="Market symbol (e.g. 'BTC-EUR')", min_length=1)
nonce: int = Field(..., description="Snapshot nonce", ge=0)
bids: list[PriceLevel] = Field(..., description="List of bid price levels (each is a list)")
asks: list[PriceLevel] = Field(..., description="List of ask price levels (each is a list)")
timestamp: int = Field(..., description="Server timestamp (likely in nanoseconds)", ge=0)
@field_validator("bids", "asks", mode="before")
@classmethod
def _normalize_levels(cls, v: Any) -> list[PriceLevel]:
# Ensure iterable -> list and let PriceLevel parse each inner entry
try:
return list(v)
except TypeError as exc:
msg = "bids/asks must be iterable of price levels"
raise ValueError(msg) from exc
def best_bid(self) -> PriceLevel | None:
return self.bids[0] if self.bids else None
def best_ask(self) -> PriceLevel | None:
return self.asks[0] if self.asks else None
class TickerPrice(BaseModel):
"""Model representing a single market ticker price entry."""
model_config = ConfigDict(
frozen=True,
extra="forbid",
str_strip_whitespace=True,
validate_assignment=True,
)
market: str = Field(..., description="Market symbol (e.g. 'BTC-EUR')", min_length=1)
price: str = Field(..., description="Price as returned by the API (string)")
@field_validator("market", "price")
@classmethod
def _non_empty_str(cls, v: str) -> str:
if not isinstance(v, str) or not v.strip():
msg = "must be a non-empty string"
raise ValueError(msg)
return v
@field_validator("price")
@classmethod
def _validate_price_is_numeric(cls, v: str) -> str:
# Keep the original string value but ensure it is a numeric representation
try:
d = Decimal(v)
except Exception as exc:
msg = "price must be a numeric string"
raise ValueError(msg) from exc
if d < 0:
msg = "price must be non-negative"
raise ValueError(msg)
return v
class TickerPrices(RootModel[list[TickerPrice]]):
"""Wrapper for a list of TickerPrice items (e.g. API tickers response)."""
model_config = ConfigDict(
frozen=True,
validate_assignment=True,
)
def __len__(self) -> int:
return len(self.root)
def __getitem__(self, index: int) -> TickerPrice:
return self.root[index]
@property
def prices(self) -> list[TickerPrice]:
return self.root
def get_price(self, market: str) -> TickerPrice | None:
"""Return the TickerPrice for the given market or None if not found."""
for tp in self.root:
if tp.market == market:
return tp
return None
def to_serializable(self) -> list[dict]:
"""Return a list of dicts suitable for JSON serialization."""
return [tp.model_dump() for tp in self.root]
class TickerBook(BaseModel):
"""Model representing best bid/ask for a single market (API /ticker/book)."""
model_config = ConfigDict(
frozen=True,
extra="forbid",
str_strip_whitespace=True,
validate_assignment=True,
)
market: str = Field(..., description="Market symbol (e.g. 'BTC-EUR')", min_length=1)
bid: str | None = Field(..., description="Best bid price as string")
bid_size: str | None = Field(..., alias="bidSize", description="Size available at best bid as string")
ask: str | None = Field(..., description="Best ask price as string")
ask_size: str | None = Field(..., alias="askSize", description="Size available at best ask as string")
@field_validator("market")
@classmethod
def _non_empty_str(cls, v: str) -> str:
if not isinstance(v, str) or not v.strip():
msg = "must be a non-empty string"
raise ValueError(msg)
return v
@field_validator("bid", "ask", "bid_size", "ask_size")
@classmethod
def _validate_numeric_str(cls, v: str | None) -> str | None:
if v is None:
return v
try:
d = Decimal(v)
except Exception as exc:
msg = "must be a numeric string"
raise ValueError(msg) from exc
if d < 0:
msg = "must be non-negative"
raise ValueError(msg)
return v
class TickerBooks(RootModel[list[TickerBook]]):
"""Wrapper for a list of TickerBook items (e.g. API /ticker/book response).
Handles both list responses (when no market is specified) and single object
responses (when a specific market is requested via query parameter).
"""
model_config = ConfigDict(
frozen=True,
validate_assignment=True,
)
@field_validator("root", mode="before")
@classmethod
def _normalize_input(cls, v: Any) -> list[dict]:
"""Convert single TickerBook dict to list for consistent handling."""
if isinstance(v, dict):
# Single ticker book object - wrap in list
return [v]
if isinstance(v, list):
# Already a list of ticker books
return v
msg = "Input must be a dict or list of dicts"
raise TypeError(msg)
def __len__(self) -> int:
return len(self.root)
def __getitem__(self, index: int) -> TickerBook:
return self.root[index]
@property
def books(self) -> list[TickerBook]:
return self.root
def get_book(self, market: str) -> TickerBook | None:
"""Return the TickerBook for the given market or None if not found."""
for tb in self.root:
if tb.market == market:
return tb
return None
def to_serializable(self) -> list[dict]:
"""Return a list of dicts suitable for JSON serialization."""
return [tb.model_dump() for tb in self.root]
class Trade(BaseModel):
"""Public trade entry (as returned by Bitvavo /trades)."""
model_config = ConfigDict(
frozen=True,
extra="forbid",
str_strip_whitespace=True,
validate_assignment=True,
)
id: str = Field(..., description="Trade identifier", min_length=1)
timestamp: int = Field(..., description="Trade timestamp in milliseconds", ge=0)
amount: str = Field(..., description="Traded amount (base asset) as string")
price: str = Field(..., description="Trade price as string")
side: str = Field(..., description="Trade side ('buy' or 'sell')")
@field_validator("id")
@classmethod
def _non_empty_id(cls, v: str) -> str:
if not isinstance(v, str) or not v.strip():
msg = "must be a non-empty string"
raise ValueError(msg)
return v
@field_validator("amount", "price")
@classmethod
def _numeric_str(cls, v: str) -> str:
try:
d = Decimal(v)
except Exception as exc:
msg = "must be a numeric string"
raise ValueError(msg) from exc
if d < 0:
msg = "must be non-negative"
raise ValueError(msg)
return v
@field_validator("side")
@classmethod
def _validate_side(cls, v: str) -> str:
if not isinstance(v, str) or not v.strip():
msg = "must be a non-empty string"
raise ValueError(msg)
side = v.strip().lower()
if side not in {"buy", "sell"}:
msg = "side must be 'buy' or 'sell'"
raise ValueError(msg)
return side
class Trades(RootModel[list[Trade]]):
"""Wrapper for a list of Trade items."""
model_config = ConfigDict(
frozen=True,
validate_assignment=True,
)
def __len__(self) -> int:
return len(self.root)
def __getitem__(self, index: int) -> Trade:
return self.root[index]
@property
def trades(self) -> list[Trade]:
return self.root
def filter_by_side(self, side: str) -> list[Trade]:
s = side.strip().lower()
if s not in {"buy", "sell"}:
return []
return [t for t in self.root if t.side == s]
def buys(self) -> list[Trade]:
return self.filter_by_side("buy")
def sells(self) -> list[Trade]:
return self.filter_by_side("sell")
def latest(self) -> Trade | None:
return max(self.root, key=lambda t: t.timestamp) if self.root else None
def to_serializable(self) -> list[dict]:
return [t.model_dump() for t in self.root]
class Candle(BaseModel):
"""Single OHLCV candle: [timestamp, open, high, low, close, volume]."""
model_config = ConfigDict(
frozen=True,
extra="forbid",
str_strip_whitespace=True,
validate_assignment=True,
)
timestamp: int = Field(..., description="Candle open time in milliseconds", ge=0)
open: str = Field(..., description="Open price as string")
high: str = Field(..., description="High price as string")
low: str = Field(..., description="Low price as string")
close: str = Field(..., description="Close price as string")
volume: str = Field(..., description="Volume as string")
@field_validator("open", "high", "low", "close", "volume")
@classmethod
def _validate_numeric_str(cls, v: str) -> str:
try:
d = Decimal(v)
except Exception as exc:
msg = "must be a numeric string"
raise ValueError(msg) from exc
if d < 0:
msg = "must be non-negative"
raise ValueError(msg)
return v
@classmethod
def from_ohlcv(cls, ohlcv: list | tuple) -> Candle:
"""Create Candle from a 6-item sequence."""
if not isinstance(ohlcv, (list, tuple)) or len(ohlcv) < 6: # noqa: PLR2004
msg = "ohlcv must be a sequence [timestamp, open, high, low, close, volume]"
raise ValueError(msg)
timestamp, open, high, low, close, volume = ohlcv[:6] # noqa: A001
return cls(
timestamp=int(timestamp), open=str(open), high=str(high), low=str(low), close=str(close), volume=str(volume)
)
def to_ohlcv(self) -> list:
"""Return as [timestamp, open, high, low, close, volume]."""
return [self.timestamp, self.open, self.high, self.low, self.close, self.volume]
class Candles(RootModel[list[Candle]]):
"""Wrapper for list of Candle items (API /candles response)."""
model_config = ConfigDict(
frozen=True,
validate_assignment=True,
)
@field_validator("root", mode="before")
@classmethod
def _normalize_ohlcv(cls, v: Any) -> Any:
# Accept list of lists/tuples and convert each to Candle
try:
items = list(v)
except TypeError:
return v
out: list[Candle | dict] = []
for item in items:
if isinstance(item, Candle):
out.append(item)
elif isinstance(item, (list, tuple)):
timestamp, open, high, low, close, volume = (item + [None] * 6)[:6] # noqa: A001
if None in (timestamp, open, high, low, close, volume):
msg = "each candle must have 6 elements"
raise ValueError(msg)
out.append(
{
"timestamp": int(timestamp),
"open": str(open),
"high": str(high),
"low": str(low),
"close": str(close),
"volume": str(volume),
}
)
else:
out.append(item)
return out
def __len__(self) -> int:
return len(self.root)
def __getitem__(self, index: int) -> Candle:
return self.root[index]
@property
def candles(self) -> list[Candle]:
return self.root
def to_ohlcv(self) -> list[list]:
"""Return list of [timestamp, open, high, low, close, volume]."""
return [c.to_ohlcv() for c in self.root]
def earliest(self) -> Candle | None:
return min(self.root, key=lambda c: c.timestamp) if self.root else None
def latest(self) -> Candle | None:
return max(self.root, key=lambda c: c.timestamp) if self.root else None
class Ticker24h(BaseModel):
"""24h ticker stats for a single market (mirrors Bitvavo /ticker/24h item)."""
model_config = ConfigDict(
frozen=True,
extra="forbid",
str_strip_whitespace=True,
validate_assignment=True,
populate_by_name=True,
)
market: str = Field(..., description="Market symbol (e.g. 'BTC-EUR')", min_length=1)
start_timestamp: int = Field(..., alias="startTimestamp", description="Window start timestamp (ms)", ge=0)
timestamp: int = Field(..., description="Current server timestamp (ms)", ge=0)
open: str | None = Field(..., description="Open price as string")
open_timestamp: int | None = Field(..., alias="openTimestamp", description="Open price timestamp (ms)", ge=0)
high: str | None = Field(..., description="High price as string")
low: str | None = Field(..., description="Low price as string")
last: str | None = Field(..., description="Last trade price as string")
close_timestamp: int | None = Field(..., alias="closeTimestamp", description="Close price timestamp (ms)", ge=0)
bid: str | None = Field(..., description="Best bid price as string")
bid_size: str | None = Field(..., alias="bidSize", description="Size available at best bid as string")
ask: str | None = Field(..., description="Best ask price as string")
ask_size: str | None = Field(..., alias="askSize", description="Size available at best ask as string")
volume: str | None = Field(..., description="Base asset volume in the last 24h as string")
volume_quote: str | None = Field(
...,
alias="volumeQuote",
description="Quote asset volume in the last 24h as string",
)
@field_validator("market")
@classmethod
def _non_empty_str(cls, v: str) -> str:
if not isinstance(v, str) or not v.strip():
msg = "must be a non-empty string"
raise ValueError(msg)
return v
class Ticker24hs(RootModel[list[Ticker24h]]):
"""Wrapper for a list of Ticker24h items (API /ticker/24h response)."""
model_config = ConfigDict(
frozen=True,
validate_assignment=True,
)
def __len__(self) -> int:
return len(self.root)
def __getitem__(self, index: int) -> Ticker24h:
return self.root[index]
@property
def tickers(self) -> list[Ticker24h]:
return self.root
def get_ticker(self, market: str) -> Ticker24h | None:
"""Return the Ticker24h for the given market or None if not found."""
for t in self.root:
if t.market == market:
return t
return None
def to_serializable(self) -> list[dict]:
"""Return a list of dicts suitable for JSON serialization."""
return [t.model_dump(by_alias=True) for t in self.root]
class OrderBookReportEntry(BaseModel):
"""Individual order entry in a MiCA-compliant order book report."""
model_config = ConfigDict(
frozen=True,
extra="forbid",
str_strip_whitespace=True,
validate_assignment=True,
# Enhanced error reporting configuration
title="OrderBookReportEntry",
validate_default=True,
loc_by_alias=False,
)
side: str = Field(..., description="Order side: 'BUYI' for bids, 'SELL' for asks")
price: str = Field(..., description="Price value as decimal string")
quantity: str = Field(..., alias="size", description="Quantity value as decimal string")
num_orders: int = Field(..., alias="numOrders", description="Number of orders at this price level", ge=1)
@field_validator("side")
@classmethod
def _validate_side(cls, v: str) -> str:
if not isinstance(v, str) or not v.strip():
msg = f"Order side must be a non-empty string, got {type(v).__name__}: {v!r}"
raise ValueError(msg)
side = v.strip().upper()
if side not in {"BUYI", "SELL"}:
msg = (
f"Order side must be 'BUY' or 'SELL', got: {side!r}. "
"Valid values: BUYI (buy orders), SELL (sell orders)"
)
raise ValueError(msg)
return side
@field_validator("price", "quantity")
@classmethod
def _validate_numeric_str(cls, v: str) -> str:
if not isinstance(v, str) or not v.strip():
msg = f"Numeric field must be a non-empty string, got {type(v).__name__}: {v!r}"
raise ValueError(msg)
try:
d = Decimal(v)
except (ValueError, TypeError) as exc:
msg = f"Numeric field must be a valid decimal string (e.g., '123.45'), got: {v!r}. Error: {exc}"
raise ValueError(msg) from exc
if d < 0:
msg = f"Numeric field must be non-negative, got: {v!r} (value: {d})"
raise ValueError(msg)
return v
class OrderBookReport(BaseModel):
"""MiCA-compliant order book report model (API /report/{market}/book response)."""
model_config = ConfigDict(
frozen=True,
extra="forbid",
str_strip_whitespace=True,
validate_assignment=True,
populate_by_name=True,
# Enhanced error reporting configuration
title="OrderBookReport",
validate_default=True,
loc_by_alias=False,
)
submission_timestamp: str = Field(
...,
alias="submissionTimestamp",
description="Timestamp when order book is submitted to database (ISO 8601)",
)
asset_code: str = Field(..., alias="assetCode", description="DTI code or symbol of the asset")
asset_name: str = Field(..., alias="assetName", description="Full name of the asset")
bids: list[OrderBookReportEntry] = Field(..., description="List of buy orders")
asks: list[OrderBookReportEntry] = Field(..., description="List of sell orders")
price_currency: str = Field(..., alias="priceCurrency", description="DTI code of price currency")
price_notation: str = Field(..., alias="priceNotation", description="Price notation (always 'MONE')")
quantity_currency: str = Field(..., alias="quantityCurrency", description="Currency for quantity expression")
quantity_notation: str = Field(..., alias="quantityNotation", description="Quantity notation (always 'CRYP')")
venue: str = Field(..., description="Market Identifier Code (always 'VAVO')")
trading_system: str = Field(..., alias="tradingSystem", description="Trading system identifier (always 'VAVO')")
publication_timestamp: str = Field(
...,
alias="publicationTimestamp",
description="Timestamp when book snapshot is added to database (ISO 8601)",
)
@field_validator("submission_timestamp", "publication_timestamp")
@classmethod
def _validate_timestamp(cls, v: str) -> str:
if not isinstance(v, str):
msg = "timestamp must be a string"
raise TypeError(msg)
# Allow empty timestamps
if not v.strip():
return v
# Basic format validation - should be ISO 8601 format
if not v.endswith("Z") or "T" not in v:
msg = "timestamp must be in ISO 8601 format (e.g., '2025-05-02T14:23:11.123456Z') or empty"
raise ValueError(msg)
return v
@field_validator("asset_code", "asset_name", "price_currency", "quantity_currency", "venue", "trading_system")
@classmethod
def _non_empty_str(cls, v: str) -> str:
if not isinstance(v, str) or not v.strip():
msg = "must be a non-empty string"
raise ValueError(msg)
return v
@field_validator("price_notation")
@classmethod
def _validate_price_notation(cls, v: str) -> str:
if not isinstance(v, str) or v.strip() != "MONE":
msg = "price_notation must be 'MONE'"
raise ValueError(msg)
return v
@field_validator("quantity_notation")
@classmethod
def _validate_quantity_notation(cls, v: str) -> str:
if not isinstance(v, str) or v.strip() != "CRYP":
msg = "quantity_notation must be 'CRYP'"
raise ValueError(msg)
return v
@field_validator("venue", "trading_system")
@classmethod
def _validate_venue_system(cls, v: str) -> str:
if not isinstance(v, str) or v.strip() not in ["VAVO", "CLOB"]:
msg = "venue and trading_system must be 'VAVO' or 'CLOB'"
raise ValueError(msg)
return v
def best_bid(self) -> OrderBookReportEntry | None:
"""Get the best (highest) bid."""
if not self.bids:
return None
return max(self.bids, key=lambda bid: Decimal(bid.price))
def best_ask(self) -> OrderBookReportEntry | None:
"""Get the best (lowest) ask."""
if not self.asks:
return None
return min(self.asks, key=lambda ask: Decimal(ask.price))
def spread(self) -> Decimal | None:
"""Calculate the spread between best bid and ask."""
best_bid = self.best_bid()
best_ask = self.best_ask()
if best_bid and best_ask:
return Decimal(best_ask.price) - Decimal(best_bid.price)
return None
class TradeReportEntry(BaseModel):
"""Individual trade entry in a MiCA-compliant trades report."""
model_config = ConfigDict(
frozen=True,
extra="forbid",
str_strip_whitespace=True,
validate_assignment=True,
populate_by_name=True,
)
trade_id: str = Field(
...,
alias="tradeId",
description="The unique identifier of the trade",
min_length=1,
)
transact_timestamp: str = Field(
...,
alias="transactTimestamp",
description="The timestamp when the trade is added to the database (ISO 8601 format)",
)
asset_code: str = Field(
...,
alias="assetCode",
description="The DTI code or a symbol of the asset",
min_length=1,
)
asset_name: str = Field(
...,
alias="assetName",
description="The full name of the asset",
min_length=1,
)
price: str = Field(
...,
description="The price of 1 unit of base currency in the amount of quote currency at the time of the trade",
)
missing_price: str = Field(
"",
alias="missingPrice",
description="Indicates if the price is pending (PNDG) or not applicable (NOAP). May be empty.",
)
price_notation: str = Field(
...,
alias="priceNotation",
description="Indicates whether the price is expressed as a monetary value, percentage, yield, or basis points",
)
price_currency: str = Field(
...,
alias="priceCurrency",
description="The currency in which the price is expressed",
min_length=1,
)
quantity: str = Field(
...,
description="The quantity of the asset (decimal string)",
)
quantity_currency: str = Field(
...,
alias="quantityCurrency",
description="The currency in which the quantity of the crypto asset is expressed",
min_length=1,
)
quantity_notation: str = Field(
...,
alias="quantityNotation",
description="Indicates whether the quantity is expressed as units, nominal value, monetary value, or crypto",
)
venue: str = Field(
...,
description="The Market Identifier Code of the Bitvavo trading platform",
min_length=1,
)
publication_timestamp: str = Field(
...,
alias="publicationTimestamp",
description="The timestamp when the trade is added to the database (ISO 8601 format)",
)
publication_venue: str = Field(
...,
alias="publicationVenue",
description="The Market Identifier Code of the trading platform that publishes the transaction",
min_length=1,
)
@field_validator(
"trade_id", "asset_code", "asset_name", "price_currency", "quantity_currency", "venue", "publication_venue"
)