refactored update for position manager
This commit is contained in:
parent
50c961ec31
commit
0d48d3768a
@ -1,4 +1,5 @@
|
|||||||
#![feature(drain_filter)]
|
#![feature(drain_filter)]
|
||||||
|
#![feature(bool_to_option)]
|
||||||
|
|
||||||
use fern::colors::{Color, ColoredLevelConfig};
|
use fern::colors::{Color, ColoredLevelConfig};
|
||||||
use log::LevelFilter::Debug;
|
use log::LevelFilter::Debug;
|
||||||
|
@ -3,7 +3,7 @@ use std::collections::HashMap;
|
|||||||
use crate::connectors::{Client, ExchangeKind};
|
use crate::connectors::{Client, ExchangeKind};
|
||||||
use crate::currency::SymbolPair;
|
use crate::currency::SymbolPair;
|
||||||
use crate::events::{Event, SignalKind};
|
use crate::events::{Event, SignalKind};
|
||||||
use crate::models::{Order, Position, PriceTicker};
|
use crate::models::{Order, OrderForm, Position, PriceTicker};
|
||||||
use crate::strategy::PositionStrategy;
|
use crate::strategy::PositionStrategy;
|
||||||
use crate::BoxError;
|
use crate::BoxError;
|
||||||
|
|
||||||
@ -160,51 +160,55 @@ impl PositionManager {
|
|||||||
self.current_tick
|
self.current_tick
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update(&mut self, tick: u64) -> Result<Option<Vec<Event>>, BoxError> {
|
pub async fn update(
|
||||||
|
&mut self,
|
||||||
|
tick: u64,
|
||||||
|
) -> Result<(Option<Vec<Event>>, Option<OrderForm>), BoxError> {
|
||||||
let opt_active_positions = self.client.active_positions(&self.pair).await?;
|
let opt_active_positions = self.client.active_positions(&self.pair).await?;
|
||||||
let mut events = vec![];
|
let mut events = vec![];
|
||||||
|
|
||||||
self.current_tick = tick;
|
self.current_tick = tick;
|
||||||
|
|
||||||
if opt_active_positions.is_none() {
|
|
||||||
return Ok(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
// we assume there is only ONE active position per pair
|
// we assume there is only ONE active position per pair
|
||||||
match opt_active_positions
|
match opt_active_positions {
|
||||||
.unwrap()
|
// no open positions, no events and no order forms returned
|
||||||
.into_iter()
|
None => return Ok((None, None)),
|
||||||
.filter(|x| x.pair() == &self.pair)
|
|
||||||
.next()
|
|
||||||
{
|
|
||||||
Some(position) => {
|
|
||||||
// applying strategy to position
|
|
||||||
let active_position = {
|
|
||||||
match &self.strategy {
|
|
||||||
Some(strategy) => {
|
|
||||||
let (pos, strategy_events, _) = strategy.on_new_tick(&position, &self);
|
|
||||||
|
|
||||||
events.extend(strategy_events);
|
Some(positions) => {
|
||||||
pos
|
// checking if there are positions open for our pair
|
||||||
}
|
match positions
|
||||||
None => position,
|
.into_iter()
|
||||||
|
.filter(|x| x.pair() == &self.pair)
|
||||||
|
.next()
|
||||||
|
{
|
||||||
|
// no open positions for our pair, setting active position to none
|
||||||
|
None => self.active_position = None,
|
||||||
|
|
||||||
|
// applying strategy to open position and saving into struct
|
||||||
|
Some(position) => {
|
||||||
|
let position_after_strategy = {
|
||||||
|
match &self.strategy {
|
||||||
|
Some(strategy) => {
|
||||||
|
let (pos, strategy_events, _) =
|
||||||
|
strategy.on_new_tick(&position, &self);
|
||||||
|
|
||||||
|
events.extend(strategy_events);
|
||||||
|
|
||||||
|
pos
|
||||||
|
}
|
||||||
|
None => position,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.positions_history
|
||||||
|
.insert(self.current_tick(), position_after_strategy.clone());
|
||||||
|
self.active_position = Some(position_after_strategy);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
self.positions_history
|
|
||||||
.insert(self.current_tick(), active_position.clone());
|
|
||||||
self.active_position = Some(active_position);
|
|
||||||
}
|
}
|
||||||
None => {
|
};
|
||||||
self.active_position = None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if events.is_empty() {
|
Ok(((events.is_empty().then_some(events)), None))
|
||||||
Ok(None)
|
|
||||||
} else {
|
|
||||||
Ok(Some(events))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn position_previous_tick(&self, id: u64, tick: Option<u64>) -> Option<&Position> {
|
pub fn position_previous_tick(&self, id: u64, tick: Option<u64>) -> Option<&Position> {
|
||||||
|
Loading…
Reference in New Issue
Block a user