Instruments - Loren1166/NautilusTrader- GitHub Wiki
Instruments
金融工具(金融产品)
The Instrument base class represents the core specification for any tradable asset/contract. There are currently a number of subclasses representing a range of asset classes and instrument classes which are supported by the platform: Instrument 基类代表任何可交易资产/合约的核心规范。目前有多个子类表示Instrument支持的一系列资产类别和标的类别:
- Equity (generic Equity)
- Future (generic Futures Contract)
- Option (generic Options Contract)
- CurrencyPair (represents a Fiat FX or Cryptocurrency pair in a spot/cash market)
- CryptoPerpetual (Perpetual Futures Contract a.k.a. Perpetual Swap)
- CryptoFuture (Deliverable Futures Contract with Crypto assets as underlying, and for price quotes and settlement)
- BettingInstrument (Sports, gaming, or other betting)
Symbology
交易标的代码体系
All instruments should have a unique InstrumentId, which is made up of both the native symbol and venue ID, separated by a period. For example, on the Binance Futures crypto exchange, the Ethereum Perpetual Futures Contract has the instrument ID ETHUSDT-PERP.BINANCE. 每个平台的每个交易品种应具有唯一的 InstrumentId,由本地符号和交易所 ID 组成,两者之间用点分隔。例如,在 Binance Futures 加密交易所,以太坊永续期货合约的Instrument ID 为 ETHUSDT-PERP.BINANCE。
All native symbols should be unique for a venue (this is not always the case e.g. Binance share native symbols between spot and futures markets), and the {symbol.venue} combination must be unique for a Nautilus system. 所有本地符号在某个交易所应唯一(这并不总是如此,例如 Binance 在现货和期货市场之间共享本地符号),并且 {symbol.venue} 组合在 Nautilus 系统中必须唯一。
warning
警告
The correct instrument must be matched to a market dataset such as ticks or order book data for logically sound operation. An incorrectly specified instrument may truncate data or otherwise produce surprising results. 正确的Instrument必须与市场数据集(如 ticks 或订单簿数据)匹配,以确保逻辑操作的合理性。指定不正确的Instrument可能会截断数据或产生意外结果。
Backtesting
回测
Generic test instruments can be instantiated through the TestInstrumentProvider: 通用测试Instrument可以通过 TestInstrumentProvider 实例化:
from nautilus_trader.test_kit.providers import TestInstrumentProvider
audusd = TestInstrumentProvider.default_fx_ccy("AUD/USD")
Exchange specific instruments can be discovered from live exchange data using an adapter's InstrumentProvider: 特定交易所的Instrument可以通过适配器的 InstrumentProvider 从实时交易所数据中发现:
from nautilus_trader.adapters.binance.spot.providers import BinanceSpotInstrumentProvider
from nautilus_trader.model.identifiers import InstrumentId
provider = BinanceSpotInstrumentProvider(client=binance_http_client)
await self.provider.load_all_async()
btcusdt = InstrumentId.from_str("BTCUSDT.BINANCE")
instrument = provider.find(btcusdt)
Or flexibly defined by the user through an Instrument constructor, or one of its more specific subclasses: 或者通过 Instrument 构造函数或其更具体的子类灵活定义:
from nautilus_trader.model.instruments import Instrument
instrument = Instrument(...) # <-- provide all necessary parameters
See the full instrument API Reference. 请参阅完整的Instrument API 参考。
Live trading
实时交易
Live integration adapters have defined InstrumentProvider classes which work in an automated way to cache the latest instrument definitions for the exchange. Refer to a particular Instrument object by passing the matching InstrumentId to data and execution related methods, and classes which require one. 实时集成适配器定义了 InstrumentProvider 类,这些类以自动化的方式缓存交易所最新的Instrument定义。通过将匹配的 InstrumentId 传递给数据和执行相关方法及需要的类,引用特定的 Instrument 对象。
Finding instruments
查找Instrument
Since the same actor/strategy classes can be used for both backtest and live trading, you can get instruments in exactly the same way through the central cache: 由于相同的 actor/strategy 类可以用于回测和实时交易,您可以通过中央缓存以完全相同的方式获取Instrument:
from nautilus_trader.model.identifiers import InstrumentId
instrument_id = InstrumentId.from_str("ETHUSDT-PERP.BINANCE")
instrument = self.cache.instrument(instrument_id)
It's also possible to subscribe to any changes to a particular instrument: 还可以订阅对特定Instrument的任何更改:
self.subscribe_instrument(instrument_id)
Or subscribe to all instrument changes for an entire venue: 或订阅整个交易所的所有Instrument更改:
from nautilus_trader.model.identifiers import Venue
binance = Venue("BINANCE")
self.subscribe_instruments(binance)
When an update to the instrument(s) is received by the DataEngine, the object(s) will be passed to the actors/strategies on_instrument() method. A user can override this method with actions to take upon receiving an instrument update: 当 DataEngine 接收到对Instrument的更新时,对象将被传递给 actors/strategies 的 on_instrument() 方法。用户可以重写此方法,以便在接收到Instrument更新时采取行动:
def on_instrument(instrument: Instrument) -> None:
# Take some action on an instrument update
pass
Precisions and increments
精度和增量
The instrument objects are a convenient way to organize the specification of an instrument through read-only properties. Correct price and quantity precisions, as well as minimum price and size increments, multipliers, and standard lot sizes, are available. Instrument对象是一种方便的方式,通过只读属性组织Instrument的规范。可以提供正确的价格和数量精度,以及最小价格和大小增量、乘数和标准手数。
note
备注
Most of these limits are checked by the Nautilus RiskEngine, otherwise invalid values for prices and quantities can result in the exchange rejecting orders. 大多数这些限制由 Nautilus RiskEngine 检查,否则价格和数量的无效值可能导致交易所拒绝订单。
Limits
限制
Certain value limits are optional for instruments and can be None, these are exchange dependent and can include: 某些值限制对Instrument来说是可选的,可以为 None,这取决于交易所,可能包括:
- max_quantity (maximum quantity for a single order)
- min_quantity (minimum quantity for a single order)
- max_notional (maximum value of a single order)
- min_notional (minimum value of a single order)
- max_price (maximum valid quote or order price)
- min_price (minimum valid quote or order price)
note
备注
Most of these limits are checked by the Nautilus RiskEngine, otherwise exceeding published limits can result in the exchange rejecting orders. 大多数这些限制由 Nautilus RiskEngine 检查,否则超出公布的限制可能导致交易所拒绝订单。
Prices and quantities
价格和数量
Instrument objects also offer a convenient way to create correct prices and quantities based on given values. Instrument对象还提供了一种方便的方法,根据给定值创建正确的价格和数量。
instrument = self.cache.instrument(instrument_id)
price = instrument.make_price(0.90500)
quantity = instrument.make_qty(150)
tip
提示
The above is the recommended method for creating valid prices and quantities, such as when passing them to the order factory to create an order. 上述方法是创建有效价格和数量的推荐方法,例如在将它们传递给订单工厂以创建订单时。
Margins and fees
保证金和费用
The current initial and maintenance margin requirements, as well as any trading fees are also available from an instrument: 当前的初始和维持保证金要求以及任何交易费用也可以通过Instrument获得:
- margin_init (initial/order margin rate)
- margin_maint (maintenance/position margin rate)
- maker_fee (the fee percentage applied to notional order values when providing liquidity)
- taker_fee (the fee percentage applied to notional order values when demanding liquidity)
Additional info
附加信息
The raw instrument definition as provided by the exchange (typically from JSON serialized data) is also included as a generic Python dictionary. This is to retain all information which is not necessarily part of the unified Nautilus API, and is available to the user at runtime by calling the .info property. 交易所提供的原始Instrument定义(通常来自 JSON 序列化数据)也作为通用 Python 字典包含。这是为了保留所有不一定是统一 Nautilus API 的信息,并且可以通过调用 .info 属性在运行时提供给用户。