← Назад к документации
position_machine
Исходный код Rust - Trading AI
📄 Rust
📦 Модуль
🔧 Исходный код
use crate::live::state::PositionSnapshot;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PositionPhase {
    Single,
    Both,
    Boosted,
    WindDown,
    ExitReady,
}

pub struct PositionMachine {
    pub phase: PositionPhase,
    pub cut_count: u32,
    pub boosted_once: bool,
}

impl PositionMachine {
    pub fn new() -> Self {
        Self {
            phase: PositionPhase::Single,
            cut_count: 0,
            boosted_once: false,
        }
    }

    pub fn reset(&mut self) {
        self.phase = PositionPhase::Single;
        self.cut_count = 0;
        self.boosted_once = false;
    }

    pub fn update_phase(&mut self, pos: &PositionSnapshot) {
        let has_long = pos.long_qty > 0.0;
        let has_short = pos.short_qty > 0.0;

        if has_long && has_short {
            if self.cut_count == 0 {
                self.phase = PositionPhase::Both;
            } else if self.cut_count == 1 {
                self.phase = PositionPhase::Boosted;
            } else if self.cut_count == 2 {
                self.phase = PositionPhase::WindDown;
            } else {
                self.phase = PositionPhase::ExitReady;
            }
        } else {
            self.phase = PositionPhase::Single;
        }
    }

    pub fn on_cut(&mut self) {
        self.cut_count += 1;
        self.boosted_once = true;
    }

    pub fn on_exit(&mut self) {
        self.reset();
    }
}