Skip to content

Commit

Permalink
Refine AssetClass and InstrumentClass enums
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Dec 30, 2023
1 parent 41abeb2 commit 473ad19
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 66 deletions.
5 changes: 5 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ Released on TBD (UTC).

### Enhancements
- Added `NautilusConfig.json_primitives` to convert object to Python dictionary with JSON primitive values
- Added `InstrumentClass.BOND`
- Improved Interactive Brokers adapter raising docker `RuntimeError` only when needed (not when using TWS), thanks @rsmb7z

### Breaking Changes
- Changed `ComponentStateChanged` Arrow schema for `config` from `string` to `binary`
- Changed `OrderInitialized` Arrow schema for `options` from `string` to `binary`
- Renamed `AssetType` enum to `InstrumentClass` (more conventional terminology)
- Renamed `asset_type` to `instrument_class` across the codebase (more conventional terminology)
- 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)
- Moved `AssetClass.SPORTS_BETTING` to `InstrumentClass.SPORTS_BETTING`

### Fixes
- Fixed handling of configuration objects to work with `StreamingFeatherWriter`
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/adapters/src/databento/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn parse_cfi_iso10926(value: &str) -> Result<(Option<AssetClass>, Option<Ins
// let cfi_attribute4 = value[5];

let mut asset_class = match cfi_category {
'D' => Some(AssetClass::Bond),
'D' => Some(AssetClass::Debt),
'E' => Some(AssetClass::Equity),
'S' => None,
_ => None,
Expand Down
37 changes: 19 additions & 18 deletions nautilus_core/model/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,31 +167,26 @@ impl FromU8 for AggressorSide {
#[allow(non_camel_case_types)]
pub enum AssetClass {
/// Foreign exchange (FOREX) assets.
#[pyo3(name = "FX")]
FX = 1,
/// Equity / stock assets.
#[pyo3(name = "EQUITY")]
Equity = 2,
/// Commodity assets.
#[pyo3(name = "COMMODITY")]
Commodity = 3,
/// Metal commodity assets.
#[pyo3(name = "METAL")]
Metal = 4,
/// Energy commodity assets.
#[pyo3(name = "ENERGY")]
Energy = 5,
/// Fixed income bond assets.
#[pyo3(name = "BOND")]
Bond = 6,
/// Index based assets.
/// Debt based assets.
#[pyo3(name = "DEBT")]
Debt = 4,
/// Index based assets (baskets).
#[pyo3(name = "INDEX")]
Index = 7,
Index = 5,
/// Cryptocurrency or crypto token assets.
#[pyo3(name = "CRYPTO_CURRENCY")]
Cryptocurrency = 8,
/// Sports betting instruments.
#[pyo3(name = "SPORTS_BETTING")]
SportsBetting = 9,
#[pyo3(name = "CRYPTOCURRENCY")]
Cryptocurrency = 6,
/// Alternative assets.
#[pyo3(name = "ALTERNATIVE")]
Alternative = 7,
}

/// The asset type for a financial market product.
Expand Down Expand Up @@ -233,12 +228,18 @@ pub enum InstrumentClass {
/// A contract-for-difference (CFD) instrument class. A contract between an investor and a CFD broker to exchange the difference in the value of a financial product between the time the contract opens and closes.
#[pyo3(name = "CFD")]
Cfd = 5,
/// A bond instrument class. A type of debt investment where an investor loans money to an entity (typically corporate or governmental) which borrows the funds for a defined period of time at a variable or fixed interest rate.
#[pyo3(name = "BOND")]
Bond = 6,
/// An options contract instrument class. A type of derivative that gives the holder the right, but not the obligation, to buy or sell an underlying asset at a predetermined price before or at a certain future date.
#[pyo3(name = "OPTION")]
Option = 6,
Option = 7,
/// A warrant instrument class. A derivative that gives the holder the right, but not the obligation, to buy or sell a security—most commonly an equity—at a certain price before expiration.
#[pyo3(name = "WARRANT")]
Warrant = 7,
Warrant = 8,
/// A warrant instrument class. A derivative that gives the holder the right, but not the obligation, to buy or sell a security—most commonly an equity—at a certain price before expiration.
#[pyo3(name = "SPORTS_BETTING")]
SportsBetting = 9,
}

/// The aggregation method through which a bar is generated and closed.
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/databento/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def parse_cfi_iso10926(value: str) -> tuple[AssetClass | None, InstrumentClass |

match cfi_category:
case "D":
asset_class = AssetClass.BOND
asset_class = AssetClass.DEBT
case "E":
asset_class = AssetClass.EQUITY
case "S":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def sec_type_to_asset_class(sec_type: str) -> AssetClass:
"STK": "EQUITY",
"IND": "INDEX",
"CASH": "FX",
"BOND": "BOND",
"BOND": "DEBT",
}
return asset_class_from_str(mapping.get(sec_type, sec_type))

Expand Down
34 changes: 17 additions & 17 deletions nautilus_trader/core/includes/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,21 @@ typedef enum AssetClass {
*/
COMMODITY = 3,
/**
* Metal commodity assets.
* Debt based assets.
*/
METAL = 4,
DEBT = 4,
/**
* Energy commodity assets.
* Index based assets (baskets).
*/
ENERGY = 5,
/**
* Fixed income bond assets.
*/
BOND = 6,
/**
* Index based assets.
*/
INDEX = 7,
INDEX = 5,
/**
* Cryptocurrency or crypto token assets.
*/
CRYPTOCURRENCY = 8,
CRYPTOCURRENCY = 6,
/**
* Sports betting instruments.
* Alternative assets.
*/
SPORTS_BETTING = 9,
ALTERNATIVE = 7,
} AssetClass;

/**
Expand Down Expand Up @@ -237,14 +229,22 @@ typedef enum InstrumentClass {
* A contract-for-difference (CFD) instrument class. A contract between an investor and a CFD broker to exchange the difference in the value of a financial product between the time the contract opens and closes.
*/
CFD = 5,
/**
* A bond instrument class. A type of debt investment where an investor loans money to an entity (typically corporate or governmental) which borrows the funds for a defined period of time at a variable or fixed interest rate.
*/
BOND = 6,
/**
* An options contract instrument class. A type of derivative that gives the holder the right, but not the obligation, to buy or sell an underlying asset at a predetermined price before or at a certain future date.
*/
OPTION = 6,
OPTION = 7,
/**
* A warrant instrument class. A derivative that gives the holder the right, but not the obligation, to buy or sell a security—most commonly an equity—at a certain price before expiration.
*/
WARRANT = 8,
/**
* A warrant instrument class. A derivative that gives the holder the right, but not the obligation, to buy or sell a security—most commonly an equity—at a certain price before expiration.
*/
WARRANT = 7,
SPORTS_BETTING = 9,
} InstrumentClass;

/**
Expand Down
10 changes: 5 additions & 5 deletions nautilus_trader/core/nautilus_pyo3.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -314,21 +314,21 @@ class AggressorSide(Enum):
class AssetClass(Enum):
EQUITY = "EQUITY"
COMMODITY = "COMMODITY"
METAL = "METAL"
ENERGY = "ENERGY"
BOND = "BOND"
DEBT = "DEBT"
INDEX = "INDEX"
CRYPTO_CURRENCY = "CRYPTO_CURRENCY"
SPORTS_BETTING = "SPORTS_BETTING"
CRYPTOCURRENCY = "CRYPTOCURRENCY"
ALTERNATIVE = "ALTERNATIVE"

class InstrumentClass(Enum):
SPOT = "SPOT"
SWAP = "SWAP"
FUTURE = "FUTURE"
FORWARD = "FORWARD"
CFD = "CFD"
BOND = "BOND"
OPTION = "OPTION"
WARRANT = "WARRANT"
SPORTS_BETTING = "SPORTS_BETTING"

class BarAggregation(Enum):
TICK = "TICK"
Expand Down
26 changes: 13 additions & 13 deletions nautilus_trader/core/rust/model.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,14 @@ cdef extern from "../includes/model.h":
EQUITY # = 2,
# Commodity assets.
COMMODITY # = 3,
# Metal commodity assets.
METAL # = 4,
# Energy commodity assets.
ENERGY # = 5,
# Fixed income bond assets.
BOND # = 6,
# Index based assets.
INDEX # = 7,
# Debt based assets.
DEBT # = 4,
# Index based assets (baskets).
INDEX # = 5,
# Cryptocurrency or crypto token assets.
CRYPTOCURRENCY # = 8,
# Sports betting instruments.
SPORTS_BETTING # = 9,
CRYPTOCURRENCY # = 6,
# Alternative assets.
ALTERNATIVE # = 7,

# The type of order book action for an order book event.
cpdef enum BookAction:
Expand Down Expand Up @@ -130,10 +126,14 @@ cdef extern from "../includes/model.h":
FORWARD # = 4,
# A contract-for-difference (CFD) instrument class. A contract between an investor and a CFD broker to exchange the difference in the value of a financial product between the time the contract opens and closes.
CFD # = 5,
# A bond instrument class. A type of debt investment where an investor loans money to an entity (typically corporate or governmental) which borrows the funds for a defined period of time at a variable or fixed interest rate.
BOND # = 6,
# An options contract instrument class. A type of derivative that gives the holder the right, but not the obligation, to buy or sell an underlying asset at a predetermined price before or at a certain future date.
OPTION # = 6,
OPTION # = 7,
# A warrant instrument class. A derivative that gives the holder the right, but not the obligation, to buy or sell a security—most commonly an equity—at a certain price before expiration.
WARRANT # = 7,
WARRANT # = 8,
# A warrant instrument class. A derivative that gives the holder the right, but not the obligation, to buy or sell a security—most commonly an equity—at a certain price before expiration.
SPORTS_BETTING # = 9,

# The type of event for an instrument close.
cpdef enum InstrumentCloseType:
Expand Down
4 changes: 2 additions & 2 deletions nautilus_trader/model/instruments/betting.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ cdef class BettingInstrument(Instrument):
super().__init__(
instrument_id=InstrumentId(symbol=symbol, venue=Venue(venue_name)),
raw_symbol=symbol,
asset_class=AssetClass.SPORTS_BETTING,
instrument_class=InstrumentClass.SPOT,
asset_class=AssetClass.ALTERNATIVE,
instrument_class=InstrumentClass.SPORTS_BETTING,
quote_currency=Currency.from_str_c(currency),
is_inverse=False,
size_precision=4,
Expand Down
16 changes: 8 additions & 8 deletions tests/unit_tests/model/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,10 @@ class TestAssetClass:
[AssetClass.FX, "FX"],
[AssetClass.EQUITY, "EQUITY"],
[AssetClass.COMMODITY, "COMMODITY"],
[AssetClass.METAL, "METAL"],
[AssetClass.ENERGY, "ENERGY"],
[AssetClass.BOND, "BOND"],
[AssetClass.DEBT, "DEBT"],
[AssetClass.INDEX, "INDEX"],
[AssetClass.CRYPTOCURRENCY, "CRYPTOCURRENCY"],
[AssetClass.SPORTS_BETTING, "SPORTS_BETTING"],
[AssetClass.ALTERNATIVE, "ALTERNATIVE"],
],
)
def test_asset_class_to_str(self, enum, expected):
Expand All @@ -217,12 +215,10 @@ def test_asset_class_to_str(self, enum, expected):
["FX", AssetClass.FX],
["EQUITY", AssetClass.EQUITY],
["COMMODITY", AssetClass.COMMODITY],
["METAL", AssetClass.METAL],
["ENERGY", AssetClass.ENERGY],
["BOND", AssetClass.BOND],
["DEBT", AssetClass.DEBT],
["INDEX", AssetClass.INDEX],
["CRYPTOCURRENCY", AssetClass.CRYPTOCURRENCY],
["SPORTS_BETTING", AssetClass.SPORTS_BETTING],
["ALTERNATIVE", AssetClass.ALTERNATIVE],
],
)
def test_asset_class_from_str(self, string, expected):
Expand All @@ -242,8 +238,10 @@ class TestInstrumentClass:
[InstrumentClass.FUTURE, "FUTURE"],
[InstrumentClass.FORWARD, "FORWARD"],
[InstrumentClass.CFD, "CFD"],
[InstrumentClass.BOND, "BOND"],
[InstrumentClass.OPTION, "OPTION"],
[InstrumentClass.WARRANT, "WARRANT"],
[InstrumentClass.SPORTS_BETTING, "SPORTS_BETTING"],
],
)
def test_instrument_class_to_str(self, enum, expected):
Expand All @@ -261,8 +259,10 @@ def test_instrument_class_to_str(self, enum, expected):
["FUTURE", InstrumentClass.FUTURE],
["FORWARD", InstrumentClass.FORWARD],
["CFD", InstrumentClass.CFD],
["BOND", InstrumentClass.BOND],
["OPTION", InstrumentClass.OPTION],
["WARRANT", InstrumentClass.WARRANT],
["SPORTS_BETTING", InstrumentClass.SPORTS_BETTING],
],
)
def test_instrument_class_from_str(self, string, expected):
Expand Down

0 comments on commit 473ad19

Please sign in to comment.