cleanup of TrailingStop update_stop_percentage helper function

This commit is contained in:
Giulio De Pasquale 2021-02-25 19:38:39 +00:00
parent 1948b0f032
commit 9be8c2e6ff

View File

@ -119,28 +119,22 @@ impl TrailingStop {
fn update_stop_percentage(&mut self, position: &Position) {
if let Some(profit_state) = position.profit_state() {
let profit_state_delta = match profit_state {
PositionProfitState::MinimumProfit => Some(self.min_profit_trailing_delta),
PositionProfitState::Profit => Some(self.good_profit_trailing_delta),
_ => None,
PositionProfitState::MinimumProfit => self.min_profit_trailing_delta,
PositionProfitState::Profit => self.good_profit_trailing_delta,
_ => return
};
if let Some(profit_state_delta) = profit_state_delta {
let current_stop_percentage = position.pl_perc() - profit_state_delta;
let current_trailing_delta = position.pl_perc() - profit_state_delta;
if let PositionProfitState::MinimumProfit | PositionProfitState::Profit =
profit_state
{
match self.stop_percentages.get(&position.id()) {
None => {
self.stop_percentages
.insert(position.id(), current_stop_percentage);
.insert(position.id(), current_trailing_delta);
}
Some(existing_threshold) => {
if existing_threshold < &current_stop_percentage {
if existing_threshold < &current_trailing_delta {
self.stop_percentages
.insert(position.id(), current_stop_percentage);
}
}
.insert(position.id(), current_trailing_delta);
}
}
}