Created
December 9, 2024 23:39
-
-
Save jmcph4/d1baeebe6e204990585751f8f181a680 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn r#match<'a>( | |
| &mut self, | |
| order: T, | |
| opposing_side: impl Iterator<Item = (&'a F64, &'a mut VecDeque<T>)>, | |
| ) where | |
| T: 'a, | |
| { | |
| let reduce_depth = |reduction| { | |
| let curr_depth = *self.depth.read().unwrap(); | |
| match order.kind() { | |
| OrderKind::Bid => { | |
| *self.depth.write().unwrap() = | |
| (curr_depth.0, curr_depth.1.saturating_sub(reduction)) | |
| } | |
| OrderKind::Ask => { | |
| *self.depth.write().unwrap() = | |
| (curr_depth.0.saturating_sub(reduction), curr_depth.1) | |
| } | |
| } | |
| }; | |
| let mut ltp = order.price(); | |
| let mut quantity_remaining = order.quantity(); | |
| for (level, orders) in opposing_side { | |
| dbg!(&quantity_remaining, level); | |
| if quantity_remaining == 0 { | |
| break; | |
| } | |
| let side_in_range = || { | |
| if order.kind() == OrderKind::Bid { | |
| *level <= F64(order.price()) | |
| } else { | |
| *level >= F64(order.price()) | |
| } | |
| }; | |
| if side_in_range() { | |
| while let Some(incumbent) = orders.iter_mut().next() { | |
| if quantity_remaining > 0 { | |
| let incumbent_quantity = incumbent.quantity(); | |
| match incumbent_quantity.cmp(&quantity_remaining) { | |
| Ordering::Greater => { | |
| self.events.write().unwrap().push(Event::new( | |
| EventKind::Match(Match::Partial( | |
| MatchInfo { | |
| incumbent: incumbent.clone(), | |
| others: vec![( | |
| order.clone(), | |
| order.quantity(), | |
| )], | |
| }, | |
| )), | |
| )); | |
| *incumbent.quantity_mut() = incumbent | |
| .quantity() | |
| .saturating_sub(order.quantity()); | |
| quantity_remaining = 0; | |
| reduce_depth(order.quantity()); | |
| } | |
| Ordering::Equal => { | |
| self.events.write().unwrap().push(Event::new( | |
| EventKind::Match(Match::Full(MatchInfo { | |
| incumbent: incumbent.clone(), | |
| others: vec![( | |
| order.clone(), | |
| order.quantity(), | |
| )], | |
| })), | |
| )); | |
| self.removals | |
| .write() | |
| .unwrap() | |
| .push(incumbent.id()); | |
| quantity_remaining -= incumbent_quantity; | |
| reduce_depth(incumbent_quantity); | |
| } | |
| Ordering::Less => { | |
| self.events.write().unwrap().push(Event::new( | |
| EventKind::Match(Match::Full(MatchInfo { | |
| incumbent: incumbent.clone(), | |
| others: vec![( | |
| order.clone(), | |
| order.quantity(), | |
| )], | |
| })), | |
| )); | |
| self.removals | |
| .write() | |
| .unwrap() | |
| .push(incumbent.id()); | |
| quantity_remaining -= incumbent_quantity; | |
| reduce_depth(incumbent_quantity); | |
| } | |
| } | |
| ltp = incumbent.price(); | |
| } else { | |
| break; | |
| } | |
| } | |
| } else { | |
| break; | |
| } | |
| } | |
| /* post whatever remains */ | |
| if quantity_remaining > 0 { | |
| self.add_order(order); | |
| } | |
| *self.ltp.write().unwrap() = Some(ltp); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment