- Parameter: int period
- For each bar i from 0 to Count-1:
- If i < period-1: set output[i] = NaN
- Else: output[i] = (Sum of Close[i – period + 1 .. i]) / period
This manual implementation helps when you later build weighted or adaptive versions.
Example 2 — Adaptive Momentum Indicator
Goal: create an indicator that combines momentum with volatility-adjusted smoothing.
Design:
- Inputs: periodMomentum, periodVolatility, smoothingWindow
- Step 1: Momentum = Close – Close(periodMomentum)
- Step 2: Volatility = StdDev(Momentum, periodVolatility)
- Step 3: AdaptiveSmooth = EMA(Momentum, smoothingWindow * (Volatility / Volatility.Mean))
Implementation notes:
- Avoid divide-by-zero when volatility is near zero by applying a floor.
- Use rolling mean of volatility to normalize scaling.
- Cache intermediate series to avoid redundant computation.
Coding tips & patterns
- Vectorized loops: compute values in a single pass when possible.
- Reuse built-in helpers: Wealth-Lab exposes common functions (EMA, StdDev) — call them rather than reimplement.
- Defensive programming: handle insufficient bars, parameter validation, and NaN propagation.
- Parameter exposure: mark parameters so they appear in the UI and can be optimized.
- Performance: minimize allocations inside loops; prefer pre-sized arrays or Series.
Testing your indicator
- Visual inspection: overlay the indicator on multiple charts (different timeframes, instruments) to check behavior.
- Unit tests: if your workflow supports it, write tests for edge cases (short series, constant prices).
- Synthetic data: test using constructed price series (ramp, sine wave, spikes) to verify expected reactions.
- Compare with known implementations: for common components (e.g., EMA), ensure outputs match Wealth-Lab built-ins.
Using indicators in strategies
- Entry/exit rules: use crossovers, threshold breaches, or slope changes of your indicator to trigger trades.
- Filters: combine custom indicators with volume or volatility filters for better signal quality.
- Multi-timeframe: compute indicator on higher timeframe data within a strategy for trend confirmation.
- Optimization: expose sensible parameter ranges and use walk-forward testing to avoid overfitting.
Optimization and avoiding overfitting
- Limit parameter count: more parameters increase overfitting risk.
- Use coarse-to-fine search: start with wide ranges and coarse steps, then refine around promising regions.
- Walk-forward analysis: perform rolling in-sample/out-of-sample tests.
- Validate robustness: test across multiple symbols, sectors, and market regimes.
Example: Full custom indicator code (conceptual)
Below is a conceptual C#-style snippet structure (adapt to your WL version):
public class AdaptiveMomentum : IndicatorBase { public int MomentumPeriod { get; set; } public int VolPeriod { get; set; } public int SmoothWindow { get; set; } public AdaptiveMomentum() { MomentumPeriod = 10; VolPeriod = 20; SmoothWindow = 5; // set metadata... } protected override void Populate() { int count = Bars.Count; var momentum = new Series(count); var volatility = new Series(count); var output = new Series(count); for (int i = 0; i < count; i++) { if (i < Math.Max(MomentumPeriod, VolPeriod)) { output[i] = double.NaN; continue; } momentum[i] = Bars.Close[i] - Bars.Close[Math.Max(0, i - MomentumPeriod)]; volatility[i] = StdDev(momentum, VolPeriod, i); // conceptual helper } // adaptive smoothing pass... for (int i = 0; i < count; i++) { double vol = Math.Max(1e-6, volatility[i]); double adaptiveWindow = SmoothWindow * (vol / volatility.Mean()); output[i] = EMA(momentum, (int)Math.Max(1, Math.Round(adaptiveWindow)), i); } this.Values = output; } }
Adjust API calls to match your Wealth-Lab Developer version.
Visual design & UX considerations
- Scale: normalize indicator values to be visually meaningful (e.g., bounded range).
- Color & thickness: use contrasting colors for signals vs. baseline.
- Plot types: line, histogram, or filled area depending on interpretation.
- Alerts: provide alert hooks when indicator crosses thresholds.
Common pitfalls
- Improper lookback handling that leaks future data into past bars.
- Overly sensitive parameters that react to noise.
- Ignoring data anomalies (bad ticks, dividends, splits) — use adjusted price series where appropriate.
- Not testing across market regimes.
Deployment & sharing
- Package your indicator with clear descriptions and default parameters.
- Include example charts and sample scripts that use the indicator in strategies.
- Version your code and document changes in release notes.
- Consider performance profiling for indicators used in large-scale scans.
Summary
Building custom indicators in Wealth-Lab Developer is a powerful way to encode trading ideas, improve signal specificity, and integrate proprietary analytics into strategies. Start simple, validate thoroughly, and iterate with careful testing and optimization to produce robust, reusable indicators.
Leave a Reply