Skip to main content

BotConfig

Struct BotConfig 

Source
pub struct BotConfig {
    pub name: String,
    pub symbols: Vec<Symbol>,
    pub shutdown_timeout: Duration,
    pub install_signal_handler: bool,
    pub market_bus_capacity: usize,
    pub signal_bus_capacity: usize,
    pub close_positions_on_shutdown: bool,
    pub risk: RiskConfig,
    pub per_symbol_risk: HashMap<Symbol, RiskConfig>,
    pub per_class_risk: HashMap<AssetClass, RiskConfig>,
    pub portfolio: PortfolioRiskConfig,
    pub bracket_failure_policy: BracketFailurePolicy,
}
Expand description

Configuration for a Bot.

Construct via BotConfig::builder. The builder validates every field on BotConfigBuilder::build and returns Error::Config on any violation — the framework never panics on bad config. Bot::new does a final brain-count check on top.

§Example

use std::time::Duration;
use rustrade::BotConfig;

let config = BotConfig::builder()
    .name("market-maker")
    .symbols(["BTCUSDT", "ETHUSDT"])
    .shutdown_timeout(Duration::from_secs(5))
    .without_signal_handler() // tests + embedded use
    .build()
    .unwrap();

assert_eq!(config.name, "market-maker");
assert_eq!(config.symbols.len(), 2);

Fields§

§name: String

Human-readable name used in logs, tracing spans, and supervisor service identification.

§symbols: Vec<Symbol>

Symbols this bot trades. Every symbol gets a pre-seeded entry in the risk-state map and the position cache. Must be non-empty — the position cache and risk-state map would otherwise be empty, which is a silent footgun.

§shutdown_timeout: Duration

Maximum time to wait for services to drain on shutdown. Must be > 0; the supervisor’s drain logic needs a non-zero deadline.

§install_signal_handler: bool

Whether the supervisor installs its own Ctrl-C / SIGTERM handler. Disable when the host service drives shutdown via BotHandle::shutdown.

§market_bus_capacity: usize

Capacity of the in-process market-data broadcast bus. Backed by tokio::sync::broadcast, which has drop-oldest semantics: a slow subscriber that falls behind by more than capacity events sees RecvError::Lagged(n) and the oldest dropped events are gone. Size this to absorb the worst-case latency between publish and slowest subscriber’s recv.

§signal_bus_capacity: usize

Capacity of the in-process signal broadcast bus. Same drop-oldest semantics as market_bus_capacity. Typically smaller — signals are emitted ~once per non-Hold decision, far less frequent than market events.

§close_positions_on_shutdown: bool

On shutdown, attempt to close any open position for each symbol before exit, using ExchangeClient::close_position. Best-effort: failures are logged but do not propagate.

§risk: RiskConfig

Risk-layer defaults applied to every configured symbol that has no entry in Self::per_symbol_risk.

§per_symbol_risk: HashMap<Symbol, RiskConfig>

Per-symbol risk overrides. A symbol present here uses its own RiskConfig (session-PnL cap, circuit breaker, and sizing) instead of Self::risk — e.g. a tighter drawdown cap on a volatile alt, or a larger size on a flagship symbol. Symbols absent here use the default. Resolve with Self::resolve_risk.

§per_class_risk: HashMap<AssetClass, RiskConfig>

Per-AssetClass risk overrides. A symbol whose InstrumentSpec reports a class present here uses that class’s RiskConfig — unless a per-symbol override also exists, which wins. Lets one bot apply crypto-perp / spot / FX / futures rules side by side. See RiskConfig::preset_for for starting presets and Self::resolve_risk for the precedence.

§portfolio: PortfolioRiskConfig

Account-wide risk applied across all symbols: a daily-loss halt, a max-concurrent-positions cap, and a gross-exposure cap. Complements the per-symbol RiskConfig gates. Defaults to all-off (opt-in), so a bot that doesn’t set it behaves exactly as before.

§bracket_failure_policy: BracketFailurePolicy

What to do when a bracket entry fills but its protective stop-loss leg fails to place. Defaults to BracketFailurePolicy::CloseEntry: the unprotected entry is closed with a reduce-only market order.

Implementations§

Source§

impl BotConfig

Source

pub fn builder() -> BotConfigBuilder

Begin building a config with sensible defaults.

Source

pub fn risk_for(&self, symbol: &Symbol) -> &RiskConfig

The effective RiskConfig for symbol, ignoring asset class: its per-symbol override if set, else the bot-wide default (Self::risk). Prefer Self::resolve_risk, which also honours per-class overrides.

Source

pub fn resolve_risk( &self, symbol: &Symbol, asset_class: AssetClass, ) -> &RiskConfig

The effective RiskConfig for symbol of the given asset_class, applying the precedence per-symbol → per-class → default. The framework resolves asset_class from ExchangeClient::instrument_spec at startup, so a multi-asset bot gets the right rules per symbol without listing every one.

Trait Implementations§

Source§

impl Clone for BotConfig

Source§

fn clone(&self) -> BotConfig

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BotConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more