NautilusTrader Docs · ภาษาไทย

Rust

Nautilus มี implementation ที่สมบูรณ์ด้วย Rust อยู่ใต้ไดเรกทอรี crates/ คุณเขียน actor, strategy, รัน backtest และทำ live trading ได้โดยไม่ต้องใช้ Python เลย

Rust API ยังอยู่ระหว่างการพัฒนาอย่างต่อเนื่อง signature ของ method และข้อกำหนดของ trait อาจเปลี่ยนแปลงได้ระหว่าง release

ส่วนประกอบของระบบ

implementation แบบ Rust ล้วนอยู่ใต้ crates/ และรันได้โดยไม่ต้องมี Python runtime ให้ performance แบบ native และมีฟังก์ชันการเทรดหลักครบทั้งหมด เหมาะกับ deployment ที่อ่อนไหวต่อ latency หรือทีมที่ชอบภาษาแบบ compile

ตารางความสามารถ

ComponentRust
Strategy✓
Actor✓
DataEngine✓
ExecutionEngine✓
RiskEngine✓
BacktestEngine✓
BacktestNode✓
LiveNode✓
OrderEmulator✓
Matching engine✓
Portfolio✓
Accounts✓
Cache✓
MessageBus✓
Data catalog✓
Indicators✓
Exec algorithmsTWAP
Controller-
Tearsheets-

runtime ของ Controller เขียนด้วย Rust แต่ตารางระบุว่าไม่มีใน Rust เพราะเส้นทางการลงทะเบียนที่รองรับ (controller config ที่ import ได้) ยังใช้จาก Rust ไม่ได้

Adapters

AdapterRust
Architect AX✓
Betfair✓
Binance✓
Blockchain✓
Bybit✓
Coinbase✓
Databento✓
Deribit✓
Derive✓
dYdX✓
Hyperliquid✓
Interactive Brokers✓
Kraken✓
Lighter✓
OKX✓
Polymarket✓
Sandbox✓
Tardis✓

การตั้งค่าโปรเจกต์

crate ของ Nautilus publish อยู่บน crates.io เพิ่มลงใน Cargo.toml ของคุณ:

[dependencies]
nautilus-backtest = "0.66"
nautilus-common = "0.66"
nautilus-execution = "0.66"
nautilus-model = { version = "0.66", features = ["test-support"] }
nautilus-trading = { version = "0.66", features = ["examples"] }

anyhow = "1"
log = "0.4"

สำหรับ live trading ให้เพิ่ม crate live และ adapter ของ venue ที่ใช้:

[dependencies]
nautilus-live = "0.66"
nautilus-okx = "0.66"

ถ้าต้องการตาม branch พัฒนาล่าสุด ให้ชี้ dependency ของ Nautilus ทั้งหมดไปที่ git source เดียวกัน เพื่อเลี่ยง type ไม่ตรงกันระหว่างเวอร์ชันจาก crates.io กับ git:

[dependencies]
nautilus-backtest = { git = "https://github.com/nautechsystems/nautilus_trader.git", branch = "develop" }
nautilus-common = { git = "https://github.com/nautechsystems/nautilus_trader.git", branch = "develop" }
nautilus-execution = { git = "https://github.com/nautechsystems/nautilus_trader.git", branch = "develop" }
nautilus-model = { git = "https://github.com/nautechsystems/nautilus_trader.git", branch = "develop", features = ["test-support"] }
nautilus-trading = { git = "https://github.com/nautechsystems/nautilus_trader.git", branch = "develop", features = ["examples"] }

Rust เวอร์ชันต่ำสุดที่รองรับ (MSRV) คือ 1.99.1

Feature flags

FlagCrateผล
high-precisionnautilus-modelfixed precision 16 หลัก (ค่า default คือ 9)
test-supportnautilus-modeltest fixture, builder, spec และค่า default
examplesnautilus-tradingstrategy ตัวอย่าง (EmaCross, GridMarketMaker)
streamingnautilus-backtestการ stream ข้อมูลจาก catalog ผ่าน BacktestNode
definautilus-modeldata type สำหรับ DeFi เปิด high-precision ให้ด้วย

