core/rustybot/src/events.rs

190 lines
5.5 KiB
Rust
Raw Normal View History

use tokio::sync::oneshot;
2021-01-02 12:16:48 +00:00
2021-01-24 20:53:33 +00:00
use crate::managers::OptionUpdate;
use crate::models::OrderForm;
#[derive(Debug)]
pub struct ActorMessage {
pub(crate) message: Message,
pub(crate) respond_to: oneshot::Sender<OptionUpdate>,
}
#[derive(Debug)]
pub enum Message {
Update { tick: u64 },
ClosePosition { position_id: u64 },
SubmitOrder { order: OrderForm },
2021-01-02 12:16:48 +00:00
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
2021-01-02 15:22:48 +00:00
pub struct EventMetadata {
2021-01-02 12:16:48 +00:00
position_id: Option<u64>,
order_id: Option<u64>,
}
2021-01-04 19:29:01 +00:00
impl EventMetadata {
pub fn new(position_id: Option<u64>, order_id: Option<u64>) -> Self {
EventMetadata {
position_id,
order_id,
}
}
}
2021-01-05 19:50:14 +00:00
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
2021-01-02 15:22:48 +00:00
pub enum EventKind {
2021-01-02 12:16:48 +00:00
NewMinimum,
NewMaximum,
ReachedLoss,
ReachedBreakEven,
ReachedMinProfit,
ReachedGoodProfit,
ReachedMaxLoss,
TrailingStopSet,
TrailingStopMoved,
OrderSubmitted,
NewTick,
2021-01-04 13:23:59 +00:00
Any,
2021-01-02 12:16:48 +00:00
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
2021-01-02 14:10:16 +00:00
pub struct Event {
2021-01-02 12:16:48 +00:00
kind: EventKind,
tick: u64,
metadata: Option<EventMetadata>,
}
impl Event {
pub fn new(kind: EventKind, tick: u64, metadata: Option<EventMetadata>) -> Self {
Event {
kind,
tick,
metadata,
}
}
fn has_metadata(&self) -> bool {
self.metadata.is_some()
}
2021-01-02 18:34:13 +00:00
pub fn kind(&self) -> EventKind {
self.kind
}
pub fn tick(&self) -> u64 {
self.tick
}
pub fn metadata(&self) -> Option<EventMetadata> {
self.metadata
}
2021-01-02 12:16:48 +00:00
}
//
// pub struct Dispatcher {
// event_handlers: HashMap<EventKind, Vec<Box<dyn Fn(&Event, &PriceManager) -> JoinHandle<()>>>>,
// profit_state_handlers:
// HashMap<PositionProfitState, Vec<Box<dyn Fn(&Position, &PriceManager) -> JoinHandle<()>>>>,
// signal_handlers: HashMap<SignalKind, Vec<Box<dyn Fn(&SignalKind) -> JoinHandle<()>>>>,
//
// on_any_event_handlers: Vec<Box<dyn Fn(&Event, &PriceManager) -> JoinHandle<()>>>,
// on_any_profit_state_handlers: Vec<Box<dyn Fn(&Position, &PriceManager) -> JoinHandle<()>>>,
// }
//
// impl Dispatcher {
// pub fn new() -> Self {
// Dispatcher {
// event_handlers: HashMap::new(),
// profit_state_handlers: HashMap::new(),
// signal_handlers: HashMap::new(),
// on_any_event_handlers: Vec::new(),
// on_any_profit_state_handlers: Vec::new(),
// }
// }
//
// pub fn call_signal_handlers(&self, signal: &SignalKind) {
// if let Some(functions) = self.signal_handlers.get(&signal) {
// for f in functions {
// f(signal);
// }
// }
// }
//
// pub fn call_event_handlers(&self, event: &Event, status: &PriceManager) {
// if let Some(functions) = self.event_handlers.get(&event.kind()) {
// for f in functions {
// f(event, status);
// }
// }
//
// for f in &self.on_any_event_handlers {
// f(event, status);
// }
// }
//
// pub fn call_position_state_handlers(&self, position: &Position, status: &PriceManager) {
// if let Some(profit_state) = &position.profit_state() {
// if let Some(functions) = self.profit_state_handlers.get(profit_state) {
// for f in functions {
// f(position, status);
// }
// }
// }
//
// for f in &self.on_any_profit_state_handlers {
// f(position, status);
// }
// }
//
// pub fn register_event_handler<F: 'static, Fut: 'static>(&mut self, event: EventKind, f: F)
// where
// F: Fn(&Event, &PriceManager) -> Fut,
// Fut: Future<Output = ()> + Send,
// {
// match event {
// EventKind::Any => self
// .on_any_event_handlers
// .push(Box::new(move |e, s| tokio::spawn(f(&e, s)))),
// _ => self
// .event_handlers
// .entry(event)
// .or_default()
// .push(Box::new(move |e, s| tokio::spawn(f(&e, s)))),
// }
// }
//
// pub fn register_positionstate_handler<F: 'static, Fut: 'static>(
// &mut self,
// state: PositionProfitState,
// f: F,
// ) where
// F: Fn(&Position, &PriceManager) -> Fut,
// Fut: Future<Output = ()> + Send,
// {
// match state {
// // PositionProfitState::Any => self
// // .on_any_position_state_handlers
// // .push(Box::new(move |p, s| tokio::spawn(f(&p, s)))),
// _ => self
// .profit_state_handlers
// .entry(state)
// .or_default()
// .push(Box::new(move |p, s| tokio::spawn(f(&p, s)))),
// }
// }
//
// pub fn register_signal_handler<F: 'static, Fut: 'static>(&mut self, signal: SignalKind, f: F)
// where
// F: Fn(&SignalKind) -> Fut,
// Fut: Future<Output = ()> + Send,
// {
// match signal {
// // PositionProfitState::Any => self
// // .on_any_position_state_handlers
// // .push(Box::new(move |p, s| tokio::spawn(f(&p, s)))),
// _ => self
// .signal_handlers
// .entry(signal)
// .or_default()
// .push(Box::new(move |s| tokio::spawn(f(s)))),
// }
// }
// }