Chande Variable Moving Average Indicator for ThinkorSwim

skynetgen

Well-known member
TOS has built in variable MA , however what I wanted is flatter ma in case of chop. One from tradingview by lazybear fit the bill.
https://www.tradingview.com/script/6Ix0E5Yr-Variable-Moving-Average-LazyBear/here its adaption for TOS
https://tos.mx/yANqUy
Code:
Code:
#added coloration
# formula from lazy bear #https://www.tradingview.com/script/HT99VkZb-Variable-Moving-Average-Bands-LazyBear/

input price = close;
input length = 13;

script VMA
{
input price=close;
input length=13;
def k = 1.0/length;
def src=if isnan(price[1]) then 0 else price;
def pdm = max((src - src[1]), 0);
def mdm = max((src[1] - src), 0);
def pdmS = if isnan(pdms[1]) then k*pdm else ((1 - k)*(pdmS[1]) + k*pdm);
def mdmS = if isnan(mdms[1]) then k*mdm else ((1 - k)*(mdmS[1]) + k*mdm);
def s = pdmS + mdmS;
def pdi = pdmS/s; def mdi = mdmS/s;
def pdiS = if isnan(pdis[1]) then k*pdi else ((1 - k)*(pdiS[1]) + k*pdi);
def mdiS = if isnan(mdis[1]) then k*mdi else ((1 - k)*(mdiS[1]) + k*mdi);
def d = absvalue(pdiS - mdiS);
def s1 = pdiS + mdiS;
def iS = if isnan(is[1]) then  k*d/s1 else ((1 - k)*(iS[1]) + k*d/s1);
def hhv = highest(iS, length);
def llv = lowest(iS, length) ;
def d1 = hhv - llv;
def vI = (iS - llv)/d1;
def val= if isnan(val[1]) then k*vI*src else (1 - k*vI)*(val[1]) + k*vI*src;
plot vma=val;
};


plot pVMA = vma(price,length);
pVMA.assignValueColor(if pvma>pvma[1] then color.blue
         else if   pvma<pvma[1] then color.orange
         else color.gray);
pvma.setlineWeight(3);

Fixed some bugs
 
Last edited:
@skynetgen if you so choose, put Chande in front of the VMA Title. Otherwise others may think it's just another one of "those" and not open the thread. This is a great indicator. Thank you for translating...
 
@skynetgen this looks great. need to try. so in the particular configuration, blue = long, yellow/orange = short, and gray is neutral.

could be a great chop reducer with a 9 length/1 minute.
 
Last edited by a moderator:
Ruby:
#https://www.tradingview.com/script/6Ix0E5Yr-Variable-Moving-Average-LazyBear/
def src = close;
def l = 6;
def std = no;
def bc = no;
def k = 1.0 / l;
script nz {
    input data = 0;
    def ret_val = if IsNaN(data) then 0 else data;
    plot return = ret_val;
}
def pdm = Max((src - src[1]), 0);
def mdm = Max((src[1] - src), 0);
def pdmS = ((1 - k) * nz(pdmS[1]) + k * pdm);
def mdmS = ((1 - k) * nz(mdmS[1]) + k * mdm);
def s = pdmS + mdmS;
def pdi = pdmS / s;
def mdi = mdmS / s;
def pdiS = ((1 - k) * nz(pdiS[1]) + k * pdi);
def mdiS = ((1 - k) * nz(mdiS[1]) + k * mdi);
def d = AbsValue(pdiS - mdiS);
def s1 = pdiS + mdiS;
def iS = ((1 - k) * nz(iS[1]) + k * d / s1);
def hhv = Highest(iS, l);
def llv = Lowest(iS, l);
def d1 = hhv - llv;
def vI = (iS - llv) / d1;
def vma = (1 - k * vI) * nz(vma[1]) + k * vI * src;
plot line = vma;
line.AssignValueColor(if line> line[1] then Color.GREEN else if line <line[1] then Color.MAGENTA else color.gray);
line.SetLineWeight(4);
 
Here are some labels for the script for quick glance interpretations
https://tos.mx/!PsGv1mg5

Code:
# === Variable Moving Average (LazyBear version for TOS) ===
# Original TradingView script: LazyBear
# enhancements by antwerks
# Enhancements: Trend labels, strength interpretation



