Objects - Loren1166/NautilusTrader- GitHub Wiki
Objects | 对象
Defines fundamental value objects for the trading domain. 定义交易领域的基本值对象。
class AccountBalance | 账户余额
class AccountBalance:
"""
Represents an account balance denominated in a particular currency. 表示以特定货币计价的账户余额。
Parameters:
参数:
total (Money) – The total account balance. 账户总余额。
locked (Money) – The account balance locked (assigned to pending orders). 已锁定账户余额(分配给待处理订单)。
free (Money) – The account balance free for trading. 可用于交易的账户余额。
Raises:
引发:
ValueError – If money currencies are not equal. 如果货币不相同。
ValueError – If any money is negative (< 0). 如果任何金额为负数 (< 0)。
ValueError – If total - locked != free. 如果 total - locked != free。
"""
def __init__(self, total: Money, locked: Money, free: Money) -> None:
...
Properties | 属性
@property
def currency(self) -> Currency:
"""
The currency of the account. 账户的货币。
Returns: Currency
"""
...
@property
def free(self) -> Money:
"""
The account balance free for trading. 可用于交易的账户余额。
Returns: Money
"""
...
@property
def locked(self) -> Money:
"""
The account balance locked (assigned to pending orders). 已锁定账户余额(分配给待处理订单)。
Returns: Money
"""
...
@property
def total(self) -> Money:
"""
The total account balance. 账户总余额。
Returns: Money
"""
...
Methods | 方法
@staticmethod
def from_dict(values: dict) -> AccountBalance:
"""
Return an account balance from the given dict values. 从给定的字典值返回账户余额。
Parameters: values (dict *[*str , object ]) – The values for initialization. 用于初始化的值。
Return type: AccountBalance
"""
...
def to_dict(self) -> dict:
"""
Return a dictionary representation of this object. 返回此对象的字典表示形式。
Return type: dict[str, object]
"""
...
class Currency | 货币
class Currency:
"""
Represents a medium of exchange in a specified denomination with a fixed decimal precision. 表示以指定面额进行交换的媒介,具有固定的十进制精度。
Handles up to 9 decimals of precision. 处理高达 9 位小数的精度。
Parameters:
参数:
code (str) – The currency code. 货币代码。
precision (uint8_t) – The currency decimal precision. 货币十进制精度。
iso4217 (uint16) – The currency ISO 4217 code. 货币 ISO 4217 代码。
name (str) – The currency name. 货币名称。
currency_type (CurrencyType) – The currency type. 货币类型。
Raises:
引发:
ValueError – If code is not a valid string. 如果代码不是有效的字符串。
OverflowError – If precision is negative (< 0). 如果精度为负数 (< 0)。
ValueError – If precision greater than 9. 如果精度大于 9。
ValueError – If name is not a valid string. 如果名称不是有效的字符串。
"""
def __init__(
self,
code: str,
precision: int,
iso4217: int,
name: str,
currency_type: CurrencyType
) -> None:
...
Properties | 属性
@property
def code(self) -> str:
"""
str
Return the currency code. 返回货币代码。
Return type: str
Type: Currency.code
"""
...
@property
def currency_type(self) -> CurrencyType:
"""
CurrencyType
Return the currency type. 返回货币类型。
Return type: CurrencyType
Type: Currency.currency_type
"""
...
@property
def iso4217(self) -> int:
"""
int
Return the currency ISO 4217 code. 返回货币 ISO 4217 代码。
Return type: str
Type: Currency.iso4217
"""
...
@property
def name(self) -> str:
"""
str
Return the currency name. 返回货币名称。
Return type: str
Type: Currency.name
"""
...
@property
def precision(self) -> int:
"""
int
Return the currency decimal precision. 返回货币十进制精度。
Return type: uint8
Type: Currency.precision
"""
...
Methods | 方法
@staticmethod
def from_internal_map(code: str):
"""
Return the currency with the given code from the built-in internal map (if found). 从内置的内部映射中返回具有给定代码的货币(如果找到)。
Parameters: code (str) – The code of the currency. 货币代码。
Return type: Currency or None
"""
...
@staticmethod
def from_str(code: str, strict: bool = False):
"""
Parse a currency from the given string (if found). 从给定的字符串解析货币(如果找到)。
Parameters:
参数:
code (str) – The code of the currency. 货币代码。
strict (bool , default False) – If not strict mode then an unknown currency will very likely be a Cryptocurrency, so for robustness will then return a new Currency object using the given code with a default precision of 8. 如果不是严格模式,则未知货币很可能是加密货币,因此为了稳健性,将使用给定代码和默认精度 8 返回一个新的货币对象。
Return type: Currency or None
"""
...
@staticmethod
def is_crypto(code: str) -> bool:
"""
Return whether a currency with the given code is CRYPTO. 返回具有给定代码的货币是否为加密货币。
Parameters: code (str) – The code of the currency. 货币代码。
Returns: True if CRYPTO, else False. 如果为加密货币,则返回 True,否则返回 False。
Return type: bool
Raises: ValueError – If code is not a valid string. 如果代码不是有效的字符串。
"""
...
@staticmethod
def is_fiat(code: str) -> bool:
"""
Return whether a currency with the given code is FIAT. 返回具有给定代码的货币是否为法定货币。
Parameters: code (str) – The code of the currency. 货币代码。
Returns: True if FIAT, else False. 如果为法定货币,则返回 True,否则返回 False。
Return type: bool
Raises: ValueError – If code is not a valid string. 如果代码不是有效的字符串。
"""
...
@staticmethod
def register(currency: Currency, overwrite: bool = False):
"""
Register the given currency. 注册给定的货币。
Will override the internal currency map. 将覆盖内部货币映射。
Parameters:
参数:
currency (Currency) – The currency to register 要注册的货币
overwrite (bool) – If the currency in the internal currency map should be overwritten. 是否应覆盖内部货币映射中的货币。
"""
...
class MarginBalance | 保证金余额
class MarginBalance:
"""
Represents a margin balance optionally associated with a particular instrument. 表示可选地与特定工具关联的保证金余额。
Parameters:
参数:
initial (Money) – The initial (order) margin requirement for the instrument. 该工具的初始(订单)保证金要求。
maintenance (Money) – The maintenance (position) margin requirement for the instrument. 该工具的维持(持仓)保证金要求。
instrument_id (InstrumentId , optional) – The instrument ID associated with the margin. 与保证金关联的工具 ID。
Raises:
引发:
ValueError – If margin_init currency does not equal currency. 如果 margin_init 货币与货币不相等。
ValueError – If margin_maint currency does not equal currency. 如果 margin_maint 货币与货币不相等。
ValueError – If any margin is negative (< 0). 如果任何保证金为负数 (< 0)。
"""
def __init__(
self,
initial: Money,
maintenance: Money,
instrument_id: InstrumentId | None = None
) -> None:
...
Properties | 属性
@property
def currency(self) -> Currency:
"""
The currency of the margin. 保证金的货币。
Returns: Currency
"""
...
@property
def initial(self) -> Money:
"""
The initial margin requirement. 初始保证金要求。
Returns: Money
"""
...
@property
def instrument_id(self) -> InstrumentId | None:
"""
The instrument ID associated with the margin. 与保证金关联的工具 ID。
Returns: InstrumentId or None
"""
...
@property
def maintenance(self) -> Money:
"""
The maintenance margin requirement. 维持保证金要求。
Returns: Money
"""
...
Methods | 方法
@staticmethod
def from_dict(values: dict) -> MarginAccountBalance:
"""
Return a margin balance from the given dict values. 从给定的字典值返回保证金余额。
Parameters: values (dict *[*str , object ]) – The values for initialization. 用于初始化的值。
Return type: MarginAccountBalance
"""
...
def to_dict(self) -> dict:
"""
Return a dictionary representation of this object. 返回此对象的字典表示形式。
Return type: dict[str, object]
"""
...
class Money | 金额
class Money:
"""
Represents an amount of money in a specified currency denomination. 表示以指定货币面额计价的金额。
MONEY_MAX = 9_223_372_036
MONEY_MIN = -9_223_372_036
Parameters:
参数:
value (integer , float , string or Decimal) – The amount of money in the currency denomination. 以货币面额计价的金额。
currency (Currency) – The currency of the money. 金额的货币。
Raises:
引发:
ValueError – If value is greater than 9_223_372_036. 如果值大于 9_223_372_036。
ValueError – If value is less than -9_223_372_036. 如果值小于 -9_223_372_036。
"""
MONEY_MAX = 9_223_372_036
MONEY_MIN = -9_223_372_036
def __init__(self, value: int | float | str | Decimal, currency: Currency) -> None:
...
Properties | 属性
@property
def currency(self) -> Currency:
"""
Currency
Return the currency for the money. 返回金额的货币。
Return type: Currency
Type: Money.currency
"""
...
@property
def raw(self) -> int:
"""
int64_t
Return the raw memory representation of the money amount. 返回金额的原始内存表示形式。
Return type: int64_t
Type: Money.raw
"""
...
Methods | 方法
def as_decimal(self) -> Decimal:
"""
Return the value as a built-in Decimal. 返回值为内置的 Decimal。
Return type: Decimal
"""
...
def as_double(self) -> float:
"""
Return the value as a double. 返回值为双精度浮点数。
Return type: double
"""
...
@staticmethod
def from_raw(raw: int, currency: Currency) -> Money:
"""
Return money from the given raw fixed-point integer and currency. 从给定的原始定点整数和货币返回金额。
Parameters:
参数:
raw (int64_t) – The raw fixed-point money amount. 原始的定点金额。
currency (Currency) – The currency of the money. 金额的货币。
Return type: Money
WARNING
Small raw values can produce a zero money amount depending on the precision of the currency. 较小的原始值可能会根据货币的精度产生零金额。
"""
...
@staticmethod
def from_str(value: str) -> Money:
"""
Return money parsed from the given string. 返回从给定字符串解析的金额。
Must be correctly formatted with a value and currency separated by a whitespace delimiter. 必须使用值和货币以空格分隔符正确格式化。
Example: “1000000.00 USD”. 示例:“1000000.00 USD”。
Parameters: value (str) – The value to parse. 要解析的值。
Return type: Money
Raises:
引发:
ValueError – If inferred currency precision is greater than 9. 如果推断的货币精度大于 9。
OverflowError – If inferred currency precision is negative (< 0). 如果推断的货币精度为负数 (< 0)。
"""
...
def to_formatted_str(self) -> str:
"""
Return the formatted string representation of the money. 返回货币的格式化字符串表示形式。
Return type: str
"""
...
class Price | 价格
class Price:
"""
Represents a price in a market. 表示市场中的价格。
The number of decimal places may vary. For certain asset classes, prices may have negative values. For example, prices for options instruments can be negative under certain conditions. 小数位数可能有所不同。对于某些资产类别,价格可能具有负值。例如,在某些情况下,期权工具的价格可能为负数。
Handles up to 9 decimals of precision. 处理高达 9 位小数的精度。
PRICE_MAX = 9_223_372_036
PRICE_MIN = -9_223_372_036
Parameters:
参数:
value (integer , float , string or Decimal) – The value of the price. 价格的值。
precision (uint8_t) – The precision for the price. Use a precision of 0 for whole numbers (no fractional units). 价格的精度。对于整数(无小数单位),请使用精度 0。
Raises:
引发:
ValueError – If value is greater than 9_223_372_036. 如果值大于 9_223_372_036。
ValueError – If value is less than -9_223_372_036. 如果值小于 -9_223_372_036。
ValueError – If precision is greater than 9. 如果精度大于 9。
OverflowError – If precision is negative (< 0). 如果精度为负数 (< 0)。
"""
PRICE_MAX = 9_223_372_036
PRICE_MIN = -9_223_372_036
def __init__(self, value: int | float | str | Decimal, precision: int) -> None:
...
Properties | 属性
@property
def precision(self) -> int:
"""
int
Return the precision for the price. 返回价格的精度。
Return type: uint8_t
Type: Price.precision
"""
...
@property
def raw(self) -> int:
"""
int64_t
Return the raw memory representation of the price value. 返回价格值的原始内存表示形式。
Return type: int64_t
Type: Price.raw
"""
...
Methods | 方法
def as_decimal(self) -> Decimal:
"""
Return the value as a built-in Decimal. 返回值为内置的 Decimal。
Return type: Decimal
"""
...
def as_double(self) -> float:
"""
Return the value as a double. 返回值为双精度浮点数。
Return type: double
"""
...
@staticmethod
def from_int(value: int) -> Price:
"""
Return a price from the given integer value. 从给定的整数值返回价格。
A precision of zero will be inferred. 将推断出精度为零。
Parameters: value (int) – The value for the price. 价格的值。
Return type: Price
"""
...
@staticmethod
def from_raw(raw: int, precision: int) -> Price:
"""
Return a price from the given raw fixed-point integer and precision. 从给定的原始定点整数和精度返回价格。
Handles up to 9 decimals of precision. 处理高达 9 位小数的精度。
Parameters:
参数:
raw (int64_t) – The raw fixed-point price value. 原始定点价格值。
precision (uint8_t) – The precision for the price. Use a precision of 0 for whole numbers (no fractional units). 价格的精度。对于整数(无小数单位),请使用精度 0。
Return type: Price
Raises:
引发:
ValueError – If precision is greater than 9. 如果精度大于 9。
OverflowError – If precision is negative (< 0). 如果精度为负数 (< 0)。
WARNING
Small raw values can produce a zero price depending on the precision. 较小的原始值可能会根据精度产生零价格。
"""
...
@staticmethod
def from_str(value: str) -> Price:
"""
Return a price parsed from the given string. 返回从给定字符串解析的价格。
Handles up to 9 decimals of precision. 处理高达 9 位小数的精度。
Parameters: value (str) – The value to parse. 要解析的值。
Return type: Price
WARNING
The decimal precision will be inferred from the number of digits following the ‘.’ point (if no point then precision zero). 十进制精度将从“.”后面的位数推断出来(如果没有点,则精度为零)。
Raises:
引发:
ValueError – If inferred precision is greater than 9. 如果推断的精度大于 9。
OverflowError – If inferred precision is negative (< 0). 如果推断的精度为负数 (< 0)。
"""
...
def to_formatted_str(self) -> str:
"""
Return the formatted string representation of the price. 返回价格的格式化字符串表示形式。
Return type: str
"""
...
class Quantity | 数量
class Quantity:
"""
Represents a quantity with a non-negative value. 表示具有非负值的量。
Capable of storing either a whole number (no decimal places) of ‘contracts’ or ‘shares’ (instruments denominated in whole units) or a decimal value containing decimal places for instruments denominated in fractional units. 能够存储“合约”或“股票”(以整数单位计价的工具)的整数(无小数位)或包含以小数单位计价的工具的小数位的小数值。
Handles up to 9 decimals of precision. 处理高达 9 位小数的精度。
QUANTITY_MAX = 18_446_744_073
QUANTITY_MIN = 0
Parameters:
参数:
value (integer , float , string , Decimal) – The value of the quantity. 数量的值。
precision (uint8_t) – The precision for the quantity. Use a precision of 0 for whole numbers (no fractional units). 数量的精度。对于整数(无小数单位),请使用精度 0。
Raises:
引发:
ValueError – If value is greater than 18_446_744_073. 如果值大于 18_446_744_073。
ValueError – If value is negative (< 0). 如果值为负数 (< 0)。
ValueError – If precision is greater than 9. 如果精度大于 9。
OverflowError – If precision is negative (< 0). 如果精度为负数 (< 0)。
"""
QUANTITY_MAX = 18_446_744_073
QUANTITY_MIN = 0
def __init__(self, value: int | float | str | Decimal, precision: int) -> None:
...
Properties | 属性
@property
def precision(self) -> int:
"""
int
Return the precision for the quantity. 返回数量的精度。
Return type: uint8_t
Type: Quantity.precision
"""
...
@property
def raw(self) -> int:
"""
uint64_t
Return the raw memory representation of the quantity value. 返回值量的原始内存表示形式。
Return type: uint64_t
Type: Quantity.raw
"""
...
Methods | 方法
def as_decimal(self) -> Decimal:
"""
Return the value as a built-in Decimal. 返回值为内置的 Decimal。
Return type: Decimal
"""
...
def as_double(self) -> float:
"""
Return the value as a double. 返回值为双精度浮点数。
Return type: double
"""
...
@staticmethod
def from_int(value: int) -> Quantity:
"""
Return a quantity from the given integer value. 从给定的整数值返回数量。
A precision of zero will be inferred. 将推断出精度为零。
Parameters: value (int) – The value for the quantity. 数量的值。
Return type: Quantity
"""
...
@staticmethod
def from_raw(raw: int, precision: int) -> Quantity:
"""
Return a quantity from the given raw fixed-point integer and precision. 从给定的原始定点整数和精度返回数量。
Handles up to 9 decimals of precision. 处理高达 9 位小数的精度。
Parameters:
参数:
raw (int64_t) – The raw fixed-point quantity value. 原始定点数量值。
precision (uint8_t) – The precision for the quantity. Use a precision of 0 for whole numbers (no fractional units). 数量的精度。对于整数(无小数单位),请使用精度 0。
Return type: Quantity
Raises:
引发:
ValueError – If precision is greater than 9. 如果精度大于 9。
OverflowError – If precision is negative (< 0). 如果精度为负数 (< 0)。
WARNING
Small raw values can produce a zero quantity depending on the precision. 较小的原始值可能会根据精度产生零数量。
"""
...
@staticmethod
def from_str(value: str) -> Quantity:
"""
Return a quantity parsed from the given string. 返回从给定字符串解析的数量。
Handles up to 9 decimals of precision. 处理高达 9 位小数的精度。
Parameters: value (str) – The value to parse. 要解析的值。
Return type: Quantity
Raises:
引发:
ValueError – If inferred precision is greater than 9. 如果推断的精度大于 9。
OverflowError – If inferred precision is negative (< 0). 如果推断的精度为负数 (< 0)。
WARNING
The decimal precision will be inferred from the number of digits following the ‘.’ point (if no point then precision zero). 十进制精度将从“.”后面的位数推断出来(如果没有点,则精度为零)。
"""
...
@staticmethod
def raw_to_f64(raw) -> float:
...
def to_formatted_str(self) -> str:
"""
Return the formatted string representation of the quantity. 返回数量的格式化字符串表示形式。
Return type: str
"""
...
@staticmethod
def zero(precision: int = 0) -> Quantity:
"""
Return a quantity with a value of zero. 返回值为零的数量。
precision : The precision for the quantity. 数量的精度。
Return type: Quantity
Raises:
引发:
ValueError – If precision is greater than 9. 如果精度大于 9。
OverflowError – If precision is negative (< 0). 如果精度为负数 (< 0)。
WARNING
The default precision is zero. 默认精度为零。
"""
...