Skip to main content

barter/engine/action/
mod.rs

1use crate::engine::{
2    action::{
3        generate_algo_orders::GenerateAlgoOrdersOutput,
4        send_requests::{SendCancelsAndOpensOutput, SendRequestsOutput},
5    },
6    error::UnrecoverableEngineError,
7};
8use barter_execution::order::request::{RequestCancel, RequestOpen};
9use barter_instrument::{exchange::ExchangeIndex, instrument::InstrumentIndex};
10use barter_integration::collection::one_or_many::OneOrMany;
11use derive_more::From;
12use serde::{Deserialize, Serialize};
13use std::fmt::Debug;
14
15/// Defines the `Engine` action for cancelling open order requests.
16pub mod cancel_orders;
17
18/// Defines the `Engine` action for generating and sending order requests for closing open positions.
19pub mod close_positions;
20
21/// Defines the `Engine` action for generating and sending algorithmic order requests.
22pub mod generate_algo_orders;
23
24/// Defines the `Engine` action for sending order `ExecutionRequests` to the execution manager.
25pub mod send_requests;
26
27/// Output of the `Engine` after actioning a [`Command`](super::command::Command).
28#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, From)]
29#[allow(clippy::large_enum_variant)]
30pub enum ActionOutput<ExchangeKey = ExchangeIndex, InstrumentKey = InstrumentIndex> {
31    GenerateAlgoOrders(GenerateAlgoOrdersOutput<ExchangeKey, InstrumentKey>),
32    CancelOrders(SendRequestsOutput<RequestCancel, ExchangeKey, InstrumentKey>),
33    OpenOrders(SendRequestsOutput<RequestOpen, ExchangeKey, InstrumentKey>),
34    ClosePositions(SendCancelsAndOpensOutput<ExchangeKey, InstrumentKey>),
35}
36
37impl<ExchangeKey, InstrumentKey> ActionOutput<ExchangeKey, InstrumentKey> {
38    /// Returns any unrecoverable errors that occurred during an `Engine` action.
39    pub fn unrecoverable_errors(&self) -> Option<OneOrMany<UnrecoverableEngineError>> {
40        match self {
41            ActionOutput::GenerateAlgoOrders(algo) => algo.cancels_and_opens.unrecoverable_errors(),
42            ActionOutput::CancelOrders(cancels) => cancels.unrecoverable_errors(),
43            ActionOutput::OpenOrders(opens) => opens.unrecoverable_errors(),
44            ActionOutput::ClosePositions(requests) => requests.unrecoverable_errors(),
45        }
46        .into_option()
47    }
48}