From 293ea60919857a06958080274856d01dad5f01f4 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Mon, 11 Jan 2021 17:16:17 +0000 Subject: [PATCH] using signals --- rustybot/src/events.rs | 24 +++++++++++++++++++++--- rustybot/src/strategy.rs | 4 +++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/rustybot/src/events.rs b/rustybot/src/events.rs index 83cbf17..c6a0fc3 100644 --- a/rustybot/src/events.rs +++ b/rustybot/src/events.rs @@ -8,9 +8,9 @@ use crate::pairs::PairStatus; use crate::positions::{Position, PositionProfitState, PositionState}; use crate::BoxError; -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum SignalKind { - ClosePosition, + ClosePosition { position_id: u64 }, OpenPosition, } @@ -80,7 +80,7 @@ pub struct EventDispatcher { event_handlers: HashMap JoinHandle<()>>>>, profit_state_handlers: HashMap JoinHandle<()>>>>, - + signal_handlers: HashMap JoinHandle<()>>>>, on_any_event_handlers: Vec JoinHandle<()>>>, on_any_profit_state_handlers: Vec JoinHandle<()>>>, } @@ -90,6 +90,7 @@ impl EventDispatcher { EventDispatcher { 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(), } @@ -157,4 +158,21 @@ impl EventDispatcher { .push(Box::new(move |p, s| tokio::spawn(f(&p, s)))), } } + + pub fn register_signal_handler(&mut self, signal: SignalKind, f: F) + where + F: Fn(&SignalKind) -> Fut, + Fut: Future + 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)))), + } + } } diff --git a/rustybot/src/strategy.rs b/rustybot/src/strategy.rs index eeaab7d..af4e4c1 100644 --- a/rustybot/src/strategy.rs +++ b/rustybot/src/strategy.rs @@ -59,7 +59,9 @@ impl Strategy for TrailingStop { } else if TrailingStop::MAX_LOSS_PERC < pl_perc && pl_perc < 0.0 { PositionProfitState::Loss } else { - signals.push(SignalKind::ClosePosition); + signals.push(SignalKind::ClosePosition { + position_id: position.position_id(), + }); PositionProfitState::Critical } };