Skip to content

Commit

Permalink
Refine instruments
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Dec 30, 2023
1 parent 1a01d5c commit 581290b
Show file tree
Hide file tree
Showing 25 changed files with 63 additions and 76 deletions.
2 changes: 2 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Released on TBD (UTC).
- Renamed `AssetClass.BOND` to `AssetClass.DEBT` (more conventional terminology)
- Removed `AssetClass.METAL` (not strictly an asset class, more a futures category)
- Removed `AssetClass.ENERGY` (not strictly an asset class, more a futures category)
- Removed `multiplier` param from `Equity` constructor (not applicable)
- Removed `size_precision`, `size_increment`, and `multiplier` fields from `Equity` dictionary representation (not applicable)
- Moved `AssetClass.SPORTS_BETTING` to `InstrumentClass.SPORTS_BETTING`

### Fixes
Expand Down
1 change: 0 additions & 1 deletion nautilus_core/adapters/src/databento/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ pub fn parse_equity(
currency,
currency.precision,
parse_min_price_increment(record.min_price_increment, currency)?,
Quantity::new(1.0, 0)?,
dec!(0), // TBD
dec!(0), // TBD
dec!(0), // TBD
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/instruments/crypto_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
};

#[repr(C)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[cfg_attr(
feature = "python",
pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/instruments/crypto_perpetual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
};

#[repr(C)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[cfg_attr(
feature = "python",
pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/instruments/currency_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
};

#[repr(C)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[cfg_attr(
feature = "python",
pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
Expand Down
12 changes: 5 additions & 7 deletions nautilus_core/model/src/instruments/equity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use nautilus_core::time::UnixNanos;
use pyo3::prelude::*;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use ustr::Ustr;

