From a03e0bc57482038a30e8780b3bd290cdf1e9e858 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Sat, 2 Jan 2021 18:34:13 +0000 Subject: [PATCH] stuff --- rustybot/src/events.rs | 22 ++++++++++++++++------ rustybot/src/pairs.rs | 31 ++++++++++++++++++++++++++++++- rustybot/src/positions.rs | 4 ++-- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/rustybot/src/events.rs b/rustybot/src/events.rs index 86a096f..2a7de09 100644 --- a/rustybot/src/events.rs +++ b/rustybot/src/events.rs @@ -53,10 +53,20 @@ impl Event { 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 { + self.metadata + } } pub struct EventDispatcher { - event_handlers: HashMap JoinHandle<()>>>>, + event_handlers: HashMap JoinHandle<()>>>>, } impl EventDispatcher { @@ -66,21 +76,21 @@ impl EventDispatcher { } } - pub fn call_handler(&mut self, event: &EventKind, position: &Position, status: &PairStatus) { - if let Some(callbacks) = self.event_handlers.get_mut(event) { + 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(position, status); + f(event.clone(), status); } } } pub fn register_event_handler(&mut self, event: EventKind, mut f: F) where - F: FnMut(&Position, &PairStatus) -> Fut, + F: FnMut(Event, &PairStatus) -> Fut, Fut: Future + Send, { self.event_handlers .entry(event) .or_default() - .push(Box::new(move |p, s| tokio::spawn(f(p, 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 a3e834c..506bf51 100644 --- a/rustybot/src/pairs.rs +++ b/rustybot/src/pairs.rs @@ -1,6 +1,8 @@ use std::collections::HashMap; -use crate::events::{EventDispatcher, Event}; +use bitfinex::positions::Position; + +use crate::events::{Event, EventDispatcher}; use crate::positions::PositionWrapper; use crate::strategy::Strategy; @@ -27,4 +29,31 @@ impl PairStatus { strategy, } } + + 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![]) + } + }; + + self.positions + .entry(self.current_tick) + .or_default() + .push(pw.clone()); + + // self.dispatcher.call_state_handlers(pw); + + for e in events { + self.add_event(e); + } + } + pub fn add_event(&mut self, event: Event) { + self.events.push(event); + + // self.dispatcher.call_handler(&event); + } } \ No newline at end of file diff --git a/rustybot/src/positions.rs b/rustybot/src/positions.rs index 9284030..b199ed8 100644 --- a/rustybot/src/positions.rs +++ b/rustybot/src/positions.rs @@ -24,11 +24,11 @@ pub struct PositionWrapper { position: Position, net_pl: f64, net_pl_perc: f64, - state: PositionState, + state: Option, } impl PositionWrapper { - pub fn new(position: Position, net_pl: f64, net_pl_perc: f64, state: PositionState) -> Self { + pub fn new(position: Position, net_pl: f64, net_pl_perc: f64, state: Option) -> Self { PositionWrapper { position, net_pl,