modified event constructor, added builder method with_metadata.

code cleanup
This commit is contained in:
Giulio De Pasquale 2021-02-20 19:27:15 +00:00
parent 7252dd4f8b
commit 35d967e6d9
2 changed files with 42 additions and 41 deletions

View File

@ -56,14 +56,19 @@ pub struct Event {
} }
impl Event { impl Event {
pub fn new(kind: EventKind, tick: u64, metadata: Option<EventMetadata>) -> Self { pub fn new(kind: EventKind, tick: u64) -> Self {
Event { Event {
kind, kind,
tick, tick,
metadata, metadata: None,
} }
} }
pub fn with_metadata(mut self, metadata: Option<EventMetadata>) -> Self {
self.metadata = metadata;
self
}
fn has_metadata(&self) -> bool { fn has_metadata(&self) -> bool {
self.metadata.is_some() self.metadata.is_some()
} }

View File

@ -4,11 +4,11 @@ use std::fmt::{Debug, Formatter};
use dyn_clone::DynClone; use dyn_clone::DynClone;
use log::info; use log::info;
use crate::BoxError;
use crate::connectors::Connector; use crate::connectors::Connector;
use crate::events::{ActionMessage, Event, EventKind, EventMetadata}; use crate::events::{ActionMessage, Event, EventKind, EventMetadata};
use crate::managers::OptionUpdate; use crate::managers::OptionUpdate;
use crate::models::{ActiveOrder, OrderBook, OrderForm, OrderKind, Position, PositionProfitState}; use crate::models::{ActiveOrder, OrderBook, OrderForm, OrderKind, Position, PositionProfitState};
use crate::BoxError;
/*************** /***************
* DEFINITIONS * DEFINITIONS
@ -156,6 +156,7 @@ impl Default for HiddenTrailingStop {
} }
} }
} }
impl PositionStrategy for HiddenTrailingStop { impl PositionStrategy for HiddenTrailingStop {
fn name(&self) -> String { fn name(&self) -> String {
"Hidden Trailing Stop".into() "Hidden Trailing Stop".into()
@ -197,45 +198,40 @@ impl PositionStrategy for HiddenTrailingStop {
None => return (new_position, None, None), None => return (new_position, None, None),
}; };
let events = { let event = match state {
let mut events = vec![]; PositionProfitState::Critical => {
Event::new(
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(
EventKind::ReachedMaxLoss, EventKind::ReachedMaxLoss,
current_tick, 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(vec![event]), None)
};
(new_position, Some(events), None)
} }
fn post_tick( fn post_tick(