precision มาตรฐาน 9 หลักเพียงพอสำหรับ instrument การเงินแบบดั้งเดิมส่วนใหญ่ เปิด high-precision สำหรับ venue crypto ที่ราคาหรือ quantity ต้องการทศนิยมมากกว่าเก้าตำแหน่ง

Memory allocator

CLI nautilus ใช้ mimalloc สำหรับการจองหน่วยความจำของ Rust binary ของ Rust จะเลือก allocator ของตัวเอง ดังนั้นให้เพิ่ม mimalloc ลงใน binary ของคุณเพื่อให้ตรงกัน:

[dependencies]
mimalloc = "0.1"
use mimalloc::MiMalloc;
use nautilus_common::logging::headers::register_allocator_mimalloc;

#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

fn main() {
    register_allocator_mimalloc();
}

การประกาศ GLOBAL คือการเลือกใช้ mimalloc ให้เรียก register_allocator_mimalloc ตอนต้นของ main ก่อน สร้าง Nautilus node เพื่อให้ version header รายงาน allocator: mimalloc <version> การลงทะเบียนนี้ อัปเดตเฉพาะ metadata ใน header ไม่ได้เป็นการเลือก allocator

system allocator แบบ default ก็ใช้ได้เช่นกัน เมื่อเปรียบเทียบ allocator ให้วัด throughput และ resident memory บน workload และ platform ของคุณเอง ดูพื้นหลังเพิ่มเติมได้ที่ architecture guide

Actors

actor รับ market data, custom data/signal และ system event แต่ไม่จัดการ order ให้ implement trait DataActor และใช้ nautilus_actor! เพื่อเชื่อมฟิลด์ DataActorCore ของคุณเข้ากับ runtime contract type ของคุณต้อง implement หรือ derive Debug ส่วน macro จะจัดการการเชื่อมต่อกับ native runtime ให้

โดยปกติโค้ดของผู้ใช้จะใช้ facade method ของ DataActor สำหรับการ subscribe, การเข้าถึง cache และ clock

Handler methods

override handler ใดก็ได้บน trait DataActor เพื่อรับข้อมูลหรือ event ที่ตรงกัน handler ทุกตัวมี implementation default ที่ไม่ทำอะไร (no-op) คุณจึง override เฉพาะที่ต้องใช้

Handlerรับอะไร
on_startactor เริ่มทำงาน
on_stopactor หยุดทำงาน
on_quoteQuoteTick
on_tradeTradeTick
on_barBar
on_book_deltasOrderBookDeltas
on_bookOrderBook (ตาม interval)
on_instrumentInstrumentAny
on_mark_priceMarkPriceUpdate
on_index_priceIndexPriceUpdate
on_funding_rateFundingRateUpdate
on_option_greeksOptionGreeks
on_option_chainOptionChainSlice
on_instrument_statusInstrumentStatus
on_time_eventTimeEvent

ดูคำอธิบายทีละขั้นได้ที่คู่มือ เขียน Actor (Rust) และดูตัวอย่างที่สมบูรณ์ได้ที่ BookImbalanceActor

Strategies

strategy ขยาย actor ด้วยความสามารถในการจัดการ order ให้ implement DataActor สำหรับการจัดการข้อมูล และใช้ nautilus_strategy! เพื่อเชื่อมฟิลด์ StrategyCore ของคุณเข้ากับ strategy runtime contract StrategyCore เก็บ state ของ strategy ตอน runtime และการลงทะเบียนกับ runtime ต้องใช้ native wiring ที่ macro สร้างให้

logic ของ strategy ตามปกติใช้ method ของ Strategy และ facade method บน self เพื่อเข้าถึง state นั้น strategy ยัง override order event handler บน trait Strategy ได้ด้วย เช่น on_order_filled (OrderFilled) และ on_order_canceled (OrderCanceled)

