Skip to content

Martingale42/nautilus-dev / skills/nt-signals

nt-signals

Unverified

Use when working with indicators, signal generation, bar aggregation, custom data types, analysis statistics, or tearsheets in NautilusTrader.

FileKindSize
SKILL.mdskill md7.8 KB
references/api/.gitkeepother0 B
references/api/analysis.mdmarkdown534 B
references/api/book.mdmarkdown160 B
references/api/data.mdmarkdown154 B
references/api/indicators.mdmarkdown1.2 KB
references/api/portfolio.mdmarkdown380 B
references/concepts/.gitkeepother0 B
references/concepts/data.mdmarkdown68.9 KB
references/concepts/portfolio.mdmarkdown6.7 KB
references/concepts/reports.mdmarkdown16.2 KB
references/concepts/visualization.mdmarkdown19.3 KB
references/examples/.gitkeepother0 B
references/examples/bar_aggregation/README.mdmarkdown500 B
references/examples/bar_aggregation/run_example.pyscript3.9 KB
references/examples/bar_aggregation/strategy.pyscript5.1 KB
references/examples/cascaded_indicators/README.mdmarkdown839 B
references/examples/cascaded_indicators/run_example.pyscript4.2 KB
references/examples/cascaded_indicators/strategy.pyscript4.7 KB
references/examples/indicators/README.mdmarkdown632 B
references/examples/indicators/run_example.pyscript3.8 KB
references/examples/indicators/strategy.pyscript5.3 KB
references/guides/.gitkeepother0 B
references/guides/custom_data_patterns.mdmarkdown10.4 KB
references/guides/indicators_guide.mdmarkdown14.6 KB
references/python/analysis/__init__.pyscript5.2 KB
references/python/analysis/analyzer.pyscript16.6 KB
references/python/analysis/config.pyscript6.4 KB
references/python/analysis/reporter.pyscript6.2 KB
references/python/analysis/statistic.pyscript3.8 KB
references/python/analysis/tearsheet.pyscript58.4 KB
references/python/analysis/themes.pyscript6.3 KB
references/rust/analysis/Cargo.tomlmanifest1.1 KB
references/rust/analysis/LICENSEother13 B
references/rust/analysis/README.mdmarkdown2.9 KB
references/rust/analysis/src/analyzer.rsscript31.1 KB
references/rust/analysis/src/lib.rsscript3.2 KB
references/rust/analysis/src/python/analyzer.rsscript13.0 KB
references/rust/analysis/src/python/mod.rsscript2.7 KB
references/rust/analysis/src/python/statistics/cagr.rsscript1.6 KB
references/rust/analysis/src/python/statistics/calmar_ratio.rsscript1.6 KB
references/rust/analysis/src/python/statistics/expectancy.rsscript1.9 KB
references/rust/analysis/src/python/statistics/long_ratio.rsscript2.7 KB
references/rust/analysis/src/python/statistics/loser_avg.rsscript1.9 KB
references/rust/analysis/src/python/statistics/loser_max.rsscript1.9 KB
references/rust/analysis/src/python/statistics/loser_min.rsscript1.9 KB
references/rust/analysis/src/python/statistics/max_drawdown.rsscript1.5 KB
references/rust/analysis/src/python/statistics/mod.rsscript1.6 KB
references/rust/analysis/src/python/statistics/profit_factor.rsscript1.8 KB
references/rust/analysis/src/python/statistics/returns_avg.rsscript1.8 KB
references/rust/analysis/src/python/statistics/returns_avg_loss.rsscript1.8 KB
references/rust/analysis/src/python/statistics/returns_avg_win.rsscript1.8 KB
references/rust/analysis/src/python/statistics/returns_volatility.rsscript1.9 KB
references/rust/analysis/src/python/statistics/risk_return_ratio.rsscript1.8 KB
references/rust/analysis/src/python/statistics/sharpe_ratio.rsscript1.9 KB
references/rust/analysis/src/python/statistics/sortino_ratio.rsscript1.9 KB
references/rust/analysis/src/python/statistics/win_rate.rsscript1.9 KB
references/rust/analysis/src/python/statistics/winner_avg.rsscript1.9 KB
references/rust/analysis/src/python/statistics/winner_max.rsscript1.9 KB
references/rust/analysis/src/python/statistics/winner_min.rsscript1.9 KB
references/rust/analysis/src/statistic.rsscript4.1 KB
references/rust/analysis/src/statistics/cagr.rsscript7.0 KB
references/rust/analysis/src/statistics/calmar_ratio.rsscript5.5 KB
references/rust/analysis/src/statistics/expectancy.rsscript6.8 KB
references/rust/analysis/src/statistics/long_ratio.rsscript8.0 KB
references/rust/analysis/src/statistics/loser_avg.rsscript4.1 KB
references/rust/analysis/src/statistics/loser_max.rsscript4.1 KB
references/rust/analysis/src/statistics/loser_min.rsscript4.2 KB
references/rust/analysis/src/statistics/max_drawdown.rsscript4.8 KB
references/rust/analysis/src/statistics/mod.rsscript1.3 KB
references/rust/analysis/src/statistics/profit_factor.rsscript5.8 KB
references/rust/analysis/src/statistics/returns_avg.rsscript3.9 KB
references/rust/analysis/src/statistics/returns_avg_loss.rsscript4.5 KB
references/rust/analysis/src/statistics/returns_avg_win.rsscript4.0 KB
references/rust/analysis/src/statistics/returns_volatility.rsscript4.9 KB
references/rust/analysis/src/statistics/risk_return_ratio.rsscript4.3 KB
references/rust/analysis/src/statistics/sharpe_ratio.rsscript4.8 KB
references/rust/analysis/src/statistics/sortino_ratio.rsscript5.1 KB
references/rust/analysis/src/statistics/win_rate.rsscript4.1 KB
references/rust/analysis/src/statistics/winner_avg.rsscript3.6 KB
references/rust/analysis/src/statistics/winner_max.rsscript3.6 KB
references/rust/analysis/src/statistics/winner_min.rsscript4.0 KB
templates/.gitkeepother0 B
templates/custom_data.pyscript2.0 KB
templates/indicator.pyscript2.6 KB
templates/portfolio_statistic.pyscript2.3 KB

