2021-01-04 13:23:59 +00:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2021-01-02 12:16:48 +00:00
|
|
|
use std::future::Future;
|
|
|
|
|
2021-01-02 15:22:48 +00:00
|
|
|
use bitfinex::positions::Position;
|
2021-01-04 13:23:59 +00:00
|
|
|
use tokio::stream::StreamExt;
|
2021-01-02 12:16:48 +00:00
|
|
|
use tokio::task::JoinHandle;
|
2021-01-02 14:10:16 +00:00
|
|
|
|
2021-01-02 15:22:48 +00:00
|
|
|
use crate::pairs::PairStatus;
|
2021-01-04 13:23:59 +00:00
|
|
|
use crate::positions::{PositionState, PositionWrapper};
|
2021-01-04 12:28:40 +00:00
|
|
|
use crate::BoxError;
|
2021-01-02 12:16:48 +00:00
|
|
|
|
2021-01-02 15:22:48 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum SignalKind {
|
2021-01-02 12:16:48 +00:00
|
|
|
ClosePosition,
|
|
|
|
OpenPosition,
|
|
|
|
}
|
|
|
|
|
2021-01-02 15:22:48 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
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-02 15:22:48 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-02 15:22:48 +00:00
|
|
|
#[derive(Copy, Clone)]
|
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
|
|
|
}
|
|
|
|
|
2021-01-02 14:10:16 +00:00
|
|
|
pub struct EventDispatcher {
|
2021-01-04 12:28:40 +00:00
|
|
|
event_handlers: HashMap<EventKind, Vec<Box<dyn Fn(&Event, &PairStatus) -> JoinHandle<()>>>>,
|
2021-01-04 13:23:59 +00:00
|
|
|
position_state_handlers:
|
|
|
|
HashMap<PositionState, Vec<Box<dyn Fn(&PositionWrapper, &PairStatus) -> JoinHandle<()>>>>,
|
|
|
|
|
|
|
|
on_any_event_handlers: Vec<Box<dyn Fn(&Event, &PairStatus) -> JoinHandle<()>>>,
|
|
|
|
on_any_position_state_handlers:
|
|
|
|
Vec<Box<dyn Fn(&PositionWrapper, &PairStatus) -> JoinHandle<()>>>,
|
2021-01-02 12:16:48 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 14:10:16 +00:00
|
|
|
impl EventDispatcher {
|
2021-01-02 12:16:48 +00:00
|
|
|
pub fn new() -> Self {
|
2021-01-02 14:10:16 +00:00
|
|
|
EventDispatcher {
|
2021-01-02 15:22:48 +00:00
|
|
|
event_handlers: HashMap::new(),
|
2021-01-04 13:23:59 +00:00
|
|
|
position_state_handlers: HashMap::new(),
|
|
|
|
on_any_event_handlers: Vec::new(),
|
|
|
|
on_any_position_state_handlers: Vec::new(),
|
2021-01-02 12:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 12:28:40 +00:00
|
|
|
pub fn call_event_handlers(&self, event: &Event, status: &PairStatus) {
|
|
|
|
if let Some(functions) = self.event_handlers.get(&event.kind()) {
|
|
|
|
for f in functions {
|
|
|
|
f(event, status);
|
2021-01-02 15:22:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-04 13:23:59 +00:00
|
|
|
|
|
|
|
for f in &self.on_any_event_handlers {
|
|
|
|
f(event, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn call_position_state_handlers(&self, pw: &PositionWrapper, status: &PairStatus) {
|
|
|
|
if let Some(state) = pw.state() {
|
|
|
|
if let Some(functions) = self.position_state_handlers.get(&state) {
|
|
|
|
for f in functions {
|
|
|
|
f(pw, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for f in &self.on_any_position_state_handlers {
|
|
|
|
f(pw, status);
|
|
|
|
}
|
2021-01-02 14:10:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 12:28:40 +00:00
|
|
|
pub fn register_event_handler<F: 'static, Fut: 'static>(&mut self, event: EventKind, f: F)
|
|
|
|
where
|
|
|
|
F: Fn(&Event, &PairStatus) -> Fut,
|
|
|
|
Fut: Future<Output = ()> + Send,
|
|
|
|
{
|
2021-01-04 13:27:30 +00:00
|
|
|
match event {
|
2021-01-04 13:35:34 +00:00
|
|
|
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)))),
|
2021-01-04 13:27:30 +00:00
|
|
|
}
|
2021-01-02 12:16:48 +00:00
|
|
|
}
|
2021-01-04 13:23:59 +00:00
|
|
|
|
|
|
|
pub fn register_positionstate_handler<F: 'static, Fut: 'static>(
|
|
|
|
&mut self,
|
|
|
|
state: PositionState,
|
|
|
|
f: F,
|
|
|
|
) where
|
|
|
|
F: Fn(&PositionWrapper, &PairStatus) -> Fut,
|
|
|
|
Fut: Future<Output = ()> + Send,
|
|
|
|
{
|
2021-01-04 13:27:30 +00:00
|
|
|
match state {
|
2021-01-04 13:35:34 +00:00
|
|
|
PositionState::Any => self
|
|
|
|
.on_any_position_state_handlers
|
|
|
|
.push(Box::new(move |pw, s| tokio::spawn(f(&pw, s)))),
|
2021-01-04 13:27:30 +00:00
|
|
|
_ => self
|
|
|
|
.position_state_handlers
|
|
|
|
.entry(state)
|
|
|
|
.or_default()
|
2021-01-04 13:35:34 +00:00
|
|
|
.push(Box::new(move |pw, s| tokio::spawn(f(&pw, s)))),
|
2021-01-04 13:27:30 +00:00
|
|
|
}
|
2021-01-04 13:23:59 +00:00
|
|
|
}
|
2021-01-02 15:22:48 +00:00
|
|
|
}
|