การจัดการ order

trait Strategy มี method สำหรับ order ผ่าน facade:

Methodการทำงาน
submit_ordersubmit order ใหม่ไปยัง venue
submit_order_listsubmit รายการ contingent order
modify_orderแก้ไข price, quantity หรือ trigger price
cancel_ordercancel order ที่ระบุ
cancel_orderscancel ชุด order ที่กรองไว้
cancel_all_orderscancel order ทั้งหมดของ instrument หนึ่ง
close_positionปิด position ด้วย market order
close_all_positionsปิด position ที่เปิดอยู่ทั้งหมด

OrderApi (เข้าถึงผ่าน self.order()) ใช้สร้าง order และ order list:

  • generate_client_order_id
  • generate_order_list_id
  • market
  • limit
  • stop_market
  • stop_limit
  • market_to_limit
  • market_if_touched
  • limit_if_touched
  • trailing_stop_market
  • trailing_stop_limit
  • bracket
  • create_list

การเข้าถึง cache และ clock

การเข้าถึง cache และ clock รายงาน borrow conflict ผ่าน interface ที่ต่างกัน:

  • lookup แบบ try_* ของ cache facade คืน variant Access ของ lookup error เมื่อเกิด borrow conflict ส่วน NotFound ยังคงหมายถึงไม่มีข้อมูลที่ร้องขอ
  • method get() ของ cache facade และ method ตั้งเวลาของ clock ที่ใช้ native backend คืน access error ผ่าน anyhow::Result
  • accessor แบบ infallible ที่มีอยู่เดิมยังคง panic เมื่อเกิด conflict

ให้ปล่อย borrow ที่ขัดแย้งกันก่อน retry error เหล่านี้ไม่ได้เลื่อน callback หรือเปลี่ยนลำดับการส่ง callback

Macro สำหรับเชื่อม core

actor, strategy และ execution algorithm ของ Rust เก็บ runtime core ไว้เป็นฟิลด์ของ struct macro จะบอก trait ว่าฟิลด์นั้นอยู่ที่ไหน

MacroCore fieldสร้างอะไร
nautilus_actor!(Type)DataActorCoreRuntime wiring
nautilus_strategy!(Type)StrategyCoreRuntime wiring และ Strategy
nautilus_execution_algorithm!(Type, { ... })ExecutionAlgorithmCoreRuntime wiring และ algorithm

macro คาดหวังฟิลด์ชื่อ core ถ้าจำเป็นให้ส่งชื่อฟิลด์เป็น argument ตัวที่สอง macro ไม่ได้ทำให้ actor, strategy หรือ StrategyCore deref ไปยังส่วนภายในของ runtime macro ของ execution algorithm รับ implementation block ของ on_order() เพราะ method นั้นนิยามการจัดการ order ที่ algorithm ต้องมี

Native trait

โดย default ให้ใช้ facade method:

  • actor_id()
  • trader_id()
  • is_registered()
  • config()
  • strategy_id()
  • clock()
  • cache()
  • order()
  • portfolio()

DataActorNative, StrategyNative และ ExecutionAlgorithmNative มีไว้สำหรับการเข้าถึงแบบ native-only ที่อยู่ ต่ำกว่า facade หัวข้อนี้อธิบายโค้ด Rust แบบ native สำหรับ engine, runtime และ path ที่อ่อนไหวต่อ latency อย่างชัดเจน ไม่ใช่วิธีเขียน component แบบ portable ตามปกติ

วิธีเขียนใช้ native trait?API ปกติ
Native Rust binaryเฉพาะเมื่อจำเป็นfacade ของ Strategy และ DataActor

native trait เปิดให้เข้าถึง core state แบบ borrow, Rc<RefCell<_>> และ reference ของ runtime ใช้เมื่อโค้ด Rust แบบ native ตั้งใจยอมรับกฎการ borrow เหล่านั้นสำหรับ path ที่อ่อนไหวต่อ latency อย่างชัดเจน

