ordermanager stub

This commit is contained in:
Giulio De Pasquale 2021-01-16 19:51:13 +00:00
parent dfd676612e
commit 268000b218
2 changed files with 31 additions and 8 deletions

View File

@ -10,6 +10,7 @@ use crate::events::{Dispatcher, Event, SignalKind};
use crate::models::{ExecutedOrder, OrderForm, OrderKind, Position, PriceTicker}; use crate::models::{ExecutedOrder, OrderForm, OrderKind, Position, PriceTicker};
use crate::strategy::{FastOrderStrategy, OrderStrategy, PositionStrategy}; use crate::strategy::{FastOrderStrategy, OrderStrategy, PositionStrategy};
use crate::BoxError; use crate::BoxError;
use tokio::sync::mpsc::Receiver;
pub struct EventManager { pub struct EventManager {
events: Vec<Event>, events: Vec<Event>,
@ -206,8 +207,11 @@ impl PositionManager {
} }
} }
pub type TrackedPositionsMap = HashMap<u64, ExecutedOrder>;
pub struct OrderManager { pub struct OrderManager {
tracked_positions: HashMap<u64, ExecutedOrder>, // receiver: Receiver<SignalKind>,
tracked_positions: TrackedPositionsMap,
pair: SymbolPair, pair: SymbolPair,
open_orders: Vec<ExecutedOrder>, open_orders: Vec<ExecutedOrder>,
client: Client, client: Client,
@ -217,8 +221,14 @@ pub struct OrderManager {
impl OrderManager { impl OrderManager {
const UNDERCUT_PERC: f64 = 0.005; const UNDERCUT_PERC: f64 = 0.005;
pub fn new(pair: SymbolPair, client: Client, strategy: Box<dyn OrderStrategy>) -> Self { pub fn new(
// receiver: Receiver<SignalKind>,
pair: SymbolPair,
client: Client,
strategy: Box<dyn OrderStrategy>,
) -> Self {
OrderManager { OrderManager {
// receiver,
pair, pair,
open_orders: Vec::new(), open_orders: Vec::new(),
client, client,
@ -228,11 +238,17 @@ impl OrderManager {
} }
pub async fn close_position(&mut self, position: &Position) -> Result<(), BoxError> { pub async fn close_position(&mut self, position: &Position) -> Result<(), BoxError> {
let open_order = self.tracked_positions.get(&position.id());
// checking if the position has an open order. // checking if the position has an open order.
// If so, the strategy method is called, otherwise we open // If so, the strategy method is called, otherwise we open
// an undercut limit order at the best current price. // an undercut limit order at the best current price.
match self.tracked_positions.get(&position.id()) { match open_order {
Some(open_order) => self.strategy.on_position_close(open_order, &self), Some(open_order) => {
self.tracked_positions = self
.strategy
.on_position_close(open_order, &self.tracked_positions);
}
None => { None => {
let current_prices = self.client.current_prices(&self.pair).await?; let current_prices = self.client.current_prices(&self.pair).await?;
let closing_price = self.best_closing_price(&position, &current_prices)?; let closing_price = self.best_closing_price(&position, &current_prices)?;
@ -337,7 +353,6 @@ impl ExchangeManager {
tick: u64, tick: u64,
) -> Result<Option<Vec<Event>>, BoxError> { ) -> Result<Option<Vec<Event>>, BoxError> {
for mgr in &mut self.position_managers { for mgr in &mut self.position_managers {
println!("Manager: {:?}", mgr);
mgr.update(tick).await?; mgr.update(tick).await?;
} }

View File

@ -4,7 +4,7 @@ use std::fmt::{Debug, Formatter};
use dyn_clone::DynClone; use dyn_clone::DynClone;
use crate::events::{Event, EventKind, EventMetadata, SignalKind}; use crate::events::{Event, EventKind, EventMetadata, SignalKind};
use crate::managers::{OrderManager, PositionManager}; use crate::managers::{OrderManager, PositionManager, TrackedPositionsMap};
use crate::models::{ExecutedOrder, OrderForm, Position, PositionProfitState}; use crate::models::{ExecutedOrder, OrderForm, Position, PositionProfitState};
/*************** /***************
@ -34,7 +34,11 @@ pub trait OrderStrategy: DynClone {
fn on_update(&self); fn on_update(&self);
/// This method is called when the OrderManager is requested to close /// This method is called when the OrderManager is requested to close
/// a position that has an open order associated to it. /// a position that has an open order associated to it.
fn on_position_close(&self, order: &ExecutedOrder, manager: &mut OrderManager); fn on_position_close(
&self,
order: &ExecutedOrder,
tracked_positions: &HashMap<u64, ExecutedOrder>,
) -> TrackedPositionsMap;
} }
impl Debug for dyn OrderStrategy { impl Debug for dyn OrderStrategy {
@ -170,7 +174,11 @@ impl OrderStrategy for FastOrderStrategy {
unimplemented!() unimplemented!()
} }
fn on_position_close(&self, order: &ExecutedOrder, manager: &mut OrderManager) { fn on_position_close(
&self,
order: &ExecutedOrder,
tracked_positions: &HashMap<u64, ExecutedOrder>,
) -> TrackedPositionsMap {
unimplemented!() unimplemented!()
} }
} }