
This strategy is a trend following trading system based on the VIDYA (Variable Index Dynamic Average) indicator. The strategy adapts to market volatility by dynamically adjusting weights, combining Chande’s Momentum Oscillator (CMO) and Standard Deviation (StDev) calculation methods to achieve more accurate trend identification and trading signal generation. The system introduces an adaptive mechanism on top of traditional moving averages, automatically adjusting sensitivity based on market conditions.
The core of the strategy is the VIDYA indicator, with calculation process including these key steps: 1. Setting basic period (default 21) and smoothing coefficient alpha 2. Incorporating CMO or StDev as volatility calculation methods 3. Using dynamic weight k to adjust VIDYA’s sensitivity to price changes 4. Generating long signals when VIDYA crosses upward and short signals when crossing downward
The strategy allows users to choose between CMO or standard deviation for volatility coefficient calculation, increasing flexibility. CMO mode uses a fixed 9-period cycle, while StDev mode maintains consistency with the base period.
The VIDYA strategy provides a relatively reliable trend following solution through innovative adaptive weight mechanisms. While maintaining simplicity and ease of use, the strategy improves adaptability to market changes through dynamic adjustments. Although some inherent limitations exist, the provided optimization directions can further enhance strategy stability and reliability. The dual calculation methods offer greater flexibility for application in different market environments.
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-04 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © GriffinJames
//@version=5
strategy("VIDYA Strategy", overlay=true, initial_capital=25000)
// Inputs
src = input(close, title="Source")
pds = input.int(21, title="Length")
fixCMO = input.bool(true, title="Fixed CMO Length (9)?")
select = input.bool(true, title="Calculation Method: CMO/StDev?")
alpha = 2 / (pds + 1)
momm = ta.change(src)
// Functions to calculate MOM
f1(m) => m >= 0.0 ? m : 0.0
f2(m) => m >= 0.0 ? 0.0 : -m
m1 = f1(momm)
m2 = f2(momm)
sm1 = fixCMO ? math.sum(m1, 9) : math.sum(m1, pds)
sm2 = fixCMO ? math.sum(m2, 9) : math.sum(m2, pds)
percent(nom, div) => 100 * nom / div
chandeMO = na(percent(sm1 - sm2, sm1 + sm2)) ? 0 : percent(sm1 - sm2, sm1 + sm2)
// Select calculation method
k = select ? math.abs(chandeMO) / 100 : ta.stdev(src, pds)
// Calculate VIDYA
var float VIDYA = na
VIDYA := na(VIDYA[1]) ? src : alpha * k * src + (1 - alpha * k) * VIDYA[1]
// Conditions for long and short
col12 = VIDYA > VIDYA[1]
col32 = VIDYA < VIDYA[1]
// Plot VIDYA with dynamic colors
color2 = col12 ? color.new(color.blue, 0) : col32 ? color.new(color.maroon, 0) : color.new(color.blue, 0)
plot(VIDYA, "VAR", color=color2, linewidth=2)
// Long and Short Strategy
if (col12)
strategy.entry("Go Long", strategy.long)
if (col32)
strategy.entry("Go Short", strategy.short)
// Alert for VIDYA color change
alertcondition(ta.cross(VIDYA, VIDYA[1]), title="Color ALARM!", message="VIDYA has changed color!")