diff --git a/rustybot/src/events.rs b/rustybot/src/events.rs index 2a7de09..3d4ef6c 100644 --- a/rustybot/src/events.rs +++ b/rustybot/src/events.rs @@ -4,8 +4,8 @@ use std::future::Future; use bitfinex::positions::Position; use tokio::task::JoinHandle; -use crate::BoxError; use crate::pairs::PairStatus; +use crate::BoxError; #[derive(Copy, Clone)] pub enum SignalKind { @@ -66,7 +66,7 @@ impl Event { } pub struct EventDispatcher { - event_handlers: HashMap JoinHandle<()>>>>, + event_handlers: HashMap JoinHandle<()>>>>, } impl EventDispatcher { @@ -76,21 +76,22 @@ impl EventDispatcher { } } - pub fn call_handler(&mut self, event: &Event, status: &PairStatus) { - if let Some(callbacks) = self.event_handlers.get_mut(&event.kind()) { - for f in callbacks { - f(event.clone(), status); + 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); } } } - pub fn register_event_handler(&mut self, event: EventKind, mut f: F) - where - F: FnMut(Event, &PairStatus) -> Fut, - Fut: Future + Send, { + pub fn register_event_handler(&mut self, event: EventKind, f: F) + where + F: Fn(&Event, &PairStatus) -> Fut, + Fut: Future + Send, + { self.event_handlers .entry(event) .or_default() - .push(Box::new(move |e, s| tokio::spawn(f(e, s)))); + .push(Box::new(move |e, s| tokio::spawn(f(&e, s)))); } } diff --git a/rustybot/src/pairs.rs b/rustybot/src/pairs.rs index cefd369..41b579c 100644 --- a/rustybot/src/pairs.rs +++ b/rustybot/src/pairs.rs @@ -2,10 +2,10 @@ use std::collections::HashMap; use bitfinex::positions::Position; +use crate::currency::SymbolPair; use crate::events::{Event, EventDispatcher}; use crate::positions::PositionWrapper; use crate::strategy::Strategy; -use crate::currency::SymbolPair; pub struct PairStatus { pair: SymbolPair, @@ -34,17 +34,18 @@ impl PairStatus { pub fn add_position(&mut self, position: Position) { let (pw, events) = { match &self.strategy { - Some(strategy) => { - strategy.position_on_new_tick(position) - }, - None => (PositionWrapper::new(position.clone(), position.pl(), position.pl_perc(), None), vec![]) + Some(strategy) => strategy.position_on_new_tick(&position, &self), + None => ( + PositionWrapper::new(position.clone(), position.pl(), position.pl_perc(), None), + vec![], + ), } }; self.positions .entry(self.current_tick) .or_default() - .push(pw.clone()); + .push(pw); // self.dispatcher.call_state_handlers(pw); @@ -52,9 +53,10 @@ impl PairStatus { self.add_event(e); } } + pub fn add_event(&mut self, event: Event) { self.events.push(event); - // self.dispatcher.call_handler(&event); + self.dispatcher.call_event_handlers(&event, &self); } -} \ No newline at end of file +} diff --git a/rustybot/src/strategy.rs b/rustybot/src/strategy.rs index b8acf7d..8a5b482 100644 --- a/rustybot/src/strategy.rs +++ b/rustybot/src/strategy.rs @@ -2,7 +2,8 @@ use bitfinex::positions::Position; use crate::events::Event; use crate::positions::PositionWrapper; +use crate::pairs::PairStatus; pub trait Strategy { - fn position_on_new_tick(&self, position: Position) -> (PositionWrapper, Vec); + fn position_on_new_tick(&self, position: &Position, status: &PairStatus) -> (PositionWrapper, Vec); } \ No newline at end of file