core/rustybot/src/pairs.rs
Giulio De Pasquale 168f324d6b stuff
2021-01-04 12:07:03 +00:00

60 lines
1.6 KiB
Rust

use std::collections::HashMap;
use bitfinex::positions::Position;
use crate::events::{Event, EventDispatcher};
use crate::positions::PositionWrapper;
use crate::strategy::Strategy;
use crate::currency::SymbolPair;
pub struct PairStatus {
pair: SymbolPair,
dispatcher: EventDispatcher,
prices: HashMap<u64, f64>,
events: Vec<Event>,
// orders: HashMap<u64, Vec<Order>>,
positions: HashMap<u64, Vec<PositionWrapper>>,
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,
}
}
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);
}
}