references/rust/analysis/src/statistics/cagr.rs

View on GitHub
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Compound Annual Growth Rate (CAGR) statistic.

use std::collections::BTreeMap;

use nautilus_core::UnixNanos;

use crate::statistic::PortfolioStatistic;

/// Calculates the Compound Annual Growth Rate (CAGR) for returns.
///
/// CAGR represents the mean annual growth rate of an investment over a specified period,
/// assuming the profits were reinvested at the end of each period.
///
/// Formula: CAGR = (Ending Value / Beginning Value)^(Period/Days) - 1
///
/// For returns: CAGR = ((1 + Total Return)^(Period/Days)) - 1
#[repr(C)]
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.analysis", from_py_object)
)]
pub struct CAGR {
    /// The number of periods per year for annualization (e.g., 252 for trading days).
    pub period: usize,
}

impl CAGR {
    /// Creates a new [`CAGR`] instance.
    #[must_use]
    pub fn new(period: Option<usize>) -> Self {
        Self {
            period: period.unwrap_or(252),
        }
    }
}

impl PortfolioStatistic for CAGR {
    type Item = f64;

    fn name(&self) -> String {
        format!("CAGR ({} days)", self.period)
    }

    fn calculate_from_returns(&self, returns: &BTreeMap<UnixNanos, f64>) -> Option<Self::Item> {
        if returns.is_empty() {
            return Some(0.0);
        }

        // Downsample to daily bins to count actual trading days (not calendar days or trade count)
        let daily_returns = self.downsample_to_daily_bins(returns);

        // Calculate total return (cumulative)
        let total_return: f64 = daily_returns.values().map(|&r| 1.0 + r).product::<f64>() - 1.0;

        // Use the number of trading days (bins) for annualization
        // Minimum of 1 day to handle intraday-only strategies
        let days = daily_returns.len().max(1) as f64;

        // CAGR = (1 + total_return)^(period/days) - 1
        let cagr = (1.0 + total_return).powf(self.period as f64 / days) - 1.0;

        if cagr.is_finite() {
            Some(cagr)
        } else {
            Some(0.0)
        }
    }
}

#[cfg(test)]
mod tests {
    use rstest::rstest;

    use super::*;

