Synthetic Instruments - Loren1166/NautilusTrader- GitHub Wiki
Synthetic Instruments 合成交易品种
The platform supports creating customized synthetic instruments, which can generate synthetic quote and trade ticks. These are useful for:
该平台支持创建自定义合成交易品种,这些品种可以生成合成报价和交易。这些对于以下方面很有用:
- Enabling Actor and Strategy components to subscribe to quote or trade feeds. 使参与者和策略组件能够订阅报价或交易源。
- Triggering emulated orders. 触发模拟订单。
- Constructing bars from synthetic quotes or trades. 从合成报价或交易构造K线。
Synthetic instruments cannot be traded directly, as they are constructs that only exist locally within the platform. They serve as analytical tools, providing useful metrics based on their component instruments.
合成交易品种不能直接交易,因为它们只是平台内本地存在的构造。它们充当分析工具,根据其成分标的提供有用的指标。
In the future, we plan to support order management for synthetic instruments, enabling trading of their component instruments based on the synthetic instrument's behavior.
将来,我们计划支持合成交易品种的订单管理,从而能够根据合成交易品种的行为对其成分标的进行交易。
info 信息
The venue for a synthetic instrument is always designated as 'SYNTH'.
合成交易品种的交易平台始终指定为“SYNTH”。
Formula 公式
A synthetic instrument is composed of a combination of two or more component instruments (which can include instruments from multiple venues), as well as a "derivation formula". Utilizing the dynamic expression engine powered by the
evalexpr
Rust crate, the platform can evaluate the formula to calculate the latest synthetic price tick from the incoming component instrument prices.合成交易品种由两个或多个成分标的(可以包括来自多个交易平台的标的)以及“派生公式”的组合组成。利用由
evalexpr
Rust crate 提供支持的动态表达式引擎,该平台可以评估公式以根据传入的成分标的价格计算最新的合成价格。
See the evalexpr documentation for a full description of available features, operators and precedence.
有关可用功能、运算符和优先级的完整说明,请参阅 evalexpr 文档。
tip 提示
Before defining a new synthetic instrument, ensure that all component instruments are already defined and exist in the cache.
在定义新的合成交易品种之前,请确保所有成分标的都已定义并存在于缓存中。
Subscribing 订阅
The following example demonstrates the creation of a new synthetic instrument with an actor/strategy. This synthetic instrument will represent a simple spread between Bitcoin and Ethereum spot prices on Binance. For this example, it is assumed that spot instruments for
BTCUSDT.BINANCE
andETHUSDT.BINANCE
are already present in the cache.以下示例演示了如何使用参与者/策略创建新的合成交易品种。此合成交易品种将表示 Binance 上比特币和以太坊现货价格之间的简单价差。对于此示例,假设
BTCUSDT.BINANCE
和ETHUSDT.BINANCE
的现货Instrument已存在于缓存中。
from nautilus_trader.model.instruments import SyntheticInstrument
btcusdt_binance_id = InstrumentId.from_str("BTCUSDT.BINANCE")
ethusdt_binance_id = InstrumentId.from_str("ETHUSDT.BINANCE")
# Define the synthetic instrument
# 定义合成交易品种
synthetic = SyntheticInstrument(
symbol=Symbol("BTC-ETH:BINANCE"),
price_precision=8,
components=[
btcusdt_binance_id,
ethusdt_binance_id,
],
formula=f"{btcusdt_binance_id} - {ethusdt_binance_id}",
ts_event=self.clock.timestamp_ns(),
ts_init=self.clock.timestamp_ns(),
)
# Recommended to store the synthetic instruments ID somewhere
# 建议将合成交易品种 ID 存储在某个位置
self._synthetic_id = synthetic.id
# Add the synthetic instrument for use by other components
# 添加合成交易品种以供其他组件使用
self.add_synthetic(synthetic)
# Subscribe to quote ticks for the synthetic instrument
# 订阅合成交易品种的报价
self.subscribe_quote_ticks(self._synthetic_id)
note 注意
The
instrument_id
for the synthetic instrument in the above example will be structured as{symbol}.{SYNTH}
, resulting in 'BTC-ETH:BINANCE.SYNTH'.上述示例中合成交易品种的
instrument_id
将被构造为{symbol}.{SYNTH}
,结果为“BTC-ETH:BINANCE.SYNTH”。
Updating formulas 更新公式
It's also possible to update a synthetic instrument formulas at any time. The following example shows how to achieve this with an actor/strategy.
也可以随时更新合成交易品种的公式。以下示例展示了如何使用参与者/策略来实现这一点。
# Recover the synthetic instrument from the cache (assuming `synthetic_id` was assigned)
# 从缓存中恢复合成交易品种(假设已分配 `synthetic_id`)
synthetic = self.cache.synthetic(self._synthetic_id)
# Update the formula, here is a simple example of just taking the average
# 更新公式,这里是一个简单的取平均值的示例
new_formula = "(BTCUSDT.BINANCE + ETHUSDT.BINANCE) / 2"
synthetic.change_formula(new_formula)
# Now update the synthetic instrument
# 现在更新合成交易品种
self.update_synthetic(synthetic)
Trigger instrument IDs 触发Instrument ID
The platform allows for emulated orders to be triggered based on synthetic instrument prices. In the following example, we build upon the previous one to submit a new emulated order. This order will be retained in the emulator until a trigger from synthetic quote ticks releases it. It will then be submitted to Binance as a
MARKET
order:该平台允许基于合成交易品种价格触发模拟订单。在以下示例中,我们在前一个示例的基础上提交了一个新的模拟订单。此订单将保留在模拟器中,直到合成报价的触发将其释放。然后,它将作为
MARKET
订单提交到 Binance:
order = self.strategy.order_factory.limit(
instrument_id=ETHUSDT_BINANCE.id,
order_side=OrderSide.BUY,
quantity=Quantity.from_str("1.5"),
price=Price.from_str("30000.00000000"), # <-- Synthetic instrument price 合成交易品种价格
emulation_trigger=TriggerType.DEFAULT,
trigger_instrument_id=self._synthetic_id, # <-- Synthetic instrument identifier 合成交易品种标识符
)
self.strategy.submit_order(order)
Error handling 错误处理
Considerable effort has been made to validate inputs, including the derivation formula for synthetic instruments. Despite this, caution is advised as invalid or erroneous inputs may lead to undefined behavior.
已经付出了相当大的努力来验证输入,包括合成交易品种的派生公式。尽管如此,还是建议谨慎行事,因为无效或错误的输入可能会导致未定义的行为。
info 信息
See the
SyntheticInstrument
API reference for a detailed understanding of input requirements and potential exceptions.有关输入要求和潜在异常的详细了解,请参阅
SyntheticInstrument
API 参考。
Previous Emulated Orders Next Portfolio Statistics Formula Subscribing Updating formulas Trigger instrument IDs Error handling © 2024 Nautech Systems Pty Ltd. All rights reserved.