Emulated Orders - Loren1166/NautilusTrader- GitHub Wiki
Emulated Orders 模拟订单
The platform makes it possible to emulate most order types locally, regardless of whether the type is supported on a trading venue. The logic and code paths for order emulation are exactly the same for all environment contexts and utilize a common
OrderEmulatorcomponent.该平台可以在本地模拟大多数订单类型,无论交易平台是否支持该类型。所有环境上下文中订单模拟的逻辑和代码路径完全相同,并且使用通用的
OrderEmulator组件。
info 信息
There is no limitation on the number of emulated orders you can have per running instance.
每个正在运行的实例可以拥有的模拟订单数量没有限制。
Submitting for emulation 提交模拟
The only requirement to emulate an order is to pass a
TriggerTypeto theemulation_triggerparameter of anOrderconstructor, orOrderFactorycreation method. The following emulation trigger types are currently supported:模拟订单的唯一要求是将
TriggerType传递给Order构造函数或OrderFactory创建方法的emulation_trigger参数。当前支持以下模拟触发类型:
DEFAULT(which is the same asBID_ASK)DEFAULT(与BID_ASK相同)BID_ASK(emulated using quote ticks)BID_ASK(使用报价模拟)LAST(emulated using trade ticks)LAST(使用交易模拟)
Emulated orders are subject to the same risk controls as 'regular' orders, and can be modified and canceled by a trading strategy in the normal way. They will also be included when canceling all orders.
模拟订单与“常规”订单受到相同的风险控制,并且可以通过交易策略以正常方式进行修改和取消。取消所有订单时,它们也将包含在内。
info 信息
An emulated order will retain its original client order ID throughout its entire life cycle, making it easy to query through the cache.
模拟订单将在其整个生命周期中保留其原始客户端订单 ID,从而可以轻松地通过缓存进行查询。
Life cycle 生命周期
An emulated order will progress through the following stages:
模拟订单将经历以下阶段:
- Submitted by a
Strategythrough thesubmit_ordermethod. 通过submit_order方法由Strategy提交。- Then sent to the
RiskEnginefor pre-trade risk checks (it may be denied at this point). 然后发送到RiskEngine以进行交易前风险检查(此时可能会被拒绝)。- Then sent to the
OrderEmulatorwhere it is held / emulated. 然后发送到OrderEmulator,在那里它被持有/模拟。
Held emulated orders 持有的模拟订单
The following will occur for an emulated order now held by the
OrderEmulatorcomponent:对于现在由
OrderEmulator组件持有的模拟订单,将发生以下情况:
- The original
SubmitOrdercommand will be cached. 原始SubmitOrder命令将被缓存。- The emulated order will be processed inside a local
MatchingCorecomponent. 模拟订单将在本地MatchingCore组件内部进行处理。- The
OrderEmulatorwill subscribe to any needed market data (if not already) to update the matching core.OrderEmulator将订阅任何需要的市场数据(如果尚未订阅)以更新匹配核心。- The emulated order can be modified (by the trader) and updated (by the market) until released or canceled. 在发布或取消之前,模拟订单可以由交易者修改,也可以由市场更新。
Released emulated orders 发布的模拟订单
Once an emulated order is triggered / matched locally based on the arrival of data, the following release actions will occur:
一旦根据数据的到达在本地触发/匹配模拟订单,将发生以下发布操作:
- The order will be transformed to either a
MARKETorLIMITorder (see below table) through an additionalOrderInitializedevent. 该订单将通过一个额外的OrderInitialized事件转换为MARKET或LIMIT订单(见下表)。- The orders
emulation_triggerwill be set toNONE(it will no longer be treated as an emulated order by any component). 订单的emulation_trigger将设置为NONE(它将不再被任何组件视为模拟订单)。- The order attached to the original
SubmitOrdercommand will be sent back to theRiskEnginefor additional checks since any modification/updates. 自任何修改/更新以来,附加到原始SubmitOrder命令的订单将被发送回RiskEngine以进行其他检查。- If not denied, then the command will continue to the
ExecutionEngineand on to the trading venue via anExecutionClientas normal. 如果没有被拒绝,则该命令将继续发送到ExecutionEngine,然后通过ExecutionClient正常发送到交易平台。
The following table lists which order types are possible to emulate, and which order type they transform to when being released for submission to the trading venue.
下表列出了可以模拟的订单类型,以及在发布以提交到交易平台时它们转换为哪种订单类型。
| Order types | Can emulate | Released type |
|---|---|---|
| MARKET | - | |
| MARKET_TO_LIMIT | - | |
| LIMIT | ✓ | MARKET |
| STOP_MARKET | ✓ | MARKET |
| STOP_LIMIT | ✓ | LIMIT |
| MARKET_IF_TOUCHED | ✓ | MARKET |
| LIMIT_IF_TOUCHED | ✓ | LIMIT |
| TRAILING_STOP_MARKET | ✓ | MARKET |
| TRAILING_STOP_LIMIT | ✓ | LIMIT |
Querying 查询
When writing trading strategies, it may be necessary to know the state of emulated orders in the system. It's possible to query for emulated orders through the following
Cachemethods:在编写交易策略时,可能需要了解系统中模拟订单的状态。可以通过以下
Cache方法查询模拟订单:
self.cache.orders_emulated(...)
self.cache.is_order_emulated(...)
self.cache.orders_emulated_count(...)
See the full API reference for additional details.
有关其他详细信息,请参阅完整的 API 参考。
You can also query order objects directly:
您还可以直接查询订单对象:
order.is_emulated
If either of these return
False, then the order has been released from theOrderEmulator, and so is no longer considered an emulated order.如果其中任何一个返回
False,则该订单已从OrderEmulator中释放,因此不再被视为模拟订单。
warning 警告
It's not advised to hold a local reference to an emulated order, as the order object will be transformed when/if the emulated order is released. You should rely on the
Cachewhich is made for the job.不建议保留对模拟订单的本地引用,因为如果/当模拟订单被释放时,订单对象将被转换。您应该依赖于为此工作而设计的
Cache。
Persisted emulated orders 持久化的模拟订单
If a running system either crashes or shuts down with active emulated orders, then they will be reloaded inside the
OrderEmulatorfrom any configured cache database.如果正在运行的系统在具有活动模拟订单的情况下崩溃或关闭,则它们将从任何已配置的缓存数据库重新加载到
OrderEmulator中。