    fn create_returns(values: Vec<f64>) -> BTreeMap<UnixNanos, f64> {
        let mut returns = BTreeMap::new();
        let nanos_per_day = 86_400_000_000_000;
        let start_time = 1_600_000_000_000_000_000;

        for (i, &value) in values.iter().enumerate() {
            let timestamp = start_time + i as u64 * nanos_per_day;
            returns.insert(UnixNanos::from(timestamp), value);
        }

        returns
    }

    #[rstest]
    fn test_name() {
        let cagr = CAGR::new(Some(252));
        assert_eq!(cagr.name(), "CAGR (252 days)");
    }

    #[rstest]
    fn test_empty_returns() {
        let cagr = CAGR::new(Some(252));
        let returns = BTreeMap::new();
        let result = cagr.calculate_from_returns(&returns);
        assert_eq!(result, Some(0.0));
    }

    #[rstest]
    fn test_positive_cagr() {
        let cagr = CAGR::new(Some(252));
        // Simulate 252 days with 0.1% daily return
        // Total return = (1.001)^252 - 1 ≈ 0.288 (28.8%)
        // CAGR should be approximately same as total return for full year
        let returns = create_returns(vec![0.001; 252]);
        let result = cagr.calculate_from_returns(&returns).unwrap();

        // For 252 days of 0.1% daily return
        // CAGR = (1 + 0.288)^(252/252) - 1 = 0.288
        assert!((result - 0.288).abs() < 0.01);
    }

    #[rstest]
    fn test_cagr_half_year() {
        let cagr = CAGR::new(Some(252));
        // Simulate 126 days (half year) with total return of 10%
        let daily_return = (1.10_f64.powf(1.0 / 126.0)) - 1.0;
        let returns = create_returns(vec![daily_return; 126]);
        let result = cagr.calculate_from_returns(&returns).unwrap();

        // CAGR should annualize the 10% half-year return
        // CAGR = (1.10)^(252/126) - 1 = (1.10)^2 - 1 ≈ 0.21 (21%)
        assert!((result - 0.21).abs() < 0.01);
    }

    #[rstest]
    fn test_negative_returns() {
        let cagr = CAGR::new(Some(252));
        // Simulate losses
        let returns = create_returns(vec![-0.001; 252]);
        let result = cagr.calculate_from_returns(&returns).unwrap();

        // Should be negative
        assert!(result < 0.0);
    }

    #[rstest]
    fn test_multiple_trades_per_day() {
        let cagr = CAGR::new(Some(252));

        // Simulate 500 trades over 252 days
        let mut returns = BTreeMap::new();
        let nanos_per_day = 86_400_000_000_000;
        let start_time = 1_600_000_000_000_000_000;

        // Create 500 trades with small returns spread across 252 days (~2 trades per day)
        for i in 0..500 {
            let day = (i * 252) / 500; // Map trade index to day
            let timestamp =
                start_time + day as u64 * nanos_per_day + (i % 3) as u64 * 1_000_000_000;
            returns.insert(UnixNanos::from(timestamp), 0.0005);
        }

        let result = cagr.calculate_from_returns(&returns).unwrap();

        // With downsample_to_daily_bins, we get 252 bins (trading days)
        // Daily returns are aggregated, then we compound and annualize
        // The CAGR should reflect 252 trading days, NOT 500 trades
        assert!((result - 0.285).abs() < 0.02);
        assert!(result > 0.2); // Should be much higher than what trade-count formula would give
    }

    #[rstest]
    fn test_intraday_trading() {
        let cagr = CAGR::new(Some(252));

        // Simulate multiple trades within a single day
        let mut returns = BTreeMap::new();
        let start_time = 1_600_000_000_000_000_000;

        // 10 trades within the same day, each with 1% return
        for i in 0..10 {
            let timestamp = start_time + i as u64 * 3_600_000_000_000; // 1 hour apart
            returns.insert(UnixNanos::from(timestamp), 0.01);
        }

        let result = cagr.calculate_from_returns(&returns).unwrap();

        // Total return: (1.01)^10 - 1 ≈ 0.1046 (10.46%)
        // This should be treated as 1 trading day
        // Annualized: (1.1046)^(252/1) - 1 = very large number
        // The key is it should NOT return 0.0
        assert!(result > 0.0);
        assert!(result.is_finite());
    }
}
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall