ordermanager stub
This commit is contained in:
parent
dfd676612e
commit
268000b218
@ -10,6 +10,7 @@ use crate::events::{Dispatcher, Event, SignalKind};
|
||||
use crate::models::{ExecutedOrder, OrderForm, OrderKind, Position, PriceTicker};
|
||||
use crate::strategy::{FastOrderStrategy, OrderStrategy, PositionStrategy};
|
||||
use crate::BoxError;
|
||||
use tokio::sync::mpsc::Receiver;
|
||||
|
||||
pub struct EventManager {
|
||||
events: Vec<Event>,
|
||||
@ -206,8 +207,11 @@ impl PositionManager {
|
||||
}
|
||||
}
|
||||
|
||||
pub type TrackedPositionsMap = HashMap<u64, ExecutedOrder>;
|
||||
|
||||
pub struct OrderManager {
|
||||
tracked_positions: HashMap<u64, ExecutedOrder>,
|
||||
// receiver: Receiver<SignalKind>,
|
||||
tracked_positions: TrackedPositionsMap,
|
||||
pair: SymbolPair,
|
||||
open_orders: Vec<ExecutedOrder>,
|
||||
client: Client,
|
||||
@ -217,8 +221,14 @@ pub struct OrderManager {
|
||||
impl OrderManager {
|
||||
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 {
|
||||
// receiver,
|
||||
pair,
|
||||
open_orders: Vec::new(),
|
||||
client,
|
||||
@ -228,11 +238,17 @@ impl OrderManager {
|
||||
}
|
||||
|
||||
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.
|
||||
// If so, the strategy method is called, otherwise we open
|
||||
// an undercut limit order at the best current price.
|
||||
match self.tracked_positions.get(&position.id()) {
|
||||
Some(open_order) => self.strategy.on_position_close(open_order, &self),
|
||||
match open_order {
|
||||
Some(open_order) => {
|
||||
self.tracked_positions = self
|
||||
.strategy
|
||||
.on_position_close(open_order, &self.tracked_positions);
|
||||
}
|
||||
None => {
|
||||
let current_prices = self.client.current_prices(&self.pair).await?;
|
||||
let closing_price = self.best_closing_price(&position, ¤t_prices)?;
|
||||
@ -337,7 +353,6 @@ impl ExchangeManager {
|
||||
tick: u64,
|
||||
) -> Result<Option<Vec<Event>>, BoxError> {
|
||||
for mgr in &mut self.position_managers {
|
||||
println!("Manager: {:?}", mgr);
|
||||
mgr.update(tick).await?;
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ use std::fmt::{Debug, Formatter};
|
||||
use dyn_clone::DynClone;
|
||||
|
||||
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};
|
||||
|
||||
/***************
|
||||
@ -34,7 +34,11 @@ pub trait OrderStrategy: DynClone {
|
||||
fn on_update(&self);
|
||||
/// This method is called when the OrderManager is requested to close
|
||||
/// 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 {
|
||||
@ -170,7 +174,11 @@ impl OrderStrategy for FastOrderStrategy {
|
||||
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!()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user