rust #10
@ -1,4 +1,5 @@
|
||||
#![feature(drain_filter)]
|
||||
#![feature(bool_to_option)]
|
||||
|
||||
use fern::colors::{Color, ColoredLevelConfig};
|
||||
use log::LevelFilter::Debug;
|
||||
|
@ -3,7 +3,7 @@ use std::collections::HashMap;
|
||||
use crate::connectors::{Client, ExchangeKind};
|
||||
use crate::currency::SymbolPair;
|
||||
use crate::events::{Event, SignalKind};
|
||||
use crate::models::{Order, Position, PriceTicker};
|
||||
use crate::models::{Order, OrderForm, Position, PriceTicker};
|
||||
use crate::strategy::PositionStrategy;
|
||||
use crate::BoxError;
|
||||
|
||||
@ -160,51 +160,55 @@ impl PositionManager {
|
||||
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 mut events = vec![];
|
||||
|
||||
self.current_tick = tick;
|
||||
|
||||
if opt_active_positions.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// we assume there is only ONE active position per pair
|
||||
match opt_active_positions
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.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);
|
||||
match opt_active_positions {
|
||||
// no open positions, no events and no order forms returned
|
||||
None => return Ok((None, None)),
|
||||
|
||||
events.extend(strategy_events);
|
||||
pos
|
||||
}
|
||||
None => position,
|
||||
Some(positions) => {
|
||||
// checking if there are positions open for our pair
|
||||
match positions
|
||||
.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(None)
|
||||
} else {
|
||||
Ok(Some(events))
|
||||
}
|
||||
Ok(((events.is_empty().then_some(events)), None))
|
||||
}
|
||||
|
||||
pub fn position_previous_tick(&self, id: u64, tick: Option<u64>) -> Option<&Position> {
|
||||
|
Loading…
Reference in New Issue
Block a user