rust #10
@ -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<EventKind, Vec<Box<dyn FnMut(Event, &PairStatus) -> JoinHandle<()>>>>,
|
||||
event_handlers: HashMap<EventKind, Vec<Box<dyn Fn(&Event, &PairStatus) -> 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<F: 'static, Fut: 'static>(&mut self, event: EventKind, mut f: F)
|
||||
where
|
||||
F: FnMut(Event, &PairStatus) -> Fut,
|
||||
Fut: Future<Output=()> + Send, {
|
||||
pub fn register_event_handler<F: 'static, Fut: 'static>(&mut self, event: EventKind, f: F)
|
||||
where
|
||||
F: Fn(&Event, &PairStatus) -> Fut,
|
||||
Fut: Future<Output = ()> + 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))));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<Event>);
|
||||
fn position_on_new_tick(&self, position: &Position, status: &PairStatus) -> (PositionWrapper, Vec<Event>);
|
||||
}
|
Loading…
Reference in New Issue
Block a user