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
27pub mod algo;
30
31pub mod close_positions;
34
35pub mod on_disconnect;
38
39pub mod on_trading_disabled;
42
43#[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}