第一版

This commit is contained in:
YuWuKunCheng
2026-05-25 02:16:35 +08:00
parent ff4e6ae570
commit f92eef11fb
37 changed files with 11615 additions and 0 deletions
+348
View File
@@ -0,0 +1,348 @@
use crate::structure::dash_line::线;
use crate::structure::fractal_obj::;
use crate::types::;
use std::rc::Rc;
/// 中枢 — 三段虚线重叠区间构成的价格中枢
#[derive(Debug, Clone)]
pub struct {
pub : i64,
pub : String,
pub : i64,
pub : Vec<Rc<线>>,
pub 线: Option<Rc<线>>,
pub _第三买卖线: Option<Rc<线>>,
}
impl {
pub fn new(: i64, : String, : i64, : Vec<Rc<线>>) -> Self {
Self {
,
,
,
: .into_iter().take(3).collect(),
线: None,
_第三买卖线: None,
}
}
pub fn 线(&mut self, 线: Rc<线>) {
self..push(线);
self._第三买卖线 = None;
self.线 = None;
}
pub fn (&self) -> String {
format!(
"{}:{}:{}:{}",
self.().., self.().., self., self.
)
}
pub fn (&self) -> Rc<线> {
Rc::clone(&self.[self..len() - 1])
}
pub fn (&self) -> {
self.[0].().()
}
pub fn (&self) -> f64 {
self.[..3]
.iter()
.map(|x| x.())
.min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or(0.0)
}
pub fn (&self) -> f64 {
self.[..3]
.iter()
.map(|x| x.())
.max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or(0.0)
}
pub fn (&self) -> f64 {
self.
.iter()
.map(|x| x.())
.max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or(0.0)
}
pub fn (&self) -> f64 {
self.
.iter()
.map(|x| x.())
.min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or(0.0)
}
pub fn (&self) -> Rc<> {
Rc::clone(&self.[0].)
}
pub fn (&self) -> Rc<> {
Rc::clone(&self.[self..len() - 1].)
}
pub fn 线(&mut self, 线: Rc<线>) {
self.线 = Some(线);
}
pub fn (&self) -> String {
let 线_str = match &self.线 {
Some(x) => format!("{}", x),
None => "None".to_string(),
};
let _第三买卖线_str = match &self._第三买卖线 {
Some(x) => format!("{}", x),
None => "None".to_string(),
};
format!(
"{}, {}, {}, 文:({},{}), 武:({},{}), {}, {}",
self.,
self.,
self.,
self.().,
crate::utils::format_f64_g(self.().),
self.().,
crate::utils::format_f64_g(self.().),
线_str,
_第三买卖线_str,
)
}
/// 校验中枢合法性
pub fn (&self, 线: &[Rc<线>], _中枢序列: &[Rc<>]) -> bool {
// 1. 检查基础序列中的元素是否仍在虚线序列中
for in &self. {
if !线.iter().any(|x| Rc::as_ptr(x) == Rc::as_ptr()) {
return false;
}
}
// 2. 检查前三根重叠是否有效
if self..len() >= 3 {
let = self.();
let = self.();
if <= {
return false;
}
}
true
}
// ---- 关联函数 ----
/// 基础检查 — 三根虚线是否能形成中枢
pub fn (: &线, : &线, : &线) -> bool {
if !.() || !.() {
return false;
}
let = [.(), .(), .()]
.iter()
.copied()
.min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or(0.0);
let = [.(), .(), .()]
.iter()
.copied()
.max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or(0.0);
>
}
/// 创建中枢
pub fn (: Rc<线>, : Rc<线>, : Rc<线>, : i64, : &str) -> Self {
Self::new(0, format!("{}中枢<{}>", , .), , vec![, , ])
}
/// 从序列中获取中枢
pub fn (
线: &[Rc<线>],
: ,
: &str,
) -> Option<Rc<>> {
for i in 2..线.len() {
let = &线[i - 2];
let = &线[i - 1];
let = &线[i];
if Self::(, , ) && .() == {
let = Self::(Rc::clone(), Rc::clone(), Rc::clone(), 0, );
return Some(Rc::new());
}
}
None
}
/// 向中枢序列尾部添加
pub fn (: &mut Vec<Rc<>>, : Rc<>) {
// Dedup
if let Some() = .last() {
if .() == .() && .() == .() {
return;
}
}
// Set序号: Python — if中枢序列: 待添加中枢.序号 = 中枢序列[-1].序号 + 1 else: stays 0
if let Some() = .last() {
let = . + 1;
// Clone to mutate since Rc is immutable
let mut cloned = (*).clone();
cloned. = ;
.push(Rc::new(cloned));
} else {
.push();
}
}
/// 从中枢序列尾部弹出
pub fn (: &mut Vec<Rc<>>, : &Rc<>) -> Option<Rc<>> {
if .last().map(|x| Rc::as_ptr(x)) == Some(Rc::as_ptr()) {
.pop()
} else {
None
}
}
/// 中枢分析 — 从虚线序列生成中枢序列(增量算法)
///
/// 每收到新的虚线序列数据后调用,更新中枢序列
pub fn (
线: &[Rc<线>],
: &mut Vec<Rc<>>,
: bool,
: &str,
: i64,
) {
if 线.len() < 3 {
return;
}
// 初始化第一个中枢
if .is_empty() {
for i in 1..线.len() - 1 {
let = &线[i - 1];
let = &线[i];
let = &线[i + 1];
if Self::(, , ) {
// Python: 序号 = 虚线序列.index(左)
let = 线.iter().position(|x| Rc::as_ptr(x) == Rc::as_ptr()).unwrap_or(i - 1);
if && (. == 0 || == 0) {
continue;
}
if >= 2 {
let = &线[ - 2];
let = crate::types::::(.(), .(), .(), .());
if .() && .().() {
continue;
}
if .() && .().() {
continue;
}
}
let = Rc::new(Self::(
Rc::clone(),
Rc::clone(),
Rc::clone(),
.,
,
));
Self::(, );
// Python: return 中枢递归分析(虚线序列, 中枢序列, ...)
Self::(线, , , , );
return;
}
}
return;
}
// 增量更新
let _idx = .len() - 1;
let = Rc::clone(&[_idx]);
if !.(线, ) {
Self::(, &);
Self::(线, , , , );
return;
}
// 找到当前中枢最后一个元素在虚线序列中的位置
let = &.[..len() - 1];
let = match 线
.iter()
.position(|x| Rc::as_ptr(x) == Rc::as_ptr())
{
Some(idx) => idx + 1,
None => return,
};
let = .();
let = .();
let mut : Vec<Rc<线>> = Vec::new();
let mut = (*).clone();
for i in ..线.len() {
let 线 = Rc::clone(&线[i]);
// 检查是否超出中枢范围(缺口)
if crate::types::::(, , 线.(), 线.()).() {
.push(线.clone());
// Python: if 当前中枢.基础序列[-1].之后是(当前虚线):
if ..last().unwrap().(&线) {
.线(线.clone());
}
} else {
if .is_empty() {
// 仍在范围内:延伸中枢
.线(线);
} else {
.push(线);
}
}
// 候选序列积满3个:尝试创建新中枢
while .len() >= 3 {
let = ..last().unwrap().().();
match Self::(&, , ) {
Some() => {
[_idx] = Rc::new(.clone());
Self::(, );
return;
}
None => {
.remove(0); // 滑动窗口
}
}
}
}
// 更新当前中枢
[_idx] = Rc::new();
}
}
impl std::fmt::Display for {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let _str = self
.
.iter()
.map(|d| format!("{}", d))
.collect::<Vec<_>>()
.join(", ");
write!(
f,
"{}({}, {}, 元素数量: {}, [{}], {} ===>>> {})",
self.,
crate::utils::format_f64_g(self.()),
crate::utils::format_f64_g(self.()),
self..len(),
_str,
self.(),
self.(),
)
}
}