# Indicator Math
A Rust library for technical analysis indicators (SMA, EMA, WMA, HMA, EHMA) and automated advanced trend analysis.
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
indicator_math = "0.7.0"
```
## การใช้งาน (Usage)
ฟังก์ชันหลักสำหรับการสร้างข้อมูลวิเคราะห์คือ `generate_analysis_data` ซึ่งจะคืนค่าออกมาเป็น Vector ของ Struct `EmaAnalysis`
```rust
use indicator_math::{Candle, generate_analysis_data, MaType};
// ... (เตรียมข้อมูล candles) ...
// เรียกใช้ฟังก์ชัน generate_analysis_data
// พารามิเตอร์: &candles, short_period, medium_period, long_period, short_type, medium_type, long_type
let analysis_results = generate_analysis_data(
&candles,
10, 20, 50,
MaType::EMA, MaType::EMA, MaType::EMA
// ... (Usage example)
// 4. Get simple action recommendation
let last_index = analysis_results.len() - 1;
let action_simple = get_action_by_simple(&analysis_results, last_index);
println!("Simple Action: {}", action_simple);
// 5. Get Action by Cut Type (New)
use indicator_math::CutStrategy; // Import Enum
// Strategy 1: Use Short/Medium Cut + Short Slope
let action_short = get_action_by_cut_type(&analysis_results, last_index, CutStrategy::ShortCut);
println!("Short Cut Action: {}", action_short);
// Strategy 2: Use Medium/Long Cut + Medium Slope
let action_long = get_action_by_cut_type(&analysis_results, last_index, CutStrategy::LongCut);
println!("Long Cut Action: {}", action_long);
```
## โครงสร้างข้อมูลผลลัพธ์ (Output Structure)
ข้อมูลที่ได้จากการวิเคราะห์จะอยู่ใน Struct `EmaAnalysis` โดยมีรายละเอียดดังนี้:
| `time_candle` | `u64` | เวลาของแท่งเทียน (Unix Timestamp) |
| `index` | `usize` | ลำดับข้อมูลใน Array (0, 1, 2, ...) |
| `color_candle` | `String` | สีของแท่งเทียนปัจจุบัน (`Green`, `Red`, `Equal`) |
| `next_color_candle` | `String` | สีของแท่งเทียนถัดไป (สำหรับดูอนาคตในการ Train AI) |
| **Short EMA** | | **ข้อมูลเส้นค่าเฉลี่ยระยะสั้น** |
| `ema_short_value` | `f64` | ค่าของ EMA Short |
| `ema_short_slope_value` | `f64` | ค่าความชัน (ส่วนต่างกับแท่งก่อนหน้า) |
| `ema_short_slope_direction` | `String` | ทิศทางความชัน (`Up`, `Down`, `Flat`) |
| `is_ema_short_turn_type` | `String` | การกลับตัวของเส้น (`TurnUp`, `TurnDown`, `None`) |
| `ema_short_cut_position` | `String` | ตำแหน่งราคาเทียบกับเส้น Short (`1`=เหนือ High, `2`=Wickบน, `B1`=บนBody, `B2`=กลางBody, `B3`=ล่างBody, `3`=Wickล่าง, `4`=ต่ำกว่า Low) |
| **Medium EMA** | | **ข้อมูลเส้นค่าเฉลี่ยระยะกลาง** |
| `ema_medium_value` | `f64` | ค่าของ EMA Medium |
| `ema_medium_slope_direction` | `String` | ทิศทางความชัน (`Up`, `Down`, `Flat`) |
| **Long EMA** | | **ข้อมูลเส้นค่าเฉลี่ยระยะยาว** |
| `ema_long_value` | `f64` | ค่าของ EMA Long |
| `ema_long_slope_direction` | `String` | ทิศทางความชัน (`Up`, `Down`, `Flat`) |
| **Relationships** | | **ความสัมพันธ์ระหว่างเส้น** |
| `ema_above` | `String` | เส้น Short เทียบ Medium (`ShortAbove`, `MediumAbove`) |
| `ema_long_above` | `String` | เส้น Medium เทียบ Long (`MediumAbove`, `LongAbove`) |
| `macd_12` | `f64` | ระยะห่างสัมบูรณ์ระหว่าง Short กับ Medium (Abs Diff) |
| `macd_23` | `f64` | ระยะห่างสัมบูรณ์ระหว่าง Medium กับ Long (Abs Diff) |
| **Convergence** | | **การลู่เข้า/ออกของเส้น** |
| `ema_convergence_type` | `String` | Short vs Medium (`divergence`=แยกออก, `convergence`=เข้าหา, `neutral`) |
| `ema_long_convergence_type`| `String` | Medium vs Long (`divergence`=แยกออก, `convergence`=เข้าหา, `neutral`) |
| **Trend Analysis** | | **การวิเคราะห์เทรนด์ (Golden/Death Cross)** |
| `ema_cut_short_type` | `String` | การตัดกันของ Short/Medium (`UpTrend`=ตัดขึ้น, `DownTrend`=ตัดลง, `None`) |
| `candles_since_short_cut` | `usize` | จำนวนแท่งเทียนนับตั้งแต่เกิดการตัดกันของ Short/Medium ล่าสุด |
| `ema_cut_long_type` | `String` | การตัดกันของ Medium/Long (`UpTrend`=Golden Cross, `DownTrend`=Death Cross, `None`) |
| `candles_since_ema_cut` | `usize` | จำนวนแท่งเทียนนับตั้งแต่เกิดการตัดกันของ Medium/Long ล่าสุด |
| **History** | | **ข้อมูลย้อนหลัง** |
| `previous_color_back1` | `String` | สีของแท่งเทียนก่อนหน้า 1 แท่ง |
| `previous_color_back3` | `String` | สีของแท่งเทียนก่อนหน้า 3 แท่ง |