on any state on any state
This commit is contained in:
parent
f56a3f84f8
commit
0e33a09d8f
@ -1,10 +1,12 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
|
|
||||||
use bitfinex::positions::Position;
|
use bitfinex::positions::Position;
|
||||||
|
use tokio::stream::StreamExt;
|
||||||
use tokio::task::JoinHandle;
|
use tokio::task::JoinHandle;
|
||||||
|
|
||||||
use crate::pairs::PairStatus;
|
use crate::pairs::PairStatus;
|
||||||
|
use crate::positions::{PositionState, PositionWrapper};
|
||||||
use crate::BoxError;
|
use crate::BoxError;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
@ -32,6 +34,7 @@ pub enum EventKind {
|
|||||||
TrailingStopMoved,
|
TrailingStopMoved,
|
||||||
OrderSubmitted,
|
OrderSubmitted,
|
||||||
NewTick,
|
NewTick,
|
||||||
|
Any,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
@ -67,12 +70,21 @@ impl Event {
|
|||||||
|
|
||||||
pub struct EventDispatcher {
|
pub struct EventDispatcher {
|
||||||
event_handlers: HashMap<EventKind, Vec<Box<dyn Fn(&Event, &PairStatus) -> JoinHandle<()>>>>,
|
event_handlers: HashMap<EventKind, Vec<Box<dyn Fn(&Event, &PairStatus) -> JoinHandle<()>>>>,
|
||||||
|
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<()>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventDispatcher {
|
impl EventDispatcher {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
EventDispatcher {
|
EventDispatcher {
|
||||||
event_handlers: HashMap::new(),
|
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);
|
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<F: 'static, Fut: 'static>(&mut self, event: EventKind, f: F)
|
pub fn register_event_handler<F: 'static, Fut: 'static>(&mut self, event: EventKind, f: F)
|
||||||
@ -94,4 +124,18 @@ impl EventDispatcher {
|
|||||||
.or_default()
|
.or_default()
|
||||||
.push(Box::new(move |e, s| tokio::spawn(f(&e, s))));
|
.push(Box::new(move |e, s| tokio::spawn(f(&e, s))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
{
|
||||||
|
self.position_state_handlers
|
||||||
|
.entry(state)
|
||||||
|
.or_default()
|
||||||
|
.push(Box::new(move |pw, s| tokio::spawn(f(&pw, s))));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,10 +45,12 @@ impl PairStatus {
|
|||||||
self.positions
|
self.positions
|
||||||
.entry(self.current_tick)
|
.entry(self.current_tick)
|
||||||
.or_default()
|
.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 {
|
for e in events {
|
||||||
self.add_event(e);
|
self.add_event(e);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use bitfinex::positions::Position;
|
use bitfinex::positions::Position;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
|
||||||
pub enum PositionState {
|
pub enum PositionState {
|
||||||
Critical,
|
Critical,
|
||||||
Loss,
|
Loss,
|
||||||
@ -12,10 +12,11 @@ pub enum PositionState {
|
|||||||
impl PositionState {
|
impl PositionState {
|
||||||
fn color(self) -> String {
|
fn color(self) -> String {
|
||||||
match self {
|
match self {
|
||||||
PositionState::Critical | PositionState::Loss => { "red" }
|
PositionState::Critical | PositionState::Loss => "red",
|
||||||
PositionState::BreakEven => { "yellow" }
|
PositionState::BreakEven => "yellow",
|
||||||
PositionState::MinimumProfit | PositionState::Profit => { "green" }
|
PositionState::MinimumProfit | PositionState::Profit => "green",
|
||||||
}.into()
|
}
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,7 +29,12 @@ pub struct PositionWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl PositionWrapper {
|
impl PositionWrapper {
|
||||||
pub fn new(position: Position, net_pl: f64, net_pl_perc: f64, state: Option<PositionState>) -> Self {
|
pub fn new(
|
||||||
|
position: Position,
|
||||||
|
net_pl: f64,
|
||||||
|
net_pl_perc: f64,
|
||||||
|
state: Option<PositionState>,
|
||||||
|
) -> Self {
|
||||||
PositionWrapper {
|
PositionWrapper {
|
||||||
position,
|
position,
|
||||||
net_pl,
|
net_pl,
|
||||||
@ -36,8 +42,17 @@ impl PositionWrapper {
|
|||||||
state,
|
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<PositionState> {
|
||||||
|
self.state
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user