use crate::{
engine::{
Engine,
state::{
EngineState,
instrument::{data::InstrumentDataState, filter::InstrumentFilter},
},
},
strategy::{
algo::AlgoStrategy,
close_positions::{ClosePositionsStrategy, close_open_positions_with_market_orders},
on_disconnect::OnDisconnectStrategy,
on_trading_disabled::OnTradingDisabled,
},
};
use barter_execution::order::{
id::{ClientOrderId, StrategyId},
request::{OrderRequestCancel, OrderRequestOpen},
};
use barter_instrument::{
asset::AssetIndex,
exchange::{ExchangeId, ExchangeIndex},
instrument::InstrumentIndex,
};
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
pub mod algo;
pub mod close_positions;
pub mod on_disconnect;
pub mod on_trading_disabled;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DefaultStrategy<State> {
pub id: StrategyId,
phantom: PhantomData<State>,
}
impl<State> Default for DefaultStrategy<State> {
fn default() -> Self {
Self {
id: StrategyId::new("default"),
phantom: PhantomData,
}
}
}
impl<State, ExchangeKey, InstrumentKey> AlgoStrategy<ExchangeKey, InstrumentKey>
for DefaultStrategy<State>
{
type State = State;
fn generate_algo_orders(
&self,
_: &Self::State,
) -> (
impl IntoIterator<Item = OrderRequestCancel<ExchangeKey, InstrumentKey>>,
impl IntoIterator<Item = OrderRequestOpen<ExchangeKey, InstrumentKey>>,
) {
(std::iter::empty(), std::iter::empty())
}
}
impl<GlobalData, InstrumentData> ClosePositionsStrategy
for DefaultStrategy<EngineState<GlobalData, InstrumentData>>
where
InstrumentData: InstrumentDataState,
{
type State = EngineState<GlobalData, InstrumentData>;
fn close_positions_requests<'a>(
&'a self,
state: &'a Self::State,
filter: &'a InstrumentFilter,
) -> (
impl IntoIterator<Item = OrderRequestCancel<ExchangeIndex, InstrumentIndex>> + 'a,
impl IntoIterator<Item = OrderRequestOpen<ExchangeIndex, InstrumentIndex>> + 'a,
)
where
ExchangeIndex: 'a,
AssetIndex: 'a,
InstrumentIndex: 'a,
{
close_open_positions_with_market_orders(&self.id, state, filter, |_| {
ClientOrderId::random()
})
}
}
impl<Clock, State, ExecutionTxs, Risk> OnDisconnectStrategy<Clock, State, ExecutionTxs, Risk>
for DefaultStrategy<State>
{
type OnDisconnect = ();
fn on_disconnect(
_: &mut Engine<Clock, State, ExecutionTxs, Self, Risk>,
_: ExchangeId,
) -> Self::OnDisconnect {
}
}
impl<Clock, State, ExecutionTxs, Risk> OnTradingDisabled<Clock, State, ExecutionTxs, Risk>
for DefaultStrategy<State>
{
type OnTradingDisabled = ();
fn on_trading_disabled(
_: &mut Engine<Clock, State, ExecutionTxs, Self, Risk>,
) -> Self::OnTradingDisabled {
}
}