# --- Inputs ---
def src = close;
input l = 6;

def k = 1.0 / l;

# --- Safe "nz" function ---
script nz {
    input data = 0;
    def ret_val = if IsNaN(data) then 0 else data;
    plot return = ret_val;
}

# --- Core VMA Logic ---
def pdm = Max((src - src[1]), 0);
def mdm = Max((src[1] - src), 0);

def pdmS = ((1 - k) * nz(pdmS[1]) + k * pdm);
def mdmS = ((1 - k) * nz(mdmS[1]) + k * mdm);

def s = pdmS + mdmS;
def pdi = pdmS / s;
def mdi = mdmS / s;

def pdiS = ((1 - k) * nz(pdiS[1]) + k * pdi);
def mdiS = ((1 - k) * nz(mdiS[1]) + k * mdi);

def d = AbsValue(pdiS - mdiS);
def s1 = pdiS + mdiS;

def iS = ((1 - k) * nz(iS[1]) + k * d / s1);

def hhv = Highest(iS, l);
def llv = Lowest(iS, l);
def d1 = hhv - llv;

def vI = (iS - llv) / d1;

def vma = (1 - k * vI) * nz(vma[1]) + k * vI * src;

# --- Plot VMA Line ---
plot line = vma;
line.AssignValueColor(
    if line > line[1] then Color.GREEN
    else if line < line[1] then Color.MAGENTA
    else Color.GRAY
);
line.SetLineWeight(4);

# --- Trend Strength Calculation ---
def trendStrength = AbsValue(line - line[1]);

# --- Labels ---
AddLabel(yes,
    if line > line[1] then "VMA Rising (Bullish)"
    else if line < line[1] then "VMA Falling (Bearish)"
    else "VMA Flat (Neutral)",
    if line > line[1] then Color.GREEN
    else if line < line[1] then Color.MAGENTA
    else Color.GRAY
);

AddLabel(yes,
    if trendStrength < 0.05 then "Trend Strength: Weak (" + Round(trendStrength, 4) + ")"
    else if trendStrength < 0.15 then "Trend Strength: Moderate (" + Round(trendStrength, 4) + ")"
    else "Trend Strength: Strong (" + Round(trendStrength, 4) + ")",
    if trendStrength < 0.05 then Color.GRAY
    else if trendStrength < 0.15 then Color.YELLOW
    else if line > line[1] then Color.GREEN else Color.MAGENTA
);
 
Last edited by a moderator:
Here are some labels for the script for quick glance interpretations
https://tos.mx/!PsGv1mg5

Code:
# === Variable Moving Average (LazyBear version for TOS) ===
# Original TradingView script: LazyBear
# enhancements by antwerks
# Enhancements: Trend labels, strength interpretation



# --- Inputs ---
def src = close;
input l = 6;

def k = 1.0 / l;

# --- Safe "nz" function ---
script nz {
    input data = 0;
    def ret_val = if IsNaN(data) then 0 else data;
    plot return = ret_val;
}

# --- Core VMA Logic ---
def pdm = Max((src - src[1]), 0);
def mdm = Max((src[1] - src), 0);

def pdmS = ((1 - k) * nz(pdmS[1]) + k * pdm);
def mdmS = ((1 - k) * nz(mdmS[1]) + k * mdm);

def s = pdmS + mdmS;
def pdi = pdmS / s;
def mdi = mdmS / s;

def pdiS = ((1 - k) * nz(pdiS[1]) + k * pdi);
def mdiS = ((1 - k) * nz(mdiS[1]) + k * mdi);

def d = AbsValue(pdiS - mdiS);
def s1 = pdiS + mdiS;

def iS = ((1 - k) * nz(iS[1]) + k * d / s1);

def hhv = Highest(iS, l);
def llv = Lowest(iS, l);
def d1 = hhv - llv;

def vI = (iS - llv) / d1;

def vma = (1 - k * vI) * nz(vma[1]) + k * vI * src;

# --- Plot VMA Line ---
plot line = vma;
line.AssignValueColor(
    if line > line[1] then Color.GREEN
    else if line < line[1] then Color.MAGENTA
    else Color.GRAY
);
line.SetLineWeight(4);

