From 0e33a09d8f3da4971130dd4e4ccb8a29b8b66995 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Mon, 4 Jan 2021 13:23:59 +0000 Subject: [PATCH] on any state on any state --- rustybot/src/events.rs | 46 ++++++++++++++++++++++++++++++++++++++- rustybot/src/pairs.rs | 6 +++-- rustybot/src/positions.rs | 35 ++++++++++++++++++++--------- 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/rustybot/src/events.rs b/rustybot/src/events.rs index 3d4ef6c..ac96749 100644 --- a/rustybot/src/events.rs +++ b/rustybot/src/events.rs @@ -1,10 +1,12 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::future::Future; use bitfinex::positions::Position; +use tokio::stream::StreamExt; use tokio::task::JoinHandle; use crate::pairs::PairStatus; +use crate::positions::{PositionState, PositionWrapper}; use crate::BoxError; #[derive(Copy, Clone)] @@ -32,6 +34,7 @@ pub enum EventKind { TrailingStopMoved, OrderSubmitted, NewTick, + Any, } #[derive(Copy, Clone)] @@ -67,12 +70,21 @@ impl Event { pub struct EventDispatcher { event_handlers: HashMap JoinHandle<()>>>>, + position_state_handlers: + HashMap JoinHandle<()>>>>, + + on_any_event_handlers: Vec JoinHandle<()>>>, + on_any_position_state_handlers: + Vec JoinHandle<()>>>, } impl EventDispatcher { pub fn new() -> Self { EventDispatcher { event_handlers: HashMap::new(), + position_state_handlers: HashMap::new(), + on_any_event_handlers: Vec::new(), + on_any_position_state_handlers: Vec::new(), } } @@ -82,6 +94,24 @@ impl EventDispatcher { f(event, status); } } + + 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); + } } pub fn register_event_handler(&mut self, event: EventKind, f: F) @@ -94,4 +124,18 @@ impl EventDispatcher { .or_default() .push(Box::new(move |e, s| tokio::spawn(f(&e, s)))); } + + pub fn register_positionstate_handler( + &mut self, + state: PositionState, + f: F, + ) where + F: Fn(&PositionWrapper, &PairStatus) -> Fut, + Fut: Future + Send, + { + self.position_state_handlers + .entry(state) + .or_default() + .push(Box::new(move |pw, s| tokio::spawn(f(&pw, s)))); + } } diff --git a/rustybot/src/pairs.rs b/rustybot/src/pairs.rs index 41b579c..fd507d3 100644 --- a/rustybot/src/pairs.rs +++ b/rustybot/src/pairs.rs @@ -45,10 +45,12 @@ impl PairStatus { self.positions .entry(self.current_tick) .or_default() - .push(pw); + .push(pw.clone()); - // self.dispatcher.call_state_handlers(pw); + // calling position state callbacks + self.dispatcher.call_position_state_handlers(&pw, &self); + // adding events and calling callbacks for e in events { self.add_event(e); } diff --git a/rustybot/src/positions.rs b/rustybot/src/positions.rs index b199ed8..8c896ff 100644 --- a/rustybot/src/positions.rs +++ b/rustybot/src/positions.rs @@ -1,6 +1,6 @@ use bitfinex::positions::Position; -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Eq, PartialEq, Hash)] pub enum PositionState { Critical, Loss, @@ -12,10 +12,11 @@ pub enum PositionState { impl PositionState { fn color(self) -> String { match self { - PositionState::Critical | PositionState::Loss => { "red" } - PositionState::BreakEven => { "yellow" } - PositionState::MinimumProfit | PositionState::Profit => { "green" } - }.into() + PositionState::Critical | PositionState::Loss => "red", + PositionState::BreakEven => "yellow", + PositionState::MinimumProfit | PositionState::Profit => "green", + } + .into() } } @@ -28,7 +29,12 @@ pub struct PositionWrapper { } impl PositionWrapper { - pub fn new(position: Position, net_pl: f64, net_pl_perc: f64, state: Option) -> Self { + pub fn new( + position: Position, + net_pl: f64, + net_pl_perc: f64, + state: Option, + ) -> Self { PositionWrapper { position, net_pl, @@ -36,8 +42,17 @@ impl PositionWrapper { state, } } + + pub fn position(&self) -> &Position { + &self.position + } + pub fn net_pl(&self) -> f64 { + self.net_pl + } + pub fn net_pl_perc(&self) -> f64 { + self.net_pl_perc + } + pub fn state(&self) -> Option { + self.state + } } - - - -