name: custom-indicator description: > Creates a custom indicator class in QuantConnect/LEAN. Invoke for "create a custom indicator", "implement [name] indicator", "I need an indicator LEAN doesn't have", "PythonIndicator", "custom indicator class". Skip for LEAN built-ins (SMA, EMA, RSI).
/custom-indicator -- QuantConnect Custom Indicator
Creates a custom indicator class and wires it into an algorithm.
Step 1 -- Gather Indicator Information
Ask the user: (1) indicator name; (2) formula/logic; (3) input type -- IndicatorDataPoint (single price), TradeBar (OHLCV, default), QuoteBar (bid/ask); (4) period; (5) internal LEAN indicators (SMA, EMA, RSI) -- construct and update inside ComputeNextValue; (6) scope -- single symbol or universe; (7) warm-up -- manual (recommended) or automatic.
Step 2 -- Generate the Code
Python: from AlgorithmImports import * only. Inherit PythonIndicator. File: snake_case.py in its own file.
C#: inherit Indicator (or BarIndicator for OHLCV). Set WarmUpPeriod in constructor. File: PascalCase.cs.
Manual warm-up (default): loop History<TradeBar>(symbol, period + 1, Resolution.Daily), call indicator.Update(bar) before RegisterIndicator.
Automatic warm-up: Settings.AutomaticIndicatorWarmUp = true in Initialize.
Indicator class
public class CustomVolatility : Indicator
{
private readonly RollingWindow<decimal> _window;
public CustomVolatility(int period) : base("CustomVolatility")
{
_window = new RollingWindow<decimal>(period);
WarmUpPeriod = period;
}
public override bool IsReady => _window.IsReady;
protected override decimal ComputeNextValue(IndicatorDataPoint input)
{
_window.Add(input.Value);
if (!IsReady) return 0m;
// Compute from _window here.
return 0m;
}
}
For OHLCV in C#, inherit BarIndicator and use IBaseDataBar in ComputeNextValue.
Single-symbol integration
var symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_indicator = new CustomVolatility(period);
foreach (var bar in History<TradeBar>(symbol, period + 1, Resolution.Daily))
_indicator.Update(bar);
RegisterIndicator(symbol, _indicator);
Universe integration
C# mirrors Python: use OnSecuritiesChanged, RegisterIndicator, DeregisterIndicator, Liquidate. Gate reads on _indicators[symbol].IsReady.
Step 3 -- Write Files via MCP
quantconnect:create_filewith the indicator class.quantconnect:update_file_contentsformain.py/Main.cs.- Python only: add
from custom_volatility import CustomVolatilityafterfrom AlgorithmImports import *.
Step 4 -- Compile
quantconnect:create_compile. Pollquantconnect:read_compileuntilBuildSuccessorBuildError. Fix errors and loop.
Step 5 -- Backtest and Verify
quantconnect:create_backtest. Pollquantconnect:read_backtestuntil complete.- Confirm
IsReadybecomes true and at least one order placed (if the algorithm has entry conditions).