# --- Trend Strength Calculation ---
def trendStrength = AbsValue(line - line[1]);

# --- Labels ---
AddLabel(yes,
    if line > line[1] then "VMA Rising (Bullish)"
    else if line < line[1] then "VMA Falling (Bearish)"
    else "VMA Flat (Neutral)",
    if line > line[1] then Color.GREEN
    else if line < line[1] then Color.MAGENTA
    else Color.GRAY
);

AddLabel(yes,
    if trendStrength < 0.05 then "Trend Strength: Weak (" + Round(trendStrength, 4) + ")"
    else if trendStrength < 0.15 then "Trend Strength: Moderate (" + Round(trendStrength, 4) + ")"
    else "Trend Strength: Strong (" + Round(trendStrength, 4) + ")",
    if trendStrength < 0.05 then Color.GRAY
    else if trendStrength < 0.15 then Color.YELLOW
    else if line > line[1] then Color.GREEN else Color.MAGENTA
);
HI @antwerks Is it possible to convert this in multitime frame? If yes, can you please help
 
Well I might have gotten carried away - this does change with the charts as you change the timeframe but it has a fast and a slow VMA allowing for a quicker signal and a confirming signal with the slower VMA - you can toggle them on or off and if you go inside the script you can tweak as you see fit- we can also tweak per volatility (IV) but that might make it give false signals. Better to "eye" it. (Not 100% what you meant by multi-timeframe? so I did both with the Aggperiod and giving you multiple VMAs)

Code:
# === Dual VMA (LazyBear, Dynamic & Toggleable) ===

declare upper;

def src = close;
def agg = GetAggregationPeriod();

# === Dynamic lengths based on timeframe ===
def fastL = if agg <= AggregationPeriod.FIVE_MIN then 3
    else if agg <= AggregationPeriod.FIFTEEN_MIN then 5
    else if agg <= AggregationPeriod.HOUR then 7
    else if agg <= AggregationPeriod.DAY then 9
    else 12;

def slowL = if agg <= AggregationPeriod.FIVE_MIN then 6
    else if agg <= AggregationPeriod.FIFTEEN_MIN then 8
    else if agg <= AggregationPeriod.HOUR then 10
    else if agg <= AggregationPeriod.DAY then 14
    else 20;

input showFast = yes;
input showSlow = yes;

# === Safe "nz" ===
script nz {
    input data = 0;
    def ret_val = if IsNaN(data) then 0 else data;
    plot return = ret_val;
}

# === VMA function ===
script vmaLine {
    input src = close;
    input l = 6;

    def k = 1.0 / l;

    def pdm = Max((src - src[1]), 0);
    def mdm = Max((src[1] - src), 0);

    def pdmS = ((1 - k) * nz(pdmS[1]) + k * pdm);
    def mdmS = ((1 - k) * nz(mdmS[1]) + k * mdm);

    def s = pdmS + mdmS;
    def pdi = pdmS / s;
    def mdi = mdmS / s;

    def pdiS = ((1 - k) * nz(pdiS[1]) + k * pdi);
    def mdiS = ((1 - k) * nz(mdiS[1]) + k * mdi);

    def d = AbsValue(pdiS - mdiS);
    def s1 = pdiS + mdiS;

    def iS = ((1 - k) * nz(iS[1]) + k * d / s1);

    def hhv = Highest(iS, l);
    def llv = Lowest(iS, l);
    def d1 = hhv - llv;

    def vI = (iS - llv) / d1;

    def vma = (1 - k * vI) * nz(vma[1]) + k * vI * src;

    plot out = vma;
}

# === Plotting Fast and Slow VMA ===

plot VMAfast = if showFast then vmaLine(src, fastL) else Double.NaN;
plot VMAslow = if showSlow then vmaLine(src, slowL) else Double.NaN;

VMAfast.SetDefaultColor(Color.GREEN);
VMAfast.SetLineWeight(3);

VMAslow.SetDefaultColor(Color.BLUE);
VMAslow.SetLineWeight(2);

# === Labels ===
AddLabel(showFast, "Fast VMA (l=" + fastL + ")", Color.GREEN);
AddLabel(showSlow, "Slow VMA (l=" + slowL + ")", Color.BLUE);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
834 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top