copy_trading/position_manager/
market_update_loop.rs

1use crate::types::position::Position;
2use crate::types::position_manager::PositionManager;
3use crate::types::position_status::PositionStatus;
4use std::sync::Arc;
5
6impl PositionManager {
7  /// Monitor market price updates and trigger stop-loss checks for open positions. Receives market
8  /// updates from broadcast channel and forwards them to open positions for stop-loss processing.
9  pub async fn market_update_loop(position_manager: Arc<PositionManager>) {
10    let mut market_update_receiver = position_manager.market_update_receiver.resubscribe();
11    while let Ok(update) = market_update_receiver.recv().await {
12      // We have a position for the market of this update
13      if let Some(position_arc) = position_manager
14        .open_positions
15        .read()
16        .unwrap()
17        .get(&update.market_address)
18      {
19        let mut position = position_arc.lock().unwrap();
20        let status = position.status.clone();
21
22        // The position status is open, call position's stop loss logic
23        if status == PositionStatus::Open {
24          // Release the lock of this function so that process_market_update can acquire it
25          Position::process_market_update(&mut position, &update, &position_arc, &position_manager);
26          continue;
27        }
28      }
29    }
30  }
31}