using signals
This commit is contained in:
parent
d2858ff021
commit
293ea60919
@ -8,9 +8,9 @@ use crate::pairs::PairStatus;
|
|||||||
use crate::positions::{Position, PositionProfitState, PositionState};
|
use crate::positions::{Position, PositionProfitState, PositionState};
|
||||||
use crate::BoxError;
|
use crate::BoxError;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
pub enum SignalKind {
|
pub enum SignalKind {
|
||||||
ClosePosition,
|
ClosePosition { position_id: u64 },
|
||||||
OpenPosition,
|
OpenPosition,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ 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<()>>>>,
|
||||||
profit_state_handlers:
|
profit_state_handlers:
|
||||||
HashMap<PositionProfitState, Vec<Box<dyn Fn(&Position, &PairStatus) -> JoinHandle<()>>>>,
|
HashMap<PositionProfitState, Vec<Box<dyn Fn(&Position, &PairStatus) -> JoinHandle<()>>>>,
|
||||||
|
signal_handlers: HashMap<SignalKind, Vec<Box<dyn Fn(&SignalKind) -> JoinHandle<()>>>>,
|
||||||
on_any_event_handlers: Vec<Box<dyn Fn(&Event, &PairStatus) -> JoinHandle<()>>>,
|
on_any_event_handlers: Vec<Box<dyn Fn(&Event, &PairStatus) -> JoinHandle<()>>>,
|
||||||
on_any_profit_state_handlers: Vec<Box<dyn Fn(&Position, &PairStatus) -> JoinHandle<()>>>,
|
on_any_profit_state_handlers: Vec<Box<dyn Fn(&Position, &PairStatus) -> JoinHandle<()>>>,
|
||||||
}
|
}
|
||||||
@ -90,6 +90,7 @@ impl EventDispatcher {
|
|||||||
EventDispatcher {
|
EventDispatcher {
|
||||||
event_handlers: HashMap::new(),
|
event_handlers: HashMap::new(),
|
||||||
profit_state_handlers: HashMap::new(),
|
profit_state_handlers: HashMap::new(),
|
||||||
|
signal_handlers: HashMap::new(),
|
||||||
on_any_event_handlers: Vec::new(),
|
on_any_event_handlers: Vec::new(),
|
||||||
on_any_profit_state_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)))),
|
.push(Box::new(move |p, s| tokio::spawn(f(&p, s)))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn register_signal_handler<F: 'static, Fut: 'static>(&mut self, signal: SignalKind, f: F)
|
||||||
|
where
|
||||||
|
F: Fn(&SignalKind) -> Fut,
|
||||||
|
Fut: Future<Output = ()> + 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)))),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,9 @@ impl Strategy for TrailingStop {
|
|||||||
} else if TrailingStop::MAX_LOSS_PERC < pl_perc && pl_perc < 0.0 {
|
} else if TrailingStop::MAX_LOSS_PERC < pl_perc && pl_perc < 0.0 {
|
||||||
PositionProfitState::Loss
|
PositionProfitState::Loss
|
||||||
} else {
|
} else {
|
||||||
signals.push(SignalKind::ClosePosition);
|
signals.push(SignalKind::ClosePosition {
|
||||||
|
position_id: position.position_id(),
|
||||||
|
});
|
||||||
PositionProfitState::Critical
|
PositionProfitState::Critical
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user