StrategyQuant
Q AI AssistantAlgoCloudQuantAnalyzerQuantDataManager
Free CourseCompany
BlogDocumentationForumContact us
BlogMarketplace
Log in
Create free account

Product

  • Pricing & licenses
  • Learn
  • Marketplace
  • Start free trial

Legal

  • Terms of Service
  • Privacy Policy
  • Cookies

Contact

[email protected]

Company

StrategyQuant s.r.o.

Korunní 2569/108, Vinohrady, 101 00 Praha 10, Czech Republic, EU

IČ: 04000498

DIČ: CZ04000498

Social

  • Legal Notice | PROCESSING AND PROTECTION OF PERSONAL DATA

    Past performance is no guarantee of future success. All information should be viewed as educational only

    UNITED STATES Disclaimer required by Government - Commodity Futures Trading Commission informs: Futures and options trading have great potential rewards, but also great potential risk. You must be aware of the risks and be willing to accept them in order to invest in the futures and options markets. Don't trade with money you can't afford to lose. This is neither a solicitation nor an offer to buy/sell futures, shares or options on the same. No representation is being made that any account will or is likely to realise profits or losses similar to those discussed in this document. The past performance of any trading system or methodology is not necessarily indicative of future results.

    CFTC RULE 4.41 - HYPOTHETICAL OR SIMULATED PERFORMANCE RESULTS HAVE CERTAIN LIMITATIONS. UNLIKE A RECORD OF ACTUAL PERFORMANCE, SIMULATED RESULTS DO NOT REPRESENT ACTUAL TRADING. ALSO, SINCE THE TRADES WERE NOT EXECUTED, THE RESULTS MAY HAVE OFFSET THE IMPACT, IF ANY, OF CERTAIN MARKET FACTORS, SUCH AS LACK OF LIQUIDITY. SIMULATED TRADING PROGRAMMES IN GENERAL ARE ALSO SUBJECT TO THE FACT THAT THEY ARE DESIGNED WITH THE BENEFIT OF HINDSIGHT. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO REALISE PROFITS OR LOSSES SIMILAR TO THOSE SHOWN.

    NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL, OR IS LIKELY TO, REALISE PROFITS OR LOSSES SIMILAR TO THOSE DISCUSSED WITHIN THIS SITE. IF YOU DECIDE TO INVEST REAL MONEY, ALL TRADING DECISIONS IN THE FINANCIAL MARKETS SHOULD BE YOUR OWN.

    © StrategyQuant 2026

    Powered by Klaudie.ai

    Back to marketplaceDIGITAL

    CUSUM

    StrategyQuantIndicators
    CUSUM

    Detects subtle but sustained shifts in price behavior using cumulative sum analysis. Tracks upward and downward deviations with configurable thresholds to generate reliable trading signals.

    CUSUM Indicator in StrategyQuant

    In today’s post, we’ll take a closer look at a custom implementation of the CUSUM (Cumulative Sum) indicator. Often used in statistical process control, the CUSUM technique helps detect small shifts in the mean level of a series. This particular implementation is designed for integration into StrategyQuant, offering traders a dynamic way to monitor and react to market changes.


    What Is CUSUM?

    CUSUM is a sequential analysis technique that accumulates deviations from a target value over time. By comparing the cumulative sum against defined thresholds, it becomes easier to spot even subtle shifts in price behavior. In our implementation, two separate accumulators—one for upward movements (CUSUM Positive) and one for downward movements (CUSUM Negative)—signal when the change in the underlying price series becomes significant.


    Code Walkthrough

    The indicator is defined as a Java class that extends

    IndicatorBlock

    , ensuring seamless integration with StrategyQuant’s ecosystem. Let’s break down the key components:


    Parameters and Inputs

    • Chart Data: The indicator uses a chart (typically the close price) as the data source.
    • Period: Determines the window size over which the rolling mean and standard deviation are calculated. This is crucial as it defines the sensitivity of the indicator.
    • Threshold (k): Sets the detection threshold. A higher threshold means the indicator becomes less sensitive, reducing false signals.
    • Drift (μ): Accounts for the expected drift in the process, enabling the detection of smaller but persistent changes.

    @Parameter(defaultChartIndex=0)
    public ChartData Chart;
    @Parameter(category="Default", name="Period", minValue=1, maxValue=10000, defaultValue="20", step=1)
    public int Period;
    @Parameter(category="Default", name="Threshold", minValue=0.1, maxValue=100, defaultValue="2", step=0.1)
    @Help("Detection threshold (k) - higher values make the indicator less sensitive")
    public double Threshold;
    @Parameter(category="Default", name="Drift", minValue=0, maxValue=10, defaultValue="0.5", step=0.1)
    @Help("Expected drift in the process (mu) - helps detect smaller persistent changes")
    public double Drift;


    Rolling Statistics Calculation

    For each new bar (or time point), the code computes the rolling mean and standard deviation over the defined period. These statistics serve as the baseline to standardize the current price data.

    double sum = 0;
    double sumSquares = 0;
    for (int i = 0; i < Period; i++) {
    double price = Chart.Close(i);
    sum += price;
    sumSquares += price * price;
    }
    double mean = sum / Period;
    double variance = (sumSquares - (sum * sum / Period)) / (Period - 1);
    double stdDev = Math.sqrt(variance);
    // Prevent division by zero
    if (stdDev == 0) {
    stdDev = 1;
    }


    Standardization and CUSUM Calculation

    The current price is standardized by subtracting the mean and dividing by the standard deviation. The standardized value is then used to update the CUSUM accumulators:

    • CUSUM Positive: Accumulates positive deviations.
    • CUSUM Negative: Accumulates negative deviations.

    Each update subtracts both the drift and the threshold, ensuring that only significant deviations contribute to the cumulative sum.

    // Get current standardized value
    double currentValue = (Chart.Close(0) - mean) / stdDev;
    // Calculate CUSUM values
    double prevCusumPos = getCurrentBar() > Period ? CusumPos.get(1) : 0;
    double prevCusumNeg = getCurrentBar() > Period ? CusumNeg.get(1) : 0;
    // Upper CUSUM
    CusumPos.set(0, Math.max(0, prevCusumPos + currentValue - Drift - Threshold));
    // Lower CUSUM
    CusumNeg.set(0, Math.max(0, prevCusumNeg - currentValue - Drift - Threshold));


    Handling Edge Cases

    An important detail is the check for the standard deviation. When price data are too consistent (resulting in a standard deviation of zero), the code resets it to 1. This safeguard prevents division errors and keeps the indicator robust under low-volatility conditions.


    Bringing It All Together

    This CUSUM indicator is a powerful tool for traders looking to catch subtle but sustained changes in price action. By dynamically adjusting to recent market data (through rolling statistics) and accounting for noise via the threshold and drift parameters, the indicator offers a balanced approach to signal generation.

    If you’re interested in incorporating this indicator into your trading strategies, the code is structured to be both adaptable and easy to integrate within the StrategyQuant environment.

    You know me as clonex. I'm Ivan Hudec — I built the free blocks you're using here, and I write about the research behind them at algotrading.space. Need something built? Work with me.

    Past performance is no guarantee of future success. All information should be viewed as educational only UNITED STATES Disclaimer required by Government - Commodity Futures Trading Commission informs: Futures and options trading have great potential rewards, but also great potential risk. You must be aware of the risks and be willing to accept them in order to invest in the futures and options markets. Don't trade with money you can't afford to lose. This is neither a solicitation nor an offer to buy/sell futures, shares or options on the same. No representation is being made that any account will or is likely to realise profits or losses similar to those discussed in this document. The past performance of any trading system or methodology is not necessarily indicative of future results. CFTC RULE 4.41 - HYPOTHETICAL OR SIMULATED PERFORMANCE RESULTS HAVE CERTAIN LIMITATIONS. UNLIKE A RECORD OF ACTUAL PERFORMANCE, SIMULATED RESULTS DO NOT REPRESENT ACTUAL TRADING. ALSO, SINCE THE TRADES WERE NOT EXECUTED, THE RESULTS MAY HAVE OFFSET THE IMPACT, IF ANY, OF CERTAIN MARKET FACTORS, SUCH AS LACK OF LIQUIDITY. SIMULATED TRADING PROGRAMMES IN GENERAL ARE ALSO SUBJECT TO THE FACT THAT THEY ARE DESIGNED WITH THE BENEFIT OF HINDSIGHT. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO REALISE PROFITS OR LOSSES SIMILAR TO THOSE SHOWN. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL, OR IS LIKELY TO, REALISE PROFITS OR LOSSES SIMILAR TO THOSE DISCUSSED WITHIN THIS SITE. IF YOU DECIDE TO INVEST REAL MONEY, ALL TRADING DECISIONS IN THE FINANCIAL MARKETS SHOULD BE YOUR OWN.
    Free

    Files

    • Snippet-CUSUM.sqm15 KBLocked

    Download unlocks after purchase or with an active tier.

    Details
    Version1.0.0
    PublishedOctober 19, 2026
    Last updatedNovember 2, 2026
    AppSQ
    TypeSnippet
    Authorclonex / Ivan Hudec
    App version145.3058
    Codebase categoryIndicators / Signals