Separate the momentum window from the EMA factor
Let N be CMO length and L be EMA length, both positive integers. For each adjacent source-price change, collect its positive part into U and its negative magnitude into D, summing the latest N changes. When U + D > 0, CMO = 100 × (U − D) / (U + D). Set a = 2 / (L + 1), alpha = a × |CMO| / 100, and V[t] = V[t−1] + alpha × (price[t] − V[t−1]). MetaQuotes documents the equivalent ratio-scale CMO without the factor of 100.
- The absolute normalized ratio lies from 0 to 1, so 0 ≤ alpha ≤ a ≤ 1. Each update stays between the previous VIDYA and current source price.
- For CMO = -60 and L = 9, a = 0.2 and alpha = 0.12. Omitting the absolute value would give -0.12; omitting the division by 100 would give 12. Both violate this weight range.
- If a script already stores CMO from -1 to 1, do not divide by 100 again. Record the scale of the intermediate value before comparing plots.
| Input | What it changes | What it does not set |
|---|
| CMO length N | Which N adjacent changes enter U and D | The maximum weight a |
| EMA length L | The cap a = 2 / (L + 1) | The number of changes in CMO |
| Source | The price series used for changes and recursion | A trading entry or exit |
Choose a starting value and a zero-window policy
The recurrence needs a previous value, so it cannot define its own initialization. The worked example waits for N valid adjacent changes, seeds VIDYA with the current source price on that first complete window, and begins recursive updates on the next bar. This is an explicit demonstration convention, not a claim that every script named VIDYA uses that seed.
- For identical later prices and weights, two different seeds retain a difference multiplied by (1 − alpha) on each update. A stretch of zero weights preserves the difference rather than removing it.
- Compare the loaded history and first calculated bar when two implementations disagree. Matching length labels does not guarantee matching initialization.
| Condition | Policy used here | Reason to record it |
|---|
| Fewer than N valid changes | No VIDYA value yet | N changes require N + 1 prices. |
| First full window | Seed with current price | An SMA or first-price seed starts a different series. |
| U = D > 0 | alpha = 0; keep previous VIDYA | The defined CMO is zero. |
| U = D = 0 | Set alpha to 0; keep previous VIDYA | The raw CMO ratio is 0 / 0, so this is a fallback. |
| Missing source observation | Clear the state; wait for a new full valid window and reseed | Missing data is not a zero price change. |
Follow a directional move into an alternating window
These are hypothetical completed-bar closes, not market data or a backtest. Use CMO length N = 3, EMA length L = 5, and therefore a = 1/3. Bars 0, 1 and 2 close at 100, 102 and 104. Bar 3 completes the first three changes and seeds VIDYA at 106. The table rounds only the displayed output to four decimals; calculations retain full precision.
- Bar 4 contains changes +2, +2, -2. Its CMO is 100 × 2/6, so alpha = (1/3) × (1/3) = 1/9. VIDYA = 106 + (104 − 106)/9 = 105.7778.
- Bar 7 contains +2, -2, 0. Equal positive and negative sums hold VIDYA at 105.6022 even though the window contains price movement.
- At bar 8 the +2 change leaves the window. The remaining -2, 0, 0 gives CMO = -100 and restores the maximum weight. VIDYA moves toward 104 despite the current bar having no price change.
- Bar 9 has three zero changes. Keeping 105.0681 follows the stated fallback; it does not mean the underlying 0 / 0 ratio is mathematically zero.
| Bar | Close | U | D | alpha | VIDYA |
|---|
| 3 | 106 | 6 | 0 | 1/3; seed bar | 106.0000 |
| 4 | 104 | 4 | 2 | 1/9 | 105.7778 |
| 5 | 106 | 4 | 2 | 1/9 | 105.8025 |
| 6 | 104 | 2 | 4 | 1/9 | 105.6022 |
| 7 | 104 | 2 | 2 | 0 | 105.6022 |
| 8 | 104 | 0 | 2 | 1/3 | 105.0681 |
| 9 | 104 | 0 | 0 | 0; fallback | 105.0681 |
| 10 | 106 | 2 | 0 | 1/3 | 105.3788 |
Read the weight as directional balance
The formula responds to the balance of upward and downward movement, not simply the size of the candles. A window of +1, +1, +1 and a window of +10, +10, +10 both produce |CMO| = 100. A window of +10, -10, 0 has much more total movement than the first window, yet produces alpha = 0.
- Scaling every change by the same positive number leaves the ratio unchanged. VIDYA weight is not a price-range or position-risk measure.
- A directionally consistent fall can give the same weight as a rise. The update direction comes from current price minus previous VIDYA.
- Sustained one-direction changes make alpha equal the ordinary EMA factor a. That matches the update weight, but the full line can still differ because of its earlier history and seed.
Why VIDYA and KAMA still produce different lines
TradingView documents KAMA with an efficiency ratio ER and a squared smoothing factor: [ER × (fast factor − slow factor) + slow factor]². For the same complete window of unsmoothed price changes with a positive denominator, |CMO| / 100 equals ER: U − D is the net change and U + D is the total absolute change. The distinction is how each indicator maps that ratio into a weight.
- With KAMA fast length 2 and slow length 30, ER = 0 gives (2/31)², about 0.004162. This VIDYA gives exactly zero when U = D > 0. The comparison concerns weight, not which indicator trades better.
- The ratio equivalence requires the same source and window. Added smoothing, different lookbacks or missing-data conventions can break that match.
| Calculation | Weight when the ratio is zero | Weight when the ratio is one |
|---|
| CMO-based VIDYA | 0 | 2 / (L + 1) |
| KAMA | Slow factor squared | Fast factor squared |
Audit a TradingView implementation before defining trades
Record the script author and version, source, chart type, session, timeframe, both lengths and seed. Inspect whether the plotted line is raw VIDYA or has extra smoothing, bands or an offset. For a custom implementation, expose U, D, CMO, alpha and VIDYA in the Data Window and compare completed bars against a hand calculation.
- Verify a full window, a window with balanced changes, and a flat window. Use the table as a separate arithmetic fixture; live chart prices will differ.
- An indicator can recalculate while the current bar is open. If a cross should count only at chart-bar close, specify that confirmation rule. Confirmation of a chart bar does not confirm an unfinished higher-timeframe input.
- Give the Pine Script AI Coding Agent this specification: separate CMO and EMA inputs, explicit seed, zero-denominator and missing-data handling, intermediate diagnostic values, and the desired confirmation time.
- Review generated source and validate its syntax, then check numerical agreement. If you add strategy orders later, define exits, order timing, position sizing, commission and slippage separately. A matching VIDYA line is not a complete strategy.