โค้ดของ engine, runtime, registration และ testkit สามารถ import DataActorNative, StrategyNative หรือ ExecutionAlgorithmNative เมื่อต้องเข้าถึง actor core, strategy core หรือ execution algorithm core อย่าใช้ใน logic ทั่วไปของ actor, strategy หรือ execution algorithm แบบ portable

ExecutionAlgorithmCore เป็นเจ้าของ DataActorCore แต่ไม่ได้ deref ไปยังมัน logic ปกติของ execution algorithm ควรใช้ id(), actor_id(), trader_id(), clock() และ cache() ใช้ ExecutionAlgorithmNative เฉพาะเมื่อโค้ดต้องการ state ของ execution algorithm แบบ native

ให้เลือก native handle ที่เล็กที่สุดและจำกัดขอบเขตของแต่ละ borrow ใช้ order() สำหรับการสร้าง order ตามปกติของ strategy และใช้ order_factory() เฉพาะเมื่อโค้ด native ต้องการ borrow แบบ mutable ของ factory โดยตรง

Method ของ DataActorNative

Native methodReturn shapeใช้เมื่อ
core()&DataActorCoreอ่านส่วนภายในของ actor
core_mut()&mut DataActorCoreแก้ไขส่วนภายในของ actor
clock_mut()RefMut<'_, dyn Clock>ต้องการ borrow clock แบบ mutable
clock_rc()Rc<RefCell<dyn Clock>>เก็บหรือส่งต่อ clock ที่ใช้ร่วมกัน
cache_ref()Ref<'_, Cache>ต้องการอ่าน live cache แบบสั้น ๆ
cache_rc()Rc<RefCell<Cache>>แก้ไข เก็บ หรือส่งต่อ cache

ใช้ try_cache_ref() และ try_clock_mut() เพื่อจัดการ native borrow ที่ล้มเหลวโดยไม่ panic ให้ import ComponentAccessError จาก nautilus_common::component:

  • try_cache_ref() คืน ComponentAccessError::ReadConflict เมื่อ cache ถูก borrow แบบ mutable อยู่
  • try_clock_mut() คืน ComponentAccessError::WriteConflict เมื่อ clock ถูก borrow อยู่แล้ว
  • ทั้งคู่คืน ComponentAccessError::NotRegistered เมื่อการลงทะเบียนยังไม่ได้จัดหา resource นั้นให้

error จะระบุ resource และการทำงานที่พยายามทำ การ reentry ของ callback อาจทำให้เกิด conflict ได้ แต่ conflict เพียงอย่างเดียวไม่ได้บอกสาเหตุ ดูความหมายของข้อความและวิธีแก้ได้ที่ reentrancy diagnostics

Method ของ StrategyNative

Native methodReturn shapeใช้เมื่อ
strategy_core()&StrategyCoreอ่านส่วนภายในของ strategy
strategy_core_mut()&mut StrategyCoreแก้ไขส่วนภายในของ strategy
order_factory()RefMut<'_, OrderFactory>ต้องการ borrow factory แบบ mutable โดยตรง
order_factory_rc()Rc<RefCell<OrderFactory>>เก็บหรือส่งต่อ factory
portfolio_rc()Rc<RefCell<Portfolio>>เก็บหรือส่งต่อ portfolio

Method ของ ExecutionAlgorithmNative

Native methodReturn shapeใช้เมื่อ
exec_algorithm_core()&ExecutionAlgorithmCoreอ่านส่วนภายในของ execution algorithm
exec_algorithm_core_mut()&mut ExecutionAlgorithmCoreแก้ไขส่วนภายในของ execution algorithm

ดูคำอธิบายทีละขั้นได้ที่คู่มือ เขียน Strategy (Rust) และดูตัวอย่างที่สมบูรณ์ได้ที่ EmaCross และ GridMarketMaker

การรัน Rust component

ตัวอย่างด้านล่างใช้ strategy แต่ใช้รูปแบบเดียวกันกับ actor ผ่าน add_actor

Rust ล้วน

