Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsable raw string representation of Money object #1612

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions nautilus_trader/model/instruments/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ cdef class Instrument(Data):
"lot_size": str(obj.lot_size) if obj.lot_size is not None else None,
"max_quantity": str(obj.max_quantity) if obj.max_quantity is not None else None,
"min_quantity": str(obj.min_quantity) if obj.min_quantity is not None else None,
"max_notional": obj.max_notional.to_str() if obj.max_notional is not None else None,
"min_notional": obj.min_notional.to_str() if obj.min_notional is not None else None,
"max_notional": obj.max_notional.to_raw_str() if obj.max_notional is not None else None,
"min_notional": obj.min_notional.to_raw_str() if obj.min_notional is not None else None,
"max_price": str(obj.max_price) if obj.max_price is not None else None,
"min_price": str(obj.min_price) if obj.min_price is not None else None,
"margin_init": str(obj.margin_init),
Expand Down
4 changes: 2 additions & 2 deletions nautilus_trader/model/instruments/cfd.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ cdef class Cfd(Instrument):
"lot_size": str(obj.lot_size) if obj.lot_size is not None else None,
"max_quantity": str(obj.max_quantity) if obj.max_quantity is not None else None,
"min_quantity": str(obj.min_quantity) if obj.min_quantity is not None else None,
"max_notional": obj.max_notional.to_str() if obj.max_notional is not None else None,
"min_notional": obj.min_notional.to_str() if obj.min_notional is not None else None,
"max_notional": obj.max_notional.to_raw_str() if obj.max_notional is not None else None,
"min_notional": obj.min_notional.to_raw_str() if obj.min_notional is not None else None,
"max_price": str(obj.max_price) if obj.max_price is not None else None,
"min_price": str(obj.min_price) if obj.min_price is not None else None,
"margin_init": str(obj.margin_init),
Expand Down
4 changes: 2 additions & 2 deletions nautilus_trader/model/instruments/commodity.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ cdef class Commodity(Instrument):
"lot_size": str(obj.lot_size) if obj.lot_size is not None else None,
"max_quantity": str(obj.max_quantity) if obj.max_quantity is not None else None,
"min_quantity": str(obj.min_quantity) if obj.min_quantity is not None else None,
"max_notional": obj.max_notional.to_str() if obj.max_notional is not None else None,
"min_notional": obj.min_notional.to_str() if obj.min_notional is not None else None,
"max_notional": obj.max_notional.to_raw_str() if obj.max_notional is not None else None,
"min_notional": obj.min_notional.to_raw_str() if obj.min_notional is not None else None,
"max_price": str(obj.max_price) if obj.max_price is not None else None,
"min_price": str(obj.min_price) if obj.min_price is not None else None,
"margin_init": str(obj.margin_init),
Expand Down
4 changes: 2 additions & 2 deletions nautilus_trader/model/instruments/crypto_future.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ cdef class CryptoFuture(Instrument):
"lot_size": str(obj.lot_size),
"max_quantity": str(obj.max_quantity) if obj.max_quantity is not None else None,
"min_quantity": str(obj.min_quantity) if obj.min_quantity is not None else None,
"max_notional": obj.max_notional.to_str() if obj.max_notional is not None else None,
"min_notional": obj.min_notional.to_str() if obj.min_notional is not None else None,
"max_notional": obj.max_notional.to_raw_str() if obj.max_notional is not None else None,
"min_notional": obj.min_notional.to_raw_str() if obj.min_notional is not None else None,
"max_price": str(obj.max_price) if obj.max_price is not None else None,
"min_price": str(obj.min_price) if obj.min_price is not None else None,
"margin_init": str(obj.margin_init),
Expand Down
4 changes: 2 additions & 2 deletions nautilus_trader/model/instruments/crypto_perpetual.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ cdef class CryptoPerpetual(Instrument):
"lot_size": str(obj.lot_size),
"max_quantity": str(obj.max_quantity) if obj.max_quantity is not None else None,
"min_quantity": str(obj.min_quantity) if obj.min_quantity is not None else None,
"max_notional": obj.max_notional.to_str() if obj.max_notional is not None else None,
"min_notional": obj.min_notional.to_str() if obj.min_notional is not None else None,
"max_notional": obj.max_notional.to_raw_str() if obj.max_notional is not None else None,
"min_notional": obj.min_notional.to_raw_str() if obj.min_notional is not None else None,
"max_price": str(obj.max_price) if obj.max_price is not None else None,
"min_price": str(obj.min_price) if obj.min_price is not None else None,
"margin_init": str(obj.margin_init),
Expand Down
4 changes: 2 additions & 2 deletions nautilus_trader/model/instruments/currency_pair.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ cdef class CurrencyPair(Instrument):
"lot_size": str(obj.lot_size) if obj.lot_size is not None else None,
"max_quantity": str(obj.max_quantity) if obj.max_quantity is not None else None,
"min_quantity": str(obj.min_quantity) if obj.min_quantity is not None else None,
"max_notional": obj.max_notional.to_str() if obj.max_notional is not None else None,
"min_notional": obj.min_notional.to_str() if obj.min_notional is not None else None,
"max_notional": obj.max_notional.to_raw_str() if obj.max_notional is not None else None,
"min_notional": obj.min_notional.to_raw_str() if obj.min_notional is not None else None,
"max_price": str(obj.max_price) if obj.max_price is not None else None,
"min_price": str(obj.min_price) if obj.min_price is not None else None,
"margin_init": str(obj.margin_init),
Expand Down
1 change: 1 addition & 0 deletions nautilus_trader/model/objects.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ cdef class Money:
cdef void sub_assign(self, Money other)

cpdef str to_str(self)
cpdef str to_raw_str(self)
cpdef object as_decimal(self)
cpdef double as_double(self)

Expand Down
11 changes: 11 additions & 0 deletions nautilus_trader/model/objects.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,17 @@ cdef class Money:
"""
return f"{self.as_f64_c():,.{self._mem.currency.precision}f} {self.currency_code_c()}".replace(",", "_")

cpdef str to_raw_str(self):
"""
Return the raw string representation of the money.

Returns
-------
str

"""
return f"{self.as_f64_c():.{self._mem.currency.precision}f} {self.currency_code_c()}"


cdef class Currency:
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/model/test_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def test_crypto_perpetual_instrument_to_dict(self):
"size_increment": "1",
"max_quantity": None,
"min_quantity": None,
"max_notional": "10_000_000.00 USD",
"max_notional": "10000000.00 USD",
"min_notional": "1.00 USD",
"max_price": "1000000.0",
"min_price": "0.5",
Expand Down