pub struct OrderManager { /* private fields */ }Implementations§
Source§impl OrderManager
impl OrderManager
Sourcepub fn new(
rest_client: Arc<BinanceRestClient>,
symbol: &str,
market: MarketKind,
order_amount_usdt: f64,
risk_config: &RiskConfig,
) -> Self
pub fn new( rest_client: Arc<BinanceRestClient>, symbol: &str, market: MarketKind, order_amount_usdt: f64, risk_config: &RiskConfig, ) -> Self
Create a new order manager bound to a single symbol/market context.
The instance keeps in-memory position, cached balances, and an embedded
RiskModule that enforces pre-trade checks and global rate budget.
§Caution
This manager is stateful (last_price, balances, active orders). Reuse
the same instance for a symbol stream instead of recreating per tick.
Sourcepub fn position(&self) -> &Position
pub fn position(&self) -> &Position
Return current in-memory position snapshot.
Values reflect fills processed by this process. They are not a full exchange reconciliation snapshot.
pub fn market_kind(&self) -> MarketKind
Sourcepub fn balances(&self) -> &HashMap<String, f64>
pub fn balances(&self) -> &HashMap<String, f64>
Return latest cached free balances.
Cache is updated by refresh_balances. Missing assets should be treated
as zero balance.
Sourcepub fn update_unrealized_pnl(&mut self, current_price: f64)
pub fn update_unrealized_pnl(&mut self, current_price: f64)
Update last price and recompute unrealized PnL.
§Usage
Call on every market data tick before submit_order, so risk checks use
a valid last_price.
pub fn last_price(&self) -> Option<f64>
pub fn open_order_count(&self) -> usize
pub fn reserved_cash_usdt(&self) -> f64
Sourcepub fn rate_budget_snapshot(&self) -> RateBudgetSnapshot
pub fn rate_budget_snapshot(&self) -> RateBudgetSnapshot
Return current global rate-budget snapshot from the risk module.
Intended for UI display and observability.
pub fn orders_rate_budget_snapshot(&self) -> RateBudgetSnapshot
pub fn account_rate_budget_snapshot(&self) -> RateBudgetSnapshot
pub fn market_data_rate_budget_snapshot(&self) -> RateBudgetSnapshot
Sourcepub fn would_exceed_symbol_exposure_limit(
&self,
side: OrderSide,
qty: f64,
) -> bool
pub fn would_exceed_symbol_exposure_limit( &self, side: OrderSide, qty: f64, ) -> bool
Return whether a hypothetical fill would exceed symbol exposure limit.
This is intended for validation and tests; it does not mutate state.
Sourcepub async fn refresh_balances(&mut self) -> Result<HashMap<String, f64>>
pub async fn refresh_balances(&mut self) -> Result<HashMap<String, f64>>
Fetch account balances from Binance and update internal state.
Returns the map asset -> free for assets with non-zero total (spot) or
non-trivial wallet balance (futures).
§Usage
Refresh before order submission cycles to reduce false “insufficient balance” rejections from stale cache.
§Caution
Network/API failures return Err(_) and leave previous cache untouched.
Sourcepub async fn refresh_order_history(
&mut self,
limit: usize,
) -> Result<OrderHistorySnapshot>
pub async fn refresh_order_history( &mut self, limit: usize, ) -> Result<OrderHistorySnapshot>
Fetch order history from exchange and format rows for UI display.
This method combines order and trade endpoints, persists snapshots to local sqlite, and emits a best-effort history view even if one endpoint fails.
§Caution
trade_data_complete = false means derived PnL may be partial.
Sourcepub async fn submit_order(
&mut self,
signal: Signal,
source_tag: &str,
) -> Result<Option<OrderUpdate>>
pub async fn submit_order( &mut self, signal: Signal, source_tag: &str, ) -> Result<Option<OrderUpdate>>
Build an order intent, run risk checks, and submit to broker when approved.
§Behavior
Signal::HoldreturnsOk(None).- For buy/sell signals, this method:
- Builds
OrderIntent. - Calls
RiskModule::evaluate_intent. - Reserves one global rate token via
reserve_rate_budget. - Submits market order to spot/futures broker endpoint.
- Builds
- Rejections are returned as
Ok(Some(OrderUpdate::Rejected { .. }))with structuredreason_code.
§Usage
Recommended sequence:
update_unrealized_pnl(last_price)refresh_balances()(periodic or before trading loop)submit_order(signal, source_tag)
§Caution
- Spot sell requires base-asset balance (e.g.
ETHforETHUSDT). - If balances are stale, you may see “No position to sell” or
"Insufficient <asset>"even though exchange state changed recently. - This method returns transport/runtime errors as
Err(_); business rejections are encoded inOrderUpdate::Rejected.
Sourcepub async fn place_protective_stop_for_open_position(
&mut self,
source_tag: &str,
stop_price: f64,
) -> Result<Option<String>>
pub async fn place_protective_stop_for_open_position( &mut self, source_tag: &str, stop_price: f64, ) -> Result<Option<String>>
Attempt to place a protective stop for the currently open position.
Futures: submits STOP_MARKET reduce-only order.
Spot: currently returns Ok(None) (spot stop order path is not yet wired).
Sourcepub async fn ensure_protective_stop(
&mut self,
source_tag: &str,
fallback_stop_price: f64,
) -> Result<bool>
pub async fn ensure_protective_stop( &mut self, source_tag: &str, fallback_stop_price: f64, ) -> Result<bool>
Ensure a protective stop exists for open position.
Returns:
Ok(true)when flat (no protection needed) or placement succeededOk(false)when protection was needed but could not be placed
Sourcepub async fn emergency_close_position(
&mut self,
source_tag: &str,
reason_code: &str,
) -> Result<Option<OrderUpdate>>
pub async fn emergency_close_position( &mut self, source_tag: &str, reason_code: &str, ) -> Result<Option<OrderUpdate>>
Emergency close helper for runtime/system-triggered liquidation paths.
If position is already flat, returns Ok(None) without broker call.
Auto Trait Implementations§
impl Freeze for OrderManager
impl !RefUnwindSafe for OrderManager
impl Send for OrderManager
impl Sync for OrderManager
impl Unpin for OrderManager
impl UnsafeUnpin for OrderManager
impl !UnwindSafe for OrderManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more