barter/strategy/
mod.rs

1use crate::{
2    engine::{
3        Engine,
4        state::{
5            EngineState,
6            instrument::{data::InstrumentDataState, filter::InstrumentFilter},
7        },
8    },
9    strategy::{
10        algo::AlgoStrategy,
11        close_positions::{ClosePositionsStrategy, close_open_positions_with_market_orders},
12        on_disconnect::OnDisconnectStrategy,
13        on_trading_disabled::OnTradingDisabled,
14    },
15};
16use barter_execution::order::{
17    id::{ClientOrderId, StrategyId},
18    request::{OrderRequestCancel, OrderRequestOpen},
19};
20use barter_instrument::{
21    asset::AssetIndex,
22    exchange::{ExchangeId, ExchangeIndex},
23    instrument::InstrumentIndex,
24};
25use std::marker::PhantomData;
26
27/// Defines a strategy interface for generating algorithmic open and cancel order requests based
28/// on the current `EngineState`.
29pub mod algo;
30
31/// Defines a strategy interface for generating open and cancel order requests that close open
32/// positions.
33pub mod close_positions;
34
35/// Defines a strategy interface enables custom [`Engine`] to be performed in the event of an
36/// exchange disconnection.
37pub mod on_disconnect;
38
39/// Defines a strategy interface enables custom [`Engine`] to be performed in the event that the
40/// `TradingState` gets set to `TradingState::Disabled`.
41pub mod on_trading_disabled;
42
43/// Naive implementation of all strategy interfaces.
44///
45/// *THIS IS FOR DEMONSTRATION PURPOSES ONLY, NEVER USE FOR REAL TRADING OR IN PRODUCTION*.
46///
47/// This strategy:
48/// - Generates no algorithmic orders (AlgoStrategy).
49/// - Closes positions via the naive [`close_open_positions_with_market_orders`] logic (ClosePositionsStrategy).
50/// - Does nothing when an exchange disconnects (OnDisconnectStrategy).
51/// - Does nothing when trading state is set to disabled (OnDisconnectStrategy).
52#[derive(Debug, Clone)]
53pub struct DefaultStrategy<State> {
54    pub id: StrategyId,
55    phantom: PhantomData<State>,
56}
57
58impl<State> Default for DefaultStrategy<State> {
59    fn default() -> Self {
60        Self {
61            id: StrategyId::new("default"),
62            phantom: PhantomData,
63        }
64    }
65}
66
67impl<State, ExchangeKey, InstrumentKey> AlgoStrategy<ExchangeKey, InstrumentKey>
68    for DefaultStrategy<State>
69{
70    type State = State;
71
72    fn generate_algo_orders(
73        &self,
74        _: &Self::State,
75    ) -> (
76        impl IntoIterator<Item = OrderRequestCancel<ExchangeKey, InstrumentKey>>,
77        impl IntoIterator<Item = OrderRequestOpen<ExchangeKey, InstrumentKey>>,
78    ) {
79        (std::iter::empty(), std::iter::empty())
80    }
81}
82
83impl<GlobalData, InstrumentData> ClosePositionsStrategy
84    for DefaultStrategy<EngineState<GlobalData, InstrumentData>>
85where
86    InstrumentData: InstrumentDataState,
87{
88    type State = EngineState<GlobalData, InstrumentData>;
89
90    fn close_positions_requests<'a>(
91        &'a self,
92        state: &'a Self::State,
93        filter: &'a InstrumentFilter,
94    ) -> (
95        impl IntoIterator<Item = OrderRequestCancel<ExchangeIndex, InstrumentIndex>> + 'a,
96        impl IntoIterator<Item = OrderRequestOpen<ExchangeIndex, InstrumentIndex>> + 'a,
97    )
98    where
99        ExchangeIndex: 'a,
100        AssetIndex: 'a,
101        InstrumentIndex: 'a,
102    {
103        close_open_positions_with_market_orders(&self.id, state, filter, |_| {
104            ClientOrderId::random()
105        })
106    }
107}
108
109impl<Clock, State, ExecutionTxs, Risk> OnDisconnectStrategy<Clock, State, ExecutionTxs, Risk>
110    for DefaultStrategy<State>
111{
112    type OnDisconnect = ();
113
114    fn on_disconnect(
115        _: &mut Engine<Clock, State, ExecutionTxs, Self, Risk>,
116        _: ExchangeId,
117    ) -> Self::OnDisconnect {
118    }
119}
120
121impl<Clock, State, ExecutionTxs, Risk> OnTradingDisabled<Clock, State, ExecutionTxs, Risk>
122    for DefaultStrategy<State>
123{
124    type OnTradingDisabled = ();
125
126    fn on_trading_disabled(
127        _: &mut Engine<Clock, State, ExecutionTxs, Self, Risk>,
128    ) -> Self::OnTradingDisabled {
129    }
130}