use super::Instrument;
use crate::{
Expand All @@ -31,7 +32,7 @@ use crate::{
};

#[repr(C)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[cfg_attr(
feature = "python",
pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
Expand All @@ -40,11 +41,10 @@ pub struct Equity {
pub id: InstrumentId,
pub raw_symbol: Symbol,
/// The instruments ISIN (International Securities Identification Number).
pub isin: Option<String>,
pub isin: Option<Ustr>,
pub currency: Currency,
pub price_precision: u8,
pub price_increment: Price,
pub multiplier: Quantity,
pub margin_init: Decimal,
pub margin_maint: Decimal,
pub maker_fee: Decimal,
Expand All @@ -63,11 +63,10 @@ impl Equity {
pub fn new(
id: InstrumentId,
raw_symbol: Symbol,
isin: Option<String>,
isin: Option<Ustr>,
currency: Currency,
price_precision: u8,
price_increment: Price,
multiplier: Quantity,
margin_init: Decimal,
margin_maint: Decimal,
maker_fee: Decimal,
Expand All @@ -87,7 +86,6 @@ impl Equity {
currency,
price_precision,
price_increment,
multiplier,
margin_init,
margin_maint,
maker_fee,
Expand Down Expand Up @@ -167,7 +165,7 @@ impl Instrument for Equity {
}

fn multiplier(&self) -> Quantity {
self.multiplier
Quantity::from(1)
}

fn lot_size(&self) -> Option<Quantity> {
Expand Down
7 changes: 4 additions & 3 deletions nautilus_core/model/src/instruments/futures_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use nautilus_core::time::UnixNanos;
use pyo3::prelude::*;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use ustr::Ustr;

use super::Instrument;
use crate::{
Expand All @@ -31,7 +32,7 @@ use crate::{
};

#[repr(C)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[cfg_attr(
feature = "python",
pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
Expand All @@ -40,7 +41,7 @@ pub struct FuturesContract {
pub id: InstrumentId,
pub raw_symbol: Symbol,
pub asset_class: AssetClass,
pub underlying: String,
pub underlying: Ustr,
pub activation_ns: UnixNanos,
pub expiration_ns: UnixNanos,
pub currency: Currency,
Expand All @@ -66,7 +67,7 @@ impl FuturesContract {
id: InstrumentId,
raw_symbol: Symbol,
asset_class: AssetClass,
underlying: String,
underlying: Ustr,
activation_ns: UnixNanos,
expiration_ns: UnixNanos,
currency: Currency,
Expand Down
6 changes: 3 additions & 3 deletions nautilus_core/model/src/instruments/stubs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use chrono::{TimeZone, Utc};
use nautilus_core::time::UnixNanos;
use rstest::fixture;
use rust_decimal::Decimal;
use ustr::Ustr;

use crate::{
enums::{AssetClass, OptionKind},
Expand Down Expand Up @@ -171,11 +172,10 @@ pub fn equity_aapl() -> Equity {
Equity::new(
InstrumentId::from("AAPL.NASDAQ"),
Symbol::from("AAPL"),
Some(String::from("US0378331005")),
Some(Ustr::from("US0378331005")),
Currency::from("USD"),
2,
Price::from("0.01"),
Quantity::from(1),
Decimal::from_str("0.0").unwrap(),
Decimal::from_str("0.0").unwrap(),
Decimal::from_str("0.001").unwrap(),
Expand All @@ -199,7 +199,7 @@ pub fn futures_contract_es() -> FuturesContract {
InstrumentId::new(Symbol::from("ESZ21"), Venue::from("CME")),
Symbol::from("ESZ21"),
AssetClass::Index,
String::from("ES"),
Ustr::from("ES"),
activation.timestamp_nanos_opt().unwrap() as UnixNanos,
expiration.timestamp_nanos_opt().unwrap() as UnixNanos,
Currency::USD(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl CryptoFuture {
#[pyo3(name = "to_dict")]
fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
let dict = PyDict::new(py);
dict.set_item("type", stringify!(CryptoPerpetual))?;
dict.set_item("type", stringify!(CryptoFuture))?;
dict.set_item("id", self.id.to_string())?;
dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
dict.set_item("underlying", self.underlying.code.to_string())?;
Expand Down
6 changes: 2 additions & 4 deletions nautilus_core/model/src/python/instruments/equity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use nautilus_core::{
};
use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
use rust_decimal::{prelude::ToPrimitive, Decimal};
use ustr::Ustr;

use crate::{
identifiers::{instrument_id::InstrumentId, symbol::Symbol},
Expand All @@ -41,7 +42,6 @@ impl Equity {
currency: Currency,
price_precision: u8,
price_increment: Price,
multiplier: Quantity,
margin_init: Decimal,
margin_maint: Decimal,
maker_fee: Decimal,
Expand All @@ -58,11 +58,10 @@ impl Equity {
Self::new(
id,
raw_symbol,
isin,
isin.map(|x| Ustr::from(&x)),
currency,
price_precision,
price_increment,
multiplier,
margin_init,
margin_maint,
maker_fee,
Expand Down Expand Up @@ -106,7 +105,6 @@ impl Equity {
dict.set_item("currency", self.currency.code.to_string())?;
dict.set_item("price_precision", self.price_precision)?;
dict.set_item("price_increment", self.price_increment.to_string())?;
dict.set_item("multiplier", self.multiplier.to_string())?;
dict.set_item("margin_init", self.margin_init.to_f64())?;
dict.set_item("margin_maint", self.margin_maint.to_f64())?;
dict.set_item("maker_fee", self.maker_fee.to_f64())?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl FuturesContract {
id,
raw_symbol,
asset_class,
underlying,
underlying.into(),
activation_ns,
expiration_ns,
currency,
Expand Down
3 changes: 1 addition & 2 deletions nautilus_trader/adapters/databento/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ def parse_equity(
currency=currency,
price_precision=currency.precision,
price_increment=parse_min_price_increment(record.min_price_increment, currency),
multiplier=Quantity(1, precision=0),
lot_size=Quantity(record.min_lot_size_round_lot, precision=0),
lot_size=Quantity.from_int(record.min_lot_size_round_lot),
isin=None, # TODO
ts_event=record.ts_recv, # More accurate and reliable timestamp
ts_init=ts_init,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ def parse_equity_contract(details: IBContractDetails) -> Equity:
currency=Currency.from_str(details.contract.currency),
price_precision=price_precision,
price_increment=Price(details.minTick, price_precision),
multiplier=Quantity.from_int(1),
lot_size=Quantity.from_int(1),
lot_size=Quantity.from_int(100),
isin=_extract_isin(details),
ts_event=timestamp,
ts_init=timestamp,
Expand Down
8 changes: 7 additions & 1 deletion nautilus_trader/core/nautilus_pyo3.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ class CryptoFuture:
max_price: Price | None = None,
min_price: Price | None = None,
) -> None: ...

class CryptoPerpetual:
def __init__(
self,
Expand Down Expand Up @@ -728,6 +729,7 @@ class CryptoPerpetual:
max_price: Price | None = None,
min_price: Price | None = None,
) -> None: ...

class CurrencyPair:
def __init__(
self,
Expand All @@ -751,6 +753,7 @@ class CurrencyPair:
max_price: Price | None = None,
min_price: Price | None = None,
) -> None: ...

class Equity:
def __init__(
self,
Expand All @@ -760,7 +763,6 @@ class Equity:
currency: Currency,
price_precision: int,
price_increment: Price,
multiplier: Quantity,
margin_init: float,
margin_maint: float,
maker_fee: float,
Expand All @@ -773,6 +775,7 @@ class Equity:
max_price: Price | None = None,
min_price: Price | None = None,
) -> None: ...

class FuturesContract:
def __init__(
self,
Expand All @@ -798,6 +801,7 @@ class FuturesContract:
max_price: Price | None = None,
min_price: Price | None = None,
) -> None: ...

class OptionsContract:
def __init__(
self,
Expand All @@ -824,9 +828,11 @@ class OptionsContract:
max_price: Price | None = None,
min_price: Price | None = None,
) -> None : ...

class SyntheticInstrument: ...

### Events

class OrderDenied:
def __init__(
self,
Expand Down
13 changes: 2 additions & 11 deletions nautilus_trader/model/instruments/equity.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ cdef class Equity(Instrument):
The price decimal precision.
price_increment : Decimal
The minimum price increment (tick size).
multiplier : Decimal
The contract value multiplier (determines tick value).
lot_size : Quantity
The rounded lot unit size (standard/board).
isin : str, optional
Expand All @@ -67,12 +65,10 @@ cdef class Equity(Instrument):
Raises
------
ValueError
If `multiplier` is not positive (> 0).
ValueError
If `price_precision` is negative (< 0).
ValueError
If `tick_size` is not positive (> 0).
If `price_increment` is not positive (> 0).
ValueError
If `lot_size` is not positive (> 0).
"""
Expand All @@ -84,7 +80,6 @@ cdef class Equity(Instrument):
Currency currency not None,
int price_precision,
Price price_increment not None,
Quantity multiplier not None,
Quantity lot_size not None,
uint64_t ts_event,
uint64_t ts_init,
Expand All @@ -106,7 +101,7 @@ cdef class Equity(Instrument):
size_precision=0, # No fractional units
price_increment=price_increment,
size_increment=Quantity.from_int_c(1),
multiplier=multiplier,
multiplier=Quantity.from_int_c(1),
lot_size=lot_size,
max_quantity=None,
min_quantity=Quantity.from_int_c(1),
Expand All @@ -133,7 +128,6 @@ cdef class Equity(Instrument):
currency=Currency.from_str_c(values["currency"]),
price_precision=values["price_precision"],
price_increment=Price.from_str(values["price_increment"]),
multiplier=Quantity.from_str(values["multiplier"]),
lot_size=Quantity.from_str(values["lot_size"]),
isin=values.get("isin"), # Can be None,
margin_init=Decimal(values.get("margin_init", 0)), # Can be None,
Expand All @@ -154,9 +148,6 @@ cdef class Equity(Instrument):
"currency": obj.quote_currency.code,
"price_precision": obj.price_precision,
"price_increment": str(obj.price_increment),
"size_precision": obj.size_precision,
"size_increment": str(obj.size_increment),
"multiplier": str(obj.multiplier),
"lot_size": str(obj.lot_size),
"isin": obj.isin,
"margin_init": str(obj.margin_init),
Expand Down
3 changes: 1 addition & 2 deletions nautilus_trader/test_kit/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,7 @@ def equity(symbol: str = "AAPL", venue: str = "XNAS") -> Equity:
currency=USD,
price_precision=2,
price_increment=Price.from_str("0.01"),
multiplier=Quantity.from_int(1),
lot_size=Quantity.from_int(1),
lot_size=Quantity.from_int(100),
isin="US0378331005",
ts_event=0,
ts_init=0,
Expand Down
3 changes: 1 addition & 2 deletions nautilus_trader/test_kit/rust/instruments_pyo3.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,11 @@ def appl_equity() -> Equity:
currency=TestTypesProviderPyo3.currency_usd(),
price_precision=2,
price_increment=Price.from_str("0.01"),
multiplier=Quantity.from_str("1"),
margin_init=0.0,
margin_maint=0.0,
maker_fee=0.001,
taker_fee=0.001,
lot_size=Quantity.from_str("1.0"),
lot_size=Quantity.from_int(100),
max_quantity=None,
min_quantity=None,
max_price=None,
Expand Down
Loading

0 comments on commit 581290b

Please sign in to comment.