Skip to content

Improve AMA indicator parity with Cython #2626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2025
Merged

Improve AMA indicator parity with Cython #2626

merged 2 commits into from
May 12, 2025

Conversation

nicolad
Copy link
Collaborator

@nicolad nicolad commented May 12, 2025

Detailed comparison

Concern Python / Cython (source of truth) Rust (before) Rust (after this PR)
Parameter validation ✔ Positive ints, slow > fast identical asserts
count increments now increments + tested
prior_value reset ✘ (left stale) cleared in reset
Comments accurate n/a ✘ (misleading) fixed
Test coverage full partial full parity

flowchart TD
    A[call<br>AdaptiveMovingAverage::new] --> B{Validate arguments}
    B -->|any zero| P1[[panic]]
    B -->|slow ≤ fast| P2[[panic]]
    B --> C[Construct fields<br>+ alpha_fast/slow<br>+ EfficiencyRatio::new]
    C --> D[[Ready instance]]
Loading
sequenceDiagram
    participant Caller
    participant AMA as AdaptiveMovingAverage
    participant ER  as EfficiencyRatio

    Caller ->> AMA: update_raw(v)
    AMA    ->> AMA: count += 1
    AMA    ->> AMA: first input?\n(no → skip init branch)
    AMA    ->> ER : update_raw(v)
    AMA    ->> AMA: sc = (ER.value · α_diff + α_slow)^2
    AMA    ->> AMA: value = prior + sc · (v - prior)
    AMA    ->> AMA: prior = value_old
    AMA    ->> AMA: initialized = ER.initialized()

Loading
stateDiagram-v2
    direction LR
    [*] --> Idle
    Idle   -->|first input| Primed
    Primed -->|ER window filled| Ready
    Ready  -->|reset()| Idle

Loading

Related to #2507


image

@nicolad nicolad requested a review from cjdsellers May 12, 2025 11:39
@nicolad nicolad self-assigned this May 12, 2025
@nicolad
Copy link
Collaborator Author

nicolad commented May 12, 2025

Ten practical ways to put AdaptiveMovingAverage (AMA) to work

# Use-case Why AMA adds value How you’d wire it in
1 Dynamic trend-following strategy AMA hugs price when markets are quiet and backs off in noisy bursts, reducing whipsaws vs. fixed-length EMAs. Feed live QuoteTicks; go long when price crosses above AMA, flat/short on cross-down.
2 Adaptive trade exit filter Instead of a static stop, trail the AMA a few ticks behind the position—lag expands in choppy phases, avoiding premature exits. Recompute AMA on every trade tick; update stop-loss to AMA(value) – k·ATR.
3 Signal confirmation layer Pair with fast oscillators (RSI, Stoch). Trade only when oscillator crosses and price is also on the AMA’s “right” side. Require RSI > 70 and price > AMA for shorts, the reverse for longs.
4 Volatility-weighted position sizing AMA’s α adapts to Efficiency Ratio; its smoothing constant is a real-time proxy for noise. Larger dispersion ⇒ smaller size. Compute sc = (ER·α_diff + α_slow)^2; scale order size by 1 / sc.
5 Regime detection (trend vs. range) Flat AMA (low slope) + low ER → ranging; steep AMA + high ER → trending. Monitor slope = (AMA_t – AMA_{t-1}) / price; switch strategies per regime.
6 Event-driven alpha tests Measure how quickly AMA “snaps” after news releases—fast convergence implies strong consensus. Timestamp news headline; record ticks until `
7 Portfolio rotation filter On daily bars across a watch-list, rank symbols by distance above/below their AMA; rotate into top momentum names. Batch-update AMA per symbol in a nightly job; sort by (close/AMA – 1).
8 Latency-robust back-tester Because AMA embeds its own noise filter, it’s forgiving of sparse historical trade data—fewer look-back artifacts than HMA/TEMA. Drive update_raw() with OHLC Bars instead of tick data.
9 Risk clock for market-making Widen spreads when AMA widens (indicating volatility) and tighten when AMA compresses. Compute σ of (price – AMA) over sliding window; map to spread tier.
10 Embedded metric in on-chain oracle AMA’s low-noise property lends itself to DeFi price oracles that must resist manipulation spikes but stay reactive. Run AMA in a Rust WASM module; publish AMA.value to the chain each block.

Implementation pointers

  • Period selection – Common combos: (ER=10, fast=2, slow=30) intraday; (ER=20, fast=2, slow=20) daily. Tune with walk-forward.
  • Feed choice – Pass PriceType::Last for mid-quotes; PriceType::Bid/Ask for aggressive market-making logic.
  • Thread safetyAdaptiveMovingAverage is #[repr(C)] but not Send + Sync; wrap in Mutex for multi-threaded engines.
  • Zero-state re-use – Call reset() between back-test runs to avoid cross-contamination of statistics.

@cjdsellers cjdsellers changed the title Indicators: AMA - parity with Cython. Indicators: AMA - parity with Cython May 12, 2025
@faysou
Copy link
Collaborator

faysou commented May 12, 2025

Thanks, I like your practical ways table, I suppose it's possible to get similar tables from LLMs for other indicators, it's useful.

@cjdsellers
Copy link
Member

Great work @nicolad!

Many thanks for the contribution 🙏, the chart and table are useful.

Copy link
Member

@cjdsellers cjdsellers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

@cjdsellers cjdsellers changed the title Indicators: AMA - parity with Cython Improve AMA indicator parity with Cython May 12, 2025
@cjdsellers cjdsellers merged commit f3920f6 into develop May 12, 2025
17 checks passed
@cjdsellers cjdsellers deleted the 2507-ama branch May 12, 2025 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants