chanlun 26.5.2

基于缠论(缠中说禅)理论的量化技术分析核心库,支持流式数据处理和多周期联立分析。
Documentation
/*
 * MIT License
 *
 * Copyright (c) 2026 YuYuKunKun
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

use crate::kline::chan_kline::缠论K线;
use crate::types::分型结构;
use crate::types::相对方向;
use std::rc::Rc;

/// 分型 — 由三根缠K构成(可能缺左或右)
#[derive(Debug, Clone)]
pub struct 分型 {
    pub : Option<Rc<缠论K线>>,
    pub : Rc<缠论K线>,
    pub : Option<Rc<缠论K线>>,
    pub 结构: 分型结构,
    pub 时间戳: i64,
    pub 分型特征值: f64,
}

impl 分型 {
    pub fn new(
        : Option<Rc<缠论K线>>, : Rc<缠论K线>, : Option<Rc<缠论K线>>
    ) -> Self {
        let 结构 =.分型.unwrap_or(分型结构::);
        let 时间戳 =.时间戳;
        let 分型特征值 =.分型特征值;
        Self {,,,
            结构,
            时间戳,
            分型特征值,
        }
    }

    /// 左中右三组关系
    pub fn 关系组(&self) -> Option<(相对方向, 相对方向, 相对方向)> {
        let= self..as_ref()?;
        let= self..as_ref()?;
        Some((
            相对方向::分析(.,., self.., self..),
            相对方向::分析(self.., self..,.,.),
            相对方向::分析(.,.,.,.),
        ))
    }

    /// 分型强度 — 返回 "强"/"中"/"弱"/"未知"
    pub fn 强度(&self) -> &str {
        if self.结构 != 分型结构::&& self.结构 != 分型结构::{
            return "未知";
        }
        if self..is_none() || self..is_none() {
            return "未知";
        }

        if let Some(ref 关系组) = self.关系组() {
            if self.结构 == 分型结构::{
                if 关系组.2.是否向下() {
                    return "";
                } else if 关系组.2.是否向上() {
                    return "";
                } else {
                    return "";
                }
            } else if self.结构 == 分型结构::{
                if 关系组.2.是否向上() {
                    return "";
                } else if 关系组.2.是否向下() {
                    return "";
                } else {
                    return "";
                }
            }
        }

        if let (Some(ref), Some(ref)) = (&self., &self.) {
            if self.结构 == 分型结构::{
                if.标的K线.收盘价 >.标的K线.{
                    return "";
                } else if.标的K线.收盘价 > self..标的K线.{
                    return "";
                } else {
                    return "";
                }
            } else if self.结构 == 分型结构::{
                if.标的K线.收盘价 <.标的K线.{
                    return "";
                } else if.标的K线.收盘价 < self..标的K线.{
                    return "";
                } else {
                    return "";
                }
            }
        }
        "未知"
    }

    /// MACD柱子分型匹配 — 检查左中右MACD柱形成顶/底形态
    pub fn 与MACD柱子分型匹配(&self) -> bool {
        if let (Some(ref), Some(ref)) = (&self., &self.) {
            if self.结构 == 分型结构::{
                if let (Some(ref 左macd), Some(ref 中macd), Some(ref 右macd)) =
                    (&.标的K线.macd, &self..标的K线.macd, &.标的K线.macd)
                {
                    return 左macd.MACD柱 > 中macd.MACD柱 && 中macd.MACD柱 < 右macd.MACD柱;
                }
            }
            if self.结构 == 分型结构::{
                if let (Some(ref 左macd), Some(ref 中macd), Some(ref 右macd)) =
                    (&.标的K线.macd, &self..标的K线.macd, &.标的K线.macd)
                {
                    return 左macd.MACD柱 < 中macd.MACD柱 && 中macd.MACD柱 > 右macd.MACD柱;
                }
            }
        }
        false
    }

    /// 判断两个分型是否匹配
    pub fn 判断分型(: &Rc<分型>, : &Rc<分型>, 模式: &str) -> bool {
        match 模式 {
            "" => Rc::as_ptr() == Rc::as_ptr(),
            _ => false,
        }
    }

    /// 从缠K序列中获取以指定缠K为中元素的分型
    pub fn 从缠K序列中获取分型(
        K线序列: &[Rc<缠论K线>], : &Rc<缠论K线>
    ) -> Option<Self> {
        let idx = K线序列
            .iter()
            .position(|k| Rc::as_ptr(k) == Rc::as_ptr())?;
        let= if idx > 0 {
            Some(Rc::clone(&K线序列[idx - 1]))
        } else {
            None
        };
        let= if idx + 1 < K线序列.len() {
            Some(Rc::clone(&K线序列[idx + 1]))
        } else {
            None
        };
        Some(Self::new(, Rc::clone(),))
    }

    /// 向分型序列中添加新分型
    pub fn 向序列中添加(分型序列: &mut Vec<Rc<分型>>, 当前分型: Rc<分型>) {
        if 分型序列.is_empty() {
            if 当前分型.结构 != 分型结构::&& 当前分型.结构 != 分型结构::{
                panic!("首次添加分型不为 顶底 {:?}", 当前分型);
            }
        } else {
            let 前一个 = &分型序列[分型序列.len() - 1];
            if 前一个.结构 == 当前分型.结构 {
                panic!("分型相同无法添加 {:?} {:?}", 前一个, 当前分型);
            }
            if 前一个..is_none() {
                eprintln!("分型.向序列中添加, 分型异常 {:?}", 前一个);
            }
        }
        分型序列.push(当前分型);
    }
}

impl crate::types::fractal::有高低 for 分型 {
    fn (&self) -> f64 {
        self..}
    fn (&self) -> f64 {
        self..}
}

impl std::fmt::Display for 分型 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}<{}, {}, None: {}, None: {}>",
            self..分型.unwrap_or(crate::types::分型结构::),
            self.时间戳,
            crate::utils::format_f64_g(self.分型特征值),
            if self..is_none() { "True" } else { "False" },
            if self..is_none() { "True" } else { "False" },
        )
    }
}