77 lines
2.1 KiB
Rust
77 lines
2.1 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use crate::currency::SymbolPair;
|
|
use crate::events::{Event, EventDispatcher};
|
|
use crate::orders::Order;
|
|
use crate::positions::Position;
|
|
use crate::strategy::Strategy;
|
|
|
|
pub struct PairStatus {
|
|
pair: SymbolPair,
|
|
dispatcher: EventDispatcher,
|
|
prices: HashMap<u64, f64>,
|
|
events: Vec<Event>,
|
|
orders: HashMap<u64, Vec<Order>>,
|
|
positions: HashMap<u64, Vec<Position>>,
|
|
current_tick: u64,
|
|
strategy: Option<Box<dyn Strategy>>,
|
|
}
|
|
|
|
impl PairStatus {
|
|
pub fn new(pair: SymbolPair, current_tick: u64, strategy: Option<Box<dyn Strategy>>) -> Self {
|
|
PairStatus {
|
|
pair,
|
|
dispatcher: EventDispatcher::new(),
|
|
prices: HashMap::new(),
|
|
events: Vec::new(),
|
|
positions: HashMap::new(),
|
|
current_tick,
|
|
strategy,
|
|
orders: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
pub fn add_position(&mut self, position: Position) {
|
|
let (pw, events, signals) = {
|
|
match &self.strategy {
|
|
Some(strategy) => strategy.position_on_new_tick(&position, &self),
|
|
None => (
|
|
position,
|
|
// PositionWrapper::new(position.clone(), position.pl(), position.pl_perc(), None),
|
|
vec![],
|
|
vec![],
|
|
),
|
|
}
|
|
};
|
|
|
|
self.positions
|
|
.entry(self.current_tick)
|
|
.or_default()
|
|
.push(pw.clone());
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
|
|
pub fn add_event(&mut self, event: Event) {
|
|
self.events.push(event);
|
|
|
|
self.dispatcher.call_event_handlers(&event, &self);
|
|
}
|
|
|
|
pub fn current_tick(&self) -> u64 {
|
|
self.current_tick
|
|
}
|
|
|
|
pub fn previous_pw(&self, id: u64) -> Option<&Position> {
|
|
self.positions
|
|
.get(&(self.current_tick - 1))
|
|
.and_then(|x| x.iter().find(|x| x.position_id() == id))
|
|
}
|
|
}
|