modified event constructor, added builder method with_metadata.
code cleanup
This commit is contained in:
parent
7252dd4f8b
commit
35d967e6d9
@ -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()
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
@ -93,7 +93,7 @@ impl HiddenTrailingStop {
|
|||||||
let current_stop_percentage = position.pl_perc() - profit_state_delta;
|
let current_stop_percentage = position.pl_perc() - profit_state_delta;
|
||||||
|
|
||||||
if let PositionProfitState::MinimumProfit | PositionProfitState::Profit =
|
if let PositionProfitState::MinimumProfit | PositionProfitState::Profit =
|
||||||
profit_state
|
profit_state
|
||||||
{
|
{
|
||||||
match self.stop_percentages.get(&position.id()) {
|
match self.stop_percentages.get(&position.id()) {
|
||||||
None => {
|
None => {
|
||||||
@ -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(
|
||||||
@ -577,8 +573,8 @@ impl OrderStrategy for MarketEnforce {
|
|||||||
*order.order_form().platform(),
|
*order.order_form().platform(),
|
||||||
order.order_form().amount(),
|
order.order_form().amount(),
|
||||||
)
|
)
|
||||||
.with_leverage(order.order_form().leverage())
|
.with_leverage(order.order_form().leverage())
|
||||||
.with_metadata(order.order_form().metadata().clone()),
|
.with_metadata(order.order_form().metadata().clone()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user