เขียน strategy และฟังก์ชัน main ด้วย Rust แล้ว build เป็น binary แบบ standalone ด้วย cargo build วิธีนี้ไม่ต้องมี Python runtime

let strategy = GridMarketMaker::new(config);
node.add_strategy(strategy)?;
node.run().await?;

ดูคำอธิบายฉบับเต็มได้ที่ Run Live Trading (Rust)

Backtesting

ดูคำอธิบายพร้อม annotation ของทั้งสอง API ได้ที่คู่มือ Run a Backtest (Rust)

BacktestEngine (API ระดับล่าง)

สร้าง engine เพิ่ม venue และ instrument โหลดข้อมูล ลงทะเบียน strategy แล้วรัน ดูตัวอย่างที่ใช้งานได้ฉบับเต็ม:

cargo run -p nautilus-backtest --features examples --example engine-ema-cross

Source: crates/backtest/examples/engine_ema_cross.rs

BacktestNode (API ระดับสูง)

โหลดข้อมูลจาก ParquetDataCatalog และรองรับการ stream เป็น chunk ที่ตั้งขนาดได้ ต้องเปิด feature streaming บน nautilus-backtest ดูตัวอย่างที่ใช้งานได้ฉบับเต็ม:

cargo run -p nautilus-backtest --features examples,streaming --example node-ema-cross

Source: crates/backtest/examples/node_ema_cross.rs

Live trading

ดูคำอธิบายพร้อม annotation ได้ที่คู่มือ Run Live Trading (Rust)

LiveNode เชื่อมต่อกับ venue และแหล่งข้อมูลจริงผ่าน adapter client โดยใช้ builder pattern ตั้งค่า data client และ execution client จากนั้น run() จะเริ่ม async event loop adapter แต่ละตัวมี factory และ config type ของตัวเอง

AdapterExample
Architect AXcrates/adapters/architect_ax/examples/
Betfaircrates/adapters/betfair/examples/
Binancecrates/adapters/binance/examples/
Blockchaincrates/adapters/blockchain/examples/
Bybitcrates/adapters/bybit/examples/
Coinbasecrates/adapters/coinbase/examples/
Databentocrates/adapters/databento/examples/
Deribitcrates/adapters/deribit/examples/
Derivecrates/adapters/derive/examples/
dYdXcrates/adapters/dydx/examples/
Hyperliquidcrates/adapters/hyperliquid/examples/
Interactive Brokerscrates/adapters/interactive_brokers/examples/
Krakencrates/adapters/kraken/examples/
Lightercrates/adapters/lighter/examples/
OKXcrates/adapters/okx/examples/
Polymarketcrates/adapters/polymarket/examples/
Sandboxcrates/adapters/sandbox/examples/
Tardiscrates/adapters/tardis/examples/

adapter ส่วนใหญ่มีตัวอย่าง node_data_tester.rs และ node_exec_tester.rs ซึ่งทดสอบการ request ข้อมูล การ stream และการ execute order กับ venue จริง

  • เขียน Actor (Rust) - คำอธิบาย actor ทีละขั้น
  • เขียน Strategy (Rust) - คำอธิบาย strategy ทีละขั้น
  • Run a Backtest (Rust) - การใช้ BacktestEngine และ BacktestNode
  • Run Live Trading (Rust) - การตั้งค่า LiveNode และการเชื่อมต่อ venue
  • Architecture - การออกแบบระบบและ flow ของ data/execution
  • Actors - แนวคิดของ actor
  • Strategies - แนวคิดของ strategy และ reference ของ handler
  • Events - ประเภทของ event และการ dispatch handler
  • Backtesting - แนวคิดของ backtest และพฤติกรรมของ matching engine

แปลจาก เอกสาร NautilusTrader © Nautech Systems Pty Ltd และผู้ร่วมพัฒนา เผยแพร่ภายใต้ LGPL-3.0 ฉบับแปลนี้ตัดเนื้อหาเฉพาะ Python ออกและเน้น Rust