diff --git a/src/events.rs b/src/events.rs index a0f370c..42f27f0 100644 --- a/src/events.rs +++ b/src/events.rs @@ -56,14 +56,19 @@ pub struct Event { } impl Event { - pub fn new(kind: EventKind, tick: u64, metadata: Option) -> Self { + pub fn new(kind: EventKind, tick: u64) -> Self { Event { kind, tick, - metadata, + metadata: None, } } + pub fn with_metadata(mut self, metadata: Option) -> Self { + self.metadata = metadata; + self + } + fn has_metadata(&self) -> bool { self.metadata.is_some() } diff --git a/src/strategy.rs b/src/strategy.rs index 1052c57..521142c 100644 --- a/src/strategy.rs +++ b/src/strategy.rs @@ -4,11 +4,11 @@ use std::fmt::{Debug, Formatter}; use dyn_clone::DynClone; use log::info; +use crate::BoxError; use crate::connectors::Connector; use crate::events::{ActionMessage, Event, EventKind, EventMetadata}; use crate::managers::OptionUpdate; use crate::models::{ActiveOrder, OrderBook, OrderForm, OrderKind, Position, PositionProfitState}; -use crate::BoxError; /*************** * DEFINITIONS @@ -93,7 +93,7 @@ impl HiddenTrailingStop { let current_stop_percentage = position.pl_perc() - profit_state_delta; if let PositionProfitState::MinimumProfit | PositionProfitState::Profit = - profit_state + profit_state { match self.stop_percentages.get(&position.id()) { None => { @@ -156,6 +156,7 @@ impl Default for HiddenTrailingStop { } } } + impl PositionStrategy for HiddenTrailingStop { fn name(&self) -> String { "Hidden Trailing Stop".into() @@ -197,45 +198,40 @@ impl PositionStrategy for HiddenTrailingStop { None => return (new_position, None, None), }; - let events = { - let mut events = vec![]; - - if state == PositionProfitState::Profit { - events.push(Event::new( - EventKind::ReachedGoodProfit, - current_tick, - Some(event_metadata), - )); - } else if state == PositionProfitState::MinimumProfit { - events.push(Event::new( - EventKind::ReachedMinProfit, - current_tick, - Some(event_metadata), - )); - } else if state == PositionProfitState::BreakEven { - events.push(Event::new( - EventKind::ReachedBreakEven, - current_tick, - Some(event_metadata), - )); - } else if state == PositionProfitState::Loss { - events.push(Event::new( - EventKind::ReachedLoss, - current_tick, - Some(event_metadata), - )); - } else { - events.push(Event::new( + let event = match state { + PositionProfitState::Critical => { + Event::new( EventKind::ReachedMaxLoss, current_tick, - Some(event_metadata), - )); + ) } + PositionProfitState::Loss => { + Event::new( + EventKind::ReachedLoss, + current_tick, + ) + } + PositionProfitState::BreakEven => { + Event::new( + EventKind::ReachedBreakEven, + current_tick, + ) + } + PositionProfitState::MinimumProfit => { + Event::new( + EventKind::ReachedMinProfit, + current_tick, + ) + } + PositionProfitState::Profit => { + Event::new( + EventKind::ReachedGoodProfit, + current_tick, + ) + } + }.with_metadata(Some(event_metadata)); - events - }; - - (new_position, Some(events), None) + (new_position, Some(vec![event]), None) } fn post_tick( @@ -577,8 +573,8 @@ impl OrderStrategy for MarketEnforce { *order.order_form().platform(), order.order_form().amount(), ) - .with_leverage(order.order_form().leverage()) - .with_metadata(order.order_form().metadata().clone()), + .with_leverage(order.order_form().leverage()) + .with_metadata(order.order_form().metadata().clone()), }) }