diff --git a/src/strategy.rs b/src/strategy.rs index f30b632..16efa99 100644 --- a/src/strategy.rs +++ b/src/strategy.rs @@ -323,259 +323,6 @@ impl PositionStrategy for HiddenTrailingStop { } } -// #[derive(Clone, Debug)] -// pub struct TrailingStop { -// stop_percentages: HashMap, -// capital_max_loss: f64, -// capital_min_profit: f64, -// capital_good_profit: f64, -// min_profit_trailing_delta: f64, -// good_profit_trailing_delta: f64, -// leverage: f64, -// min_profit_percentage: f64, -// good_profit_percentage: f64, -// max_loss_percentage: f64, -// } -// -// impl TrailingStop { -// fn update_stop_percentage(&mut self, position: &Position) -> Option { -// let mut order_form = None; -// -// 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, -// }; -// -// if let Some(profit_state_delta) = profit_state_delta { -// let current_stop_percentage = position.pl_perc() - profit_state_delta; -// let price_percentage_delta = { -// if position.is_short() { -// // 1.0 is the base price -// 1.0 - current_stop_percentage / 100.0 -// } else { -// 1.0 + current_stop_percentage / 100.0 -// } -// }; -// -// println!("Delta: {}", price_percentage_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); -// -// trace!("Setting trailing stop, asking order manager to cancel previous orders."); -// order_form = Some( -// OrderForm::new( -// position.pair().clone(), -// OrderKind::Limit { -// price: position.base_price() * price_percentage_delta, -// }, -// position.platform(), -// position.amount().neg(), -// ) -// .with_metadata(OrderMetadata::with_position_id(position.id())), -// ); -// } -// Some(existing_threshold) => { -// // follow and update trailing stop -// if existing_threshold < ¤t_stop_percentage { -// self.stop_percentages -// .insert(position.id(), current_stop_percentage); -// -// trace!("Updating threshold, asking order manager to cancel previous orders."); -// order_form = Some( -// OrderForm::new( -// position.pair().clone(), -// OrderKind::Limit { -// price: position.base_price() * price_percentage_delta, -// }, -// position.platform(), -// position.amount().neg(), -// ) -// .with_metadata(OrderMetadata::with_position_id(position.id())), -// ); -// } -// } -// } -// } -// } -// -// info!( -// "\tState: {:?} | PL: {:0.2}{} ({:0.2}%) | Stop: {:0.2}", -// position.profit_state().unwrap(), -// position.pl(), -// position.pair().quote(), -// position.pl_perc(), -// self.stop_percentages.get(&position.id()).unwrap_or(&0.0) -// ); -// } -// -// order_form -// } -// } -// -// impl Default for TrailingStop { -// fn default() -> Self { -// let leverage = 5.0; -// -// // in percentage -// let capital_max_loss = 15.0; -// let capital_min_profit = 1.0; -// let capital_good_profit = 6.0; -// -// let weighted_min_profit = capital_min_profit / leverage; -// let weighted_good_profit = capital_good_profit / leverage; -// let weighted_max_loss = capital_max_loss / leverage; -// -// let min_profit_trailing_delta = weighted_min_profit * 0.2; -// let good_profit_trailing_delta = weighted_good_profit * 0.08; -// -// let min_profit_percentage = weighted_min_profit + min_profit_trailing_delta; -// let good_profit_percentage = weighted_good_profit + good_profit_trailing_delta; -// let max_loss_percentage = -weighted_max_loss; -// -// TrailingStop { -// stop_percentages: Default::default(), -// capital_max_loss, -// capital_min_profit, -// capital_good_profit, -// min_profit_trailing_delta, -// good_profit_trailing_delta, -// leverage, -// min_profit_percentage, -// good_profit_percentage, -// max_loss_percentage, -// } -// } -// } -// impl PositionStrategy for TrailingStop { -// fn name(&self) -> String { -// "Trailing Stop".into() -// } -// -// /// Sets the profit state of an open position -// fn on_tick( -// &mut self, -// position: Position, -// current_tick: u64, -// positions_history: &HashMap, -// ) -> (Position, Option>, Option>) { -// let pl_perc = position.pl_perc(); -// -// let state = { -// if pl_perc > self.good_profit_percentage { -// PositionProfitState::Profit -// } else if (self.min_profit_percentage..self.good_profit_percentage).contains(&pl_perc) { -// PositionProfitState::MinimumProfit -// } else if (0.0..self.min_profit_percentage).contains(&pl_perc) { -// PositionProfitState::BreakEven -// } else if (self.max_loss_percentage..0.0).contains(&pl_perc) { -// PositionProfitState::Loss -// } else { -// PositionProfitState::Critical -// } -// }; -// -// let opt_prev_position = positions_history.get(&(current_tick - 1)); -// let event_metadata = EventMetadata::new(Some(position.id()), None); -// let new_position = position.with_profit_state(Some(state)); -// -// match opt_prev_position { -// Some(prev) => { -// if prev.profit_state() == Some(state) { -// return (new_position, None, None); -// } -// } -// 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( -// EventKind::ReachedMaxLoss, -// current_tick, -// Some(event_metadata), -// )); -// } -// -// events -// }; -// -// (new_position, Some(events), None) -// } -// -// fn post_tick( -// &mut self, -// position: Position, -// _: u64, -// _: &HashMap, -// ) -> (Position, Option>, Option>) { -// let close_message = ActionMessage::ClosePosition { -// position_id: position.id(), -// }; -// -// // if critical, early return with close position -// if let Some(PositionProfitState::Critical) = position.profit_state() { -// info!("Maximum loss reached. Closing position."); -// return (position, None, Some(vec![close_message])); -// }; -// -// // let's check if we surpassed an existing stop percentage -// if let Some(existing_stop_percentage) = self.stop_percentages.get(&position.id()) { -// if &position.pl_perc() <= existing_stop_percentage { -// info!("Stop percentage surpassed. Closing position."); -// return (position, None, Some(vec![close_message])); -// } -// } -// -// // updated or new trailing stop. should cancel orders and submit new one -// if let Some(order_form) = self.update_stop_percentage(&position) { -// let mut messages = vec![]; -// -// messages.push(ActionMessage::ClosePositionOrders { -// position_id: position.id(), -// }); -// messages.push(ActionMessage::SubmitOrder { order: order_form }); -// -// return (position, None, Some(messages)); -// } -// -// (position, None, None) -// } -// } - /* * ORDER STRATEGIES */