Files
QuanTAlib/lib/channels/maenv/maenv.md
T
86fe32a682 SIMD Refactor: Merge simd-dev into dev (#55)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
Co-authored-by: Warp <agent@warp.dev>
2026-01-18 19:02:03 -08:00

98 lines
3.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Moving Average Envelope
Moving Average Envelope consists of three lines: a moving average in the middle and two lines plotted at a fixed percentage above and below it. The envelope provides a simple way to identify potential support and resistance levels based on a percentage deviation from the average price.
## Calculation
```
Middle = MA(Source, Length)
Upper = Middle + (Middle × Percentage/100)
Lower = Middle - (Middle × Percentage/100)
```
Where:
* MA = Moving Average (can be SMA, EMA, or WMA)
* Source = Price series (typically close price)
* Length = Lookback period for moving average
* Percentage = Fixed percentage for band width
## Parameters
* Source (default: close) - Price series used for the moving average
* Length (default: 20) - Period used for moving average calculation
* Percentage (default: 1.0) - Fixed percentage distance from MA to bands
* MA Type (default: 1) - Moving average type: 0:SMA, 1:EMA, or 2:WMA
## Interpretation
* The middle line shows the average price trend
* Upper and lower bands create a channel based on fixed percentage
* Price reaching the bands may indicate overbought/oversold conditions
* Unlike volatility-based bands, envelope width changes proportionally with price
* Band penetration may signal potential trend reversals
* Works best in trending markets with consistent volatility
## Implementation
The implementation includes:
* Choice of three moving average types (SMA, EMA, WMA)
* Optimized calculations for each MA type
* Circular buffer for efficient SMA calculation
* Alpha smoothing for EMA
* Linear weighting for WMA
* Proper handling of NA values
* Input validation
* Percentage-based band width calculation
## Performance Profile
### Operation Count (Streaming Mode, per Bar)
| Operation | EMA Type | SMA Type | WMA Type | Cost |
| :--- | :---: | :---: | :---: | :---: |
| ADD/SUB | 2 | 2 | 1 | 1 cycle |
| MUL | 4 | 2 | 2 | 3 cycles |
| DIV | 0 | 1 | 1 | 15 cycles |
**Per-bar totals:**
- **EMA type**: 2×1 + 4×3 = ~14 cycles
- **SMA type**: 2×1 + 2×3 + 1×15 = ~23 cycles (running sum)
- **WMA type**: 1×1 + 2×3 + 1×15 = ~22 cycles (running sums)
### Complexity Analysis
| Mode | Complexity | Notes |
| :--- | :---: | :--- |
| Streaming (EMA) | O(1) | IIR recursion, constant time |
| Streaming (SMA) | O(1) | Running sum with circular buffer |
| Streaming (WMA) | O(1) | Incremental weight adjustment |
| Batch | O(n) | Linear scan, n = series length |
**Memory**: Fixed ~64 bytes state regardless of period.
### SIMD Analysis
| Optimization | Applicable | Notes |
| :--- | :---: | :--- |
| AVX2 vectorization | ❌ | EMA/SMA recursion prevents parallelization |
| FMA | ✅ | Band calculation: `Middle ± Middle × factor` |
| Batch parallelism | Partial | Band calc vectorizable after MA computed |
### Quality Metrics
| Metric | Score | Notes |
| :--- | :---: | :--- |
| **Accuracy** | 10/10 | Exact computation |
| **Timeliness** | 5/10 | MA lag inherited (period/2 for SMA) |
| **Overshoot** | 2/10 | Fixed percentage, no volatility adaptation |
| **Smoothness** | 7/10 | Follows MA smoothness |
## Validation
| Library | Status | Notes |
| :--- | :---: | :--- |
| **TA-Lib** | N/A | Not implemented |
| **Skender** | N/A | Not implemented |
| **Tulip** | N/A | Not implemented |
| **Ooples** | N/A | Not implemented |
| **Internal** | ✅ | Mode consistency verified |