core/rustybot/src/events.rs
2021-01-23 16:13:37 +00:00

199 lines
5.7 KiB
Rust

use std::collections::HashMap;
use std::future::Future;
use tokio::task::JoinHandle;
use crate::managers::{OptionUpdate, OrderManager, PositionManager, PriceManager};
use crate::models::{OrderForm, OrderKind, Position, PositionProfitState};
use tokio::sync::oneshot;
#[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: Position,
order_form: OrderForm,
},
OpenPosition,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct EventMetadata {
position_id: Option<u64>,
order_id: Option<u64>,
}
impl EventMetadata {
pub fn new(position_id: Option<u64>, order_id: Option<u64>) -> Self {
EventMetadata {
position_id,
order_id,
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum EventKind {
NewMinimum,
NewMaximum,
ReachedLoss,
ReachedBreakEven,
ReachedMinProfit,
ReachedGoodProfit,
ReachedMaxLoss,
TrailingStopSet,
TrailingStopMoved,
OrderSubmitted,
NewTick,
Any,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct Event {
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()
}
pub fn kind(&self) -> EventKind {
self.kind
}
pub fn tick(&self) -> u64 {
self.tick
}
pub fn metadata(&self) -> Option<EventMetadata> {
self.metadata
}
}
//
// 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)))),
// }
// }
// }